Skip to content

Commit dfc77f4

Browse files
committed
Refactor BetterSqliteDriver.
1 parent f79ff7e commit dfc77f4

File tree

7 files changed

+90
-46
lines changed

7 files changed

+90
-46
lines changed

packages/sqlite-js-better-sqlite3/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
"clean": "tsc -b --clean && tsc -b ./test/tsconfig.json --clean && rm -rf lib test/lib"
1010
},
1111
"exports": {
12-
".": "./lib/index.js",
13-
"./sync-driver": "./lib/sync-driver.js",
14-
"./worker-driver": "./lib/worker-driver.js"
12+
".": "./lib/index.js"
1513
},
1614
"keywords": [],
1715
"author": "",
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import {
2+
ReserveConnectionOptions,
3+
ReservedConnection,
4+
SqliteDriverConnectionPool,
5+
UpdateListener
6+
} from '@powersync/sqlite-js-driver';
7+
import {
8+
ConnectionPoolOptions,
9+
LazyConnectionPool,
10+
ReadWriteConnectionPool
11+
} from '@powersync/sqlite-js-driver/util';
12+
import { WorkerDriverConnection } from '@powersync/sqlite-js-driver/worker_threads';
13+
import type * as bsqlite from 'better-sqlite3';
14+
15+
export interface BetterSqliteDriverOptions
16+
extends ConnectionPoolOptions,
17+
bsqlite.Options {
18+
/**
19+
* Specify a custom path to a worker script, to customize the loading process.
20+
*/
21+
workerPath?: string | URL;
22+
}
23+
24+
export class BetterSqliteDriver implements SqliteDriverConnectionPool {
25+
/**
26+
* Opens a single in-process connection.
27+
*
28+
* Uses blocking I/O.
29+
*/
30+
static openInProcess(
31+
path: string,
32+
options?: bsqlite.Options
33+
): BetterSqliteDriver {
34+
const connection = new LazyConnectionPool(async () => {
35+
const { BetterSqliteConnection } = await import('./sync-driver.js');
36+
return BetterSqliteConnection.open(path, options);
37+
});
38+
return new BetterSqliteDriver(connection);
39+
}
40+
41+
/**
42+
* Opens a connection pool with non-blocking I/O using worker_threads.
43+
*/
44+
static open(
45+
path: string,
46+
options?: BetterSqliteDriverOptions
47+
): BetterSqliteDriver {
48+
const workerPath =
49+
options?.workerPath ?? new URL('./worker.js', import.meta.url);
50+
51+
const connection = new ReadWriteConnectionPool({
52+
async openConnection(connectionOptions) {
53+
return new WorkerDriverConnection(workerPath, path, {
54+
...options,
55+
readonly: (options?.readonly ?? connectionOptions?.readonly) || false
56+
});
57+
}
58+
});
59+
return new BetterSqliteDriver(connection);
60+
}
61+
62+
private constructor(private connection: SqliteDriverConnectionPool) {}
63+
64+
reserveConnection(
65+
options?: ReserveConnectionOptions
66+
): Promise<ReservedConnection> {
67+
return this.connection.reserveConnection(options);
68+
}
69+
70+
close(): Promise<void> {
71+
return this.connection.close();
72+
}
73+
74+
onUpdate(
75+
listener: UpdateListener,
76+
options?: { tables?: string[]; batchLimit?: number }
77+
): () => void {
78+
return this.connection.onUpdate(listener, options);
79+
}
80+
81+
[Symbol.asyncDispose](): Promise<void> {
82+
return this.connection[Symbol.asyncDispose]();
83+
}
84+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from './worker-driver.js';
1+
export * from './driver.js';

packages/sqlite-js-better-sqlite3/src/sync-driver.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ import {
3434
SqliteStep
3535
} from '@powersync/sqlite-js-driver/worker_threads';
3636

37-
export function betterSqlitePool(
38-
path: string,
39-
poolOptions?: bsqlite.Options
40-
): SqliteDriverConnectionPool {
41-
return new ReadWriteConnectionPool({
42-
async openConnection(options) {
43-
return BetterSqliteConnection.open(path, {
44-
...poolOptions,
45-
readonly: (poolOptions?.readonly ?? options?.readonly) || false
46-
});
47-
}
48-
});
49-
}
50-
5137
interface InternalStatement extends SqliteDriverStatement {
5238
getColumnsSync(): string[];
5339
stepSync(n?: number, options?: StepOptions): SqliteStepResult;

packages/sqlite-js-better-sqlite3/src/worker-driver.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { betterSqliteAsyncPool } from '../../lib/worker-driver.js';
2-
31
import { describeDriverTests } from '@powersync/sqlite-js-driver-tests';
2+
import { BetterSqliteDriver } from '../../lib/index.js';
43

54
describeDriverTests(
65
'better-sqlite3-async-pool',
76
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
8-
betterSqliteAsyncPool
7+
(path) => BetterSqliteDriver.open(path)
98
);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { betterSqlitePool } from '../../lib/sync-driver.js';
1+
import { BetterSqliteDriver } from '../../lib/index.js';
22
import { describeDriverTests } from '@powersync/sqlite-js-driver-tests';
33

44
describeDriverTests(
55
'better-sqlite3',
66
{ getColumns: true, rawResults: true, allowsMissingParameters: false },
7-
betterSqlitePool
7+
(path) => BetterSqliteDriver.openInProcess(path)
88
);

0 commit comments

Comments
 (0)