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

op-sqlite-test-db.ts

133 lines3.0 KB

1import BetterSqlite3 from 'better-sqlite3'
2import type { OpSQLiteDatabaseLike } from '../../src/op-sqlite-driver'
3​
4const QUERY_SQL_PATTERN = /^\s*(SELECT|WITH|PRAGMA|EXPLAIN)\b/i
5​
6export type OpSQLiteTestResultShape =
7 | `rows-array`
8 | `rows-object`
9 | `rows-list`
10 | `statement-array`
11​
12type OpSQLiteRowsListLike<T> = {
13 length: number
14 item: (index: number) => T | null
15 _array: Array<T>
17​
18export type OpSQLiteTestDatabase = OpSQLiteDatabaseLike & {
19 close: () => void | Promise<void>
20 getNativeDatabase?: () => InstanceType<typeof BetterSqlite3>
22​
23export type MobileSQLiteTestDatabaseFactory = (options: {
24 filename: string
25 resultShape?: OpSQLiteTestResultShape
26}) => OpSQLiteTestDatabase
27​
28declare global {
29 var __tanstackDbCreateMobileSQLiteTestDatabase:
30 | MobileSQLiteTestDatabaseFactory
31 | undefined
33​
34function createRowsList<T>(rows: Array<T>): OpSQLiteRowsListLike<T> {
35 return {
36 length: rows.length,
37 item: (index) => rows[index] ?? null,
38 _array: rows,
39 }
41​
42function formatQueryRows<T>(
43 rows: Array<T>,
44 resultShape: OpSQLiteTestResultShape,
45): unknown {
46 switch (resultShape) {
47 case `rows-array`:
48 return rows
49 case `rows-object`:
50 return { rows }
51 case `rows-list`:
52 return {
53 rows: createRowsList(rows),
54 }
55 case `statement-array`:
56 return [{ rows }]
57 default:
58 return { rows }
59 }
61​
62function formatWriteResult(
63 rowsAffected: number,
64 resultShape: OpSQLiteTestResultShape,
65): unknown {
66 switch (resultShape) {
67 case `rows-array`:
68 return []
69 case `rows-object`:
70 return {
71 rows: [],
72 rowsAffected,
73 }
74 case `rows-list`:
75 return {
76 rows: createRowsList([]),
77 rowsAffected,
78 }
79 case `statement-array`:
80 return [
81 {
82 rows: [],
83 rowsAffected,
84 },
85 ]
86 default:
87 return {
88 rows: [],
89 rowsAffected,
90 }
91 }
93​
94export function createOpSQLiteTestDatabase(options: {
95 filename: string
96 resultShape?: OpSQLiteTestResultShape
97}): OpSQLiteTestDatabase {
98 if (
99 typeof globalThis.__tanstackDbCreateMobileSQLiteTestDatabase === `function`
100 ) {
101 return globalThis.__tanstackDbCreateMobileSQLiteTestDatabase(options)
102 }
103​
104 const nativeDatabase = new BetterSqlite3(options.filename)
105 const resultShape = options.resultShape ?? `rows-object`
106​
107 const execute = (sql: string, params: ReadonlyArray<unknown> = []) => {
108 const statement = nativeDatabase.prepare(sql)
109 const parameterValues = [...params]
110​
111 if (QUERY_SQL_PATTERN.test(sql)) {
112 const rows =
113 parameterValues.length > 0
114 ? statement.all(...parameterValues)
115 : statement.all()
116 return formatQueryRows(rows, resultShape)
117 }
118​
119 const runResult =
120 parameterValues.length > 0
121 ? statement.run(...parameterValues)
122 : statement.run()
123 return formatWriteResult(runResult.changes, resultShape)
124 }
125​
126 return {
127 execute,
128 close: () => {
129 nativeDatabase.close()
130 },
131 getNativeDatabase: () => nativeDatabase,
132 }