(options: {
setupContext?: LockContext
when: Record<DiffTriggerOperation, string>
writeType: (rowId: string) => OperationType
batchQuery: (
lockContext: LockContext,
batchSize: number,
cursor: number,
) => Promise<Array<TableType>>
onReady: () => void
})
| 318 | } |
| 319 | |
| 320 | async function createDiffTrigger(options: { |
| 321 | setupContext?: LockContext |
| 322 | when: Record<DiffTriggerOperation, string> |
| 323 | writeType: (rowId: string) => OperationType |
| 324 | batchQuery: ( |
| 325 | lockContext: LockContext, |
| 326 | batchSize: number, |
| 327 | cursor: number, |
| 328 | ) => Promise<Array<TableType>> |
| 329 | onReady: () => void |
| 330 | }) { |
| 331 | const { setupContext, when, writeType, batchQuery, onReady } = options |
| 332 | |
| 333 | return await database.triggers.createDiffTrigger({ |
| 334 | source: viewName, |
| 335 | destination: trackedTableName, |
| 336 | setupContext, |
| 337 | when, |
| 338 | hooks: { |
| 339 | beforeCreate: async (context) => { |
| 340 | let currentBatchCount = syncBatchSize |
| 341 | let cursor = 0 |
| 342 | while (currentBatchCount == syncBatchSize) { |
| 343 | begin() |
| 344 | |
| 345 | const batchItems = await batchQuery( |
| 346 | context, |
| 347 | syncBatchSize, |
| 348 | cursor, |
| 349 | ) |
| 350 | currentBatchCount = batchItems.length |
| 351 | cursor += currentBatchCount |
| 352 | for (const row of batchItems) { |
| 353 | write({ |
| 354 | type: writeType(row.id), |
| 355 | value: deserializeSyncRow(row), |
| 356 | }) |
| 357 | } |
| 358 | commit() |
| 359 | } |
| 360 | onReady() |
| 361 | database.logger.info( |
| 362 | `Sync is ready for ${viewName} into ${trackedTableName}`, |
| 363 | ) |
| 364 | }, |
| 365 | }, |
| 366 | }) |
| 367 | } |
| 368 | |
| 369 | async function flushDiffRecords(): Promise<void> { |
| 370 | await database |
no test coverage detected