Skip to content

Commit 020b4a2

Browse files
author
emmanuel
committed
Results from each operation returned sequentially in a list
1 parent bf11783 commit 020b4a2

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

lib/task.js

+28-15
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,20 @@ var Task = function(){
227227
var dbTask = new TaskMdl({steps: steps});
228228
steps = [];
229229
index = 0;
230+
var results = [];
230231

231232
return dbTask.save().then(function (_task) {
232233
_task.steps.forEach(function (step) {
233234
chain = chain.then(function () {
234-
return getResolveFunc(step)(step, _task);
235+
return getResolveFunc(step)(step, _task, results);
235236
});
236237
});
237238

238-
return chain.then(function () {
239-
return _task.remove();
239+
return chain.then(function (results) {
240+
return _task.remove()
241+
.then(function () {
242+
return Promise.resolve(results);
243+
});
240244
}).catch(function (err) {
241245
return Roller.rollOne(_task).then(function () {
242246
throw err;
@@ -267,10 +271,11 @@ function getResolveFunc(step){
267271
*
268272
* @param update the update step
269273
* @param task the task containing update
274+
* @param results array of results from previous operations
270275
*
271276
* @returns {Promise|*}
272277
*/
273-
function performUpdate(update, task) {
278+
function performUpdate(update, task, results) {
274279
var Collection = getModel(update.name);
275280

276281
handle$Token(update.condition, true);
@@ -280,11 +285,13 @@ function performUpdate(update, task) {
280285
return storeOldData(update, task)
281286
.then(function(){
282287
return updateState(task, update.index, PENDING)
283-
.then(function(_task){
288+
.then(function () {
284289
return Collection.update(update.condition, update.data, update.options)
285290
.exec()
286-
.then(function(){
287-
return updateState(_task, update.index, DONE);
291+
.then(function (result) {
292+
results.push(result);
293+
294+
return updateState(task, update.index, DONE, results);
288295
});
289296
});
290297
});
@@ -296,10 +303,11 @@ function performUpdate(update, task) {
296303
*
297304
* @param save the save step
298305
* @param task the task containing save
306+
* @param results array of results from previous operations
299307
*
300308
* @returns {Promise|*}
301309
*/
302-
function performSave(save, task){
310+
function performSave(save, task, results) {
303311
var Collection = getModel(save.name);
304312
var doc = new Collection(save.data);
305313
var dataStore = [];
@@ -310,9 +318,11 @@ function performSave(save, task){
310318
task.steps[save.index].dataStore = dataStore;
311319
task.steps[save.index].markModified("dataStore");
312320

313-
return updateState(task, save.index, PENDING).then(function (_task) {
314-
return doc.save().then(function () {
315-
return updateState(_task, save.index, DONE);
321+
return updateState(task, save.index, PENDING).then(function () {
322+
return doc.save().then(function (result) {
323+
results.push(result);
324+
325+
return updateState(task, save.index, DONE, results);
316326
});
317327
});
318328
}
@@ -322,18 +332,21 @@ function performSave(save, task){
322332
*
323333
* @param remove the remove step
324334
* @param task the task containing remove
335+
* @param results array of results from previous operations
325336
*
326337
* @returns {Promise|*}
327338
*/
328-
function performRemove(remove, task){
339+
function performRemove(remove, task, results) {
329340
var Collection = getModel(remove.name);
330341

331342
handle$Token(remove.condition, true);
332343

333344
return storeOldData(remove, task).then(function () {
334-
return updateState(task, remove.index, PENDING).then(function (_task) {
335-
return Collection.remove(remove.condition).exec().then(function () {
336-
return updateState(_task, remove.index, DONE);
345+
return updateState(task, remove.index, PENDING).then(function () {
346+
return Collection.remove(remove.condition).exec().then(function (result) {
347+
results.push(result);
348+
349+
return updateState(task, remove.index, DONE, results);
337350
});
338351
});
339352
});

lib/utils.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
* @since 8/9/16
66
*/
77
var modelCache = {};
8+
var Promise = require("bluebird");
89

910
module.exports = function(_mongoose){
1011
var mongoose = _mongoose || require("mongoose");
1112
var Schema = mongoose.Schema;
1213

13-
function updateState(task, index, state){
14+
function updateState(task, index, state, results) {
1415
task.steps[index].state = state;
15-
return task.save();
16+
17+
return task.save()
18+
.then(function () {
19+
return Promise.resolve(results);
20+
});
1621
}
1722

1823
function getCollection(name, schema){

tests/task.tests.js

+14
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,18 @@ module.exports = describe("Task", function(){
214214
return expect(TestMdlB.find().exec()).to.eventually.have.length(2);
215215
});
216216
});
217+
218+
describe("Results Array", function () {
219+
it("Should have the results of all operations", function () {
220+
var gabe = new TestMdlB({name: "Gabe", age: 34});
221+
222+
return expect(
223+
task.save(gabe)
224+
.save(TEST_COLLECTION_A, {name: "Gabe's Owner", age: 60})
225+
.update(gabe, {age: 64})
226+
.remove(TEST_COLLECTION_A, {name: "Gabe's Owner"})
227+
.run()
228+
).to.eventually.have.length(4);
229+
});
230+
})
217231
});

0 commit comments

Comments
 (0)