pg-promise 更新自定义数组中的位置

pg-promise update where in custom array

如何使用 npm pg-promise 包编写以下 postgresql 查询?

update schedule
set student_id = 'a1ef71bc6d02124977d4'
where teacher_id = '6b33092f503a3ddcc34' and (start_day_of_week, start_time) in (VALUES ('M', (cast('17:00:00' as time))), ('T', (cast('19:00:00' as time))));

我在格式化程序命名空间中没有看到任何可以帮助实现这一点的东西。 https://vitaly-t.github.io/pg-promise/formatting.html

我无法将 'cast' 片段注入到 '17:00:00' 值中,除非它被视为时间字符串本身的一部分。

第一条查询很简单。 VALUES后面的部分我想不通。

第一篇:

var query = `update schedule
set student_id = 
where teacher_id =  and (start_day_of_week, start_time) in (VALUES )`;

var inserts = [studentId, teacherId, values];

我现在以 3 美元的价格使用这种混乱(还没有用),但它完全绕过了 pg-promise 中内置的所有 escaping/security:

const buildPreparedParams = function(arr, colNames){
  let newArr = [];
  let rowNumber = 0
  arr.forEach((row) => {
  const rowVal = (rowNumber > 0 ? ', ' : '') + 
  `('${row.startDayOfWeek}', (cast('${row.startTime}' as time)))`;
    newArr.push(rowVal);
  });
  return newArr;
};

我试图转换成这个 sql 查询的结构是:

[{
  "startTime":"17:00:00",
  "startDayOfWeek":"U"
 },
 {
  "startTime":"16:00:00",
  "startDayOfWeek":"T"
}]

最后一部分使用 CSV FilterIN (VALUES :csv)

为了使数组中的每一项都正确格式化,应用 Custom Type Formatting:

const data = [{
    startTime: '17:00:00',
    startDayOfWeek: 'U'
  },
  {
    startTime: '16:00:00',
    startDayOfWeek: 'T'
  }];

const values = data.map(d => ({
    toPostgres: () => pgp.as.format('(${startDayOfWeek},(cast(${startTime} as time))', d),
    rawType: true
}));

现在为 :csv 传递 values 将正确格式化您的值:

('U',(cast('17:00:00' as time)),('T',(cast('16:00:00' as time))