0

I'm developing an application and need to add many items at a time.
How can I do that with node.js?

This is the npm module for parse.com but there is no method like

insertAll("Foo", [objs...], ...)

I don't want to insert single object every time.

1
  • Parse doesn't appear to support multi-insert in their service: parse.com/docs/rest#objects-creating. So, you'll likely have to iterate the collection and call .insert() for each. Commented Aug 13, 2013 at 12:31

1 Answer 1

1

Write a convenience function that interfaces between your application and parse.com. You will have to write the iteration code once (or debug mine)

var async = require('async');
var parseApp = require('node-parse-api').Parse;
var APP_ID = "";
var MASTER_KEY = "";
var parseApp = new Parse(APP_ID, MASTER_KEY);

function insertAll(class, objs, callback){
  // create an iterator function(obj,done) that will insert the object
  // with an appropriate group and call done() upon completion.

  var insertOne = 
  ( function(class){
      return function(obj, done){
        parseApp.insert(class, obj, function (err, response) {
          if(err){  return done(err);  }
          // maybe do other stuff here before calling done?
          var res = JSON.parse(response);
          if(!res.objectId){  return done('No object id')  };
          done(null, res.objectId);
        });
      };
    } )(class);

  // async.map calls insertOne with each obj in objs. the callback is executed
  // once every iterator function has called back `done(null,data)` or any one
  // has called back `done(err)`. use async.mapLimit if throttling is needed

  async.map(objs, insertOne, function(err, mapOutput){
    // complete
    if(err){ return callback(err) };
    // no errors
    var objectIds = mapOutput;
    callback(null, objectIds);
  });
};

// Once you've written this and made the function accessible to your other code,
// you only need this outer interface.

insertAll('Foo', [{a:'b'}, {a:'d'}], function(err, ids){
  if(err){ 
    console.log('Error inserting all the Foos');
    console.log(err);
  } else {
    console.log('Success!);
  };
});
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that there are millions of items and parse has limit for requests (1 million)
use the batch api or find a more scalable storage system imo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.