|
1 |
| -import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
2 | 1 | import { AbstractPowerSyncDatabase, Column, ColumnType, CrudEntry, Schema, Table, UpdateType } from '@powersync/common';
|
3 | 2 | import { PowerSyncDatabase } from '@powersync/web';
|
| 3 | +import pDefer from 'p-defer'; |
4 | 4 | import { v4 as uuid } from 'uuid';
|
| 5 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
5 | 6 | import { generateTestDb } from './utils/testDb';
|
6 |
| -import pDefer from 'p-defer'; |
7 | 7 |
|
8 | 8 | const testId = '2290de4f-0488-4e50-abed-f8e8eb1d0b42';
|
9 | 9 |
|
@@ -311,4 +311,35 @@ describe('CRUD Tests', () => {
|
311 | 311 | await txPromise;
|
312 | 312 | expect(await r).toEqual(null);
|
313 | 313 | });
|
| 314 | + |
| 315 | + it('CRUD Batch Limits', async () => { |
| 316 | + const initialBatch = await powersync.getCrudBatch(); |
| 317 | + expect(initialBatch, 'Initial CRUD batch should be null').null; |
| 318 | + |
| 319 | + /** |
| 320 | + * Create some items. Use Multiple transactions to demonstrate getCrudBatch does not |
| 321 | + * group by transaction. |
| 322 | + */ |
| 323 | + for (let i = 0; i < 2; i++) { |
| 324 | + await powersync.writeTransaction(async (tx) => { |
| 325 | + for (let j = 0; j < 51; j++) { |
| 326 | + await tx.execute(`INSERT INTO assets(id, description) VALUES(uuid(), ?)`, [`test-${i}-${j}`]); |
| 327 | + } |
| 328 | + }); |
| 329 | + } |
| 330 | + |
| 331 | + // This should contain CRUD entries for the first and second transaction |
| 332 | + const smallBatch = await powersync.getCrudBatch(55); |
| 333 | + expect(smallBatch, 'CRUD items should be present').exist; |
| 334 | + expect(smallBatch?.crud.length, 'Should only be 55 CRUD items').eq(55); |
| 335 | + expect(smallBatch?.haveMore, 'There should be more CRUD items pending').true; |
| 336 | + |
| 337 | + const defaultBatch = await powersync.getCrudBatch(); |
| 338 | + expect(defaultBatch?.crud.length, 'Should use default limit').eq(100); |
| 339 | + expect(defaultBatch?.haveMore, 'There should be more CRUD items pending').true; |
| 340 | + |
| 341 | + const maxBatch = await powersync.getCrudBatch(1000); |
| 342 | + expect(maxBatch?.crud.length, 'Should contain all created CRUD items').eq(102); |
| 343 | + expect(maxBatch?.haveMore, 'There should not be any more pending CRUD items').false; |
| 344 | + }); |
314 | 345 | });
|
0 commit comments