javascriptnode.jsapigeeusergrid

Apigee Usergrid: Mass delete option missing


I am using usergrid to store data for a customer project. It's got two collections carShowrooms and cars. So far I am good. But I have a scenario where I have refresh the masterdata of the collection cars. Everytime I do this, I have to delete all the existing data in cars and replace it with incoming cars data from the master inventory system.

Now, with the docu in https://www.npmjs.org/package/usergrid, I see that I can only destroy one car at a time.

car.destroy(function(err){
    if (err){
        //error - car not deleted
        //winston log - tbd
    } else {
        //success - car deleted
    }
});

This is ok for smaller showrooms, but bigger multibrand showrooms have variety of cars - sometimes even upto 50 different varieties (8 car brands * approx. 8 different options).

Is there a mass delete option? can someone please point me to a docu if I am missing something here.

P.S. I am new to usergrid, if this is a repeated question, please mark so and point me to the right url


Solution

  • There currently isn't a mass delete function in the Usergrid Node SDK, but you can create one. This is how I added a monkey-patched delete-by-query function into the Node SDK:

    Usergrid.client.prototype.delete = function(opts, callback) {
      if (_.isFunction(opts)) { callback = opts; opts = undefined; }
    
      if (!opts.qs.q) { opts.qs.q = '*'; }
    
      var options = {
        method: 'DELETE',
        endpoint: opts.type,
        qs: opts.qs
      };
      var self = this;
      this.request(options, function (err, data) {
        if (err && self.logging) {
          console.log('entities could not be deleted');
        }
        if (typeof(callback) === 'function') {
          callback(err, data);
        }
      });
    };
    

    Hope that helps! Scott