(
executeCommand: DatabaseExecuteCommand,
tableName: string,
rowIdColumnName: string,
changingColumnNames: string[],
rows: {[id: string]: any[]},
)
| 73 | // PATCHes. Update first so existing rows avoid replacement writes in the upload |
| 74 | // queue, then insert only when RETURNING shows that no row existed. |
| 75 | const powerSyncUpdateThenInsert: Upsert = async ( |
| 76 | executeCommand: DatabaseExecuteCommand, |
| 77 | tableName: string, |
| 78 | rowIdColumnName: string, |
| 79 | changingColumnNames: string[], |
| 80 | rows: {[id: string]: any[]}, |
| 81 | ) => { |
| 82 | const assignments = arrayJoin( |
| 83 | arrayMap( |
| 84 | changingColumnNames, |
| 85 | (columnName, index) => escapeId(columnName) + '=$' + (index + 1), |
| 86 | ), |
| 87 | COMMA, |
| 88 | ); |
| 89 | for (const [id, row] of objToArray(rows, (row, id): [string, any[]] => [ |
| 90 | id, |
| 91 | row, |
| 92 | ])) { |
| 93 | const rowParams = arrayMap(row, (value) => value ?? null); |
| 94 | if ( |
| 95 | arrayIsEmpty( |
| 96 | await executeCommand( |
| 97 | UPDATE + |
| 98 | escapeId(tableName) + |
| 99 | ' SET' + |
| 100 | assignments + |
| 101 | ' ' + |
| 102 | WHERE + |
| 103 | escapeId(rowIdColumnName) + |
| 104 | '=$' + |
| 105 | (row.length + 1) + |
| 106 | ' RETURNING' + |
| 107 | escapeId(rowIdColumnName), |
| 108 | [...rowParams, id], |
| 109 | ), |
| 110 | ) |
| 111 | ) { |
| 112 | const offset = [1]; |
| 113 | await executeCommand( |
| 114 | INSERT + |
| 115 | ' INTO' + |
| 116 | escapeId(tableName) + |
| 117 | '(' + |
| 118 | escapeColumnNames(rowIdColumnName, ...changingColumnNames) + |
| 119 | ')VALUES(' + |
| 120 | getPlaceholders([id, ...row], offset) + |
| 121 | ')', |
| 122 | [id, ...rowParams], |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | }; |
nothing calls this directly
no test coverage detected
searching dependent graphs…