feathersjs-ecosystem / feathers-batch

Batch multiple Feathers service calls into one
MIT License
96 stars 10 forks source link

feathers client service hooks get skipped. #106

Open dallinbjohnson opened 3 years ago

dallinbjohnson commented 3 years ago

Steps to reproduce

Because feathers-batch is a global hook it does not run individual service hooks that may add data or perform ETL functions.

I think that if we added global after hooks that could be added after all service hooks for front end and back end hooks would be wonderful and would solve this problem.

(First please check that this issue is not already solved as described here)

feathers client hooks do not run on batched calls. Because it is a global hook.

Expected behavior

Tell us what should happen

I think the batched hook should run after each service hook runs.

Actual behavior

Tell us what happens instead

System configuration

Tell us about the applicable parts of your setup.

Module versions (especially the part that's not working):

NodeJS version:

Operating System:

Browser Version:

React Native Version:

Module Loader:

DaddyWarbucks commented 2 years ago

This totally makes sense. I would like to see the client side server hooks run as well. Perhaps a good solution is to export a modified version of the collectBatches hook and allow the developer to use it explicitly.

https://github.com/feathersjs-ecosystem/feathers-batch/blob/91557003b8e75ac53515d797ec86f06e2e580af3/client/index.js#L68

You would then not use the batchClient plugin and would use the collectBatches hook directly. Something like this

  // Pass the manger in explicitly and remove excludes
  const collectBatches = manager => async context => {
    const { method, path } = context;

    const args = makeArguments(context);
    const payload = [method, path, ...args];
    const batchPromise = new Promise((resolve, reject) => manager.addBatchCall({
      resolve,
      reject,
      payload
    }));

    context.result = await batchPromise;

    return context;
  };
  // Use the hook manually
  import { BatchManger, collectBatches } from 'feathers-batch';

  const manager = new BatchManager()
  const batchHook = collectBatches(manager);

  // use batchHook wherever you want