Open History to see the last commit
1import { randomUUID } from 'node:crypto'
2import { tmpdir } from 'node:os'
3import { PowerSyncDatabase, Schema, Table, column } from '@powersync/node'
4import { createCollection, createLiveQueryCollection, eq } from '@tanstack/db'
5import { describe, expect, it, onTestFinished, vi } from 'vitest'
6import { powerSyncCollectionOptions } from '../src'
7
8const APP_SCHEMA = new Schema({
9 products: new Table({
10 name: column.text,
11 price: column.integer,
12 category: column.text,
13 }),
14})
15
16describe(`Sync Streams`, () => {
17 async function createDatabase() {
18 const db = new PowerSyncDatabase({
19 database: {
20 dbFilename: `test-sync-streams-${randomUUID()}.sqlite`,
21 dbLocation: tmpdir(),
22 implementation: { type: `node:sqlite` },
23 },
24 schema: APP_SCHEMA,
25 })
26 onTestFinished(async () => {
27 await db.disconnectAndClear()
28 await new Promise((resolve) => setTimeout(resolve, 100))
29 await db.close()
30 })
31 await db.disconnectAndClear()
32 return db
33 }
34
35 async function createTestProducts(db: PowerSyncDatabase) {
36 await db.execute(`
37 INSERT INTO products (id, name, price, category)
38 VALUES
39 (uuid(), 'Product A', 50, 'electronics'),
40 (uuid(), 'Product B', 150, 'electronics'),
41 (uuid(), 'Product C', 25, 'clothing'),
42 (uuid(), 'Product D', 200, 'electronics'),
43 (uuid(), 'Product E', 75, 'clothing')
44 `)
45 }
46
47 it(`eager mode: should call onLoad on sync start and onUnload on cleanup`, async () => {
48 const db = await createDatabase()
49 await createTestProducts(db)
50
51 const onLoadMock = vi.fn()
52 const onUnloadMock = vi.fn()
53
54 const collection = createCollection(
55 powerSyncCollectionOptions({
56 database: db,
57 table: APP_SCHEMA.props.products,
58 onLoad: async () => {
59 await onLoadMock()
60
61 return () => {
62 onUnloadMock()
63 }
64 },
65 }),
66 )
67
68 await collection.stateWhenReady()
69
70 expect(onLoadMock).toHaveBeenCalledOnce()
71 expect(onUnloadMock).not.toHaveBeenCalled()
72
73 collection.cleanup()
74
75 expect(onUnloadMock).toHaveBeenCalledOnce()
76 })
77
78 it(`on-demand mode: should call onLoadSubset/onUnloadSubset for each live query`, async () => {
79 const db = await createDatabase()
80 await createTestProducts(db)
81
82 const onLoadSubsetMock = vi.fn()
83 const onUnloadSubsetMock = vi.fn()
84
85 const collection = createCollection(
86 powerSyncCollectionOptions({
87 database: db,
88 table: APP_SCHEMA.props.products,
89 syncMode: `on-demand`,
90 onLoadSubset: () => {
91 onLoadSubsetMock()
92
93 return () => {
94 onUnloadSubsetMock()
95 }
96 },
97 }),
98 )
99 onTestFinished(() => collection.cleanup())
100
101 await collection.stateWhenReady()
102
103 // LQ1: electronics
104 const electronicsQuery = createLiveQueryCollection({
105 query: (q) =>
106 q
107 .from({ product: collection })
108 .where(({ product }) => eq(product.category, `electronics`))
109 .select(({ product }) => ({
110 id: product.id,
111 name: product.name,
112 price: product.price,
113 category: product.category,
114 })),
115 })
116
117 await electronicsQuery.preload()
118
119 await vi.waitFor(
120 () => {
121 expect(electronicsQuery.size).toBe(3)
122 },
123 { timeout: 2000 },
124 )
125
126 expect(onLoadSubsetMock).toHaveBeenCalledTimes(1)
127 expect(onUnloadSubsetMock).not.toHaveBeenCalled()
128
129 // LQ2: clothing
130 const clothingQuery = createLiveQueryCollection({
131 query: (q) =>
132 q
133 .from({ product: collection })
134 .where(({ product }) => eq(product.category, `clothing`))
135 .select(({ product }) => ({
136 id: product.id,
137 name: product.name,
138 price: product.price,
139 category: product.category,
140 })),
141 })
142
143 await clothingQuery.preload()
144
145 await vi.waitFor(
146 () => {
147 expect(clothingQuery.size).toBe(2)
148 },
149 { timeout: 2000 },
150 )
151
152 expect(onLoadSubsetMock).toHaveBeenCalledTimes(2)
153 expect(onUnloadSubsetMock).not.toHaveBeenCalled()
154
155 // Cleanup LQ1 — should trigger first unload
156 electronicsQuery.cleanup()
157
158 await vi.waitFor(
159 () => {
160 expect(onUnloadSubsetMock).toHaveBeenCalledTimes(1)
161 },
162 { timeout: 2000 },
163 )
164
165 // Cleanup LQ2 — should trigger second unload
166 clothingQuery.cleanup()
167
168 await vi.waitFor(
169 () => {
170 expect(onUnloadSubsetMock).toHaveBeenCalledTimes(2)
171 },
172 { timeout: 2000 },
173 )
174 })
175})