-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDatabase.ts
More file actions
45 lines (40 loc) · 1.41 KB
/
createDatabase.ts
File metadata and controls
45 lines (40 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { open, type DB } from '@op-engineering/op-sqlite';
/**
* Opens and configures a database connection with the specified mode
*
* @param name - Database file name
* @param mode - 'write' for read-write connection, 'read' for query-only connection
* @param onOpen - Optional callback invoked synchronously after open(), before any awaited PRAGMAs.
* Use this to register ownership of the raw connection before any async gap.
* @returns Configured database instance
*
* @example
* ```typescript
* const writeDb = await createDatabase('app.db', 'write');
* const readDb = await createDatabase('app.db', 'read');
* ```
*/
export async function createDatabase(
name: string,
mode: 'write' | 'read',
onOpen?: (db: DB) => void
): Promise<DB> {
/** OPEN DATABASE */
const db = open({ name });
onOpen?.(db);
/** CONFIGURE BUSY TIMEOUT */
// Cross-process fallback: if another process holds a lock (e.g. Android background task),
// retry for up to 10s before giving up
await db.execute('PRAGMA busy_timeout = 10000');
/** CONFIGURE WAL MODE */
// WAL mode enables concurrent reads and writes
await db.execute('PRAGMA journal_mode = WAL');
/** CONFIGURE CONNECTION MODE */
if (mode === 'write') {
await db.execute('PRAGMA synchronous = NORMAL');
await db.execute('PRAGMA locking_mode = NORMAL');
} else {
await db.execute('PRAGMA query_only = true');
}
return db;
}