@powersync/node 0.0.0-dev-20251030153614 → 0.0.0-dev-20251106124255
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/DefaultWorker.cjs.map +1 -1
- package/dist/bundle.cjs +8 -5
- package/dist/bundle.cjs.map +1 -1
- package/dist/worker.cjs.map +1 -1
- package/lib/db/AsyncDatabase.js +1 -0
- package/lib/db/AsyncDatabase.js.map +1 -0
- package/lib/db/BetterSqliteWorker.js +1 -0
- package/lib/db/BetterSqliteWorker.js.map +1 -0
- package/lib/db/DefaultWorker.js +1 -0
- package/lib/db/DefaultWorker.js.map +1 -0
- package/lib/db/NodeSqliteWorker.js +1 -0
- package/lib/db/NodeSqliteWorker.js.map +1 -0
- package/lib/db/PowerSyncDatabase.js +1 -0
- package/lib/db/PowerSyncDatabase.js.map +1 -0
- package/lib/db/RemoteConnection.js +1 -0
- package/lib/db/RemoteConnection.js.map +1 -0
- package/lib/db/SqliteWorker.js +1 -0
- package/lib/db/SqliteWorker.js.map +1 -0
- package/lib/db/WorkerConnectionPool.js +1 -0
- package/lib/db/WorkerConnectionPool.js.map +1 -0
- package/lib/db/options.js +1 -0
- package/lib/db/options.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/lib/sync/stream/NodeRemote.js +1 -0
- package/lib/sync/stream/NodeRemote.js.map +1 -0
- package/lib/sync/stream/NodeStreamingSyncImplementation.d.ts +3 -3
- package/lib/sync/stream/NodeStreamingSyncImplementation.js +8 -4
- package/lib/sync/stream/NodeStreamingSyncImplementation.js.map +1 -0
- package/lib/utils/modules.js +1 -0
- package/lib/utils/modules.js.map +1 -0
- package/lib/utils/modules_commonjs.js +1 -0
- package/lib/utils/modules_commonjs.js.map +1 -0
- package/lib/worker.js +1 -0
- package/lib/worker.js.map +1 -0
- package/package.json +9 -9
- package/src/db/AsyncDatabase.ts +26 -0
- package/src/db/BetterSqliteWorker.ts +72 -0
- package/src/db/DefaultWorker.ts +3 -0
- package/src/db/NodeSqliteWorker.ts +63 -0
- package/src/db/PowerSyncDatabase.ts +100 -0
- package/src/db/RemoteConnection.ts +105 -0
- package/src/db/SqliteWorker.ts +119 -0
- package/src/db/WorkerConnectionPool.ts +331 -0
- package/src/db/options.ts +51 -0
- package/src/index.ts +4 -0
- package/src/sync/stream/NodeRemote.ts +123 -0
- package/src/sync/stream/NodeStreamingSyncImplementation.ts +57 -0
- package/src/utils/modules.ts +6 -0
- package/src/utils/modules_commonjs.ts +7 -0
- package/src/worker.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/node",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20251106124255",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
"files": [
|
|
11
11
|
"lib",
|
|
12
12
|
"dist",
|
|
13
|
-
"download_core.js"
|
|
13
|
+
"download_core.js",
|
|
14
|
+
"src"
|
|
14
15
|
],
|
|
15
16
|
"type": "module",
|
|
16
17
|
"exports": {
|
|
@@ -47,7 +48,7 @@
|
|
|
47
48
|
},
|
|
48
49
|
"homepage": "https://docs.powersync.com/",
|
|
49
50
|
"peerDependencies": {
|
|
50
|
-
"@powersync/common": "0.0.0-dev-
|
|
51
|
+
"@powersync/common": "0.0.0-dev-20251106124255",
|
|
51
52
|
"better-sqlite3": "12.x"
|
|
52
53
|
},
|
|
53
54
|
"peerDependenciesMeta": {
|
|
@@ -56,21 +57,20 @@
|
|
|
56
57
|
}
|
|
57
58
|
},
|
|
58
59
|
"dependencies": {
|
|
59
|
-
"async-
|
|
60
|
-
"bson": "^6.6.0",
|
|
60
|
+
"async-mutex": "^0.5.0",
|
|
61
61
|
"comlink": "^4.4.2",
|
|
62
62
|
"undici": "^7.11.0",
|
|
63
|
-
"
|
|
63
|
+
"bson": "^6.10.4",
|
|
64
|
+
"@powersync/common": "0.0.0-dev-20251106124255"
|
|
64
65
|
},
|
|
65
66
|
"devDependencies": {
|
|
66
|
-
"@types/async-lock": "^1.4.0",
|
|
67
67
|
"@types/node": "^24.2.0",
|
|
68
68
|
"better-sqlite3": "^12.2.0",
|
|
69
69
|
"drizzle-orm": "^0.35.2",
|
|
70
70
|
"rollup": "4.14.3",
|
|
71
71
|
"typescript": "^5.5.3",
|
|
72
72
|
"vitest": "^3.2.4",
|
|
73
|
-
"@powersync/drizzle-driver": "0.
|
|
73
|
+
"@powersync/drizzle-driver": "0.0.0-dev-20251106124255"
|
|
74
74
|
},
|
|
75
75
|
"keywords": [
|
|
76
76
|
"data sync",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"scripts": {
|
|
83
83
|
"prepare:core": "node download_core.js",
|
|
84
84
|
"build": " pnpm prepare:core && tsc -b && rollup --config",
|
|
85
|
-
"build:prod": "pnpm prepare:core && tsc -b
|
|
85
|
+
"build:prod": "pnpm prepare:core && tsc -b && rollup --config",
|
|
86
86
|
"clean": "rm -rf lib dist tsconfig.tsbuildinfo",
|
|
87
87
|
"watch": "tsc -b -w",
|
|
88
88
|
"test": " pnpm prepare:core && vitest",
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { QueryResult } from '@powersync/common';
|
|
2
|
+
import { NodeDatabaseImplementation } from './options.js';
|
|
3
|
+
|
|
4
|
+
export type ProxiedQueryResult = Omit<QueryResult, 'rows'> & {
|
|
5
|
+
rows?: {
|
|
6
|
+
_array: any[];
|
|
7
|
+
length: number;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export interface AsyncDatabaseOpenOptions {
|
|
12
|
+
path: string;
|
|
13
|
+
isWriter: boolean;
|
|
14
|
+
implementation: NodeDatabaseImplementation;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface AsyncDatabaseOpener {
|
|
18
|
+
open(options: AsyncDatabaseOpenOptions): Promise<AsyncDatabase>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface AsyncDatabase {
|
|
22
|
+
execute: (query: string, params: any[]) => Promise<ProxiedQueryResult>;
|
|
23
|
+
executeRaw: (query: string, params: any[]) => Promise<any[][]>;
|
|
24
|
+
executeBatch: (query: string, params: any[][]) => Promise<ProxiedQueryResult>;
|
|
25
|
+
close: () => Promise<void>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import type { Database } from 'better-sqlite3';
|
|
2
|
+
import { AsyncDatabase, AsyncDatabaseOpenOptions } from './AsyncDatabase.js';
|
|
3
|
+
import { PowerSyncWorkerOptions } from './SqliteWorker.js';
|
|
4
|
+
import { threadId } from 'node:worker_threads';
|
|
5
|
+
|
|
6
|
+
class BlockingAsyncDatabase implements AsyncDatabase {
|
|
7
|
+
private readonly db: Database;
|
|
8
|
+
|
|
9
|
+
constructor(db: Database) {
|
|
10
|
+
this.db = db;
|
|
11
|
+
|
|
12
|
+
db.function('node_thread_id', () => threadId);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async close() {
|
|
16
|
+
this.db.close();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async execute(query: string, params: any[]) {
|
|
20
|
+
const stmt = this.db.prepare(query);
|
|
21
|
+
if (stmt.reader) {
|
|
22
|
+
const rows = stmt.all(params);
|
|
23
|
+
return {
|
|
24
|
+
rowsAffected: 0,
|
|
25
|
+
rows: {
|
|
26
|
+
_array: rows,
|
|
27
|
+
length: rows.length
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
} else {
|
|
31
|
+
const info = stmt.run(params);
|
|
32
|
+
return {
|
|
33
|
+
rowsAffected: info.changes,
|
|
34
|
+
insertId: Number(info.lastInsertRowid)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async executeRaw(query: string, params: any[]) {
|
|
40
|
+
const stmt = this.db.prepare(query);
|
|
41
|
+
|
|
42
|
+
if (stmt.reader) {
|
|
43
|
+
return stmt.raw().all(params) as any[][];
|
|
44
|
+
} else {
|
|
45
|
+
stmt.raw().run(params);
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async executeBatch(query: string, params: any[][]) {
|
|
51
|
+
params = params ?? [];
|
|
52
|
+
|
|
53
|
+
let rowsAffected = 0;
|
|
54
|
+
|
|
55
|
+
const stmt = this.db.prepare(query);
|
|
56
|
+
for (const paramSet of params) {
|
|
57
|
+
const info = stmt.run(paramSet);
|
|
58
|
+
rowsAffected += info.changes;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { rowsAffected };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function openDatabase(worker: PowerSyncWorkerOptions, options: AsyncDatabaseOpenOptions) {
|
|
66
|
+
const BetterSQLite3Database = await worker.loadBetterSqlite3();
|
|
67
|
+
const baseDB = new BetterSQLite3Database(options.path);
|
|
68
|
+
baseDB.loadExtension(worker.extensionPath(), 'sqlite3_powersync_init');
|
|
69
|
+
|
|
70
|
+
const asyncDb = new BlockingAsyncDatabase(baseDB);
|
|
71
|
+
return asyncDb;
|
|
72
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import { threadId } from 'node:worker_threads';
|
|
3
|
+
|
|
4
|
+
import { dynamicImport } from '../utils/modules.js';
|
|
5
|
+
import { AsyncDatabase, AsyncDatabaseOpenOptions } from './AsyncDatabase.js';
|
|
6
|
+
import { PowerSyncWorkerOptions } from './SqliteWorker.js';
|
|
7
|
+
|
|
8
|
+
class BlockingNodeDatabase implements AsyncDatabase {
|
|
9
|
+
private readonly db: DatabaseSync;
|
|
10
|
+
|
|
11
|
+
constructor(db: DatabaseSync, write: boolean) {
|
|
12
|
+
this.db = db;
|
|
13
|
+
|
|
14
|
+
db.function('node_thread_id', () => threadId);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async close() {
|
|
18
|
+
this.db.close();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async execute(query: string, params: any[]) {
|
|
22
|
+
const stmt = this.db.prepare(query);
|
|
23
|
+
const rows = stmt.all(...params);
|
|
24
|
+
return {
|
|
25
|
+
rowsAffected: 0,
|
|
26
|
+
rows: {
|
|
27
|
+
_array: rows,
|
|
28
|
+
length: rows.length
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async executeRaw(query: string, params: any[]) {
|
|
34
|
+
const stmt = this.db.prepare(query);
|
|
35
|
+
(stmt as any).setReturnArrays(true); // Missing in @types/node, https://nodejs.org/api/sqlite.html#statementsetreturnarraysenabled
|
|
36
|
+
return stmt.all(...params) as any as any[][];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async executeBatch(query: string, params: any[][]) {
|
|
40
|
+
params = params ?? [];
|
|
41
|
+
|
|
42
|
+
let rowsAffected = 0;
|
|
43
|
+
|
|
44
|
+
const stmt = this.db.prepare(query);
|
|
45
|
+
for (const paramSet of params) {
|
|
46
|
+
const info = stmt.run(...paramSet);
|
|
47
|
+
rowsAffected += info.changes as number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { rowsAffected };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function openDatabase(worker: PowerSyncWorkerOptions, options: AsyncDatabaseOpenOptions) {
|
|
55
|
+
// NOTE: We want to import node:sqlite dynamically, to avoid bundlers unconditionally requiring node:sqlite in the
|
|
56
|
+
// end, since that would make us incompatible with older Node.JS versions.
|
|
57
|
+
const { DatabaseSync } = await dynamicImport('node:sqlite');
|
|
58
|
+
|
|
59
|
+
const baseDB = new DatabaseSync(options.path, { allowExtension: true });
|
|
60
|
+
baseDB.loadExtension(worker.extensionPath(), 'sqlite3_powersync_init');
|
|
61
|
+
|
|
62
|
+
return new BlockingNodeDatabase(baseDB, options.isWriter);
|
|
63
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AbstractPowerSyncDatabase,
|
|
3
|
+
AbstractRemoteOptions,
|
|
4
|
+
AbstractStreamingSyncImplementation,
|
|
5
|
+
AdditionalConnectionOptions,
|
|
6
|
+
BucketStorageAdapter,
|
|
7
|
+
DBAdapter,
|
|
8
|
+
PowerSyncBackendConnector,
|
|
9
|
+
PowerSyncConnectionOptions,
|
|
10
|
+
PowerSyncDatabaseOptions,
|
|
11
|
+
PowerSyncDatabaseOptionsWithSettings,
|
|
12
|
+
RequiredAdditionalConnectionOptions,
|
|
13
|
+
SqliteBucketStorage,
|
|
14
|
+
SQLOpenFactory
|
|
15
|
+
} from '@powersync/common';
|
|
16
|
+
|
|
17
|
+
import { NodeCustomConnectionOptions, NodeRemote } from '../sync/stream/NodeRemote.js';
|
|
18
|
+
import { NodeStreamingSyncImplementation } from '../sync/stream/NodeStreamingSyncImplementation.js';
|
|
19
|
+
|
|
20
|
+
import { WorkerConnectionPool } from './WorkerConnectionPool.js';
|
|
21
|
+
import { NodeSQLOpenOptions } from './options.js';
|
|
22
|
+
|
|
23
|
+
export type NodePowerSyncDatabaseOptions = PowerSyncDatabaseOptions & {
|
|
24
|
+
database: DBAdapter | SQLOpenFactory | NodeSQLOpenOptions;
|
|
25
|
+
/**
|
|
26
|
+
* Options to override how the SDK will connect to the sync service.
|
|
27
|
+
*
|
|
28
|
+
* This option is intended to be used for internal tests.
|
|
29
|
+
*/
|
|
30
|
+
remoteOptions?: Partial<AbstractRemoteOptions>;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type NodeAdditionalConnectionOptions = AdditionalConnectionOptions & NodeCustomConnectionOptions;
|
|
34
|
+
|
|
35
|
+
export type NodePowerSyncConnectionOptions = PowerSyncConnectionOptions & NodeAdditionalConnectionOptions;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A PowerSync database which provides SQLite functionality
|
|
39
|
+
* which is automatically synced.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* export const db = new PowerSyncDatabase({
|
|
44
|
+
* schema: AppSchema,
|
|
45
|
+
* database: {
|
|
46
|
+
* dbFilename: 'example.db'
|
|
47
|
+
* }
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
|
|
52
|
+
constructor(options: NodePowerSyncDatabaseOptions) {
|
|
53
|
+
super(options);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _initialize(): Promise<void> {
|
|
57
|
+
await (this.database as WorkerConnectionPool).initialize();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Opens a DBAdapter using better-sqlite3 as the default SQLite open factory.
|
|
62
|
+
*/
|
|
63
|
+
protected openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter {
|
|
64
|
+
return new WorkerConnectionPool(options.database);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
protected generateBucketStorageAdapter(): BucketStorageAdapter {
|
|
68
|
+
return new SqliteBucketStorage(this.database, this.logger);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
connect(
|
|
72
|
+
connector: PowerSyncBackendConnector,
|
|
73
|
+
options?: PowerSyncConnectionOptions & NodeCustomConnectionOptions
|
|
74
|
+
): Promise<void> {
|
|
75
|
+
return super.connect(connector, options);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
protected generateSyncStreamImplementation(
|
|
79
|
+
connector: PowerSyncBackendConnector,
|
|
80
|
+
options: RequiredAdditionalConnectionOptions & NodeAdditionalConnectionOptions
|
|
81
|
+
): AbstractStreamingSyncImplementation {
|
|
82
|
+
const logger = this.logger;
|
|
83
|
+
const remote = new NodeRemote(connector, logger, {
|
|
84
|
+
dispatcher: options.dispatcher,
|
|
85
|
+
...(this.options as NodePowerSyncDatabaseOptions).remoteOptions
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
return new NodeStreamingSyncImplementation({
|
|
89
|
+
adapter: this.bucketStorageAdapter,
|
|
90
|
+
remote,
|
|
91
|
+
uploadCrud: async () => {
|
|
92
|
+
await this.waitForReady();
|
|
93
|
+
await connector.uploadData(this);
|
|
94
|
+
},
|
|
95
|
+
...options,
|
|
96
|
+
identifier: this.database.name,
|
|
97
|
+
logger
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Worker } from 'node:worker_threads';
|
|
2
|
+
import { LockContext, QueryResult } from '@powersync/common';
|
|
3
|
+
import { releaseProxy, Remote } from 'comlink';
|
|
4
|
+
import { AsyncDatabase, AsyncDatabaseOpener, ProxiedQueryResult } from './AsyncDatabase.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A PowerSync database connection implemented with RPC calls to a background worker.
|
|
8
|
+
*/
|
|
9
|
+
export class RemoteConnection implements LockContext {
|
|
10
|
+
isBusy = false;
|
|
11
|
+
|
|
12
|
+
private readonly worker: Worker;
|
|
13
|
+
private readonly comlink: Remote<AsyncDatabaseOpener>;
|
|
14
|
+
readonly database: Remote<AsyncDatabase>;
|
|
15
|
+
|
|
16
|
+
constructor(worker: Worker, comlink: Remote<AsyncDatabaseOpener>, database: Remote<AsyncDatabase>) {
|
|
17
|
+
this.worker = worker;
|
|
18
|
+
this.comlink = comlink;
|
|
19
|
+
this.database = database;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Runs the inner function, but appends the stack trace where this function was called. This is useful for workers
|
|
24
|
+
* because stack traces from worker errors are otherwise unrelated to the application issue that has caused them.
|
|
25
|
+
*/
|
|
26
|
+
private async recoverTrace<T>(inner: () => Promise<T>): Promise<T> {
|
|
27
|
+
const trace = {};
|
|
28
|
+
Error.captureStackTrace(trace);
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
return await inner();
|
|
32
|
+
} catch (e) {
|
|
33
|
+
if (e instanceof Error && e.stack) {
|
|
34
|
+
e.stack += (trace as any).stack;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
throw e;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
executeBatch(query: string, params: any[][] = []): Promise<QueryResult> {
|
|
42
|
+
return this.recoverTrace(async () => {
|
|
43
|
+
const result = await this.database.executeBatch(query, params ?? []);
|
|
44
|
+
return RemoteConnection.wrapQueryResult(result);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
execute(query: string, params?: any[] | undefined): Promise<QueryResult> {
|
|
49
|
+
return this.recoverTrace(async () => {
|
|
50
|
+
const result = await this.database.execute(query, params ?? []);
|
|
51
|
+
return RemoteConnection.wrapQueryResult(result);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
executeRaw(query: string, params?: any[] | undefined): Promise<any[][]> {
|
|
56
|
+
return this.recoverTrace(async () => {
|
|
57
|
+
return await this.database.executeRaw(query, params ?? []);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async getAll<T>(sql: string, parameters?: any[]): Promise<T[]> {
|
|
62
|
+
const res = await this.execute(sql, parameters);
|
|
63
|
+
return res.rows?._array ?? [];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async getOptional<T>(sql: string, parameters?: any[]): Promise<T | null> {
|
|
67
|
+
const res = await this.execute(sql, parameters);
|
|
68
|
+
return res.rows?.item(0) ?? null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async get<T>(sql: string, parameters?: any[]): Promise<T> {
|
|
72
|
+
const res = await this.execute(sql, parameters);
|
|
73
|
+
const first = res.rows?.item(0);
|
|
74
|
+
if (!first) {
|
|
75
|
+
throw new Error('Result set is empty');
|
|
76
|
+
}
|
|
77
|
+
return first;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async refreshSchema() {
|
|
81
|
+
await this.execute("pragma table_info('sqlite_master')");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async close() {
|
|
85
|
+
await this.database.close();
|
|
86
|
+
this.database[releaseProxy]();
|
|
87
|
+
this.comlink[releaseProxy]();
|
|
88
|
+
await this.worker.terminate();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
static wrapQueryResult(result: ProxiedQueryResult): QueryResult {
|
|
92
|
+
let rows: QueryResult['rows'] | undefined = undefined;
|
|
93
|
+
if (result.rows) {
|
|
94
|
+
rows = {
|
|
95
|
+
...result.rows,
|
|
96
|
+
item: (idx) => result.rows?._array[idx]
|
|
97
|
+
} satisfies QueryResult['rows'];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
...result,
|
|
102
|
+
rows
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import OS from 'node:os';
|
|
3
|
+
import * as path from 'node:path';
|
|
4
|
+
import url from 'node:url';
|
|
5
|
+
import { parentPort } from 'node:worker_threads';
|
|
6
|
+
import { dynamicImport, isBundledToCommonJs } from '../utils/modules.js';
|
|
7
|
+
import { AsyncDatabase, AsyncDatabaseOpener, AsyncDatabaseOpenOptions } from './AsyncDatabase.js';
|
|
8
|
+
import { openDatabase as openBetterSqliteDatabase } from './BetterSqliteWorker.js';
|
|
9
|
+
import { openDatabase as openNodeDatabase } from './NodeSqliteWorker.js';
|
|
10
|
+
|
|
11
|
+
export interface PowerSyncWorkerOptions {
|
|
12
|
+
/**
|
|
13
|
+
* A function responsible for finding the powersync DLL/so/dylib file.
|
|
14
|
+
*
|
|
15
|
+
* @returns The absolute path of the PowerSync SQLite core extensions library.
|
|
16
|
+
*/
|
|
17
|
+
extensionPath: () => string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A function that returns the `Database` constructor from the `better-sqlite3` package.
|
|
21
|
+
*/
|
|
22
|
+
loadBetterSqlite3: () => Promise<any>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @returns The relevant PowerSync extension binary filename for the current platform and architecture
|
|
27
|
+
*/
|
|
28
|
+
export function getPowerSyncExtensionFilename() {
|
|
29
|
+
const platform = OS.platform();
|
|
30
|
+
const arch = OS.arch();
|
|
31
|
+
let extensionFile: string;
|
|
32
|
+
|
|
33
|
+
if (platform == 'win32') {
|
|
34
|
+
if (arch == 'x64') {
|
|
35
|
+
extensionFile = 'powersync_x64.dll';
|
|
36
|
+
} else if (arch == 'ia32') {
|
|
37
|
+
extensionFile = 'powersync_x86.dll';
|
|
38
|
+
} else if (arch == 'arm64') {
|
|
39
|
+
extensionFile = 'powersync_aarch64.dll';
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error('Windows platform only supports arm64, ia32 and x64 architecture.');
|
|
42
|
+
}
|
|
43
|
+
} else if (platform == 'linux') {
|
|
44
|
+
if (arch == 'x64') {
|
|
45
|
+
extensionFile = 'libpowersync_x64.so';
|
|
46
|
+
} else if (arch == 'arm64') {
|
|
47
|
+
// TODO detect armv7 as an option
|
|
48
|
+
extensionFile = 'libpowersync_aarch64.so';
|
|
49
|
+
} else if (arch == 'riscv64') {
|
|
50
|
+
extensionFile = 'libpowersync_riscv64gc.so';
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error('Linux platform only supports x64, arm64 and riscv64 architectures.');
|
|
53
|
+
}
|
|
54
|
+
} else if (platform == 'darwin') {
|
|
55
|
+
if (arch == 'x64') {
|
|
56
|
+
extensionFile = 'libpowersync_x64.dylib';
|
|
57
|
+
} else if (arch == 'arm64') {
|
|
58
|
+
extensionFile = 'libpowersync_aarch64.dylib';
|
|
59
|
+
} else {
|
|
60
|
+
throw new Error('macOS platform only supports x64 and arm64 architectures.');
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error(
|
|
64
|
+
`Unknown platform: ${platform}, PowerSync for Node.js currently supports Windows, Linux and macOS.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return extensionFile;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function startPowerSyncWorker(options?: Partial<PowerSyncWorkerOptions>) {
|
|
72
|
+
const resolvedOptions: PowerSyncWorkerOptions = {
|
|
73
|
+
extensionPath() {
|
|
74
|
+
const isCommonJsModule = isBundledToCommonJs;
|
|
75
|
+
const extensionFilename = getPowerSyncExtensionFilename();
|
|
76
|
+
let resolved: string;
|
|
77
|
+
if (isCommonJsModule) {
|
|
78
|
+
resolved = path.resolve(__dirname, '../lib/', extensionFilename);
|
|
79
|
+
} else {
|
|
80
|
+
resolved = url.fileURLToPath(new URL(`../${extensionFilename}`, import.meta.url));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return resolved;
|
|
84
|
+
},
|
|
85
|
+
async loadBetterSqlite3() {
|
|
86
|
+
const module = await dynamicImport('better-sqlite3');
|
|
87
|
+
return module.default;
|
|
88
|
+
},
|
|
89
|
+
...options
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
Comlink.expose(new DatabaseOpenHelper(resolvedOptions), parentPort! as Comlink.Endpoint);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
class DatabaseOpenHelper implements AsyncDatabaseOpener {
|
|
96
|
+
private options: PowerSyncWorkerOptions;
|
|
97
|
+
|
|
98
|
+
constructor(options: PowerSyncWorkerOptions) {
|
|
99
|
+
this.options = options;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
async open(options: AsyncDatabaseOpenOptions): Promise<AsyncDatabase> {
|
|
103
|
+
let database: AsyncDatabase;
|
|
104
|
+
|
|
105
|
+
const implementation = options.implementation;
|
|
106
|
+
switch (implementation.type) {
|
|
107
|
+
case 'better-sqlite3':
|
|
108
|
+
database = await openBetterSqliteDatabase(this.options, options);
|
|
109
|
+
break;
|
|
110
|
+
case 'node:sqlite':
|
|
111
|
+
database = await openNodeDatabase(this.options, options);
|
|
112
|
+
break;
|
|
113
|
+
default:
|
|
114
|
+
throw new Error(`Unknown database implementation: ${options.implementation}.`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return Comlink.proxy(database);
|
|
118
|
+
}
|
|
119
|
+
}
|