@powersync/node 0.11.1 → 0.12.0
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/README.md +91 -10
- package/dist/DefaultWorker.cjs +140 -62
- package/dist/DefaultWorker.cjs.map +1 -1
- package/dist/bundle.cjs +25 -11
- package/dist/bundle.cjs.map +1 -1
- package/dist/bundle.d.cts +23 -1
- package/dist/worker.cjs +141 -62
- package/dist/worker.cjs.map +1 -1
- package/dist/worker.d.cts +9 -1
- package/download_core.js +75 -32
- package/lib/db/AsyncDatabase.d.ts +7 -2
- package/lib/db/BetterSqliteWorker.d.ts +26 -0
- package/lib/db/BetterSqliteWorker.js +58 -0
- package/lib/db/NodeSqliteWorker.d.ts +21 -0
- package/lib/db/NodeSqliteWorker.js +46 -0
- package/lib/db/PowerSyncDatabase.js +2 -2
- package/lib/db/SqliteWorker.d.ts +8 -0
- package/lib/db/SqliteWorker.js +75 -98
- package/lib/db/{BetterSQLite3DBAdapter.d.ts → WorkerConnectionPool.d.ts} +1 -1
- package/lib/db/{BetterSQLite3DBAdapter.js → WorkerConnectionPool.js} +25 -5
- package/lib/db/options.d.ts +23 -1
- package/lib/libpowersync_aarch64.dylib +0 -0
- package/lib/libpowersync_aarch64.so +0 -0
- package/lib/libpowersync_armv7.so +0 -0
- package/lib/libpowersync_riscv64gc.so +0 -0
- package/lib/libpowersync_x64.dylib +0 -0
- package/lib/libpowersync_x86.so +0 -0
- package/lib/powersync_aarch64.dll +0 -0
- package/lib/powersync_x64.dll +0 -0
- package/lib/powersync_x86.dll +0 -0
- package/lib/utils/modules.d.ts +2 -0
- package/lib/utils/modules.js +5 -0
- package/lib/utils/modules_commonjs.d.ts +2 -0
- package/lib/utils/modules_commonjs.js +6 -0
- package/package.json +14 -7
- /package/lib/{libpowersync.so → libpowersync_x64.so} +0 -0
package/README.md
CHANGED
|
@@ -15,12 +15,12 @@ See a summary of features [here](https://docs.powersync.com/client-sdk-reference
|
|
|
15
15
|
|
|
16
16
|
The `@powersync/node` package is currently in a Beta release.
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
## Installation
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
### Install Package
|
|
21
21
|
|
|
22
22
|
```bash
|
|
23
|
-
npm install @powersync/node
|
|
23
|
+
npm install @powersync/node better-sqlite3
|
|
24
24
|
```
|
|
25
25
|
|
|
26
26
|
Both `@powersync/node` and the `better-sqlite3` packages have install scripts that need to run to compile
|
|
@@ -47,38 +47,119 @@ To resolve this, either:
|
|
|
47
47
|
- Upgrade `node-gyp` to version 10 or later.
|
|
48
48
|
- Install Python [setuptools](https://pypi.org/project/setuptools/), which includes `distutils`.
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
#### Package better-sqlite3 not found errors
|
|
51
|
+
|
|
52
|
+
This package does not import `better-sqlite3` statically (with unconditional `require()` or static `import` statements).
|
|
53
|
+
Instead, to allow users to use `node:sqlite` instead of that package, a dynamic `require()` / `import` expression is used.
|
|
54
|
+
This may prevent bundlers from detecting that `better-sqlite3` is used by this package.
|
|
55
|
+
|
|
56
|
+
To fix this, ensure you have a dependency on `better-sqlite3` (and, if you're using TypeScript, a dev-dependency on
|
|
57
|
+
`@types/better-sqlite3`).
|
|
58
|
+
|
|
59
|
+
In your project, create a `PowerSync.worker.ts` file with the following contents:
|
|
60
|
+
|
|
61
|
+
```TypeScript
|
|
62
|
+
import Database from 'better-sqlite3';
|
|
63
|
+
|
|
64
|
+
import { startPowerSyncWorker } from '@powersync/node/worker.js';
|
|
65
|
+
|
|
66
|
+
async function resolveBetterSqlite3() {
|
|
67
|
+
return Database;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
startPowerSyncWorker({ loadBetterSqlite3: resolveBetterSqlite3 });
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Finally, when you open the `PowerSyncDatabase`, instruct PowerSync to use your custom worker:
|
|
74
|
+
|
|
75
|
+
```TypeScript
|
|
76
|
+
const db = new PowerSyncDatabase({
|
|
77
|
+
schema: AppSchema,
|
|
78
|
+
database: {
|
|
79
|
+
dbFilename: 'test.db',
|
|
80
|
+
openWorker: (_, options) => {
|
|
81
|
+
return new Worker(new URL('./PowerSync.worker.js', import.meta.url), options);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
logger
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Getting Started
|
|
51
89
|
|
|
52
90
|
The [Node.js SDK reference](https://docs.powersync.com/client-sdk-references/node)
|
|
53
91
|
contains everything you need to know to get started implementing PowerSync in your project.
|
|
54
92
|
|
|
55
|
-
|
|
93
|
+
## Examples
|
|
56
94
|
|
|
57
95
|
A simple example using `@powersync/node` is available in the [`demos/example-node/`](../demos/example-node) directory.
|
|
58
96
|
|
|
59
|
-
|
|
97
|
+
## Proxy Support
|
|
60
98
|
|
|
61
99
|
This SDK supports HTTP, HTTPS, and WebSocket proxies via environment variables.
|
|
62
100
|
|
|
63
|
-
|
|
101
|
+
### HTTP Connection Method
|
|
64
102
|
|
|
65
103
|
Internally we probe the http environment variables and apply it to fetch requests ([undici](https://www.npmjs.com/package/undici/v/5.6.0))
|
|
66
104
|
|
|
67
105
|
- Set the `HTTPS_PROXY` or `HTTP_PROXY` environment variable to automatically route HTTP requests through a proxy.
|
|
68
106
|
|
|
69
|
-
|
|
107
|
+
### WEB Socket Connection Method
|
|
70
108
|
|
|
71
109
|
Internally the [proxy-agent](https://www.npmjs.com/package/proxy-agent) dependency for WebSocket proxies, which has its own internal code for automatically picking up the appropriate environment variables:
|
|
72
110
|
|
|
73
111
|
- Set the `WS_PROXY` or `WSS_PROXY` environment variable to route the webocket connections through a proxy.
|
|
74
112
|
|
|
75
|
-
|
|
113
|
+
## Encryption
|
|
114
|
+
|
|
115
|
+
This package can be used with the [`better-sqlite3-multiple-ciphers`](https://www.npmjs.com/package/better-sqlite3-multiple-ciphers) fork of `better-sqlite3` for encryption.
|
|
116
|
+
|
|
117
|
+
This requires a custom worker loading the forked package:
|
|
118
|
+
|
|
119
|
+
```TypeScript
|
|
120
|
+
// encryption.worker.ts
|
|
121
|
+
import Database from 'better-sqlite3-multiple-ciphers';
|
|
122
|
+
|
|
123
|
+
import { startPowerSyncWorker } from '@powersync/node/worker.js';
|
|
124
|
+
|
|
125
|
+
async function resolveBetterSqlite3() {
|
|
126
|
+
return Database;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
startPowerSyncWorker({ loadBetterSqlite3: resolveBetterSqlite3 });
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Then, when opening the database, use that custom worker:
|
|
133
|
+
|
|
134
|
+
```TypeScript
|
|
135
|
+
const db = new PowerSyncDatabase({
|
|
136
|
+
schema: AppSchema,
|
|
137
|
+
database: {
|
|
138
|
+
dbFilename: 'test.db',
|
|
139
|
+
openWorker: (_, options) => {
|
|
140
|
+
return new Worker(new URL('./PowerSync.worker.js', import.meta.url), options);
|
|
141
|
+
},
|
|
142
|
+
initializeConnection: async (db) => {
|
|
143
|
+
if (encryptionKey.length) {
|
|
144
|
+
const escapedKey = encryptionKey.replace("'", "''");
|
|
145
|
+
await db.execute(`pragma key = '${escapedKey}'`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Make sure the database is readable, this fails early if the key is wrong.
|
|
149
|
+
await db.execute('pragma user_version');
|
|
150
|
+
}
|
|
151
|
+
},
|
|
152
|
+
logger
|
|
153
|
+
});
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Found a bug or need help?
|
|
76
157
|
|
|
77
158
|
- Join our [Discord server](https://discord.gg/powersync) where you can browse topics from our community, ask questions, share feedback, or just say hello :)
|
|
78
159
|
- Please open a [GitHub issue](https://github.com/powersync-ja/powersync-js/issues) when you come across a bug.
|
|
79
160
|
- Have feedback or an idea? [Submit an idea](https://roadmap.powersync.com/tabs/5-roadmap/submit-idea) via our public roadmap or [schedule a chat](https://calendly.com/powersync-product/powersync-chat) with someone from our product team.
|
|
80
161
|
|
|
81
|
-
|
|
162
|
+
## Thanks
|
|
82
163
|
|
|
83
164
|
The PowerSync Node.js SDK relies on the work contributors and maintainers have put into the upstream better-sqlite3 package.
|
|
84
165
|
In particular, we'd like to thank [@spinda](https://github.com/spinda) for contributing support for update, commit and rollback hooks!
|
package/dist/DefaultWorker.cjs
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var path = require('node:path');
|
|
4
|
-
var BetterSQLite3Database = require('@powersync/better-sqlite3');
|
|
5
3
|
var Comlink = require('comlink');
|
|
6
|
-
var node_worker_threads = require('node:worker_threads');
|
|
7
4
|
var OS = require('node:os');
|
|
8
|
-
var
|
|
5
|
+
var path = require('node:path');
|
|
6
|
+
require('node:url');
|
|
7
|
+
var node_worker_threads = require('node:worker_threads');
|
|
9
8
|
|
|
10
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
11
9
|
function _interopNamespaceDefault(e) {
|
|
12
10
|
var n = Object.create(null);
|
|
13
11
|
if (e) {
|
|
@@ -25,37 +23,21 @@ function _interopNamespaceDefault(e) {
|
|
|
25
23
|
return Object.freeze(n);
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
29
26
|
var Comlink__namespace = /*#__PURE__*/_interopNamespaceDefault(Comlink);
|
|
27
|
+
var path__namespace = /*#__PURE__*/_interopNamespaceDefault(path);
|
|
28
|
+
|
|
29
|
+
// NOTE! Do not import this file directly! We have a rollup plugin that rewrites imports to modules.js to this file when
|
|
30
|
+
// bundling to CommonJS.
|
|
31
|
+
async function dynamicImport(path) {
|
|
32
|
+
return require(path);
|
|
33
|
+
}
|
|
30
34
|
|
|
31
35
|
class BlockingAsyncDatabase {
|
|
32
36
|
db;
|
|
33
|
-
uncommittedUpdatedTables = new Set();
|
|
34
|
-
committedUpdatedTables = new Set();
|
|
35
37
|
constructor(db) {
|
|
36
38
|
this.db = db;
|
|
37
39
|
db.function('node_thread_id', () => node_worker_threads.threadId);
|
|
38
40
|
}
|
|
39
|
-
collectCommittedUpdates() {
|
|
40
|
-
const resolved = Promise.resolve([...this.committedUpdatedTables]);
|
|
41
|
-
this.committedUpdatedTables.clear();
|
|
42
|
-
return resolved;
|
|
43
|
-
}
|
|
44
|
-
installUpdateHooks() {
|
|
45
|
-
this.db.updateHook((_op, _dbName, tableName, _rowid) => {
|
|
46
|
-
this.uncommittedUpdatedTables.add(tableName);
|
|
47
|
-
});
|
|
48
|
-
this.db.commitHook(() => {
|
|
49
|
-
for (const tableName of this.uncommittedUpdatedTables) {
|
|
50
|
-
this.committedUpdatedTables.add(tableName);
|
|
51
|
-
}
|
|
52
|
-
this.uncommittedUpdatedTables.clear();
|
|
53
|
-
return true;
|
|
54
|
-
});
|
|
55
|
-
this.db.rollbackHook(() => {
|
|
56
|
-
this.uncommittedUpdatedTables.clear();
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
41
|
async close() {
|
|
60
42
|
this.db.close();
|
|
61
43
|
}
|
|
@@ -100,53 +82,149 @@ class BlockingAsyncDatabase {
|
|
|
100
82
|
return { rowsAffected };
|
|
101
83
|
}
|
|
102
84
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
85
|
+
async function openDatabase$1(worker, options) {
|
|
86
|
+
const BetterSQLite3Database = await worker.loadBetterSqlite3();
|
|
87
|
+
const baseDB = new BetterSQLite3Database(options.path);
|
|
88
|
+
baseDB.loadExtension(worker.extensionPath(), 'sqlite3_powersync_init');
|
|
89
|
+
const asyncDb = new BlockingAsyncDatabase(baseDB);
|
|
90
|
+
return asyncDb;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class BlockingNodeDatabase {
|
|
94
|
+
db;
|
|
95
|
+
constructor(db, write) {
|
|
96
|
+
this.db = db;
|
|
97
|
+
db.function('node_thread_id', () => node_worker_threads.threadId);
|
|
98
|
+
}
|
|
99
|
+
async close() {
|
|
100
|
+
this.db.close();
|
|
101
|
+
}
|
|
102
|
+
async execute(query, params) {
|
|
103
|
+
const stmt = this.db.prepare(query);
|
|
104
|
+
const rows = stmt.all(...params);
|
|
105
|
+
return {
|
|
106
|
+
rowsAffected: 0,
|
|
107
|
+
rows: {
|
|
108
|
+
_array: rows,
|
|
109
|
+
length: rows.length
|
|
110
|
+
}
|
|
111
|
+
};
|
|
107
112
|
}
|
|
108
|
-
async
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
113
|
+
async executeRaw(query, params) {
|
|
114
|
+
const stmt = this.db.prepare(query);
|
|
115
|
+
stmt.setReturnArrays(true); // Missing in @types/node, https://nodejs.org/api/sqlite.html#statementsetreturnarraysenabled
|
|
116
|
+
return stmt.all(...params);
|
|
117
|
+
}
|
|
118
|
+
async executeBatch(query, params) {
|
|
119
|
+
params = params ?? [];
|
|
120
|
+
let rowsAffected = 0;
|
|
121
|
+
const stmt = this.db.prepare(query);
|
|
122
|
+
for (const paramSet of params) {
|
|
123
|
+
const info = stmt.run(...paramSet);
|
|
124
|
+
rowsAffected += info.changes;
|
|
125
|
+
}
|
|
126
|
+
return { rowsAffected };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async function openDatabase(worker, options) {
|
|
130
|
+
// NOTE: We want to import node:sqlite dynamically, to avoid bundlers unconditionally requiring node:sqlite in the
|
|
131
|
+
// end, since that would make us incompatible with older Node.JS versions.
|
|
132
|
+
const { DatabaseSync } = await dynamicImport('node:sqlite');
|
|
133
|
+
const baseDB = new DatabaseSync(options.path, { allowExtension: true });
|
|
134
|
+
baseDB.loadExtension(worker.extensionPath(), 'sqlite3_powersync_init');
|
|
135
|
+
return new BlockingNodeDatabase(baseDB, options.isWriter);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @returns The relevant PowerSync extension binary filename for the current platform and architecture
|
|
140
|
+
*/
|
|
141
|
+
function getPowerSyncExtensionFilename() {
|
|
142
|
+
const platform = OS.platform();
|
|
143
|
+
const arch = OS.arch();
|
|
144
|
+
let extensionFile;
|
|
145
|
+
if (platform == 'win32') {
|
|
146
|
+
if (arch == 'x64') {
|
|
147
|
+
extensionFile = 'powersync_x64.dll';
|
|
148
|
+
}
|
|
149
|
+
else if (arch == 'ia32') {
|
|
150
|
+
extensionFile = 'powersync_x86.dll';
|
|
151
|
+
}
|
|
152
|
+
else if (arch == 'arm64') {
|
|
153
|
+
extensionFile = 'powersync_aarch64.dll';
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
throw new Error('Windows platform only supports arm64, ia32 and x64 architecture.');
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else if (platform == 'linux') {
|
|
160
|
+
if (arch == 'x64') {
|
|
161
|
+
extensionFile = 'libpowersync_x64.so';
|
|
162
|
+
}
|
|
163
|
+
else if (arch == 'arm64') {
|
|
164
|
+
// TODO detect armv7 as an option
|
|
165
|
+
extensionFile = 'libpowersync_aarch64.so';
|
|
166
|
+
}
|
|
167
|
+
else if (arch == 'riscv64') {
|
|
168
|
+
extensionFile = 'libpowersync_riscv64gc.so';
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
throw new Error('Linux platform only supports x64, arm64 and riscv64 architectures.');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
else if (platform == 'darwin') {
|
|
175
|
+
if (arch == 'x64') {
|
|
176
|
+
extensionFile = 'libpowersync_x64.dylib';
|
|
177
|
+
}
|
|
178
|
+
else if (arch == 'arm64') {
|
|
179
|
+
extensionFile = 'libpowersync_aarch64.dylib';
|
|
114
180
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
181
|
+
else {
|
|
182
|
+
throw new Error('macOS platform only supports x64 and arm64 architectures.');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
throw new Error(`Unknown platform: ${platform}, PowerSync for Node.js currently supports Windows, Linux and macOS.`);
|
|
118
187
|
}
|
|
188
|
+
return extensionFile;
|
|
119
189
|
}
|
|
120
190
|
function startPowerSyncWorker(options) {
|
|
121
191
|
const resolvedOptions = {
|
|
122
192
|
extensionPath() {
|
|
123
|
-
const
|
|
124
|
-
const platform = OS.platform();
|
|
125
|
-
let extensionPath;
|
|
126
|
-
if (platform === 'win32') {
|
|
127
|
-
extensionPath = 'powersync.dll';
|
|
128
|
-
}
|
|
129
|
-
else if (platform === 'linux') {
|
|
130
|
-
extensionPath = 'libpowersync.so';
|
|
131
|
-
}
|
|
132
|
-
else if (platform === 'darwin') {
|
|
133
|
-
extensionPath = 'libpowersync.dylib';
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
throw 'Unknown platform, PowerSync for Node.js currently supports Windows, Linux and macOS.';
|
|
137
|
-
}
|
|
193
|
+
const extensionFilename = getPowerSyncExtensionFilename();
|
|
138
194
|
let resolved;
|
|
139
|
-
|
|
140
|
-
resolved = path__namespace.resolve(__dirname, '../lib/',
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
resolved = url.fileURLToPath(new URL(`../${extensionPath}`, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('DefaultWorker.cjs', document.baseURI).href))));
|
|
195
|
+
{
|
|
196
|
+
resolved = path__namespace.resolve(__dirname, '../lib/', extensionFilename);
|
|
144
197
|
}
|
|
145
198
|
return resolved;
|
|
146
199
|
},
|
|
200
|
+
async loadBetterSqlite3() {
|
|
201
|
+
const module = await dynamicImport('better-sqlite3');
|
|
202
|
+
return module.default;
|
|
203
|
+
},
|
|
147
204
|
...options
|
|
148
205
|
};
|
|
149
|
-
Comlink__namespace.expose(new
|
|
206
|
+
Comlink__namespace.expose(new DatabaseOpenHelper(resolvedOptions), node_worker_threads.parentPort);
|
|
207
|
+
}
|
|
208
|
+
class DatabaseOpenHelper {
|
|
209
|
+
options;
|
|
210
|
+
constructor(options) {
|
|
211
|
+
this.options = options;
|
|
212
|
+
}
|
|
213
|
+
async open(options) {
|
|
214
|
+
let database;
|
|
215
|
+
const implementation = options.implementation;
|
|
216
|
+
switch (implementation.type) {
|
|
217
|
+
case 'better-sqlite3':
|
|
218
|
+
database = await openDatabase$1(this.options, options);
|
|
219
|
+
break;
|
|
220
|
+
case 'node:sqlite':
|
|
221
|
+
database = await openDatabase(this.options, options);
|
|
222
|
+
break;
|
|
223
|
+
default:
|
|
224
|
+
throw new Error(`Unknown database implementation: ${options.implementation}.`);
|
|
225
|
+
}
|
|
226
|
+
return Comlink__namespace.proxy(database);
|
|
227
|
+
}
|
|
150
228
|
}
|
|
151
229
|
|
|
152
230
|
startPowerSyncWorker();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DefaultWorker.cjs","sources":["../lib/db/SqliteWorker.js","../lib/db/DefaultWorker.js"],"sourcesContent":["import * as path from 'node:path';\nimport BetterSQLite3Database from '@powersync/better-sqlite3';\nimport * as Comlink from 'comlink';\nimport { parentPort, threadId } from 'node:worker_threads';\nimport OS from 'node:os';\nimport url from 'node:url';\nclass BlockingAsyncDatabase {\n db;\n uncommittedUpdatedTables = new Set();\n committedUpdatedTables = new Set();\n constructor(db) {\n this.db = db;\n db.function('node_thread_id', () => threadId);\n }\n collectCommittedUpdates() {\n const resolved = Promise.resolve([...this.committedUpdatedTables]);\n this.committedUpdatedTables.clear();\n return resolved;\n }\n installUpdateHooks() {\n this.db.updateHook((_op, _dbName, tableName, _rowid) => {\n this.uncommittedUpdatedTables.add(tableName);\n });\n this.db.commitHook(() => {\n for (const tableName of this.uncommittedUpdatedTables) {\n this.committedUpdatedTables.add(tableName);\n }\n this.uncommittedUpdatedTables.clear();\n return true;\n });\n this.db.rollbackHook(() => {\n this.uncommittedUpdatedTables.clear();\n });\n }\n async close() {\n this.db.close();\n }\n async execute(query, params) {\n const stmt = this.db.prepare(query);\n if (stmt.reader) {\n const rows = stmt.all(params);\n return {\n rowsAffected: 0,\n rows: {\n _array: rows,\n length: rows.length\n }\n };\n }\n else {\n const info = stmt.run(params);\n return {\n rowsAffected: info.changes,\n insertId: Number(info.lastInsertRowid)\n };\n }\n }\n async executeRaw(query, params) {\n const stmt = this.db.prepare(query);\n if (stmt.reader) {\n return stmt.raw().all(params);\n }\n else {\n stmt.raw().run(params);\n return [];\n }\n }\n async executeBatch(query, params) {\n params = params ?? [];\n let rowsAffected = 0;\n const stmt = this.db.prepare(query);\n for (const paramSet of params) {\n const info = stmt.run(paramSet);\n rowsAffected += info.changes;\n }\n return { rowsAffected };\n }\n}\nclass BetterSqliteWorker {\n options;\n constructor(options) {\n this.options = options;\n }\n async open(path, isWriter) {\n const baseDB = new BetterSQLite3Database(path);\n baseDB.pragma('journal_mode = WAL');\n baseDB.loadExtension(this.options.extensionPath(), 'sqlite3_powersync_init');\n if (!isWriter) {\n baseDB.pragma('query_only = true');\n }\n const asyncDb = new BlockingAsyncDatabase(baseDB);\n asyncDb.installUpdateHooks();\n return Comlink.proxy(asyncDb);\n }\n}\nexport function startPowerSyncWorker(options) {\n const resolvedOptions = {\n extensionPath() {\n const isCommonJsModule = import.meta.isBundlingToCommonJs ?? false;\n const platform = OS.platform();\n let extensionPath;\n if (platform === 'win32') {\n extensionPath = 'powersync.dll';\n }\n else if (platform === 'linux') {\n extensionPath = 'libpowersync.so';\n }\n else if (platform === 'darwin') {\n extensionPath = 'libpowersync.dylib';\n }\n else {\n throw 'Unknown platform, PowerSync for Node.js currently supports Windows, Linux and macOS.';\n }\n let resolved;\n if (isCommonJsModule) {\n resolved = path.resolve(__dirname, '../lib/', extensionPath);\n }\n else {\n resolved = url.fileURLToPath(new URL(`../${extensionPath}`, import.meta.url));\n }\n return resolved;\n },\n ...options\n };\n Comlink.expose(new BetterSqliteWorker(resolvedOptions), parentPort);\n}\n","import { startPowerSyncWorker } from './SqliteWorker.js';\nstartPowerSyncWorker();\n"],"names":["threadId","Comlink","path","parentPort"],"mappings":";;;;;;;;;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db/BetterSqliteWorker.js","../lib/db/NodeSqliteWorker.js","../lib/db/SqliteWorker.js","../lib/db/DefaultWorker.js"],"sourcesContent":["// NOTE! Do not import this file directly! We have a rollup plugin that rewrites imports to modules.js to this file when\n// bundling to CommonJS.\nexport const isBundledToCommonJs = true;\nexport async function dynamicImport(path) {\n return require(path);\n}\n","import { threadId } from 'node:worker_threads';\nclass BlockingAsyncDatabase {\n db;\n constructor(db) {\n this.db = db;\n db.function('node_thread_id', () => threadId);\n }\n async close() {\n this.db.close();\n }\n async execute(query, params) {\n const stmt = this.db.prepare(query);\n if (stmt.reader) {\n const rows = stmt.all(params);\n return {\n rowsAffected: 0,\n rows: {\n _array: rows,\n length: rows.length\n }\n };\n }\n else {\n const info = stmt.run(params);\n return {\n rowsAffected: info.changes,\n insertId: Number(info.lastInsertRowid)\n };\n }\n }\n async executeRaw(query, params) {\n const stmt = this.db.prepare(query);\n if (stmt.reader) {\n return stmt.raw().all(params);\n }\n else {\n stmt.raw().run(params);\n return [];\n }\n }\n async executeBatch(query, params) {\n params = params ?? [];\n let rowsAffected = 0;\n const stmt = this.db.prepare(query);\n for (const paramSet of params) {\n const info = stmt.run(paramSet);\n rowsAffected += info.changes;\n }\n return { rowsAffected };\n }\n}\nexport async function openDatabase(worker, options) {\n const BetterSQLite3Database = await worker.loadBetterSqlite3();\n const baseDB = new BetterSQLite3Database(options.path);\n baseDB.loadExtension(worker.extensionPath(), 'sqlite3_powersync_init');\n const asyncDb = new BlockingAsyncDatabase(baseDB);\n return asyncDb;\n}\n","import { threadId } from 'node:worker_threads';\nimport { dynamicImport } from '../utils/modules.js';\nclass BlockingNodeDatabase {\n db;\n constructor(db, write) {\n this.db = db;\n db.function('node_thread_id', () => threadId);\n }\n async close() {\n this.db.close();\n }\n async execute(query, params) {\n const stmt = this.db.prepare(query);\n const rows = stmt.all(...params);\n return {\n rowsAffected: 0,\n rows: {\n _array: rows,\n length: rows.length\n }\n };\n }\n async executeRaw(query, params) {\n const stmt = this.db.prepare(query);\n stmt.setReturnArrays(true); // Missing in @types/node, https://nodejs.org/api/sqlite.html#statementsetreturnarraysenabled\n return stmt.all(...params);\n }\n async executeBatch(query, params) {\n params = params ?? [];\n let rowsAffected = 0;\n const stmt = this.db.prepare(query);\n for (const paramSet of params) {\n const info = stmt.run(...paramSet);\n rowsAffected += info.changes;\n }\n return { rowsAffected };\n }\n}\nexport async function openDatabase(worker, options) {\n // NOTE: We want to import node:sqlite dynamically, to avoid bundlers unconditionally requiring node:sqlite in the\n // end, since that would make us incompatible with older Node.JS versions.\n const { DatabaseSync } = await dynamicImport('node:sqlite');\n const baseDB = new DatabaseSync(options.path, { allowExtension: true });\n baseDB.loadExtension(worker.extensionPath(), 'sqlite3_powersync_init');\n return new BlockingNodeDatabase(baseDB, options.isWriter);\n}\n","import * as Comlink from 'comlink';\nimport OS from 'node:os';\nimport * as path from 'node:path';\nimport url from 'node:url';\nimport { parentPort } from 'node:worker_threads';\nimport { dynamicImport, isBundledToCommonJs } from '../utils/modules.js';\nimport { openDatabase as openBetterSqliteDatabase } from './BetterSqliteWorker.js';\nimport { openDatabase as openNodeDatabase } from './NodeSqliteWorker.js';\n/**\n * @returns The relevant PowerSync extension binary filename for the current platform and architecture\n */\nexport function getPowerSyncExtensionFilename() {\n const platform = OS.platform();\n const arch = OS.arch();\n let extensionFile;\n if (platform == 'win32') {\n if (arch == 'x64') {\n extensionFile = 'powersync_x64.dll';\n }\n else if (arch == 'ia32') {\n extensionFile = 'powersync_x86.dll';\n }\n else if (arch == 'arm64') {\n extensionFile = 'powersync_aarch64.dll';\n }\n else {\n throw new Error('Windows platform only supports arm64, ia32 and x64 architecture.');\n }\n }\n else if (platform == 'linux') {\n if (arch == 'x64') {\n extensionFile = 'libpowersync_x64.so';\n }\n else if (arch == 'arm64') {\n // TODO detect armv7 as an option\n extensionFile = 'libpowersync_aarch64.so';\n }\n else if (arch == 'riscv64') {\n extensionFile = 'libpowersync_riscv64gc.so';\n }\n else {\n throw new Error('Linux platform only supports x64, arm64 and riscv64 architectures.');\n }\n }\n else if (platform == 'darwin') {\n if (arch == 'x64') {\n extensionFile = 'libpowersync_x64.dylib';\n }\n else if (arch == 'arm64') {\n extensionFile = 'libpowersync_aarch64.dylib';\n }\n else {\n throw new Error('macOS platform only supports x64 and arm64 architectures.');\n }\n }\n else {\n throw new Error(`Unknown platform: ${platform}, PowerSync for Node.js currently supports Windows, Linux and macOS.`);\n }\n return extensionFile;\n}\nexport function startPowerSyncWorker(options) {\n const resolvedOptions = {\n extensionPath() {\n const isCommonJsModule = isBundledToCommonJs;\n const extensionFilename = getPowerSyncExtensionFilename();\n let resolved;\n if (isCommonJsModule) {\n resolved = path.resolve(__dirname, '../lib/', extensionFilename);\n }\n else {\n resolved = url.fileURLToPath(new URL(`../${extensionFilename}`, import.meta.url));\n }\n return resolved;\n },\n async loadBetterSqlite3() {\n const module = await dynamicImport('better-sqlite3');\n return module.default;\n },\n ...options\n };\n Comlink.expose(new DatabaseOpenHelper(resolvedOptions), parentPort);\n}\nclass DatabaseOpenHelper {\n options;\n constructor(options) {\n this.options = options;\n }\n async open(options) {\n let database;\n const implementation = options.implementation;\n switch (implementation.type) {\n case 'better-sqlite3':\n database = await openBetterSqliteDatabase(this.options, options);\n break;\n case 'node:sqlite':\n database = await openNodeDatabase(this.options, options);\n break;\n default:\n throw new Error(`Unknown database implementation: ${options.implementation}.`);\n }\n return Comlink.proxy(database);\n }\n}\n","import { startPowerSyncWorker } from './SqliteWorker.js';\nstartPowerSyncWorker();\n"],"names":["threadId","openDatabase","path","Comlink","parentPort","openBetterSqliteDatabase","openNodeDatabase"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AACA;AAEO,eAAe,aAAa,CAAC,IAAI,EAAE;AAC1C,IAAI,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC;AACzB;;ACJA,MAAM,qBAAqB,CAAC;AAC5B,IAAI,EAAE,CAAC;AACP,IAAI,WAAW,CAAC,EAAE,EAAE;AACp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;AAClC,SAAS;AACT,QAAQ,GAAG,OAAO;AAClB,KAAK,CAAC;AACN,IAAIC,kBAAO,CAAC,MAAM,CAAC,IAAI,kBAAkB,CAAC,eAAe,CAAC,EAAEC,8BAAU,CAAC,CAAC;AACxE,CAAC;AACD,MAAM,kBAAkB,CAAC;AACzB,IAAI,OAAO,CAAC;AACZ,IAAI,WAAW,CAAC,OAAO,EAAE;AACzB,QAAQ,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;AAC/B,KAAK;AACL,IAAI,MAAM,IAAI,CAAC,OAAO,EAAE;AACxB,QAAQ,IAAI,QAAQ,CAAC;AACrB,QAAQ,MAAM,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC;AACtD,QAAQ,QAAQ,cAAc,CAAC,IAAI;AACnC,YAAY,KAAK,gBAAgB;AACjC,gBAAgB,QAAQ,GAAG,MAAMC,cAAwB,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACjF,gBAAgB,MAAM;AACtB,YAAY,KAAK,aAAa;AAC9B,gBAAgB,QAAQ,GAAG,MAAMC,YAAgB,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACzE,gBAAgB,MAAM;AACtB,YAAY;AACZ,gBAAgB,MAAM,IAAI,KAAK,CAAC,CAAC,iCAAiC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/F,SAAS;AACT,QAAQ,OAAOH,kBAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;AACvC,KAAK;AACL;;ACrGA,oBAAoB,EAAE;;"}
|
package/dist/bundle.cjs
CHANGED
|
@@ -11,7 +11,6 @@ var node_worker_threads = require('node:worker_threads');
|
|
|
11
11
|
var Comlink = require('comlink');
|
|
12
12
|
var node_async_hooks = require('node:async_hooks');
|
|
13
13
|
|
|
14
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
15
14
|
function _interopNamespaceDefault(e) {
|
|
16
15
|
var n = Object.create(null);
|
|
17
16
|
if (e) {
|
|
@@ -234,10 +233,13 @@ class RemoteConnection {
|
|
|
234
233
|
}
|
|
235
234
|
|
|
236
235
|
const READ_CONNECTIONS = 5;
|
|
236
|
+
const defaultDatabaseImplementation = {
|
|
237
|
+
type: 'better-sqlite3'
|
|
238
|
+
};
|
|
237
239
|
/**
|
|
238
240
|
* Adapter for better-sqlite3
|
|
239
241
|
*/
|
|
240
|
-
class
|
|
242
|
+
class WorkerConnectionPool extends common.BaseObserver {
|
|
241
243
|
options;
|
|
242
244
|
name;
|
|
243
245
|
readConnections;
|
|
@@ -270,16 +272,12 @@ class BetterSQLite3DBAdapter extends common.BaseObserver {
|
|
|
270
272
|
dbFilePath = path__namespace.join(this.options.dbLocation, dbFilePath);
|
|
271
273
|
}
|
|
272
274
|
const openWorker = async (isWriter) => {
|
|
273
|
-
const isCommonJsModule = true ?? false;
|
|
274
275
|
let worker;
|
|
275
276
|
const workerName = isWriter ? `write ${dbFilePath}` : `read ${dbFilePath}`;
|
|
276
277
|
const workerFactory = this.options.openWorker ?? ((...args) => new node_worker_threads.Worker(...args));
|
|
277
|
-
|
|
278
|
+
{
|
|
278
279
|
worker = workerFactory(path__namespace.resolve(__dirname, 'DefaultWorker.cjs'), { name: workerName });
|
|
279
280
|
}
|
|
280
|
-
else {
|
|
281
|
-
worker = workerFactory(new URL('./DefaultWorker.js', (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('bundle.cjs', document.baseURI).href))), { name: workerName });
|
|
282
|
-
}
|
|
283
281
|
const listeners = new WeakMap();
|
|
284
282
|
const comlink = Comlink__namespace.wrap({
|
|
285
283
|
postMessage: worker.postMessage.bind(worker),
|
|
@@ -306,8 +304,23 @@ class BetterSQLite3DBAdapter extends common.BaseObserver {
|
|
|
306
304
|
worker.once('error', (e) => {
|
|
307
305
|
console.error('Unexpected PowerSync database worker error', e);
|
|
308
306
|
});
|
|
309
|
-
const database = (await comlink.open(
|
|
310
|
-
|
|
307
|
+
const database = (await comlink.open({
|
|
308
|
+
path: dbFilePath,
|
|
309
|
+
isWriter,
|
|
310
|
+
implementation: this.options.implementation ?? defaultDatabaseImplementation
|
|
311
|
+
}));
|
|
312
|
+
if (isWriter) {
|
|
313
|
+
await database.execute("SELECT powersync_update_hooks('install');", []);
|
|
314
|
+
}
|
|
315
|
+
const connection = new RemoteConnection(worker, comlink, database);
|
|
316
|
+
if (this.options.initializeConnection) {
|
|
317
|
+
await this.options.initializeConnection(connection, isWriter);
|
|
318
|
+
}
|
|
319
|
+
await connection.execute('pragma journal_mode = WAL');
|
|
320
|
+
if (!isWriter) {
|
|
321
|
+
await connection.execute('pragma query_only = true');
|
|
322
|
+
}
|
|
323
|
+
return connection;
|
|
311
324
|
};
|
|
312
325
|
// Open the writer first to avoid multiple threads enabling WAL concurrently (causing "database is locked" errors).
|
|
313
326
|
this.writeConnection = await openWorker(true);
|
|
@@ -372,7 +385,8 @@ class BetterSQLite3DBAdapter extends common.BaseObserver {
|
|
|
372
385
|
return await fn(this.writeConnection);
|
|
373
386
|
}
|
|
374
387
|
finally {
|
|
375
|
-
const
|
|
388
|
+
const serializedUpdates = await this.writeConnection.database.executeRaw("SELECT powersync_update_hooks('get');", []);
|
|
389
|
+
const updates = JSON.parse(serializedUpdates[0][0]);
|
|
376
390
|
if (updates.length > 0) {
|
|
377
391
|
const event = {
|
|
378
392
|
tables: updates,
|
|
@@ -493,7 +507,7 @@ class PowerSyncDatabase extends common.AbstractPowerSyncDatabase {
|
|
|
493
507
|
* Opens a DBAdapter using better-sqlite3 as the default SQLite open factory.
|
|
494
508
|
*/
|
|
495
509
|
openDBAdapter(options) {
|
|
496
|
-
return new
|
|
510
|
+
return new WorkerConnectionPool(options.database);
|
|
497
511
|
}
|
|
498
512
|
generateBucketStorageAdapter() {
|
|
499
513
|
return new common.SqliteBucketStorage(this.database, this.logger);
|