(getContext: () => SyncContext<TRow, TKey> | null)
| 228 | |
| 229 | // Factory function to create write utils |
| 230 | export function createWriteUtils< |
| 231 | TRow extends object, |
| 232 | TKey extends string | number = string | number, |
| 233 | TInsertInput extends object = TRow, |
| 234 | >(getContext: () => SyncContext<TRow, TKey> | null) { |
| 235 | function ensureContext(): SyncContext<TRow, TKey> { |
| 236 | const context = getContext() |
| 237 | if (!context) { |
| 238 | throw new SyncNotInitializedError() |
| 239 | } |
| 240 | return context |
| 241 | } |
| 242 | |
| 243 | return { |
| 244 | writeInsert(data: TInsertInput | Array<TInsertInput>) { |
| 245 | const operation: SyncOperation<TRow, TKey, TInsertInput> = { |
| 246 | type: `insert`, |
| 247 | data, |
| 248 | } |
| 249 | |
| 250 | const ctx = ensureContext() |
| 251 | const batchContext = activeBatchContexts.get(ctx) |
| 252 | |
| 253 | // If we're in a batch, just add to the batch operations |
| 254 | if (batchContext?.isActive) { |
| 255 | batchContext.operations.push(operation) |
| 256 | return |
| 257 | } |
| 258 | |
| 259 | // Otherwise, perform the operation immediately |
| 260 | performWriteOperations(operation, ctx) |
| 261 | }, |
| 262 | |
| 263 | writeUpdate(data: Partial<TRow> | Array<Partial<TRow>>) { |
| 264 | const operation: SyncOperation<TRow, TKey, TInsertInput> = { |
| 265 | type: `update`, |
| 266 | data, |
| 267 | } |
| 268 | |
| 269 | const ctx = ensureContext() |
| 270 | const batchContext = activeBatchContexts.get(ctx) |
| 271 | |
| 272 | if (batchContext?.isActive) { |
| 273 | batchContext.operations.push(operation) |
| 274 | return |
| 275 | } |
| 276 | |
| 277 | performWriteOperations(operation, ctx) |
| 278 | }, |
| 279 | |
| 280 | writeDelete(key: TKey | Array<TKey>) { |
| 281 | const operation: SyncOperation<TRow, TKey, TInsertInput> = { |
| 282 | type: `delete`, |
| 283 | key, |
| 284 | } |
| 285 | |
| 286 | const ctx = ensureContext() |
| 287 | const batchContext = activeBatchContexts.get(ctx) |
no outgoing calls
no test coverage detected