beforeSave not called before upsert (^6.3.0)
Issue Description
When upserting a document, hooks applied on model are not trigerred.
What are you doing?
// Model
Model.beforeSave((instance, options) => {
console.log("before save hooks called");
instance.test = "hello";
});
// Server
Model.upsert({ ... })
What do you expect to happen?
Trigerring beforeSave when upserting
Environment
- Sequelize version: "^6.3.0"
- Node.js version: "v12.16.1"
- Operating System: macOS Cataline - v10.15.5
How does this problem relate to dialects?
- [X ] I think this problem happens regardless of the dialect.
- [ ] I think this problem happens only for the following dialect(s):
- [ ] I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX
Would you be willing to resolve this issue by submitting a Pull Request?
- [ ] Yes, I have the time and I know how to start.
- [ ] Yes, I have the time but I don't know how to start, I would need guidance.
- [ ] No, I don't have the time, although I believe I could do it if I had the time...
- [X ] No, I don't have the time and I wouldn't even know how to start.
This issue has been raised before https://github.com/sequelize/sequelize/issues/3965#issuecomment-244293255, I am tagging this as a feature request
This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂
I understand the reasoning in https://github.com/sequelize/sequelize/issues/3965#issuecomment-113399380 for create and update hooks not fired for upsert, but for save hook this should be no brainer, right? Sequelize can fire beforeSave/afterSave for upsert because the data are going to be saved in both cases. Is there any other issue I don't see? Can you make this a higher priority, please?
Not really, beforeSave is called when save is called, and upsert does not use save, so there is nothing to fix here after all.
As far as I know beforeSave is called also for Model.create for example because it saves the instance. The same way as Model.upsert saves it. It is about semantics, not about matching the method name.
It is about matching the method name because the hook lets your modify the option it gives you. If we made upsert arbitrarily call beforeSave with its option, you'd get two completely different option types in beforeSave. That's not something we can do. Instead I recommend hooking both beforeSave and beforeUpsert