首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bookshelf.js:批量更新

Bookshelf.js:批量更新
EN

Stack Overflow用户
提问于 2018-06-13 13:12:47
回答 1查看 1.7K关注 0票数 0

我正在用Bookshelf.js编辑一些记录。记录的数量可能相当高。为了速度,我希望批量更新这些记录,或者至少在事务上下文中运行update循环。我如何在Bookshelf.js中做到这一点?如果这是不可能的,我如何使用Knex?

下面是我当前的更新功能:

代码语言:javascript
复制
new Endpoint()
                    .where({
                        'organization_id': orgId,
                        'id': endpointId
                    })
                    .save(updatedEndpoint, {patch: true})
                    .then((endpoint) => {
                        Endpoint
                            .where({
                                'organization_id': orgId,
                                'id': endpointId
                            })
                            .fetch({withRelated: ['settings', 'organization']})
                            .then((newEndpoint) => {
                                ReS(res, {
                                    endpoint: newEndpoint
                                }, 200);
                            })
                            .catch(e => TE(e));
                    })
                    .catch(e => TE(e));
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 18:26:09

您将不得不求助于查询生成器(knex.js)来进行批量更新:

代码语言:javascript
复制
Endpoint
  .query()
  .whereIn('id', [1, 2, 3, /* ... */])
  .andWhere({'organization_id': orgId})
  .update({whatever: 'a value'})
  .then(function(results) {
    // ...
  })

有关更多信息,请参见第402期

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50838192

复制
相关文章

相似问题

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