Created
April 29, 2014 04:49
-
-
Save Calvein/11390901 to your computer and use it in GitHub Desktop.
Get all objects from a parse class
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
// Returned promise | |
var promise = new Parse.Promise() | |
// Error function | |
var error = function() { | |
console.error('Error:', arguments) | |
// Break it | |
response.error('Query failed, check logs') | |
} | |
var query = new Parse.Query('Object') | |
var objects = [] | |
var totalObjects | |
module.exports = function() { | |
// Get the total of objects | |
query.count({ | |
success: function(count) { totalObjects = count } | |
, error: error | |
}).done(function() { | |
var skip = 0 | |
var populate = function(_objects) { | |
objects = objects.concat(_objects) | |
// Set the new skip | |
skip += 1000 | |
// If skip passed the maximum skip limit: | |
// * Reset skip | |
// * Query on the last of the old object | |
if (skip > 10000) { | |
skip = 0 | |
var createdAt = _objects[_objects.length - 1].createdAt | |
query.greaterThanOrEqualTo('createdAt', createdAt) | |
} | |
// Loop when we don't have all the objects | |
if (objects.length < totalObjects) { | |
loop() | |
// Reslve the promise with all the objects when we have them | |
} else { | |
promise.resolve(objects) | |
} | |
} | |
var loop = function() { | |
query.ascending('createdAt').limit(1000).skip(skip) | |
query.find({ | |
success: populate | |
, error: error | |
}) | |
} | |
loop() | |
}) | |
return promise | |
} |
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 getObjects = require('./getObjects') | |
getObjects().done(function(_objects) { | |
// Do stuff | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice! I've rewrote it so I can re-use it on any class or query:
Let me know what you think :)