Skip to content

Instantly share code, notes, and snippets.

@clemblanco
Created March 18, 2016 17:20
Show Gist options
  • Save clemblanco/644eae2426a50cb2b98d to your computer and use it in GitHub Desktop.
Save clemblanco/644eae2426a50cb2b98d to your computer and use it in GitHub Desktop.
Parse - CloudCode - Retrieve all objects from a specific Parse class without limitation
Parse.Cloud.define("retrieveAllObjects", function(request, status) {
var result = [];
var chunk_size = 1000;
var processCallback = function(res) {
result = result.concat(res);
if (res.length === chunk_size) {
process(res[res.length-1].id);
} else {
status.success(result);
}
};
var process = function(skip) {
var query = new Parse.Query(request.params.object_type);
if (skip) {
query.greaterThan("objectId", skip);
}
if (request.params.update_at) {
query.greaterThan("updatedAt", request.params.update_at);
}
if (request.params.only_objectId) {
query.select("objectId");
}
query.limit(chunk_size);
query.ascending("objectId");
query.find().then(function (res) {
processCallback(res);
}, function (error) {
status.error("query unsuccessful, length of result " + result.length + ", error:" + error.code + " " + error.message);
});
};
process(false);
});
@clemblanco
Copy link
Author

Consumed by using either a Promise (or not, it's up to you):

Parse.Cloud.run('retrieveAllObjects', {
    object_type: "MyClass", // REQUIRED - string: name of your Parse class
    update_at: moment().toDate(), // OPTIONAL - JS Date object: Only retrieve objects where update_at is higher than...
    only_objectId: true|false // OPTIONAL - boolean: the result will only be composed by objectId + date fields, otherwise all attributes are returned.
}).then(function(objects) {
    /* SUCCESS */
    // if objects.length > 0 objects can be looped through
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment