我相信我看到有人写过一种从模型对象中获取内存中对象以减少数据库访问的方法。有些人可以建议或分享这份文件吗?
我想有这样的事情:
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.
});
});
提前谢谢!
答案 0 :(得分:0)
我相信在最新版的Waterline中,.save()
将重新填充其回调中的任何关联。但无论如何,你总是可以通过将它们保存在局部变量中来保持它们。假设你的意思是find
而不是create
以上(因为populate
在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.
});
});