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
Source Code
Refer to
packages/node in the powersync-js repo on GitHubAPI Reference
Full API reference for the SDK
Example Projects
Gallery of example projects/demo apps built with Node.js and PowerSync
Changelog
Changelog for the SDK
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
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.
Prerequisites: To sync data between your client-side app and your backend source database, you must have completed the necessary setup for PowerSync, which includes connecting your source database to the PowerSync Service and deploying Sync Rules (steps 1-4 in the Setup Guide).
1. Define the Client-Side Schema
This refers to the for the managed SQLite database exposed by the PowerSync Client SDKs, that your app can read from and write to. The schema is applied when the database is instantiated (as we’ll show in the next step) — . You can use this example as a reference when defining your schema. The types available aretext, integer and real. These should map directly to the values produced by the Sync Rules. If a value doesn’t match, it is cast automatically. For details on how backend source database types are mapped to the SQLite types, see Types.
Select JavaScript and replace the suggested import with @powersync/node.
2. Instantiate the PowerSync Database
Next, you need to instantiate the PowerSync database. PowerSync streams changes from your backend source database into the client-side SQLite database, based on your Sync Rules. In your client-side app, you can read from and write to the local SQLite database, whether the user is online or offline. 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.
- Upload client-side writes to your backend API. Any writes that are made to the SQLite database are placed into an upload queue by the PowerSync Client SDK and automatically uploaded to your app backend (where you apply those changes to the backend source database) when the user is connected.
- PowerSyncBackendConnector.fetchCredentials - This method will be automatically invoked by the PowerSync Client SDK every couple of minutes to obtain authentication credentials. See Authentication Setup for instructions on how the credentials should be generated.
- PowerSyncBackendConnector.uploadData - This method will be automatically invoked by the PowerSync Client SDK whenever it needs to upload client-side writes to your app backend via your backend API. Therefore, in your implememtation, you need to define how your backend API is called. See Writing Client Changes for considerations on the app backend implementation.
connect() to start syncing data with your backend:
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
Additional Usage Examples
For more usage examples including accessing connection status, monitoring sync progress, and waiting for initial sync, see the Usage Examples page.ORM Support
See JavaScript ORM Support for details.Troubleshooting
See Troubleshooting for pointers to debug common issues.Supported Platforms
See Supported Platforms -> Node.js SDK.Upgrading the SDK
Run the below command in your project folder:- npm
- yarn
- pnpm
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.
Because PowerSync attempts to dynamically load 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.