Question

I believe that I saw somebody wrote about a way to fetch the in-memory objects from a model object to reduce DB access. Could some body please advise or share me the document?

I would like to have something like this:

ModelA.create({...}).populate('modelBs').exec(function(err, instanceA) {
    // Do some nested things here, for example:
    instanceA.prop1 = someNewValue;
    instanceA.save(function(err, instanceAModified) {
       // I want to call some method from instanceAModified, to get all 'modelB' objects here, without accessing the DB once more.
    });
});

Thank you in advance!

Was it helpful?

Solution

I believe that in the newest release of Waterline, .save() will re-populate any associations in its callback. But regardless, you can always hang on to them by saving them in a local variable. Assuming you meant find and not create above (since populate doesn't do anything on create):

ModelA.find({...}).populate('modelBs').exec(function(err, instanceA) {
    // Do some nested things here, for example:
    instanceA.prop1 = someNewValue;
    var modelBs = instanceA.modelBs;
    instanceA.save(function(err, instanceAModified) {
       // modelBs will still be available here.
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top