-
-
Save robotlolita/b8cfca06b6ebfa2525d1 to your computer and use it in GitHub Desktop.
For my SO question http://stackoverflow.com/q/33175305/4825465
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Logic MUST BE identical to the synchronous way: | |
// https://gist.github.com/melissamarima/2b9e8594892103dbc02c | |
var mongoose = require('mongoose'); | |
var collectionASchema = new mongoose.Schema({field1: Number, field2: Number}); | |
var collectionBSchema = new mongoose.Schema({field1: Number, field2: Number}); | |
collectionASchema.index({field1: 1}); | |
collectionBSchema.index({field1: -1}); | |
mongoose.model('collectionA', collectionASchema); | |
mongoose.model('collectionB', collectionBSchema); | |
collectionBSchema.methods.addToCollectionB = function(cb){ | |
// `this` is the newDocB | |
var newDocB = this; | |
var collectionA = mongoose.model("collectionA"); | |
// set up the recursive promise loop | |
// returns nothing -- should break out of this recursion at the end | |
function promiseRecursion(){ | |
// 2. Get the current collection A's first element | |
var colAFirstGetPromise = collectionA.find({}).sort({price:1}).limit(1).exec(); | |
colAFirstGetPromise.then(function (collectionADocsArray){ | |
var docA = collectionADocsArray[0]; | |
// 3. check loop condition, break out of loop if satisfied | |
if (docA.field2 <= 0){ | |
return; // EXISTS LOOP! TODO: make sure this breaks out of the loop | |
} | |
// 4. check which of the two ways to modify newDocB | |
if (docA.field1 <= 0){ | |
// 6. one other way breaks out of the loop | |
newDocB.field1 = 0; | |
return; // EXISTS LOOP! TODO: make sure this breaks out of the loop | |
} | |
// else .. 5. one way removes docA from collectionA | |
newDocB.field1 -= docA.field1; | |
return collectionA.findByIdAndRemove(docA.id).exec(); | |
}).then(function(doc){ | |
// doc A should be removed at this point | |
// 7. if collectionA is empty, break out of the loop | |
return collectionA.find().exec(); | |
}).then(function(collectionADocsArray){ | |
if (collectionADocsArray.length == 0){ | |
return; // EXISTS LOOP! TODO: make sure this breaks out of the loop | |
} | |
// 8. loads the new docA and continues to the next loop | |
return colAFirstGetPromise; | |
}).then(function (collectionADocsArray){ | |
docA = collectionADocsArray[0]; | |
// loop back | |
return promiseRecursion(); | |
// handles error for all the promises above | |
}, function(err){ | |
return err; | |
}); | |
} // end of promiseRecursion(); | |
// START HERE!!!!!!!!! THIS IS THE FIRST STEP | |
// 1. If collection A is empty, push newDocB onto collection B, and stop | |
collectionA.find(function(err, collectionADocsArray){ | |
if (err){return err;} | |
if (collectionADocsArray.length == 0){ | |
newDocB.save(cb); // saves newDocB to collectionB | |
return; // ends addToCollectionB() | |
} | |
promiseRecursion(); | |
// 9. Loop is done, check if newDocB needs to be saved | |
//TODO: QUESTION QUESTION!!!!!!!!!! | |
// Was the newDocB modified in promiseRecursion() the same as the one below??? | |
if (newDocB.field1 >= 0){ | |
newDocB.save(cb); // saves newDocB to collectionB | |
return; // ends addToCollectionB() | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment