Apigee Usergrid: Bulk delete option missing

I am using usergrid to store data for a client project. He received two collections of carShowrooms and cars. So far I'm good. But I have a script where I update the masterdata of collectible cars. Every time I do this, I have to delete all existing data in cars and replace it with the data of incoming cars from the main inventory system.

Now, with the doc at 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 normal for small showrooms, but large multi-brand showrooms have a variety of vehicles - sometimes even up to 50 different varieties (8 car brands - roughly 8 different options).

Is there a bulk delete option? can someone please point me to a doc if i am missing something.

PS I am new to usergrid, if this is a duplicate question please tick so and point me to correct url

+3


source to share


2 answers


There is currently no bulk delete feature in the Usergrid Node SDK, but you can create one. This is how I added the monkey request delete function to the Node SDK file:

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 it helps! Scott

+3


source


If you are so inclined, I wrote a Node.js file that runs delete requests in parallel. It takes about 3 minutes to remove 1000 objects.

Here's an always up-to-date gist and copy for SO:



// Installation 

// 1. Install Node.js http://nodejs.org/download/
// 2. In Terminal, cd (navigate) to the directory where you saved this file
// 3. Run 'npm install request async'
// 4. Edit the script config below with your token, org, app, and collection name.
// 5. To run the script, at the Terminal prompt, run 'node api_baas_deleter.js'

// Config

var access_token = "{token}";
var as_basepath = "http://api.usergrid.com/{org}/{app}/"; // You need the trailing slash!
var collection = "{collection_name}";

// End Config

var request = require('request');
var async = require('async');

var authstring = "access_token=" + access_token;

var total = 0;
var startTime = Date.now();

function deleteRecords(callback) {
    request.get({
        url: as_basepath + collection + "?" + authstring,
        json: true
    }, function(e, r, body) {
        if (body.count === undefined) {
            var err = "Error: invalid endpoint. Check your basepath and collection name.";
            console.log(err);
            if (typeof(callback) === 'function') {
                callback(err)
            }
        } else {
            // console.log("Found " + body.count + " entities");
            if (body.count > 0) {
                var deletes = [];
                for (var i = 0; i < body.count; i++) {
                    deletes.push({
                        url: as_basepath + collection + "/" + body.entities[i].uuid + "?" + authstring,
                        json: true
                    });
                    console.log("Deleting " + body.entities[i].uuid)
                }
                async.each(deletes, function(options, callback) {
                    request.del(options, function(e, r, body) {
                        if (r.statusCode === 200) {
                            total++;
                        }
                        callback(e);
                    });
                }, function(err) {
                    setTimeout(function() {
                        deleteRecords(collection, function(e) {
                            callback(e);
                        });
                    }, 600); // Mandatory, since it seems to not retrieve entities if you make a request in < 600ms
                });
            } else {
                var timeInMinutes = minutesFromMs(Date.now() - startTime);
                console.log("Deleted " + total + " entities in " + timeInMinutes + " minute" + ((timeInMinutes > 1 || timeInMinutes < 1) ? "s" : ""));
                if (typeof(callback) === 'function') {
                    callback()
                }
            }
        }
    });
}

function minutesFromMs(time) {
    return Math.round(((time % 86400000) % 3600000) / 60000).toString();
}

deleteRecords();

      

+6


source







All Articles