Skip to content

Commit f30a605

Browse files
committed
Docs
1 parent 8101eeb commit f30a605

File tree

4 files changed

+105
-9
lines changed

4 files changed

+105
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/node': minor
3+
---
4+
5+
Experimental support for integrating with node:sqlite.

.changeset/khaki-years-design.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/node': minor
3+
---
4+
5+
Support custom better-sqlite3 forks (see an example for encryption in the README).

.changeset/wet-cooks-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/node': minor
3+
---
4+
5+
Use upstream better-sqlite3 dependency instead of the PowerSync fork.

packages/node/README.md

Lines changed: 90 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ See a summary of features [here](https://docs.powersync.com/client-sdk-reference
1515

1616
The `@powersync/node` package is currently in a Beta release.
1717

18-
# Installation
18+
## Installation
1919

20-
## Install Package
20+
### Install Package
2121

2222
```bash
2323
npm install @powersync/node better-sqlite3
@@ -47,38 +47,119 @@ To resolve this, either:
4747
- Upgrade `node-gyp` to version 10 or later.
4848
- Install Python [setuptools](https://pypi.org/project/setuptools/), which includes `distutils`.
4949

50-
# Getting Started
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 us custom `better-sqlite3` forks, 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
5189

5290
The [Node.js SDK reference](https://docs.powersync.com/client-sdk-references/node)
5391
contains everything you need to know to get started implementing PowerSync in your project.
5492

55-
# Examples
93+
## Examples
5694

5795
A simple example using `@powersync/node` is available in the [`demos/example-node/`](../demos/example-node) directory.
5896

59-
# Proxy Support
97+
## Proxy Support
6098

6199
This SDK supports HTTP, HTTPS, and WebSocket proxies via environment variables.
62100

63-
## HTTP Connection Method
101+
### HTTP Connection Method
64102

65103
Internally we probe the http environment variables and apply it to fetch requests ([undici](https://www.npmjs.com/package/undici/v/5.6.0))
66104

67105
- Set the `HTTPS_PROXY` or `HTTP_PROXY` environment variable to automatically route HTTP requests through a proxy.
68106

69-
## WEB Socket Connection Method
107+
### WEB Socket Connection Method
70108

71109
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:
72110

73111
- Set the `WS_PROXY` or `WSS_PROXY` environment variable to route the webocket connections through a proxy.
74112

75-
# Found a bug or need help?
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?
76157

77158
- Join our [Discord server](https://discord.gg/powersync) where you can browse topics from our community, ask questions, share feedback, or just say hello :)
78159
- Please open a [GitHub issue](https://github.com/powersync-ja/powersync-js/issues) when you come across a bug.
79160
- 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.
80161

81-
# Thanks
162+
## Thanks
82163

83164
The PowerSync Node.js SDK relies on the work contributors and maintainers have put into the upstream better-sqlite3 package.
84165
In particular, we'd like to thank [@spinda](https://github.com/spinda) for contributing support for update, commit and rollback hooks!

0 commit comments

Comments
 (0)