首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >:大量删除选项缺失

:大量删除选项缺失
EN

Stack Overflow用户
提问于 2014-08-08 14:31:25
回答 2查看 318关注 0票数 4

我使用用户网格来存储客户项目的数据。它有两个收藏carShowrooms和汽车。到目前为止我还不错。但我有一个场景,我刷新了收集车的主数据。每次我这样做,我必须删除所有现有的数据在汽车,并取代它从主库存系统收到的汽车数据。

现在,使用https://www.npmjs.org/package/usergrid中的docu,我看到我一次只能摧毁一辆车。

代码语言:javascript
复制
car.destroy(function(err){
    if (err){
        //error - car not deleted
        //winston log - tbd
    } else {
        //success - car deleted
    }
});

这对于较小的展厅来说是可以的,但是更大的多品牌展厅有各种各样的汽车--有时甚至多达50个不同的品种(8个汽车品牌*大约。8不同的备选方案)。

有大规模删除选项吗?如果我漏掉了什么东西,谁能给我指点一下医生吗?

我是用户网格的新手,如果这是一个重复的问题,请这样标记,并将我指向正确的url。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-08 15:43:37

目前Usergrid中没有大规模删除函数,但是您可以创建一个。这就是我如何在Node中添加了一个猴子补丁的删除查询函数:

代码语言:javascript
复制
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);
    }
  });
};

希望这能帮上忙!史考特

票数 3
EN

Stack Overflow用户

发布于 2014-08-08 16:44:49

如果您愿意的话,我已经编写了一个Node.js大容量删除器,它并行运行删除请求。删除1000个实体大约需要3分钟。

这里有一个总是最新的要旨,以及一个这样的副本:

代码语言:javascript
复制
// 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25205880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档