(
table: string,
columns: string[],
types: string[],
values: any[][],
where: string,
set: string,
params?: { trx: any },
)
| 1 | import { Model } from 'objection'; |
| 2 | |
| 3 | export async function patchMultiple( |
| 4 | table: string, |
| 5 | columns: string[], |
| 6 | types: string[], |
| 7 | values: any[][], |
| 8 | where: string, |
| 9 | set: string, |
| 10 | params?: { trx: any }, |
| 11 | ) { |
| 12 | if (values.length === 0) { |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | let query = Model.knex().raw( |
| 17 | ` |
| 18 | UPDATE |
| 19 | ${table} |
| 20 | SET |
| 21 | ${set} |
| 22 | FROM ( |
| 23 | VALUES |
| 24 | ${values |
| 25 | .map((row) => `(${row.map((_, i) => `?::${types[i]}`).join(', ')})`) |
| 26 | .join(', ')} |
| 27 | ) AS values (${columns.join(', ')}) |
| 28 | WHERE |
| 29 | ${where} |
| 30 | `, |
| 31 | |
| 32 | [...values.flat()], |
| 33 | ); |
| 34 | |
| 35 | if (params?.trx) { |
| 36 | query = query.transacting(params.trx); |
| 37 | } |
| 38 | |
| 39 | return await query; |
| 40 | } |
no outgoing calls
no test coverage detected