This page describes the PowerSync client SDK for Node.js.
If you’re interested in using PowerSync for your Node.js backend, no special package is required.
Instead, follow our guides on app backend setup.
PowerSync SDK on NPM
This SDK is distributed via NPM [External link].
Source Code
Refer to packages/node in the powersync-js repo on GitHub.
API Reference
Full API reference for the PowerSync SDK [External link].
Example Projects
Gallery of example projects/demo apps built with Node.js and PowerSync.
This SDK is currently in a beta release and can be considered production-ready for tested use cases.
SDK Features
- Real-time streaming of database changes: Changes made by one user are instantly streamed to all other users with access to that data. This keeps clients automatically in sync without manual polling or refresh logic.
- Direct access to a local SQLite database: Data is stored locally, so apps can read and write instantly without network calls. This enables offline support and faster user interactions.
- Asynchronous background execution: The SDK performs database operations in the background to avoid blocking the application’s main thread. This means that apps stay responsive, even during heavy data activity.
- Query subscriptions for live updates: The SDK supports query subscriptions that automatically push real-time updates to client applications as data changes, keeping your UI reactive and up to date.
- Automatic schema management: PowerSync syncs schemaless data and applies a client-defined schema using SQLite views. This architecture means that PowerSync SDKs can handle schema changes gracefully without requiring explicit migrations on the client-side.
Quickstart
Add the PowerSync Node NPM package to your project:- npm
- yarn
- pnpm
better-sqlite3 package for most users:
- npm
- yarn
- pnpm
Previous versions of the PowerSync SDK for Node.js used the
@powersync/better-sqlite3 fork as a
required peer dependency.
This is no longer recommended. After upgrading to @powersync/node version 0.12.0 or later, ensure
the old package is no longer installed by running @powersync/better-sqlite3.better-sqlite package requires native compilation, which depends on certain system tools.
Prebuilt assets are available and used by default, but a custom compilation may be started depending on the Node.js
or Electron version used.
This compilation process is handled by node-gyp and may fail if required dependencies are missing or misconfigured.
Refer to the PowerSync Node package README for more details.
Next, make sure that you have:
- Signed up for a PowerSync Cloud account (here) or self-host PowerSync.
- Configured your backend database and connected it to your PowerSync instance.
1. Define the schema
The first step is defining the schema for the local SQLite database. This schema represents a “view” of the downloaded data. No migrations are required — the schema is applied directly when the local PowerSync database is constructed (as we’ll show in the next step). You can use this example as a reference when defining your schema.2. Instantiate the PowerSync Database
Next, you need to instantiate the PowerSync database — this is the core managed database. Its primary functions are to record all changes in the local database, whether online or offline. In addition, it automatically uploads changes to your app backend when connected. Example:3. Integrate with your Backend
The PowerSync backend connector provides the connection between your application backend and the PowerSync client-slide managed SQLite database. It is used to:- Retrieve an auth token to connect to the PowerSync instance.
- Apply local changes on your backend application server (and from there, to your backend database)
- PowerSyncBackendConnector.fetchCredentials - This is called every couple of minutes and is used to obtain credentials for your app backend API. -> See Authentication Setup for instructions on how the credentials should be generated.
- PowerSyncBackendConnector.uploadData - Use this to upload client-side changes to your app backend. -> See Writing Client Changes for considerations on the app backend implementation.
connect to start the synchronization process:
Usage
After connecting the client database, it is ready to be used. The API to run queries and updates is identical to our JavaScript/Web SDK:Watch Queries
Thedb.watch() method executes a read query whenever a change to a dependent table is made.
- AsyncIterator approach
- Callback approach
Configure Logging
Enable verbose output in the developer tools for detailed logs.
Supported Platforms
See Supported Platforms -> Node.js SDK.Encryption and custom SQLite drivers
The SDK has an optional dependency onbetter-sqlite3 which is used as the default SQLite
driver for that package.
Because that dependency is optional, it can be replaced or removed to customize how SQLite
gets loaded. This section lists common options.
Encryption
To encrypt databases managed by the PowerSync SDK for Node.js, replace thebetter-sqlite3
dependency with the better-sqlite3-multiple-ciphers fork.
That package has the same API as better-sqlite3 while bundling SQLite3MultipleCiphers
instead of upstream SQLite.
The node example in the PowerSync
repository can use both
better-sqlite3 and better-sqlite3-multiple-ciphers and may be a useful example here.better-sqlite3 at runtime, using a different package
requires patching the database worker. To do that, create a file (say database.worker.js) with the following
contents:
initializeConnection option to install an ecryption key:
If you’re using a custom compilation toolchain, for instance because you’re compiling from TypeScript
or are applying a bundler to your project, loading workers may require additional configuration on that
toolchain.
node:sqlite
Recent versions of Node.js contain an experimental SQLite API.
Using the builtin SQLite API can reduce code size and external native dependencies. To enable it,
remove your dependency on better-sqlite3 and configure PowerSync to use the builtin APIs:
There are stability issues when using PowerSync with this API, and it’s not recommended outside of
testing purposes at the moment.