sqlite - feat: Add WAL (Write-Ahead Logging) mode support#1826
sqlite - feat: Add WAL (Write-Ahead Logging) mode support#1826jaredwray merged 4 commits intojaredwray:mainfrom
Conversation
Add optional WAL mode support to @keyv/sqlite for better concurrency and performance. Changes: - Add `wal` option to KeyvSqliteOptions interface - Execute PRAGMA journal_mode=WAL when wal: true is specified - Add comprehensive tests for WAL mode functionality - Update README with WAL option documentation WAL mode provides: - Better concurrency (readers don't block writers) - Improved performance for most write operations - Better crash resistance Note: WAL mode doesn't work with in-memory databases (:memory:). SQLite silently ignores the setting and remains in "memory" journal mode, which is the expected behavior. All tests pass with 100% code coverage. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Summary of ChangesHello @snomiao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request integrates Write-Ahead Logging (WAL) mode into the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces support for Write-Ahead Logging (WAL) mode in the @keyv/sqlite package, which can improve concurrency and performance. The changes include adding a wal option, updating the connection logic to enable WAL mode via a PRAGMA statement, and adding corresponding type definitions, documentation, and tests. The implementation is solid, but there is a minor discrepancy between the documentation and the code regarding warnings for in-memory databases, for which I've provided a suggestion.
Update README to accurately reflect that SQLite silently ignores WAL mode for in-memory databases, rather than logging a warning. This matches SQLite's native behavior. Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
|
Thanks for the review! I've fixed the documentation discrepancy. The implementation intentionally matches SQLite's native behavior - when you execute I've updated the README (commit 96e13ff) to accurately reflect this: "SQLite will silently ignore the WAL mode request and remain in 'memory' journal mode." This approach provides a consistent experience with SQLite's behavior. Users who need to verify the journal mode can query it directly using: |
There was a problem hiding this comment.
Pull request overview
This PR adds Write-Ahead Logging (WAL) mode support to the @keyv/sqlite package. WAL mode provides better concurrency (readers don't block writers and vice versa) and improved performance for many scenarios. Users can now optionally enable WAL mode by setting wal: true in the KeyvSqlite constructor options.
Changes:
- Added
wal?: booleanoption to theKeyvSqliteOptionsinterface with appropriate documentation - Modified the database connection logic to execute
PRAGMA journal_mode=WALwhen the option is enabled - Added comprehensive test coverage for WAL mode functionality including edge cases
- Updated README documentation to explain the new option and its limitations
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/sqlite/src/types.ts | Added wal boolean option to KeyvSqliteOptions interface with JSDoc comment |
| packages/sqlite/src/index.ts | Implemented WAL mode by executing PRAGMA command after database connection, added Promise type annotation |
| packages/sqlite/test/test.ts | Added three test cases covering WAL enabled, disabled by default, and in-memory database behavior |
| packages/sqlite/README.md | Updated documentation to include the new wal option with usage examples and limitations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| - `table` - The table name to use for storage (default: `'keyv'`) | ||
| - `busyTimeout` - Sets a busy handler that sleeps for a specified amount of time when a table is locked | ||
| - `wal` - Enable [Write-Ahead Logging](https://sqlite.org/wal.html) mode for better concurrency and performance (default: `false`) | ||
| - **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. |
There was a problem hiding this comment.
The documentation states "A warning will be logged if you try to enable it for an in-memory database," but the code does not actually log any warning. The code silently executes the PRAGMA command and SQLite silently ignores it for in-memory databases. Either remove this statement from the documentation, or add actual warning logging in the code when WAL is requested for an in-memory database.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1826 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 32 32
Lines 2425 2430 +5
Branches 442 443 +1
=========================================
+ Hits 2425 2430 +5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@codex review this. focus on anything that is missing |
|
Codex Review: Didn't find any major issues. Breezy! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Summary
This PR adds Write-Ahead Logging (WAL) mode support to the
@keyv/sqlitepackage. Users can now enable WAL mode via thewaloption in the KeyvSqlite constructor for better concurrency and performance.What is WAL Mode?
Write-Ahead Logging (WAL) is an alternative journaling mode in SQLite that provides:
Reference: https://sqlite.org/wal.html
Changes Made
Type Definition (
packages/sqlite/src/types.ts)wal?: booleanoption toKeyvSqliteOptionsinterfaceCore Implementation (
packages/sqlite/src/index.ts)connectfunction to executePRAGMA journal_mode=WALafter database connection ifwal: trueis specifiedTests (
packages/sqlite/test/test.ts)wal: trueis specifiedwal: trueDocumentation (
packages/sqlite/README.md)waloptionUsage Example
```javascript
import Keyv from 'keyv';
import KeyvSqlite from '@keyv/sqlite';
// Enable WAL mode
const store = new KeyvSqlite({
uri: 'sqlite://path/to/database.sqlite',
wal: true
});
const keyv = new Keyv({ store });
```
Important Notes
In-Memory Database Limitation
WAL mode does NOT work with in-memory databases (
:memory:). SQLite automatically uses a special "memory" journal mode for in-memory databases that cannot be changed. This is expected behavior and matches how SQLite works natively - it silently ignores the WAL mode request and remains in "memory" journal mode.Default Behavior
WAL mode is disabled by default to maintain backward compatibility. Users must explicitly opt-in by setting
wal: truein the options.Benefits of WAL Mode
Pros:
Cons:
Testing
All tests pass with 100% code coverage:
wal: trueis specified with file-based databasesReferences