Skip to content

Instantly share code, notes, and snippets.

@Calvein
Created April 29, 2014 04:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Calvein/11390901 to your computer and use it in GitHub Desktop.
Save Calvein/11390901 to your computer and use it in GitHub Desktop.
Get all objects from a parse class
// 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
}
var getObjects = require('./getObjects')
getObjects().done(function(_objects) {
// Do stuff
})
@markmssd
Copy link

Very nice! I've rewrote it so I can re-use it on any class or query:

import Parse from 'parse/react-native';

const ParseObjectsService = {
  getByClass: (className) => {
    let query = new Parse.Query(className);
    return this.getByQuery(query);
  },
  getByQuery: (query) => {
    const limit = 1000;

    let objects = [];
    let totalObjects;

    // Returned promise
    let promise = new Parse.Promise();

    // Get the total of objects
    query.count()
      .then(count => totalObjects = count)
      .catch(error => promise.reject)
      .then(function () {
        let skip = 0;

        const populate = function (_objects) {
          objects = objects.concat(_objects);

          // Set the new skip
          skip += limit;

          // If skip passed the maximum skip limit set by Parse:
          // * Reset skip
          // * Query on the last of the old object
          if (skip > 10000) {
            skip = 0;
            const createdAt = _objects[_objects.length - 1].createdAt;
            query.greaterThanOrEqualTo('createdAt', createdAt);
          }

          // Loop if we don't have all the objects, or
          // Resolve the promise with all the objects when we have them
          (objects.length < totalObjects) ? loop() : promise.resolve(objects);
        };

        let loop = function () {
          query.ascending('createdAt').limit(limit).skip(skip);
          query.find({
            success: populate,
            error: promise.reject
          });
        };
        loop();
      });

    return promise;
  }
};

/* Export ==================================================================== */
module.exports = ParseObjectsService;

Let me know what you think :)

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