Hi,
At the moment, there is a hook for the post init which gets called when a document is loaded from the DB.
Is there a possibility to get a hook for newly created documents, which could be used for example to default certain properties of the document based on some configuration data ???
Thanks.
do schema defaults not work for your use case?
Schema defaults are static. Consider the user case where object initialization is driven based on who is logged into the application ( just for argument sake), in that case, you would need to perform this initialization on a per user basis and if there is a hook with callback (like next in the pre-hooks) would be much useful.
+1 on this. One of the use cases on the middleware page of the docs says "Asynchronous defaults". I assumed that a pre or post create middleware would be the best way to achieve this. Is there a better way?
@romanmt is this helpful?
thats great! Exactly what I need. Thanks!
On Thu, Jun 21, 2012 at 4:03 PM, Aaron Heckmann <
[email protected]
wrote:
@romanmt is this
helpful?
Reply to this email directly or view it on GitHub:
https://github.com/LearnBoost/mongoose/issues/672#issuecomment-6492983
thats looks great....
cool, gonna close this.
This may have been added after this issue was closed, but wouldn't this also be achieved via:
ThingSchema.pre('save', function(done){
if(!this.isNew) return done();
// your 'pre-create' code
});
@bradynpoulsen, not exactly! the pre-save is called after thing.save() whereas the pre-create is called right after new ThingSchema()
I am looking for something which allows me create a hook after creating an instance of model. Mongoose-lifecycle is only supporting save()
post and pre which is not really comfortable for me. Does Mongoose supports such hooks?
Take a look at the schema.queue function in the api docs, that enables you to queue up functions to execute every time a new document is created. Mongoose uses it internally for hooks but 4.1.0 formally added that to the public facing API
I think what the OP was looking for (and I'm looking for it too), is to define a callback function to run after the document is created/inserted, _not_ every time it is saved. So, something like this:
var UserSchema = new Schema ({
email: {type: String, unique: true, sparse: true},
});
UserSchema.post('create', function(done) {
var email = new WelcomeEmail({to: this.email});
email.send(done);
});
@ajsharp the below should work. Unfortunately, it's not really possible to make an async post create hook, because creating a new doc is assumed to be synchronous.
var UserSchema = new Schema ({
email: {type: String, unique: true, sparse: true},
});
UserSchema.methods.email = function() {
var email = new WelcomeEmail({to: this.email});
email.send();
};
UserSchema.queue('email');
According to vkarpov15's final answer here, the queue
call should only call the email
method once, after the document is created.
I'm experiencing that the queue
call is being called every time the document is interacted with, including find()
query operations.
Should I open a new issue?
@rmgreenstreet yes please :+1:
Most helpful comment
This may have been added after this issue was closed, but wouldn't this also be achieved via: