( operations: Array<NormalizedOperation<TRow, TKey>>, ctx: SyncContext<TRow, TKey>, )
| 105 | |
| 106 | // Validate operations before executing |
| 107 | function 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 |
| 139 | export function performWriteOperations< |
no test coverage detected