MCPcopy Create free account
hub / github.com/TanStack/db / performWriteOperations

Function performWriteOperations

packages/query-db-collection/src/manual-sync.ts:139–227  ·  view source on GitHub ↗
(
  operations:
    | SyncOperation<TRow, TKey, TInsertInput>
    | Array<SyncOperation<TRow, TKey, TInsertInput>>,
  ctx: SyncContext<TRow, TKey>,
)

Source from the content-addressed store, hash-verified

137
138// Execute a batch of operations
139export function performWriteOperations<
140 TRow extends object,
141 TKey extends string | number = string | number,
142 TInsertInput extends object = TRow,
143>(
144 operations:
145 | SyncOperation<TRow, TKey, TInsertInput>
146 | Array<SyncOperation<TRow, TKey, TInsertInput>>,
147 ctx: SyncContext<TRow, TKey>,
148): void {
149 const normalized = normalizeOperations(operations, ctx)
150 validateOperations(normalized, ctx)
151
152 // Use immediate: true to ensure syncedData is updated synchronously,
153 // even when called from within a mutationFn with an active persisting transaction
154 ctx.begin({ immediate: true })
155
156 for (const op of normalized) {
157 switch (op.type) {
158 case `insert`: {
159 const resolved = ctx.collection.validateData(op.data, `insert`)
160 ctx.write({
161 type: `insert`,
162 value: resolved,
163 })
164 break
165 }
166 case `update`: {
167 // Get from synced store only, not the combined view
168 const currentItem = ctx.collection._state.syncedData.get(op.key)!
169 const updatedItem = {
170 ...currentItem,
171 ...op.data,
172 }
173 const resolved = ctx.collection.validateData(
174 updatedItem,
175 `update`,
176 op.key,
177 )
178 ctx.write({
179 type: `update`,
180 value: resolved,
181 })
182 break
183 }
184 case `delete`: {
185 // Get from synced store only, not the combined view
186 const currentItem = ctx.collection._state.syncedData.get(op.key)!
187 ctx.write({
188 type: `delete`,
189 value: currentItem,
190 })
191 break
192 }
193 case `upsert`: {
194 // Check synced store only, not the combined view
195 const existsInSyncedStore = ctx.collection._state.syncedData.has(op.key)
196 const resolved = ctx.collection.validateData(

Callers 5

writeInsertFunction · 0.85
writeUpdateFunction · 0.85
writeDeleteFunction · 0.85
writeUpsertFunction · 0.85
writeBatchFunction · 0.85

Calls 8

normalizeOperationsFunction · 0.85
validateOperationsFunction · 0.85
fromMethod · 0.80
validateDataMethod · 0.45
getMethod · 0.45
hasMethod · 0.45
commitMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected