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
, aPowerSyncDatabase
is created. PowerSync uses node workers to speed up database queries. This worker is part of the@powersync/node
package and wouldn't be copied into the resulting Electron app by default. For this reason, this example has its ownsrc/main/worker.ts
loaded 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/node
package and calledpowersync.dll
,libpowersync.dylib
orlibpowersync.so
depending on the operating system. We use thecopy-webpack-plugin
package 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:
- Make sure to run
pnpm install
andpnpm build:packages
in the root directory of this repo. - Copy
.env.local.template
to.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. cd
into this directory. In this mono-repo, you'll have to run./node_modules/.bin/electron-rebuild
once to make sure@powersync/better-sqlite3
was compiled with Electron's toolchain.- 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.