Skip to content

Instantly share code, notes, and snippets.

@svenvarkel
Last active March 22, 2016 14:40
Show Gist options
  • Save svenvarkel/1a6bf57066c270fd4f7c to your computer and use it in GitHub Desktop.
Save svenvarkel/1a6bf57066c270fd4f7c to your computer and use it in GitHub Desktop.
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