Skip to content

Commit 2709a2e

Browse files
committed
Node: Create dbLocation if it doesn't exist.
1 parent 098934a commit 2709a2e

File tree

6 files changed

+44
-13
lines changed

6 files changed

+44
-13
lines changed

.changeset/lazy-moons-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/node': patch
3+
---
4+
5+
Fix compilation errors on Windows.

.changeset/nice-steaks-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/node': patch
3+
---
4+
5+
Create `dbLocation` directory if it doesn't exist.

packages/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"@powersync/common": "workspace:^1.22.0"
4747
},
4848
"dependencies": {
49-
"@powersync/better-sqlite3": "^0.1.0",
49+
"@powersync/better-sqlite3": "^0.1.1",
5050
"@powersync/common": "workspace:*",
5151
"async-lock": "^1.4.0",
5252
"bson": "^6.6.0",

packages/node/src/db/BetterSQLite3DBAdapter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from 'node:fs/promises';
12
import * as path from 'node:path';
23
import { Worker } from 'node:worker_threads';
34
import * as Comlink from 'comlink';
@@ -53,6 +54,10 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
5354
async initialize() {
5455
let dbFilePath = this.options.dbFilename;
5556
if (this.options.dbLocation !== undefined) {
57+
// SQLite reports a misuse error when the target directory of the database doesn't exist,
58+
// so create it now before we access the database.
59+
await fs.mkdir(this.options.dbLocation, { recursive: true });
60+
5661
dbFilePath = path.join(this.options.dbLocation, dbFilePath);
5762
}
5863

packages/node/tests/PowerSyncDatabase.test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import * as path from 'node:path';
12
import { Worker } from 'node:worker_threads';
2-
import fs from 'node:fs/promises';
33

4-
import { vi, expect, test, onTestFinished } from 'vitest';
5-
import { AppSchema, createTempDir, databaseTest } from './utils';
4+
import { vi, expect, test } from 'vitest';
5+
import { AppSchema, databaseTest, tempDirectoryTest } from './utils';
66
import { PowerSyncDatabase } from '../lib';
77
import { WorkerOpener } from '../lib/db/options';
88

@@ -19,8 +19,7 @@ test('validates options', async () => {
1919
}).rejects.toThrowError('Needs at least one worker for reads');
2020
});
2121

22-
test('can customize loading workers', async () => {
23-
const directory = await createTempDir();
22+
tempDirectoryTest('can customize loading workers', async ({tmpdir}) => {
2423
const defaultWorker: WorkerOpener = (...args) => new Worker(...args);
2524

2625
const openFunction = vi.fn(defaultWorker); // Wrap in vi.fn to count invocations
@@ -29,7 +28,7 @@ test('can customize loading workers', async () => {
2928
schema: AppSchema,
3029
database: {
3130
dbFilename: 'test.db',
32-
dbLocation: directory,
31+
dbLocation: tmpdir,
3332
openWorker: openFunction,
3433
readWorkerCount: 2
3534
}
@@ -38,8 +37,6 @@ test('can customize loading workers', async () => {
3837
await database.get('SELECT 1;'); // Make sure the database is ready and works
3938
expect(openFunction).toHaveBeenCalledTimes(3); // One writer, two readers
4039
await database.close();
41-
42-
onTestFinished(async () => fs.rm(directory, { recursive: true }));
4340
});
4441

4542
databaseTest('links powersync', async ({ database }) => {
@@ -95,6 +92,19 @@ databaseTest('can watch tables', async ({ database }) => {
9592
await expect.poll(() => fn).toHaveBeenCalledTimes(2);
9693
});
9794

95+
tempDirectoryTest('automatically creates directory', async ({tmpdir}) => {
96+
const database = new PowerSyncDatabase({
97+
schema: AppSchema,
98+
database: {
99+
dbFilename: 'test.db',
100+
dbLocation: path.join(tmpdir, 'some', 'nested', 'location', 'that', 'does', 'not', 'exist'),
101+
readWorkerCount: 2
102+
}
103+
});
104+
105+
await database.get('SELECT 1;'); // Make sure the database is ready and works
106+
});
107+
98108
databaseTest.skip('can watch queries', async ({ database }) => {
99109
const query = await database.watch('SELECT * FROM todos;', [])[Symbol.asyncIterator]();
100110
expect((await query.next()).value.rows).toHaveLength(0);

packages/node/tests/utils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ export const AppSchema = new Schema({
2323

2424
export type Database = (typeof AppSchema)['types'];
2525

26-
export const databaseTest = test.extend<{ database: PowerSyncDatabase }>({
27-
database: async ({}, use) => {
26+
export const tempDirectoryTest = test.extend<{ tmpdir: string }>({
27+
tmpdir: async ({}, use) => {
2828
const directory = await createTempDir();
29+
await use(directory);
30+
await fs.rm(directory, { recursive: true });
31+
},
32+
});
33+
34+
export const databaseTest = tempDirectoryTest.extend<{ database: PowerSyncDatabase }>({
35+
database: async ({tmpdir}, use) => {
2936
const database = new PowerSyncDatabase({
3037
schema: AppSchema,
3138
database: {
3239
dbFilename: 'test.db',
33-
dbLocation: directory
40+
dbLocation: tmpdir
3441
}
3542
});
3643
await use(database);
37-
await fs.rm(directory, { recursive: true });
3844
}
3945
});

0 commit comments

Comments
 (0)