Skip to content

Persist store collections in SQLite, one transaction per write (#835) - #858

Merged
jimhoyd merged 2 commits into
mainfrom
claude/store-on-sqlite
Sep 28, 2026
Merged

jimhoyd merged 2 commits into
mainfrom
claude/store-on-sqlite

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

First slice of #835: this PR replaces the store's JSON-file engine with node:sqlite, using direct parameterized SQL (no query builder or ORM). The JSON engine is deleted outright: no compatibility wrapper and no import of old JSON data files.

Design

  • One database per site. The location is store({database}), else STORE_DATABASE, else data/store.sqlite beside host.mjs, and it must be outside app/.
    • The file is created 0600 in a 0700 directory. Symlinks, hard links and loose permissions are refused.
    • Connection settings: WAL, synchronous=FULL, busy_timeout=2000, trusted_schema=OFF.
    • The file carries application_id and user_version. Schema steps run forward only, one transaction each. A foreign file or a newer schema refuses activation.
  • Shared tables: store_records, store_idempotency and store_audit_outbox, keyed by collection name. A change to a collection's declaration needs no DDL. store_idempotency can take a result column later, for replay.
  • Transactions:
    • Every API write is one BEGIN IMMEDIATE transaction. It covers the idempotency claim, the If-Match check, key uniqueness, the quotas, the record row and the audit outbox event, and rolls back on any error.
    • Reads use one deferred transaction.
    • urlcode-store reassign moves every affected collection in one transaction.
    • No transaction spans the store, audit or auth databases, and none is claimed.
  • Guarantees (docs/STORE.md):
    • One serving process is supported but not enforced. The old .store.lock machinery is removed: SQLite's locking keeps a second process from corrupting data, and enforcing exclusivity would rule out online backups and live operator CLI runs.
    • A write blocked by another process waits up to 2 s, then gets 503 storage_unavailable with nothing written.
    • Network filesystems and multi-host deployments are unsupported.
  • Fixes a pre-existing race: two writes in the same millisecond shared an ETag, so concurrent If-Match writers could both win.
  • CLI: urlcode-store takes --database in place of --directory, never creates a database, and is safe beside the running server.

Evidence (macOS, Node 26)

  • npm run verify passes on the branch rebased onto main. It covers the core tests, every workspace suite, test:addons and test:proof. npm run test:package also passes.
  • Store: 91/91, form-records: 23/23.
  • New tests:
    • restart persistence
    • 8 concurrent If-Match PATCHes: exactly one wins, the other seven get 412
    • concurrent creates never overshoot maxRecords
    • audit-outbox atomicity: a trigger-injected failure rolls back both the record and the event
    • cross-collection reassign rollback while the server is serving
    • forward-init idempotency, and refusal of foreign or newer schemas
    • a busy lock answers 503
    • an online backup while serving
  • Package: 57,679 bytes packed and 223,308 unpacked, within the existing budgets.

Not claimed

No Windows run locally (CI covers it), no crash, disk-full or soak testing, and no throughput benchmark.

Follow-ups are filed separately: result-aware idempotency and multi-record transitions (the next #835 slice), SQL-side sorted lists, multi-process enforcement, a backup CLI, and the store-crud recipe's target metadata.

🤖 Generated with Claude Code

…write (#835)

The store's JSON-file engine (in-memory maps, a promise-chain serializer,
temp+fsync+rename persistence and the directory lock) is replaced by node:sqlite
with direct parameterized SQL: one database per site (store({database}),
STORE_DATABASE or data/store.sqlite), shared record, idempotency and audit
outbox tables, forward-only schema steps. Every API write is one BEGIN
IMMEDIATE transaction covering the checks, the record, the idempotency claim
and the audit outbox event; reassign moves every collection in one transaction.
Fixes concurrent If-Match writers sharing an ETag within one millisecond.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…indows EBUSY on store.sqlite)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

1 participant