@convex-dev/table-history

中文文档
0.1.6发布于 2026/10/07Apache-2.0TypeScriptES Module近 30 天下载 5387

A table history component for Convex.

原始使用文档
文档目录10 节
  1. History on a Convex table
  2. Pre-requisite: Convex
  3. Installation
  4. Usage
  5. Details of usage
  6. Serializability
  7. currentTs and maxTs are required
  8. Attribution
  9. Vacuuming
  10. Limitations

Convex TableHistory Component

npm version

Note: this Component is currently BETA. It was previously published under convex-table-history.

History on a Convex table

Attach a history component to your Convex table to keep track of changes.

  • View an audit log of all table changes in the Convex Dashboard or in a custom React component.
    • Answer questions like "was document A updated before document B?"
  • View an audit log of changes to a single document
    • Answer questions like "what was the user's email address before they changed it?"
  • Look at a snapshot of the table at any point in time.
// Paginate through all history on the "documents" table, from newest to oldest.
documentAuditLog.listHistory(ctx, args.maxTs, args.paginationOpts);

// Paginate through all history for a specific document, from newest to oldest.
documentAuditLog.listDocumentHistory(ctx, args.documentId, args.maxTs, args.paginationOpts);

// Paginate through all documents in the "documents" table at a specific timestamp.
documentAuditLog.listSnapshot(ctx, args.snapshotTs, args.currentTs, args.paginationOpts);

Found a bug? Feature request? File it here.

Pre-requisite: Convex

You'll need an existing Convex project to use the component. Convex is a hosted backend platform, including a database, serverless functions, and a ton more you can learn about here.

Run npm create convex or follow any of the quickstarts to set one up.

Installation

Install the component package:

npm install @convex-dev/table-history

Create a convex.config.ts file in your app's convex/ folder and install the component by calling use:

// convex/convex.config.ts
import { defineApp } from "convex/server";
import tableHistory from "@convex-dev/table-history/convex.config";

const app = defineApp();
app.use(tableHistory, { name: "documentAuditLog" });

export default app;

To add history to multiple tables, you can call app.use multiple times with different names. They will be available in your app as new TableHistory(components.<name>, ...).

Usage

import { components } from "./_generated/api";
import { TableHistory } from "@convex-dev/table-history";

const documentAuditLog = new TableHistory<DataModel, "documents">(components.documentAuditLog);

Add an item to the history table when a document changes:

async function patchDocument(ctx: MutationCtx, documentId: Id<"documents">, patch: Partial<Doc<"documents">>) {
  await ctx.db.patch(documentId, patch);
  const document = await ctx.db.get(documentId);
  await documentAuditLog.update(ctx, documentId, document);
}

async function insertDocument(ctx: MutationCtx, documentId: Id<"documents">, newDoc: WithoutSystemFields<Doc<"documents">>) {
  const documentId = await ctx.db.insert("documents", newDoc);
  const document = await ctx.db.get(documentId);
  await documentAuditLog.update(ctx, documentId, document);
}

async function deleteDocument(ctx: MutationCtx, documentId: Id<"documents">) {
  await ctx.db.delete(documentId);
  await documentAuditLog.update(ctx, documentId, null);
}

Or attach a trigger to automatically write to the history table when a mutation changes a document:

const triggers = new Triggers<DataModel>();
triggers.register("documents", documentAuditLog.trigger());
export const mutation = customMutation(rawMutation, customCtx(triggers.wrapDB));

Now that you're writing to the history table, you can use the listHistory, listDocumentHistory, and listSnapshot functions to query the history.

These functions are paginated, so you can use them with usePaginatedQuery to get all of the entries incrementally. The pages consist of HistoryEntry objects, which have the following fields:

  • id: the id of the document that was changed
  • doc: the new version of the document that was changed, or null if the document was deleted
  • ts: the timestamp of the change. See serializability.
  • isDeleted: whether the document was deleted
  • attribution: an optional arbitrary object that will be stored with the history entry

See more example usage in example.ts.

Details of usage

Serializability

Convex mutations are serializable transactions, which is great for maintaining consistency in your tables.

However, if you need the guarantee that history timestamps match the mutations' serializable order, that would require serializing all changes to your whole database, and your database would slow to a crawl and get OCC errors.

You can configure the serializability of the history table by setting the serializability option. The three options are:

  • "table" -- i.e. the latest ts for the table, plus one
    • history entries are serializable with other updates on the same table
  • "document" -- i.e. the latest ts for the document, plus one
    • history entries are serializable with other updates on the same document.
  • "wallclock" -- i.e. Date.now()
    • history entry timestamps don't have guarantees, but they also take no extra dependencies and are usually sufficient.

The default serializability is "wallclock".

currentTs and maxTs are required

const [currentTs] = useState(Date.now()); // stable and recent
const [yesterday] = useState(Date.now() - 24 * 60 * 60 * 1000); // stable
const snapshotOfUsersYesterday = usePaginatedQuery(
  api.documents.listSnapshot,
  {
    currentTs,
    snapshotTs: yesterday,
  },
  { initialNumItems: 100 },
);
const auditLogBeforeYesterday = usePaginatedQuery(
  api.documents.listHistory,
  {
    maxTs: yesterday,
  },
  { initialNumItems: 100 },
);
  • For listSnapshot, currentTs should be a stable recent timestamp.
    • "Stable" means it should have the same value for all pages.
      • To keep a stable timestamp for all pages, pick a value on the client and pass it as an arg of usePaginatedQuery.
    • "Recent" is relative to how often the table gets new inserts. The amount of extra work performed by the query is proportional to the number of ctx.db.insert(tableName, doc) calls since the currentTs.
      • If the timestamp isn't recent, the queries might read too much data in a single page and throw an error.
      • Don't pick a timestamp in the future, or gaps will appear between pages as new documents are inserted. The timestamp should be Date.now() or slightly in the past.
  • For listHistory and listDocumentHistory, maxTs should be stable but doesn't need to be recent.

Why is this necessary?

The TableHistory component supports paginated queries with usePaginatedQuery and manual pagination. But it needs a little help from you to make everything work seamlessly.

You want fully reactive pagination. Concretely, usePaginatedQuery results should not have gaps or duplicates.

In order to implement this feature without the built-in .paginate method, the TableHistory component assumes its own data model is append-only (which is true, except when vacuuming), and takes in a stable recent timestamp. Then it ignores history entries created after that timestamp.

Attribution

Store an update's attribution to track information like which user made the change, or what mutation made the change.

await documentAuditLog.update(ctx, documentId, document, {
  attribution: {
    actorIdentity: await ctx.auth.getUserIdentity(),
    mutationName: "patchDocument",
    source: "web",
  },
});

The default attribution when using TableHistory.update is null.

The default attribution when using TableHistory.trigger is the mutation's ctx.auth.getUserIdentity().

Vacuuming

The history table can grow large, so you can use the vacuumHistory function to remove old history entries. This function will schedule background jobs to delete old history entries. The entries which will be deleted are those which are not visible at snapshots >=minTsToKeep.

After vacuuming up to minTsToKeep, you can no longer call listSnapshot with a snapshot timestamp less than minTsToKeep.

Limitations

  • No indexes: you can't use an index to change the sort order or get a subset of results.
    • Workaround: you can paginate until isDone returns true, and sort or filter the results yourself, either on the client or in an action. Consider manual pagination.

版本与发布记录

@convex-dev/table-history 共发布 4 个版本,首次发布于 2026/04/01;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.1.6latest2026/10/071 个月前
0.1.52026/06/254 个月前
0.1.42026/06/254 个月前
0.1.32026/04/017 个月前

关于 @convex-dev/table-history 的常见问题

@convex-dev/table-history 是什么?

A table history component for Convex.

如何安装 @convex-dev/table-history?

在项目目录运行 npm install @convex-dev/table-history,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:convex(^1.46.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@convex-dev/table-history 支持 TypeScript 吗?

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

@convex-dev/table-history 支持 ESM 还是 CommonJS?

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

@convex-dev/table-history 可以商用吗?

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

@convex-dev/table-history 的下载量有多少?

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

@convex-dev/table-history 还在维护吗?

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