lonelymo
lonelymo

Reputation: 4192

Apigee BaaS: Create multiple entities using usergrid node.js sdk doesn't work

I am using Apigee BaaS. From the UI i.e., apigee.com/appservices, I can create multiple entities at the same time using a JSON array. For example the json array here creates three entities on collection /employees.

    [
        {
            "name": "John",
            "employeeId": "1"
        },
        {
            "name": "Doe",
            "employeeId": "2"
        },
        {
            "name": "Danny",
            "employeeId": "3"
        }
    ]

Now I am trying to emulate the same with the nodeJs SDK - https://github.com/usergrid/usergrid/tree/master/sdks/nodejs

        client.createEntity(options, function(err, entity) {
            if (err) {
                //error - entity not created
            } else {
                    //set is additive, so previously set properties are not overwritten
                entity.set(entityJsonArr);

                //finally, call save on the object to save it back to the database
                entity.save(function(err) {
                    if (err) {
                        //error - entity not saved
                        console.log('failure');
                    } else {
                        //success - new entity is saved
                        console.log('success');
                    }
                });
            }
        });

This doesnt help in creating multiple entities at the same time. Method createCollection creates a collection and not necessarily bunch of entities. Can anyone please help me with this?

Or should I just go ahead use request and fire a HTTP post on the Apigee BaaS? In that case I wouldnt be using the sdk.

Upvotes: 1

Views: 521

Answers (1)

Scott Ganyo
Scott Ganyo

Reputation: 311

You're correct, the existing Node SDK doesn't handle this case. In case you want and it's useful to you, I used the following monkey patch to work around this for now:

var Usergrid = require('usergrid');

Usergrid.client.prototype.batchCreate = function (type, entities, callback) {
  if (!entities.length) { callback(); }

  var data = _.map(entities, function(entity) {
    var data = (entity instanceof Usergrid.entity) ? entity.get() : entity;
    return _.omit(data, 'metadata', 'created', 'modified', 'type', 'activated');
  });

  var options =  {
    method: 'POST',
    endpoint: type,
    body: data
  };

  var self = this;
  this.request(options, function (err, data) {
    if (err && self.logging) {
      console.log('could not save entities');
      if (typeof(callback) === 'function') { callback(err, data); }
      return;
    }

    var entities = _.map(data.entities, function(data) {
      var options = {
        type: type,
        client: self,
        uuid: data.uuid,
        data: data || {}
      };
      var entity = new Usergrid.entity(options);
      entity._json = JSON.stringify(options.data, null, 2);
      return entity;
    });

    if (typeof(callback) === 'function') {
      return callback(err, entities);
    }
  });
};

Upvotes: 2

Related Questions