Skip to content

Commit 81045a5

Browse files
authored
Merge pull request #562 from powersync-ja/fix-packaged-node-electron-demo
Fix node electron example when packaged
2 parents 75c50be + 2c83d01 commit 81045a5

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

demos/example-electron-node/config.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const mainConfig: Configuration = {
8585
new CopyPlugin({
8686
patterns: [{
8787
from: path.resolve(require.resolve('@powersync/node/package.json'), `../lib/${extensionPath}`),
88-
to: extensionPath,
88+
to: path.join('powersync', extensionPath),
8989
}],
9090
}),
9191
new DefinePluginImpl({
@@ -95,7 +95,8 @@ const mainConfig: Configuration = {
9595
],
9696
resolve: {
9797
extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json']
98-
}
98+
},
99+
target: "electron-main",
99100
};
100101

101102
const rendererConfig: Configuration = {
@@ -116,7 +117,9 @@ const rendererConfig: Configuration = {
116117

117118
const config: ForgeConfig = {
118119
packagerConfig: {
119-
asar: true
120+
asar: {
121+
unpack: '**/{.**,**}/**/powersync/*'
122+
},
120123
},
121124
rebuildConfig: {
122125
force: true,

demos/example-electron-node/src/main/index.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs';
12
import { Worker } from 'node:worker_threads';
23

34
import { PowerSyncDatabase, SyncStreamConnectionMethod } from '@powersync/node';
@@ -19,10 +20,22 @@ if (require('electron-squirrel-startup')) {
1920
app.quit();
2021
}
2122

23+
const userDataDirectory = app.getPath('userData');
24+
try {
25+
if (!fs.existsSync(userDataDirectory)) {
26+
fs.mkdirSync(userDataDirectory);
27+
}
28+
} catch (e) {
29+
console.error('Could not create database directory', e);
30+
}
31+
32+
console.log('Storing data in ', userDataDirectory);
33+
2234
const database = new PowerSyncDatabase({
2335
schema: AppSchema,
2436
database: {
2537
dbFilename: 'test.db',
38+
dbLocation: userDataDirectory,
2639
openWorker(_, options) {
2740
return new Worker(new URL('./worker.ts', import.meta.url), options);
2841
}

demos/example-electron-node/src/main/worker.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ function resolvePowerSyncCoreExtension() {
1818

1919
// This example uses copy-webpack-plugin to copy the prebuilt library over. This ensures that it is
2020
// available in packaged release builds.
21-
return path.resolve(__dirname, extensionPath);
21+
let libraryPath = path.resolve(__dirname, 'powersync', extensionPath);
22+
23+
if (__dirname.indexOf('app.asar') != -1) {
24+
// Our build configuration ensures the extension is always available outside of the archive too.
25+
libraryPath = libraryPath.replace('app.asar', 'app.asar.unpacked');
26+
}
27+
28+
return libraryPath;
2229
}
2330

2431
startPowerSyncWorker({ extensionPath: resolvePowerSyncCoreExtension });

0 commit comments

Comments
 (0)