Last active
March 22, 2016 14:40
-
-
Save svenvarkel/1a6bf57066c270fd4f7c to your computer and use it in GitHub Desktop.
This file contains 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
var Promise = require('bluebird'); | |
var myType = 'mytype'; | |
var nativePromise = new Promise(function (resolve, reject) { | |
sails.models[mytype].native(function (err, collection) { | |
if (err) { | |
return reject(err); | |
} | |
var pipeline = []; //some pipeline | |
collection.aggregate(pipeline).toArray(function (err, itemList) { | |
if (err) { | |
return reject(err); | |
} | |
var unserializedValues = []; | |
itemList.forEach(function(value) { | |
value = sails.models[mytype]._transformer.unserialize(value); | |
unserializedValues.push(value); | |
}); | |
return resolve(unserializedValues); | |
}) | |
}) | |
}); | |
return nativePromise.then(function (itemList) { | |
return Promise.map(itemList, function (item) { | |
/** | |
* Here's the part that does the main trick - returns my "native" item as Waterline object. | |
*/ | |
return new sails.models[mytype]._model(item); | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment