Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stashBefore - add support for bulk operations #757

Open
muhammadyusuf-kurbonov opened this issue Aug 14, 2024 · 2 comments
Open

stashBefore - add support for bulk operations #757

muhammadyusuf-kurbonov opened this issue Aug 14, 2024 · 2 comments

Comments

@muhammadyusuf-kurbonov
Copy link

Steps to reproduce

  1. Create a Mongoose service with option multi: true.
  2. Add before patch hook: stashBefore
  3. Run service.patch(null, { query: { date: "2024-08-14" }, mongoose: { upsert: true } }

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

Expected behavior

At least, I don't want an error: Not found element with id null. At best, I want to stash these multiple item.

Actual behavior

My log is flooded with errors: Not found element with id null, because our app make some bulk writes to this table.

Module versions (especially the part that's not working):
Feathers v3 (yes it is old, but project is legacy and huge)
Feathers mongoose: 7.3.1 (tried also with 8)
Feathers-hooks-common: 4.20.2 (according source code, this error will happen in latest version)

NodeJS version:
NodeJS 16

Operating System:
Linux, Fedora 40

@muhammadyusuf-kurbonov
Copy link
Author

Sorry, that have opened issue with legacy project setup, but I'm sure that this error will happen also in latest Feathers versions too

@muhammadyusuf-kurbonov
Copy link
Author

Now I have copied hook source code and modified myself:

export default function (prop): Hook {
  const beforeField = prop || 'before';

  return context => {
    checkContext(context, 'before', ['get', 'update', 'patch', 'remove'], 'stashBefore');

    if (context.params.query && context.params.query.$disableStashBefore) {
      delete context.params.query.$disableStashBefore;
      return context;
    }

    if ((context.id === null || context.id === undefined) && !context.params.query) {
      throw new errors.BadRequest('Id is required. (stashBefore)');
    }

    const params = context.method === 'get' ? context.params : {
      provider: context.params.provider,
      authenticated: context.params.authenticated,
      user: context.params.user
    };

    params.query = params.query || {};
    params.query.$disableStashBefore = true;

    const bulkOperation = context.service.options.multi && context.id === null;

    const data = bulkOperation ? context.service.find(params) : context.service.get(context.id, params);

    return data.then(data => {
      delete params.query.$disableStashBefore;

      context.params[beforeField] = JSON.parse(JSON.stringify(data));
      return context;
    }).catch(() => context);
  };
}

```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant