@bimetal/store-sqlite

中文文档
0.42.0发布于 2026/10/30Apache-2.0TypeScriptES Module近 30 天下载 7956

SQLite EventStore implementation for @bimetal/event-sourcing — file-based, zero-config persistence

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. Snapshots ( createSqliteSnapshotStore )
  4. Measured effect
  5. Options ( SqliteStoreOptions )
  6. Testing

@bimetal/store-sqlite

SQLite-backed EventStore implementation for @bimetal/event-sourcing — file-based, zero-config native event sourcing. Use with any domain package (@bimetal/calendar-data, @bimetal/table-data, …). Built on better-sqlite3.

It is one of the storage adapters covered by the storage-parity test suite (In-Memory / SQLite / EventStoreDB produce bit-identical results), so it is a drop-in replacement for the in-memory store.

Installation

npm install @bimetal/store-sqlite

Usage

import { createSqliteEventStore } from '@bimetal/store-sqlite';
import { createCalendarStore } from '@bimetal/calendar-data';

const eventStore = createSqliteEventStore({ path: './events.db' });
const store = createCalendarStore({ eventStore });
await store.hydrate();

Snapshots (createSqliteSnapshotStore)

Without a persistent snapshot store, every process start replays the stream from its first event and holds the whole log in memory afterwards. createSqliteSnapshotStore keeps the most recent snapshot per stream in its own snapshots table — point it at the same file as the event store:

import { createSqliteEventStore, createSqliteSnapshotStore } from '@bimetal/store-sqlite';
import { createCalendarStore } from '@bimetal/calendar-data';

const eventStore = createSqliteEventStore('./events.db');
const snapshotStore = createSqliteSnapshotStore('./events.db');
const store = createCalendarStore({ eventStore, snapshotStore, snapshotEveryN: 500 });
await store.hydrate(); // loads snapshot + the events after it — not the whole log
  • One row per stream, last save wins — the same contract as createInMemorySnapshotStore, pinned by runSnapshotStoreConformance (@bimetal/event-sourcing/conformance).
  • Written like an append: save runs in a BEGIN IMMEDIATE transaction — the write lock is held before the upsert runs (checked from a second connection in the tests).
  • Format lock travels along: eventSchemaVersion is stored and returned unchanged; hydrate() rejects a snapshot from a newer schema (UnknownEventSchemaVersionError).
  • Other projection version → full replay: hydrate() ignores a stored snapshot whose projectionVersion differs and folds the full stream. The stale row stays until the next snapshot is written (snapshotEveryN or rebuild()), so until then each start replays fully.
  • Opens its own connection. ':memory:' is a private database, not shared with the event store.
  • The aggregate state is stored as JSON (maps as entry lists). It must be JSON-safe — which it is whenever it was folded from events read back out of SQLite.

Measured effect

scripts/heap-messung.mjs builds a log of 20,000 calendar events in one stream (2,000 entries × 1 create + 9 updates), writes one snapshot at its end, then hydrates in a fresh Node process each run (--expose-gc, heapUsed after full GC before opening and after hydrate, median of 3). Measured 2026-10-03, Node v22.23.3:

heap after hydratehydrate timeevents held
without snapshot13.2 MiB852 ms20,000
with snapshot7.5 MiB75 ms0

Log payload 1.2 MiB, snapshot row 4.3 MiB. What remains with the snapshot is the aggregate state itself — including the per-entity undo stacks, which is why the snapshot row is larger than the log payload in this workload. The saving is the log the store no longer loads at start: after hydrate it holds only the events after the snapshot, not the whole stream. Numbers are for this workload.

What this does not bound: only the memory at start is measured and bounded. In a running process the held log keeps growing with every event appended since the hydrate — a snapshot written via snapshotEveryN does not trim it (tracked as bimetal-316). A long-running process still needs a restart to shed that log. Rerun the measurement:

npm run build -w @bimetal/store-sqlite
node packages/storage/store-sqlite/scripts/heap-messung.mjs 2000 9 3

Options (SqliteStoreOptions)

OptionPurpose
pathPath to the SQLite database file. Use ':memory:' for an in-memory database.
wal?Enable WAL mode for better concurrent read performance. Default true.

Testing

npm test --workspace @bimetal/store-sqlite

The parity suite (__tests__/storage-parity.test.ts) runs the same append/read/subscribe sequence against In-Memory, SQLite and EventStoreDB and asserts identical results — the guarantee that makes the adapter interchangeable.

版本与发布记录

@bimetal/store-sqlite 共发布 33 个版本,首次发布于 2026/04/29;过去 12 个月发布了 33 个版本。

版本发布日期距今
0.42.0latest2026/10/309 天前
0.41.02026/10/2415 天前
0.40.02026/10/1128 天前
0.39.02026/10/061 个月前
0.38.02026/10/041 个月前
0.37.02026/09/131 个月前

其他发布通道:next → 1.0.0-rc.3。

关于 @bimetal/store-sqlite 的常见问题

@bimetal/store-sqlite 是什么?

SQLite EventStore implementation for @bimetal/event-sourcing — file-based, zero-config persistence

如何安装 @bimetal/store-sqlite?

在项目目录运行 npm install @bimetal/store-sqlite,或使用 pnpm add / yarn add / bun add。

@bimetal/store-sqlite 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bimetal/store-sqlite 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@bimetal/store-sqlite 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bimetal/store-sqlite 可以商用吗?

@bimetal/store-sqlite 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bimetal/store-sqlite 的下载量有多少?

根据 npm 官方下载统计,@bimetal/store-sqlite 在 2026/10/08 – 2026/11/06 这 30 天内被下载 7,956 次,日均约 265 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bimetal/store-sqlite 还在维护吗?

最新版本 0.42.0 发布于 2026/10/30(9 天前);至今共发布 33 个版本,其中过去 12 个月发布了 33 个;首次发布于 2026/04/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。