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

Function validateOperations

packages/query-db-collection/src/manual-sync.ts:107–136  ·  view source on GitHub ↗
(
  operations: Array<NormalizedOperation<TRow, TKey>>,
  ctx: SyncContext<TRow, TKey>,
)

Source from the content-addressed store, hash-verified

105
106// Validate operations before executing
107function validateOperations<
108 TRow extends object,
109 TKey extends string | number = string | number,
110>(
111 operations: Array<NormalizedOperation<TRow, TKey>>,
112 ctx: SyncContext<TRow, TKey>,
113): void {
114 const seenKeys = new Set<TKey>()
115
116 for (const op of operations) {
117 // Check for duplicate keys within the batch
118 if (seenKeys.has(op.key)) {
119 throw new DuplicateKeyInBatchError(op.key)
120 }
121 seenKeys.add(op.key)
122
123 // Validate operation-specific requirements
124 // NOTE: These validations check the synced store only, not the combined view (synced + optimistic)
125 // This allows write operations to work correctly even when items are optimistically modified
126 if (op.type === `update`) {
127 if (!ctx.collection._state.syncedData.has(op.key)) {
128 throw new UpdateOperationItemNotFoundError(op.key)
129 }
130 } else if (op.type === `delete`) {
131 if (!ctx.collection._state.syncedData.has(op.key)) {
132 throw new DeleteOperationItemNotFoundError(op.key)
133 }
134 }
135 }
136}
137
138// Execute a batch of operations
139export function performWriteOperations<

Callers 1

performWriteOperationsFunction · 0.85

Calls 2

hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected