This example shows how the PowerSync Node.js client SDK can be used in the main process of an Electron app.
The purpose of this example is to highlight specific build configurations that enable this setup. In particular:
- In
src/main/index.ts, aPowerSyncDatabaseis created. PowerSync uses node workers to speed up database queries. This worker is part of the@powersync/nodepackage and wouldn't be copied into the resulting Electron app by default. For this reason, this example has its ownsrc/main/worker.tsloaded withnew URL('./worker.ts', import.meta.url). - In addition to the worker, PowerSync requires access to a SQLite extension providing sync functionality.
This file is also part of the
@powersync/nodepackage and is the prebuilt release asset (for examplepowersync_x64.dll,libpowersync_x64.dyliborlibpowersync_x64.so) depending on the operating system and architecture. We use thecopy-webpack-pluginpackage to make sure a copy of that file is available to the main process, and load it in the customsrc/main/worker.ts. - The
get()andgetAll()methods are exposed to the renderer process with an IPC channel.
To see it in action:
cdinto this directory.- Run
pnpm install. - Copy
.env.local.templateto.env.local, and complete the environment variables. You can generate a temporary development token, or leave blank to test with local-only data. The example works with the schema from the PowerSync + Supabase tutorial. - In this mono-repo, you'll have to run
./node_modules/.bin/electron-rebuildonce to make surebetter-sqlite3was compiled with Electron's toolchain. Sometimes that doesn't work, you can also go tonode_modules/better-sqlite3and manually runHOME=~/.electron-gyp node-gyp rebuild --target=37.0.0 --arch=arm64 --dist-url=https://electronjs.org/headers(or--arch=x64depending on your machine). - Finally, run
pnpm start.
Apart from the build setup, this example is purposefully kept simple.
To make sure PowerSync is working, you can run await powersync.get('SELECT powersync_rs_version()'); in the DevTools
console. A result from that query implies that the PowerSync was properly configured.
For more details, see the documentation for the PowerSync node package and check other examples:
- example-node: A Node.js CLI example that connects to PowerSync to run auto-updating queries.
- example-electron: An Electron example that runs PowerSync in the render process instead of in the main one.