I have looked at the example posted here: YDN-DB with multiple deferred which contains some code that is very close to what I want, but not quite.
I am wondering if it is safe to nest deferred queries in a transaction? For example:
loadWorkOrders: function() {
var params = {
userId: 1,
status: Status.Allocated
};
var allOrders = null;
return workOrderHttpService.getWorkOrders(params).then(function(orders) {
allOrders = orders.data;
return ydndatabase.open();
}).then(function(db){
return db.run(function(runDb){
allOrders.forEach(function(workOrder){
runDb.count(Store.WorkOrder, ydn.db.KeyRange.only(workOrder.id)).then(function(count) {
if(count == 0) {
return runDb.put(Store.WorkOrder, workOrder);
} else {
return workOrder;
}
});
});
}, [Store.WorkOrder], TransactionType.ReadWrite)
});
}
EDIT: I have edited the code to show how it is preceded by an async call to an http service