Skip to content

Commit 3e4bd8b

Browse files
committed
Document better but still throw
1 parent 8b9d7a2 commit 3e4bd8b

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

packages/common/src/client/SQLOpenFactory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export interface SQLOpenOptions {
77
dbFilename: string;
88
/**
99
* Directory where the database file is located.
10+
*
11+
* When set, the directory must exist when the database is opened, it will
12+
* not be created automatically.
1013
*/
1114
dbLocation?: string;
1215

packages/node/src/db/BetterSQLite3DBAdapter.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
5454
async initialize() {
5555
let dbFilePath = this.options.dbFilename;
5656
if (this.options.dbLocation !== undefined) {
57+
// Make sure the dbLocation exists, we get a TypeError from better-sqlite3 otherwise.
58+
let directoryExists = false;
59+
try {
60+
const stat = await fs.stat(this.options.dbLocation);
61+
directoryExists = stat.isDirectory();
62+
} catch (_) {
63+
// If we can't even stat, the directory won't be accessible to SQLite either.
64+
}
65+
66+
if (!directoryExists) {
67+
throw new Error(`The dbLocation directory at "${this.options.dbLocation}" does not exist. Please create it before opening the PowerSync database!`);
68+
}
69+
5770
// SQLite reports a misuse error when the target directory of the database doesn't exist,
5871
// so create it now before we access the database.
5972
await fs.mkdir(this.options.dbLocation, { recursive: true });

packages/node/tests/PowerSyncDatabase.test.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,20 @@ databaseTest('can watch tables', async ({ database }) => {
9393
await expect.poll(() => fn).toHaveBeenCalledTimes(2);
9494
});
9595

96-
tempDirectoryTest('automatically creates directory', async ({ tmpdir }) => {
96+
tempDirectoryTest('throws error if target directory does not exist', async ({ tmpdir }) => {
9797
const directory = path.join(tmpdir, 'some', 'nested', 'location', 'that', 'does', 'not', 'exist');
9898

99-
const database = new PowerSyncDatabase({
100-
schema: AppSchema,
101-
database: {
102-
dbFilename: 'test.db',
103-
dbLocation: directory,
104-
readWorkerCount: 2
105-
}
106-
});
107-
108-
// Make sure there's a write to the file
109-
await database.writeLock((conn) => conn.execute('pragma user_version = 2'));
110-
111-
// Make sure the file is actually created in the right location
112-
await fs.access(
113-
path.join(directory, 'test.db'),
114-
fs.constants.F_OK | fs.constants.R_OK | fs.constants.W_OK
115-
);
99+
expect(async () => {
100+
const database = new PowerSyncDatabase({
101+
schema: AppSchema,
102+
database: {
103+
dbFilename: 'test.db',
104+
dbLocation: directory,
105+
readWorkerCount: 2
106+
}
107+
});
108+
await database.waitForReady();
109+
}).rejects.toThrowError(/The dbLocation directory at ".+" does not exist/);
116110
});
117111

118112
databaseTest.skip('can watch queries', async ({ database }) => {

0 commit comments

Comments
 (0)