我正在运行以下代码
const pgp = require('pg-promise')({capSQL: true});
// columns set holder object
const cs = {}
function createColumnsets(pgp) {
// create all ColumnSet objects only once:
if (!cs.insert) {
// Type TableName is useful when schema isn't default "public" ,
// otherwise you can just pass in a string for the table name.
// const table = new pgp.helpers.TableName({table: 'users', schema: 'public'});
cs.insert = new pgp.helpers.ColumnSet(
[
'email',
'password',
'employee_id',
'full_name',
'role',
{
name: 'created_by',
skip: (col) => true,
}
],
{table: 'users'});
cs.update = cs.insert.extend(
[
{
name: 'last_modified_at',
def: 'CURRENT_TIMESTAMP'
},
'last_modified_by'
],
);
// cs.update = cs.insert.extend(['?id']);
}
return cs;
}
createColumnsets(pgp);
const updateDTO = [
{
email: '[email protected]',
password: 'password',
employee_id: 121,
full_name: 'Joe Pickett',
role: 'admin',
// created_by: 'Joe Pickett',
last_modified_by: 'Joe Pickett',
}
];
const update = pgp.helpers.update(updateDTO, cs.update);
console.log('\n', update);
文档提供以下示例
{
name: 'total-val',
prop: 'total',
skip(col) {
// skip from updates, if 'amount' is 0:
return col.source.amount === 0;
}
}
我试过了
skip: true 和 skip: (col) => true
all 生成“错误:属性‘created_by’不存在。”
回到文档我相信我已经满足以下条件
跳过助手.skipCB 动态跳过列的覆盖。
由方法更新(对于单个对象)和集合使用,被方法插入和值忽略。
设置条件标志cnd时也会被忽略