MCPcopy Index your code
hub / github.com/tinyplex/tinybase / powerSyncUpdateThenInsert

Function powerSyncUpdateThenInsert

src/persisters/persister-powersync/index.ts:75–126  ·  view source on GitHub ↗
(
  executeCommand: DatabaseExecuteCommand,
  tableName: string,
  rowIdColumnName: string,
  changingColumnNames: string[],
  rows: {[id: string]: any[]},
)

Source from the content-addressed store, hash-verified

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.
75const 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};

Callers

nothing calls this directly

Calls 7

arrayJoinFunction · 0.90
arrayMapFunction · 0.90
escapeIdFunction · 0.90
objToArrayFunction · 0.90
arrayIsEmptyFunction · 0.90
escapeColumnNamesFunction · 0.90
getPlaceholdersFunction · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…