Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

powersync.test.ts

630 lines18.0 KB

1import { randomUUID } from 'node:crypto'
2import { tmpdir } from 'node:os'
3import {
4 CrudEntry,
5 PowerSyncDatabase,
6 Schema,
7 Table,
8 column,
9} from '@powersync/node'
10import {
11 createCollection,
12 createTransaction,
13 eq,
14 liveQueryCollectionOptions,
15} from '@tanstack/db'
16import { describe, expect, it, onTestFinished, vi } from 'vitest'
17import { powerSyncCollectionOptions } from '../src'
18import { PowerSyncTransactor } from '../src/PowerSyncTransactor'
19import { TEST_DATABASE_IMPLEMENTATION } from './test-db-implementation'
20import type { AbstractPowerSyncDatabase, LockContext } from '@powersync/node'
21import type { PendingMutation } from '@tanstack/db'
22​
23const APP_SCHEMA = new Schema({
24 users: new Table({
25 name: column.text,
26 active: column.integer, // Will be mapped to Boolean
27 }),
28 documents: new Table(
29 {
30 name: column.text,
31 author: column.text,
32 created_at: column.text, // Will be mapped to Date
33 },
34 {
35 trackMetadata: true,
36 },
37 ),
38})
39​
40const describePowerSync = TEST_DATABASE_IMPLEMENTATION
41 ? describe
42 : describe.skip
43​
44describePowerSync(`PowerSync Integration`, () => {
45 async function createDatabase() {
46 const db = new PowerSyncDatabase({
47 database: {
48 dbFilename: `test-${randomUUID()}.sqlite`,
49 dbLocation: tmpdir(),
50 implementation: TEST_DATABASE_IMPLEMENTATION,
51 },
52 schema: APP_SCHEMA,
53 })
54 onTestFinished(async () => {
55 await db.disconnectAndClear()
56 await db.close()
57 })
58 // Initial clear in case a test might have failed
59 await db.disconnectAndClear()
60 return db
61 }
62​
63 function createDocumentsCollection(db: PowerSyncDatabase) {
64 const collection = createCollection(
65 powerSyncCollectionOptions({
66 database: db,
67 // We get typing and a default validator from this
68 table: APP_SCHEMA.props.documents,
69 }),
70 )
71 onTestFinished(() => collection.cleanup())
72 return collection
73 }
74​
75 async function createTestData(db: AbstractPowerSyncDatabase) {
76 await db.execute(`
77 INSERT into documents (id, name)
78 VALUES
79 (uuid(), 'one'),
80 (uuid(), 'two'),
81 (uuid(), 'three')
82 `)
83 }
84​
85 describe(`sync`, () => {
86 it(`should initialize and fetch initial data`, async () => {
87 const db = await createDatabase()
88 await createTestData(db)
89 const collection = createDocumentsCollection(db)
90​
91 await collection.stateWhenReady()
92​
93 // Verify the collection state contains our items
94 expect(collection.size).toBe(3)
95 // Sort by name since keys are random UUIDs
96 expect(collection.toArray.map((entry) => entry.name).sort()).deep.equals([
97 `one`,
98 `three`,
99 `two`,
100 ])
101 })
102​
103 it(`should update when data syncs`, async () => {
104 const db = await createDatabase()
105 await createTestData(db)
106​
107 const collection = createDocumentsCollection(db)
108​
109 await collection.stateWhenReady()
110​
111 // Verify the collection state contains our items
112 expect(collection.size).toBe(3)
113​
114 // Make an update, simulates a sync from another client
115 await db.execute(`
116 INSERT into documents (id, name)
117 VALUES
118 (uuid(), 'four')
119 `)
120​
121 // The collection should update
122 await vi.waitFor(
123 () => {
124 expect(collection.size).toBe(4)
125 // Sort by name since keys are random UUIDs
126 expect(
127 collection.toArray.map((entry) => entry.name).sort(),
128 ).deep.equals([`four`, `one`, `three`, `two`])
129 },
130 { timeout: 1000 },
131 )
132​
133 await db.execute(`
134 DELETE from documents
135 WHERE name = 'two'
136 `)
137​
138 // The collection should update
139 await vi.waitFor(
140 () => {
141 expect(collection.size).toBe(3)
142 // Sort by name since keys are random UUIDs
143 expect(
144 collection.toArray.map((entry) => entry.name).sort(),
145 ).deep.equals([`four`, `one`, `three`])
146 },
147 { timeout: 1000 },
148 )
149​
150 await db.execute(`
151 UPDATE documents
152 SET name = 'updated'
153 WHERE name = 'one'
154 `)
155​
156 // The collection should update
157 await vi.waitFor(
158 () => {
159 expect(collection.size).toBe(3)
160 // Sort by name since keys are random UUIDs
161 expect(
162 collection.toArray.map((entry) => entry.name).sort(),
163 ).deep.equals([`four`, `three`, `updated`])
164 },
165 { timeout: 1000 },
166 )
167 })
168​
169 it(`should propagate collection mutations to PowerSync`, async () => {
170 const db = await createDatabase()
171 await createTestData(db)
172​
173 const collection = createDocumentsCollection(db)
174 await collection.stateWhenReady()
175​
176 // Verify the collection state contains our items
177 expect(collection.size).toBe(3)
178​
179 const id = randomUUID()
180 const tx = collection.insert({
181 id,
182 name: `new`,
183 author: `somebody`,
184 })
185​
186 // The insert should optimistically update the collection
187 const newDoc = collection.get(id)
188 expect(newDoc).toBeDefined()
189 expect(newDoc!.name).toBe(`new`)
190 expect(newDoc!.author).toBe(`somebody`)
191​
192 await tx.isPersisted.promise
193 // The item should now be present in PowerSync
194 // We should also have patched it back in to Tanstack DB (removing the optimistic state)
195​
196 // Now do an update
197 await collection.update(id, (d) => (d.name = `updatedNew`)).isPersisted
198 .promise
199​
200 const updatedDoc = collection.get(id)
201 expect(updatedDoc).toBeDefined()
202 expect(updatedDoc!.name).toBe(`updatedNew`)
203 // Only the updated field should be updated
204 expect(updatedDoc!.author).toBe(`somebody`)
205​
206 await collection.delete(id).isPersisted.promise
207​
208 // There should be a crud entries for this
209 const _crudEntries = await db.getAll(`
210 SELECT * FROM ps_crud ORDER BY id`)
211​
212 const crudEntries = _crudEntries.map((r) =>
213 CrudEntry.fromRow(r as Parameters<typeof CrudEntry.fromRow>[0]),
214 )
215​
216 expect(crudEntries.length).toBe(6)
217 // We can only group transactions for similar operations
218 expect(crudEntries.map((e) => e.op)).toEqual([
219 `PUT`,
220 `PUT`,
221 `PUT`,
222 `PUT`,
223 `PATCH`,
224 `DELETE`,
225 ])
226 })
227​
228 it(`should handle transactions`, async () => {
229 const db = await createDatabase()
230 await createTestData(db)
231​
232 const collection = createDocumentsCollection(db)
233 await collection.stateWhenReady()
234​
235 expect(collection.size).toBe(3)
236​
237 const addTx = createTransaction({
238 autoCommit: false,
239 mutationFn: async ({ transaction }) => {
240 await new PowerSyncTransactor({ database: db }).applyTransaction(
241 transaction,
242 )
243 },
244 })
245​
246 addTx.mutate(() => {
247 for (let i = 0; i < 5; i++) {
248 collection.insert({ id: randomUUID(), name: `tx-${i}` })
249 }
250 })
251​
252 await addTx.commit()
253 await addTx.isPersisted.promise
254​
255 expect(collection.size).toBe(8)
256​
257 // fetch the ps_crud items
258 // There should be a crud entries for this
259 const _crudEntries = await db.getAll(`
260 SELECT * FROM ps_crud ORDER BY id`)
261 const crudEntries = _crudEntries.map((r) =>
262 CrudEntry.fromRow(r as Parameters<typeof CrudEntry.fromRow>[0]),
263 )
264​
265 const lastTransactionId =
266 crudEntries[crudEntries.length - 1]?.transactionId
267 /**
268 * The last items, created in the same transaction, should be in the same
269 * PowerSync transaction.
270 */
271 expect(
272 crudEntries
273 .reverse()
274 .slice(0, 5)
275 .every((crudEntry) => crudEntry.transactionId == lastTransactionId),
276 ).true
277 })
278​
279 it(`should complete transactions that delete one key and insert another (same-millisecond tie)`, async () => {
280 const db = await createDatabase()
281​
282 const options = powerSyncCollectionOptions({
283 database: db,
284 table: APP_SCHEMA.props.documents,
285 })
286 const collection = createCollection(options)
287 onTestFinished(() => collection.cleanup())
288 await collection.stateWhenReady()
289​
290 // Seed the row which will be deleted in the transaction
291 await collection.insert({ id: `a`, name: `row a` }).isPersisted.promise
292​
293 const { trackedTableName } = options.utils.getMeta()
294​
295 class SameMillisecondTransactor extends PowerSyncTransactor {
296 protected override async handleDelete(
297 mutation: PendingMutation<any>,
298 context: LockContext,
299 waitForCompletion?: boolean,
300 ) {
301 const result = await super.handleDelete(
302 mutation,
303 context,
304 waitForCompletion,
305 )
306 // Simulate the same-millisecond tie: ensure the delete's diff row
307 // wins the `ORDER BY timestamp DESC` readback of the insert below.
308 await context.execute(
309 `UPDATE ${trackedTableName} SET timestamp = '9999-12-31T23:59:59.999Z'`,
310 )
311 return result
312 }
313 }
314​
315 const transactor = new SameMillisecondTransactor({ database: db })
316​
317 const tx = createTransaction({
318 autoCommit: false,
319 mutationFn: async ({ transaction }) => {
320 await transactor.applyTransaction(transaction)
321 },
322 })
323​
324 tx.mutate(() => {
325 collection.delete(`a`)
326 collection.insert({ id: `b`, name: `row b` })
327 })
328​
329 const outcome = await Promise.race([
330 tx.commit().then(() => `persisted` as const),
331 new Promise<`timed out`>((resolve) =>
332 setTimeout(() => resolve(`timed out`), 2_000),
333 ),
334 ])
335 expect(outcome).toBe(`persisted`)
336​
337 const documents = await db.getAll<{ id: string }>(
338 `SELECT id FROM documents`,
339 )
340 expect(documents.map((doc) => doc.id)).toEqual([`b`])
341 })
342​
343 it(`should handle transactions with multiple collections`, async () => {
344 const db = await createDatabase()
345 await createTestData(db)
346​
347 const documentsCollection = createDocumentsCollection(db)
348​
349 const usersCollection = createCollection(
350 powerSyncCollectionOptions({
351 database: db,
352 table: APP_SCHEMA.props.users,
353 }),
354 )
355 onTestFinished(() => usersCollection.cleanup())
356​
357 await documentsCollection.stateWhenReady()
358 await usersCollection.stateWhenReady()
359​
360 expect(documentsCollection.size).toBe(3)
361 expect(usersCollection.size).toBe(0)
362​
363 const addTx = createTransaction({
364 autoCommit: false,
365 mutationFn: async ({ transaction }) => {
366 await new PowerSyncTransactor({ database: db }).applyTransaction(
367 transaction,
368 )
369 },
370 })
371​
372 addTx.mutate(() => {
373 for (let i = 0; i < 5; i++) {
374 documentsCollection.insert({ id: randomUUID(), name: `tx-${i}` })
375 usersCollection.insert({ id: randomUUID(), name: `user` })
376 }
377 })
378​
379 await addTx.commit()
380 await addTx.isPersisted.promise
381​
382 expect(documentsCollection.size).toBe(8)
383 expect(usersCollection.size).toBe(5)
384​
385 // fetch the ps_crud items
386 // There should be a crud entries for this
387 const _crudEntries = await db.getAll(`
388 SELECT * FROM ps_crud ORDER BY id`)
389 const crudEntries = _crudEntries.map((r) =>
390 CrudEntry.fromRow(r as Parameters<typeof CrudEntry.fromRow>[0]),
391 )
392​
393 const lastTransactionId =
394 crudEntries[crudEntries.length - 1]?.transactionId
395 /**
396 * The last items, created in the same transaction, should be in the same
397 * PowerSync transaction.
398 */
399 expect(
400 crudEntries
401 .reverse()
402 .slice(0, 10)
403 .every((crudEntry) => crudEntry.transactionId == lastTransactionId),
404 ).true
405 })
406​
407 /**
408 * Metadata provided by the collection operation should be persisted to the database if supported by the SQLite table.
409 */
410 it(`should persist collection operation metadata`, async () => {
411 const db = await createDatabase()
412​
413 const collection = createDocumentsCollection(db)
414 await collection.stateWhenReady()
415​
416 const metadata = {
417 text: `some text`,
418 number: 123,
419 boolean: true,
420 }
421 const id = randomUUID()
422 await collection.insert(
423 {
424 id,
425 name: `new`,
426 author: `somebody`,
427 },
428 {
429 metadata,
430 },
431 ).isPersisted.promise
432​
433 // Now do an update
434 await collection.update(
435 id,
436 { metadata: metadata },
437 (d) => (d.name = `updatedNew`),
438 ).isPersisted.promise
439​
440 await collection.delete(id, { metadata }).isPersisted.promise
441​
442 // There should be a crud entries for this
443 const crudBatch = await db.getCrudBatch(100)
444 expect(crudBatch).toBeDefined()
445 const crudEntries = crudBatch!.crud
446​
447 // The metadata should be available in the CRUD entries for upload
448 const stringifiedMetadata = JSON.stringify(metadata)
449 expect(crudEntries.length).toBe(3)
450 expect(crudEntries[0]!.metadata).toEqual(stringifiedMetadata)
451 expect(crudEntries[1]!.metadata).toEqual(stringifiedMetadata)
452 expect(crudEntries[2]!.metadata).toEqual(stringifiedMetadata)
453​
454 // Verify the item is deleted from SQLite
455 const documents = await db.getAll(`
456 SELECT * FROM documents`)
457 expect(documents.length).toBe(0)
458 })
459 })
460​
461 describe(`General use`, () => {
462 it(`should rollback transactions on error`, async () => {
463 const db = await createDatabase()
464​
465 const options = powerSyncCollectionOptions({
466 database: db,
467 table: APP_SCHEMA.props.documents,
468 })
469​
470 // This will cause the transactor to fail when writing to SQLite
471 vi.spyOn(options.utils, `getMeta`).mockImplementation(() => ({
472 tableName: `fakeTable`,
473 trackedTableName: `error`,
474 metadataIsTracked: true,
475 serializeValue: () => ({}) as any,
476 }))
477 // Create two collections for the same table
478 const collection = createCollection(options)
479​
480 onTestFinished(() => collection.cleanup())
481 const addTx = createTransaction({
482 autoCommit: false,
483 mutationFn: async ({ transaction }) => {
484 await new PowerSyncTransactor({ database: db }).applyTransaction(
485 transaction,
486 )
487 },
488 })
489​
490 expect(collection.size).eq(0)
491 await collection.stateWhenReady()
492​
493 const id = randomUUID()
494 addTx.mutate(() => {
495 collection.insert({
496 id,
497 name: `aname`,
498 })
499 })
500​
501 // This should be present in the optimistic state, but should be reverted when attempting to persist
502 expect(collection.size).eq(1)
503​
504 try {
505 await addTx.commit()
506 await addTx.isPersisted.promise
507 expect.fail(`Should have thrown an error`)
508 } catch (error) {
509 expect(error).toBeDefined()
510 // The collection should be in a clean state
511 expect(collection.size).toBe(0)
512 }
513 })
514​
515 it(`should work with live queries`, async () => {
516 const db = await createDatabase()
517​
518 // Create two collections for the same table
519 const collection = createDocumentsCollection(db)
520​
521 await collection.stateWhenReady()
522​
523 const liveDocuments = createCollection(
524 liveQueryCollectionOptions({
525 query: (q) =>
526 q
527 .from({ document: collection })
528 .where(({ document }) => eq(document.name, `book`))
529 .select(({ document }) => ({
530 id: document.id,
531 name: document.name,
532 })),
533 }),
534 )
535​
536 expect(liveDocuments.size).eq(0)
537​
538 const bookNames = new Set<string>()
539​
540 liveDocuments.subscribeChanges((changes) => {
541 changes
542 .map((change) => change.value.name)
543 .filter((name): name is string => name !== undefined)
544 .forEach((change) => bookNames.add(change))
545 })
546​
547 await collection.insert({
548 id: randomUUID(),
549 name: `notabook`,
550 }).isPersisted.promise
551 await collection.insert({
552 id: randomUUID(),
553 name: `book`,
554 }).isPersisted.promise
555​
556 expect(collection.size).eq(2)
557 await vi.waitFor(
558 () => {
559 expect(Array.from(bookNames)).deep.equals([`book`])
560 },
561 { timeout: 1000 },
562 )
563 })
564 })
565​
566 describe(`Multiple Clients`, () => {
567 it(`should sync updates between multiple clients`, async () => {
568 const db = await createDatabase()
569​
570 // Create two collections for the same table
571 const collectionA = createDocumentsCollection(db)
572 await collectionA.stateWhenReady()
573​
574 const collectionB = createDocumentsCollection(db)
575 await collectionB.stateWhenReady()
576​
577 await createTestData(db)
578​
579 // Both collections should have the data present after insertion
580 await vi.waitFor(
581 () => {
582 expect(collectionA.size).eq(3)
583 expect(collectionB.size).eq(3)
584 },
585 { timeout: 1000 },
586 )
587 })
588 })
589​
590 describe(`Lifecycle`, () => {
591 it(`should cleanup resources`, async () => {
592 const db = await createDatabase()
593 const collectionOptions = powerSyncCollectionOptions({
594 database: db,
595 table: APP_SCHEMA.props.documents,
596 })
597​
598 const meta = collectionOptions.utils.getMeta()
599​
600 const tableExists = async (): Promise<boolean> => {
601 const result = await db.writeLock(async (tx) => {
602 return tx.get<{ count: number }>(
603 `
604 SELECT COUNT(*) as count
605 FROM sqlite_temp_master
606 WHERE type='table' AND name = ?
607 `,
608 [meta.trackedTableName],
609 )
610 })
611 return result.count > 0
612 }
613​
614 const collection = createCollection(collectionOptions)
615 await collection.stateWhenReady()
616 expect(await tableExists()).true
617​
618 await collection.cleanup()
619​
620 // It seems that even though `cleanup` is async, the sync disposer cannot be async
621 // We wait for the table to be deleted
622 await vi.waitFor(
623 async () => {
624 expect(await tableExists()).false
625 },
626 { timeout: 1000 },
627 )
628 })
629 })
630})