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

node-persistence.test.ts

419 lines12.1 KB

1import { mkdtempSync, rmSync } from 'node:fs'
2import { tmpdir } from 'node:os'
3import { join } from 'node:path'
4import BetterSqlite3 from 'better-sqlite3'
5import { describe, expect, it } from 'vitest'
6import { createNodeSQLitePersistence, persistedCollectionOptions } from '../src'
7import { BetterSqlite3SQLiteDriver } from '../src/node-driver'
8import { SingleProcessCoordinator } from '../../db-sqlite-persistence-core/src'
9import { runRuntimePersistenceContractSuite } from '../../db-sqlite-persistence-core/tests/contracts/runtime-persistence-contract'
10import type { SQLitePullSinceResult } from '../../db-sqlite-persistence-core/src'
11import type {
12 RuntimePersistenceContractTodo,
13 RuntimePersistenceDatabaseHarness,
14} from '../../db-sqlite-persistence-core/tests/contracts/runtime-persistence-contract'
15​
16function createRuntimeDatabaseHarness(): RuntimePersistenceDatabaseHarness {
17 const tempDirectory = mkdtempSync(join(tmpdir(), `db-node-persistence-`))
18 const dbPath = join(tempDirectory, `state.sqlite`)
19 const drivers = new Set<BetterSqlite3SQLiteDriver>()
20​
21 return {
22 createDriver: () => {
23 const driver = new BetterSqlite3SQLiteDriver({ filename: dbPath })
24 drivers.add(driver)
25 return driver
26 },
27 cleanup: () => {
28 for (const driver of drivers) {
29 try {
30 driver.close()
31 } catch {
32 // ignore cleanup errors from already-closed handles
33 }
34 }
35 drivers.clear()
36 rmSync(tempDirectory, { recursive: true, force: true })
37 },
38 }
40​
41runRuntimePersistenceContractSuite(`node runtime persistence helpers`, {
42 createDatabaseHarness: createRuntimeDatabaseHarness,
43 createAdapter: (driver) =>
44 createNodeSQLitePersistence({
45 database: (driver as BetterSqlite3SQLiteDriver).getDatabase(),
46 }).adapter,
47 createPersistence: (driver, coordinator) =>
48 createNodeSQLitePersistence({
49 database: (driver as BetterSqlite3SQLiteDriver).getDatabase(),
50 coordinator,
51 }),
52 createCoordinator: () => new SingleProcessCoordinator(),
53})
54​
55describe(`node persistence helpers`, () => {
56 it(`defaults coordinator to SingleProcessCoordinator`, () => {
57 const runtimeHarness = createRuntimeDatabaseHarness()
58 const driver = runtimeHarness.createDriver()
59 try {
60 const persistence = createNodeSQLitePersistence({
61 database: (driver as BetterSqlite3SQLiteDriver).getDatabase(),
62 })
63 expect(persistence.coordinator).toBeInstanceOf(SingleProcessCoordinator)
64 } finally {
65 runtimeHarness.cleanup()
66 }
67 })
68​
69 it(`allows overriding the default coordinator`, () => {
70 const runtimeHarness = createRuntimeDatabaseHarness()
71 const driver = runtimeHarness.createDriver()
72 try {
73 const coordinator = new SingleProcessCoordinator()
74 const persistence = createNodeSQLitePersistence({
75 database: (driver as BetterSqlite3SQLiteDriver).getDatabase(),
76 coordinator,
77 })
78​
79 expect(persistence.coordinator).toBe(coordinator)
80 } finally {
81 runtimeHarness.cleanup()
82 }
83 })
84​
85 it(`accepts a bare better-sqlite3 database handle`, async () => {
86 const tempDirectory = mkdtempSync(join(tmpdir(), `db-node-direct-db-`))
87 const dbPath = join(tempDirectory, `state.sqlite`)
88 const collectionId = `todos`
89 const database = new BetterSqlite3(dbPath)
90​
91 try {
92 const persistence = createNodeSQLitePersistence({
93 database,
94 })
95​
96 await persistence.adapter.applyCommittedTx(collectionId, {
97 txId: `tx-direct-db-1`,
98 term: 1,
99 seq: 1,
100 rowVersion: 1,
101 mutations: [
102 {
103 type: `insert`,
104 key: `1`,
105 value: {
106 id: `1`,
107 title: `from raw database`,
108 score: 1,
109 },
110 },
111 ],
112 })
113​
114 const rows = await persistence.adapter.loadSubset(collectionId, {})
115 expect(rows).toEqual([
116 {
117 key: `1`,
118 value: {
119 id: `1`,
120 title: `from raw database`,
121 score: 1,
122 },
123 },
124 ])
125 } finally {
126 database.close()
127 rmSync(tempDirectory, { recursive: true, force: true })
128 }
129 })
130​
131 it(`prunes applied_tx rows past the default age backstop`, async () => {
132 const tempDirectory = mkdtempSync(join(tmpdir(), `db-node-default-prune-`))
133 const dbPath = join(tempDirectory, `state.sqlite`)
134 const collectionId = `default-prune`
135 const database = new BetterSqlite3(dbPath)
136​
137 try {
138 const persistence = createNodeSQLitePersistence({ database })
139​
140 await persistence.adapter.applyCommittedTx(collectionId, {
141 txId: `tx-1`,
142 term: 1,
143 seq: 1,
144 rowVersion: 1,
145 mutations: [
146 {
147 type: `insert`,
148 key: `1`,
149 value: { id: `1`, title: `old`, score: 1 },
150 },
151 ],
152 })
153​
154 // Backdate the first row well beyond the 24h default age backstop.
155 database
156 .prepare(
157 `UPDATE applied_tx SET applied_at = 0 WHERE collection_id = ? AND seq = 1`,
158 )
159 .run(collectionId)
160​
161 await persistence.adapter.applyCommittedTx(collectionId, {
162 txId: `tx-2`,
163 term: 1,
164 seq: 2,
165 rowVersion: 2,
166 mutations: [
167 {
168 type: `insert`,
169 key: `2`,
170 value: { id: `2`, title: `new`, score: 2 },
171 },
172 ],
173 })
174​
175 const appliedRows = database
176 .prepare(
177 `SELECT seq FROM applied_tx WHERE collection_id = ? ORDER BY seq ASC`,
178 )
179 .all(collectionId) as Array<{ seq: number }>
180 expect(appliedRows.map((row) => row.seq)).toEqual([2])
181 } finally {
182 database.close()
183 rmSync(tempDirectory, { recursive: true, force: true })
184 }
185 })
186​
187 it(`forces full reload when pullSince starts before pruned replay rows`, async () => {
188 const tempDirectory = mkdtempSync(
189 join(tmpdir(), `db-node-pruned-pull-since-`),
190 )
191 const dbPath = join(tempDirectory, `state.sqlite`)
192 const collectionId = `pruned-pull-since`
193 const database = new BetterSqlite3(dbPath)
194​
195 try {
196 const persistence = createNodeSQLitePersistence({
197 database,
198 appliedTxPruneMaxRows: 2,
199 appliedTxPruneMaxAgeSeconds: 0,
200 })
201​
202 for (const seq of [1, 2, 3]) {
203 await persistence.adapter.applyCommittedTx(collectionId, {
204 txId: `tx-${seq}`,
205 term: 1,
206 seq,
207 rowVersion: seq,
208 mutations: [
209 {
210 type: `insert`,
211 key: String(seq),
212 value: { id: String(seq), title: `todo-${seq}`, score: seq },
213 },
214 ],
215 })
216 }
217​
218 const adapter = persistence.adapter as typeof persistence.adapter & {
219 pullSince: (
220 collectionId: string,
221 fromRowVersion: number,
222 ) => Promise<SQLitePullSinceResult<string | number>>
223 }
224 const result = await adapter.pullSince(collectionId, 0)
225​
226 expect(result.requiresFullReload).toBe(true)
227 } finally {
228 database.close()
229 rmSync(tempDirectory, { recursive: true, force: true })
230 }
231 })
232​
233 it(`prunes applied_tx rows past explicit row cap`, async () => {
234 const tempDirectory = mkdtempSync(join(tmpdir(), `db-node-row-prune-`))
235 const dbPath = join(tempDirectory, `state.sqlite`)
236 const collectionId = `row-prune`
237 const database = new BetterSqlite3(dbPath)
238​
239 try {
240 const persistence = createNodeSQLitePersistence({
241 database,
242 appliedTxPruneMaxRows: 2,
243 appliedTxPruneMaxAgeSeconds: 0,
244 })
245​
246 for (const seq of [1, 2, 3]) {
247 await persistence.adapter.applyCommittedTx(collectionId, {
248 txId: `tx-${seq}`,
249 term: 1,
250 seq,
251 rowVersion: seq,
252 mutations: [
253 {
254 type: `insert`,
255 key: String(seq),
256 value: { id: String(seq), title: `todo-${seq}`, score: seq },
257 },
258 ],
259 })
260 }
261​
262 const appliedRows = database
263 .prepare(
264 `SELECT seq FROM applied_tx WHERE collection_id = ? ORDER BY seq ASC`,
265 )
266 .all(collectionId) as Array<{ seq: number }>
267 expect(appliedRows.map((row) => row.seq)).toEqual([2, 3])
268 } finally {
269 database.close()
270 rmSync(tempDirectory, { recursive: true, force: true })
271 }
272 })
273​
274 it(`leaves applied_tx rows untouched when pruning is disabled`, async () => {
275 const tempDirectory = mkdtempSync(join(tmpdir(), `db-node-no-prune-`))
276 const dbPath = join(tempDirectory, `state.sqlite`)
277 const collectionId = `no-prune`
278 const database = new BetterSqlite3(dbPath)
279​
280 try {
281 const persistence = createNodeSQLitePersistence({
282 database,
283 appliedTxPruneMaxRows: 0,
284 appliedTxPruneMaxAgeSeconds: 0,
285 })
286​
287 await persistence.adapter.applyCommittedTx(collectionId, {
288 txId: `tx-1`,
289 term: 1,
290 seq: 1,
291 rowVersion: 1,
292 mutations: [
293 {
294 type: `insert`,
295 key: `1`,
296 value: { id: `1`, title: `old`, score: 1 },
297 },
298 ],
299 })
300​
301 database
302 .prepare(
303 `UPDATE applied_tx SET applied_at = 0 WHERE collection_id = ? AND seq = 1`,
304 )
305 .run(collectionId)
306​
307 await persistence.adapter.applyCommittedTx(collectionId, {
308 txId: `tx-2`,
309 term: 1,
310 seq: 2,
311 rowVersion: 2,
312 mutations: [
313 {
314 type: `insert`,
315 key: `2`,
316 value: { id: `2`, title: `new`, score: 2 },
317 },
318 ],
319 })
320​
321 const appliedRows = database
322 .prepare(
323 `SELECT seq FROM applied_tx WHERE collection_id = ? ORDER BY seq ASC`,
324 )
325 .all(collectionId) as Array<{ seq: number }>
326 expect(appliedRows.map((row) => row.seq)).toEqual([1, 2])
327 } finally {
328 database.close()
329 rmSync(tempDirectory, { recursive: true, force: true })
330 }
331 })
332​
333 it(`infers schema policy from sync mode`, async () => {
334 const tempDirectory = mkdtempSync(join(tmpdir(), `db-node-schema-infer-`))
335 const dbPath = join(tempDirectory, `state.sqlite`)
336 const collectionId = `todos`
337 const firstDatabase = new BetterSqlite3(dbPath)
338​
339 try {
340 const firstPersistence = createNodeSQLitePersistence({
341 database: firstDatabase,
342 })
343 const firstCollectionOptions = persistedCollectionOptions<
344 RuntimePersistenceContractTodo,
345 string
346 >({
347 id: collectionId,
348 schemaVersion: 1,
349 getKey: (todo) => todo.id,
350 persistence: firstPersistence,
351 })
352​
353 await firstCollectionOptions.persistence.adapter.applyCommittedTx(
354 collectionId,
355 {
356 txId: `tx-1`,
357 term: 1,
358 seq: 1,
359 rowVersion: 1,
360 mutations: [
361 {
362 type: `insert`,
363 key: `1`,
364 value: {
365 id: `1`,
366 title: `before mismatch`,
367 score: 1,
368 },
369 },
370 ],
371 },
372 )
373 } finally {
374 firstDatabase.close()
375 }
376​
377 const secondDatabase = new BetterSqlite3(dbPath)
378 try {
379 const secondPersistence = createNodeSQLitePersistence({
380 database: secondDatabase,
381 })
382 const syncAbsentOptions = persistedCollectionOptions<
383 RuntimePersistenceContractTodo,
384 string
385 >({
386 id: collectionId,
387 schemaVersion: 2,
388 getKey: (todo) => todo.id,
389 persistence: secondPersistence,
390 })
391 await expect(
392 syncAbsentOptions.persistence.adapter.loadSubset(collectionId, {}),
393 ).rejects.toThrow(`Schema version mismatch`)
394​
395 const syncPresentOptions = persistedCollectionOptions<
396 RuntimePersistenceContractTodo,
397 string
398 >({
399 id: collectionId,
400 schemaVersion: 2,
401 getKey: (todo) => todo.id,
402 sync: {
403 sync: ({ markReady }) => {
404 markReady()
405 },
406 },
407 persistence: secondPersistence,
408 })
409 const rows = await syncPresentOptions.persistence.adapter.loadSubset(
410 collectionId,
411 {},
412 )
413 expect(rows).toEqual([])
414 } finally {
415 secondDatabase.close()
416 rmSync(tempDirectory, { recursive: true, force: true })
417 }
418 })
419})