Node.js client (alpha)
SDK reference for using PowerSync in Node.js clients.
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 Project
A small CLI app showcasing bidirectional sync.
SDK Features
- Provides real-time streaming of database changes.
- Offers direct access to the SQLite database, enabling the use of SQL on both client and server sides.
- Operations run on a background worker and are asynchronous by default, enabling concurrent queries.
- Enables subscription to queries for receiving live updates.
- Eliminates the need for client-side database migrations as these are managed automatically.
Quickstart
To start using PowerSync in a Node client, first add the dependencies:
Depending on the package manager used, you might have to approve install scripts.
PowerSync currently requires install scripts on the @powersync/node
and @powersync/better-sqlite3
packages
to download native addons.
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 Postgres)
Accordingly, the connector must implement two methods:
- 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.
Example:
With your database instantiated and your connector ready, call 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 web SDK:
PowerSync runs queries asynchronously on a background pool of workers and automatically configures WAL to allow a writer and multiple readers to operate in parallel.
Was this page helpful?