Skip to content

sqlite - feat: Add WAL (Write-Ahead Logging) mode support#1826

Merged
jaredwray merged 4 commits intojaredwray:mainfrom
snomiao:sno-wal
Feb 3, 2026
Merged

sqlite - feat: Add WAL (Write-Ahead Logging) mode support#1826
jaredwray merged 4 commits intojaredwray:mainfrom
snomiao:sno-wal

Conversation

@snomiao
Copy link
Contributor

@snomiao snomiao commented Feb 2, 2026

Summary

This PR adds Write-Ahead Logging (WAL) mode support to the @keyv/sqlite package. Users can now enable WAL mode via the wal option 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:

  • Better concurrency (readers don't block writers, writers don't block readers)
  • Improved performance in many scenarios
  • Better crash resistance
  • Atomic commits

Reference: https://sqlite.org/wal.html

Changes Made

  1. Type Definition (packages/sqlite/src/types.ts)

    • Added wal?: boolean option to KeyvSqliteOptions interface
  2. Core Implementation (packages/sqlite/src/index.ts)

    • Modified the connect function to execute PRAGMA journal_mode=WAL after database connection if wal: true is specified
    • Added proper TypeScript type annotation for Promise<sqlite3.Database>
  3. Tests (packages/sqlite/test/test.ts)

    • Added test to verify WAL mode is enabled when wal: true is specified
    • Added test to verify WAL mode is not enabled by default
    • Added test to verify in-memory databases remain in "memory" journal mode even with wal: true
    • All tests pass with 100% code coverage
  4. Documentation (packages/sqlite/README.md)

    • Updated README to document the new wal option
    • Added Options section explaining all available configuration options
    • Documented the limitation with in-memory databases

Usage 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: true in the options.

Benefits of WAL Mode

Pros:

  • Better concurrency - multiple readers can access simultaneously, writers don't block readers
  • Improved performance for most write operations
  • Better crash resistance and atomic commits

Cons:

  • Creates -wal and -shm files alongside the database file
  • Not ideal for network filesystems (NFS, SMB)
  • Slightly higher disk space usage

Testing

All tests pass with 100% code coverage:

  • ✅ WAL mode is correctly enabled when wal: true is specified with file-based databases
  • ✅ Default behavior (no WAL) remains unchanged
  • ✅ In-memory databases remain in "memory" journal mode (expected behavior)
  • ✅ All existing functionality works with and without WAL mode

References

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]>
Copilot AI review requested due to automatic review settings February 2, 2026 14:54
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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 @keyv/sqlite package, providing users with an opt-in mechanism to significantly improve database concurrency and overall performance. The changes involve extending the configuration options, modifying the database connection logic to apply the WAL pragma, and updating documentation and tests to reflect this new capability and its specific behaviors, particularly concerning in-memory databases.

Highlights

  • WAL Mode Support: Introduced Write-Ahead Logging (WAL) mode support for @keyv/sqlite via a new wal option in the KeyvSqlite constructor, enhancing concurrency and performance.
  • Configuration: Added wal?: boolean to KeyvSqliteOptions and updated README.md to document the new option, including its default false value and usage example.
  • Implementation Details: Modified the connect function to execute PRAGMA journal_mode=WAL when the wal option is enabled, and added a TypeScript type annotation for Promise<sqlite3.Database>.
  • Testing and Limitations: Included comprehensive tests to verify WAL mode enablement, default behavior, and its known limitation with in-memory databases, ensuring 100% code coverage.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@snomiao
Copy link
Contributor Author

snomiao commented Feb 2, 2026

Thanks for the review! I've fixed the documentation discrepancy.

The implementation intentionally matches SQLite's native behavior - when you execute PRAGMA journal_mode=WAL on an in-memory database, SQLite silently ignores the request and returns "memory" without any warning or error. This is the expected behavior documented in SQLite's own documentation.

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: await store.query('PRAGMA journal_mode')

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?: boolean option to the KeyvSqliteOptions interface with appropriate documentation
  • Modified the database connection logic to execute PRAGMA journal_mode=WAL when 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.
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Feb 2, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5d49cac) to head (28c1064).
⚠️ Report is 1 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jaredwray
Copy link
Owner

@codex review this. focus on anything that is missing

@jaredwray jaredwray changed the title sqlite: Add WAL (Write-Ahead Logging) mode support sqlite - feat: Add WAL (Write-Ahead Logging) mode support Feb 3, 2026
@chatgpt-codex-connector
Copy link

Codex Review: Didn't find any major issues. Breezy!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@jaredwray jaredwray merged commit 0f7ece7 into jaredwray:main Feb 3, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants