Apigee Usergrid:缺少批量删除选项

lon*_*ymo 4 javascript node.js apigee usergrid

我使用usergrid来存储客户项目的数据.它有两个系列carShowrooms和汽车.到目前为止,我很好.但我有一个场景,我已经刷新了收集车的masterdata.每次我这样做,我都必须删除汽车中的所有现有数据,并将其替换为主库存系统中的传入汽车数据.

现在,通过https://www.npmjs.org/package/usergrid中的文档 ,我发现我一次只能销毁一辆汽车.

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

这对于较小的陈列室来说是好的,但更大的多品牌陈列室有各种各样的汽车 - 有时甚至多达50种不同的品种(8个汽车品牌*大约8种不同的选择).

有批量删除选项吗?如果我在这里遗漏了什么,有人可以指点我.

PS我是usergrid的新手,如果这是一个重复的问题,请标记并指出我正确的网址

bra*_*ipt 6

如果您如此倾向,我已经编写了一个Node.js批量删除器,它可以并行运行删除请求.删除1000个实体大约需要3分钟.

这是一个总是最新的要点,以及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();
Run Code Online (Sandbox Code Playgroud)