(
operations:
| SyncOperation<TRow, TKey, TInsertInput>
| Array<SyncOperation<TRow, TKey, TInsertInput>>,
ctx: SyncContext<TRow, TKey>,
)
| 137 | |
| 138 | // Execute a batch of operations |
| 139 | export 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( |
no test coverage detected