browser-db-sqlite-persistence: a way to queue app SQL behind the persistence's transactions on a shared database

未关闭
#2,363 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
sqlite, typescript, wasm
领域
backend, database

调研方向

Start by reading wa-sqlite-driver.ts and the package entry's exports field, then trace how BrowserWASQLiteDriver.transaction() queues work for a shared database handle. Decide which supported API shape safely queues application SQL behind persistence transactions; the issue lists exporting the driver, adding a persistence method, or documenting direct execution as off-limits. Done means the chosen approach prevents application statements from being rolled back unexpectedly.

由索引模型根据 Issue 内容生成。

描述

  • I've checked this against the latest DB packages (@tanstack/[email protected] from npm, and main at d054a772)

Describe the feature

I'd like a supported way to run my own SQL on the database handle that createBrowserWASQLitePersistence uses, queued behind the persistence's transactions.

Why

We keep app metadata in our own tables in the same OPFS database as our persisted collections: a sync cursor and an outbox of pending mutations. These tables are separate from the persistence's tables and never touch them. We send statements to them with database.execute() on the handle from openBrowserWASQLiteOPFSDatabase(), which is the handle we also pass to createBrowserWASQLitePersistence.

BrowserWASQLiteDriver.transaction() runs BEGIN IMMEDIATE, then several awaited execute calls, then COMMIT or ROLLBACK. Its queue (databaseExecutionStates, a WeakMap keyed by the database object) orders only the driver's own work. A statement we send to the same handle between that BEGIN IMMEDIATE and the COMMIT runs inside the driver's transaction, on the same connection. If the driver then rolls back, our write is rolled back with it, even though our execute() call already resolved.

For us this can silently drop rows from our outbox. We add rows to it and then set a "done" marker in a second statement. If a rollback lands between the two statements, the marker is kept and the rows are not. It needs a SQL error inside the driver's transaction, such as a full storage quota, so it's rare. Our callers can't detect it.

We found this by reading the driver code, not from a field report. We haven't built a standalone reproduction.

What we tried

  • Queueing behind the driver ourselves isn't possible. BrowserWASQLiteDriver is exported from wa-sqlite-driver.ts, but not from the package entry: the exports field lists only ., the wasm and the worker.
  • We could track the transaction ourselves. We could wrap the handle we pass in, watch for BEGIN IMMEDIATE, COMMIT and ROLLBACK, and hold our statements until no transaction is open. That depends on the driver's SQL text, which isn't a public contract.
  • A second connection is too heavy. It would mean a second worker and wasm instance per tab, plus lock contention on the file.

Possible shapes

Any of these would work for us:

  1. Export BrowserWASQLiteDriver from the package entry. A driver we construct on the same handle shares the WeakMap queue, so its run and transaction calls are ordered with the persistence's work.
  2. Add a method to the persistence, such as persistence.runExclusive(fn) or a driver accessor, that runs a callback in the same queue.
  3. Document the shared handle as off-limits for direct execute() calls, so other users don't run into this.

Option 1 looks smallest, since the queue already works per database object.

Context

The app is a local-first sync client. Each tab opens one OPFS database per data partition. Collections use persistedCollectionOptions with createBrowserWASQLitePersistence. Our own tables live next to the persistence's tables and use their own names.

主要语言
TypeScript
星标
4k
派生
280
平均合并
1 天 9 小时
30 天内合并 PR
139

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TanStack/db 的其他 Issue

查看 TanStack/db 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。