Skip to content

Commit 0e971c3

Browse files
jaredwrayclaude
andauthored
sqlite - feat: Add warning for WAL mode with in-memory SQLite databases (#1832)
* sqlite - feat: Add warning logging for WAL mode on in-memory databases When WAL mode is requested for an in-memory database (:memory: or empty string), the code now logs a warning message to inform developers that the option will be ignored, rather than silently passing the PRAGMA to SQLite which would silently ignore it. https://claude.ai/code/session_01ESQBgre2YMC66TdTca7YdG * Update test.ts --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent d195061 commit 0e971c3

3 files changed

Lines changed: 28 additions & 4 deletions

File tree

packages/sqlite/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const keyv = new Keyv({ store: keyvSqlite });
4848
- `table` - The table name to use for storage (default: `'keyv'`)
4949
- `busyTimeout` - Sets a busy handler that sleeps for a specified amount of time when a table is locked
5050
- `wal` - Enable [Write-Ahead Logging](https://sqlite.org/wal.html) mode for better concurrency and performance (default: `false`)
51-
- **Note:** WAL mode is not supported for in-memory databases (`:memory:`). SQLite will silently ignore the WAL mode request and remain in "memory" journal mode.
51+
- **Note:** WAL mode is not supported for in-memory databases (`:memory:`). A warning will be logged and the option will be ignored.
5252
- `keySize` - The maximum key size in bytes (default: `255`, max: `65535`)
5353

5454
You can also use a helper function to create `Keyv` with `KeyvSqlite` store.

packages/sqlite/src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,14 @@ export class KeyvSqlite extends EventEmitter implements KeyvStoreAdapter {
5858
const close = promisify(database.close).bind(database);
5959

6060
if (options.wal) {
61-
// WAL mode doesn't work with in-memory databases, but SQLite silently
62-
// ignores the request and keeps "memory" mode, so we do the same
63-
await query("PRAGMA journal_mode=WAL");
61+
const isInMemory = options.db === ":memory:" || options.db === "";
62+
if (isInMemory) {
63+
console.warn(
64+
"@keyv/sqlite: WAL mode is not supported for in-memory databases. The wal option will be ignored.",
65+
);
66+
} else {
67+
await query("PRAGMA journal_mode=WAL");
68+
}
6469
}
6570

6671
return {

packages/sqlite/test/test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,22 @@ test.it(
313313
await keyv.disconnect();
314314
},
315315
);
316+
317+
test.it("WAL mode with in-memory database logs a warning", async (t) => {
318+
const warnSpy = test.vi.spyOn(console, "warn").mockImplementation(() => {});
319+
320+
const keyv = new KeyvSqlite({
321+
uri: "sqlite://:memory:",
322+
wal: true,
323+
});
324+
325+
// Wait for the database to initialize (the warn happens during initialization)
326+
await keyv.query("SELECT 1");
327+
328+
t.expect(warnSpy).toHaveBeenCalledWith(
329+
"@keyv/sqlite: WAL mode is not supported for in-memory databases. The wal option will be ignored.",
330+
);
331+
332+
warnSpy.mockRestore();
333+
await keyv.disconnect();
334+
});

0 commit comments

Comments
 (0)