如何使用 pg Promise 更新多行?

问题描述 投票:0回答:1

我想用 pg Promise 更新多行,但我不完全理解它

这是我的代码

我不知道在哪里可以访问我的 plan_id

    const cs = new pgp.helpers.ColumnSet(
      ['user_id', 'tenant_id', 'location_id', 'start_time', 'end_time', 'time_range', 'status', 'info'], { table: 'workers_plan' });

    const cs_values: UpdateWorkersPlan[] = plan.map((el: UpdateWorkersPlan) => ({
      plan_id: el.plan_id,
      user_id: el.user_id,
      tenant_id,
      location_id: el.location_id,
      start_time: el.start_time,
      end_time: el.end_time,
      time_range: tsrange(el.start_time, el.end_time),
      status: el.status,
      info: el.info,
    }));

    pgp.as.format('WHERE plan_id = $1', [how to access plan id ?])

    await pgp.helpers.update(cs_values, cs)
node.js pg pg-promise
1个回答
0
投票

根据我的经验,要使用 pg-promise 更新多行,您可以使用

pgp.helpers.update
方法。

因此您应该在

plan_id
定义中包含
ColumnSet
并将其映射到
cs_values
:

(尚未测试此代码)

const cs = new pgp.helpers.ColumnSet(
  ['plan_id', 'user_id', 'tenant_id', 'location_id', 'start_time', 'end_time', 'time_range', 'status', 'info'], { table: 'workers_plan' });

const cs_values = plan.map((el) => ({
  plan_id: el.plan_id,
  user_id: el.user_id,
  tenant_id,
  location_id: el.location_id,
  start_time: el.start_time,
  end_time: el.end_time,
  time_range: tsrange(el.start_time, el.end_time),
  status: el.status,
  info: el.info,
}));

await pgp.helpers.update(cs_values, cs);

文档:https://vitaly-t.github.io/pg-promise/helpers.html

© www.soinside.com 2019 - 2024. All rights reserved.