Sails Js / Waterline - 有没有办法获取以前获取的对象?

时间:2014-05-01 07:57:47

标签: model sails.js waterline

我相信我看到有人写过一种从模型对象中获取内存中对象以减少数据库访问的方法。有些人可以建议或分享这份文件吗?

我想有这样的事情:

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.
    });
});

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我相信在最新版的Waterline中,.save()将重新填充其回调中的任何关联。但无论如何,你总是可以通过将它们保存在局部变量中来保持它们。假设你的意思是find而不是create以上(因为populatecreate上没有做任何事情):

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.
    });
});