@atscript/db-memory

中文文档
0.1.174发布于 2026/11/07MITTypeScriptESM + CJS零依赖近 30 天下载 3270

In-memory adapter for atscript-db.

原始使用文档
文档目录7 节
  1. Installation
  2. Provider-backed (read-only) — the primary mode
  3. Stored (read-write) — tests and trivial tables
  4. Public API
  5. Accepted limitations (v1)
  6. Documentation
  7. License

Atscript

@atscript/db-memory

Define your models once — get TypeScript types, runtime validation, and DB metadata from a single .as model.

Documentation · Adapters


In-memory adapter for @atscript/db. MemoryAdapter is a full BaseDbAdapter with no engine, no I/O, and no persistence — rows live in ordinary in-process JS structures. It runs in two modes:

  • Provider-backed (read-only) — the primary mode. A table's rows come from a closure () => Row[] | Promise<Row[]> invoked at query time. This lets a runtime-owned surface with no database — a "scheduled jobs" list zipped from Redis + a job registry, a job-runs snapshot, any computed set — be observed as a real atscript table (rendered in atscript-ui, carrying @DbAction row actions and @InputForms) without acquiring a datastore. Writes throw.
  • Stored (read-write) — a secondary convenience. Rows live in a Map, giving a fast in-memory DbSpace for tests and trivial in-app tables. Full CRUD.

Filter, sort, project, paginate, optimistic-concurrency (OCC) versioning, and primary-key + unique-index enforcement are all supported. Leaf-comparison semantics (regex, null, ordering) are JS-native and documented — deliberately not claimed identical to the SQL engines (see Accepted limitations).

Installation

pnpm add @atscript/db-memory

@atscript/db is a peer dependency.

Provider-backed (read-only) — the primary mode

Observe a runtime-owned entity as a read-only table. The provider closure is re-invoked on every read (a fresh snapshot each time), and is late-bound after syncSchema so it can close over dependencies that don't exist at factory time:

import { createAdapter, setMemoryProvider } from "@atscript/db-memory";
import { syncSchema } from "@atscript/db/sync";
import { Job } from "./models/job.as";

// A DbSpace backed by the in-memory adapter — no engine, no I/O.
const space = createAdapter();
await syncSchema(space, [Job]);

// Make `Job` a READ-ONLY table whose rows come from a runtime closure
// (e.g. Redis keys zipped with an in-code job registry). Set AFTER syncSchema.
setMemoryProvider(space, Job, async () => await loadScheduledJobs());

// Observe it like any other atscript table.
const jobs = space.getTable(Job);
await jobs.findMany({
  filter: { scheduled: { $eq: true } },
  controls: { $sort: { nextRun: 1 }, $limit: 20 },
});
// Any write (insert/update/delete) throws: the table is provider-backed.

Because the readable is built uniformly, @DbAction discovery, ?$actions=true gating, disabled predicates, @InputForm, and the atscript-ui table / /meta surface all work against this table with no adapter-side action code.

Stored (read-write) — tests and trivial tables

A drop-in in-memory DbSpace with full CRUD — the role an in-memory SQLite serves, minus the native module and the SQL round-trip:

import { createAdapter } from "@atscript/db-memory";
import { syncSchema } from "@atscript/db/sync";
import { User } from "./models/user.as";

const space = createAdapter();
await syncSchema(space, [User]);

const users = space.getTable(User);
await users.insertOne({ id: "u1", name: "Ada" });
await users.findMany({
  filter: { name: { $regex: "^A" } },
  controls: { $sort: { name: 1 } },
});

Public API

ExportPurpose
createAdapter()Builds a DbSpace backed by a fresh MemoryAdapter per table.
setMemoryProvider(space, Type, fn)Late-binds a provider closure onto Type's adapter, making that table read-only (provider mode).
MemoryAdapterThe adapter class, for constructing a DbSpace by hand (new DbSpace(() => new MemoryAdapter())).
MemoryProviderFnThe provider closure type: () => Row[] | Promise<Row[]>.
buildMemoryPredicate(filter)Compiles a FilterExpr into a JS-native (row) => boolean predicate (the engine's filter core).

Accepted limitations (v1)

Deliberate — matching a real engine here is hard or impossible and unnecessary for the small computed surfaces this adapter targets. All are documented, none silent:

  • Regex / null semantics — JS-native (RegExp, /pat/flags honored; $ne/$exists are Mongo-like). Not byte-identical to any SQL engine.
  • No collation — @db.column.collate is not honored; filters and sorts are code-point / JS-native, so case-insensitive $eq/sort differ from SQLite/Mongo.
  • No array-element matching — the dot-path getter matches scalars and nested-object paths; Mongo-style implicit array-element / $elemMatch matching is not provided.
  • Non-atomic stored batch writes — insertMany/updateMany/replaceMany/deleteMany apply sequentially with no rollback; a mid-batch conflict leaves earlier items written. Single writes are safe. (Provider tables are read-only, so this never applies to the primary mode.)
  • Provider tables are read-only, with no cross-request pagination stability — page 1 and page 2 are separate requests over separate snapshots.
  • Aggregation is an in-process scan — $groupBy / aggregates / calendar buckets / $having / $count run in JS over one snapshot with SQL semantics (null and missing group together; sum/avg over no value are null). Fine for the small sets this adapter targets; there is no index-backed grouping.
  • Relations $with — resolved by core's app-level batch loading, not natively.
  • No FTS / vector / geo / $search — unsupported.
  • Not a production datastore — nothing is persisted or shared across processes.

Documentation

License

MIT

版本与发布记录

@atscript/db-memory 共发布 62 个版本,首次发布于 2026/07/09;过去 12 个月发布了 62 个版本。

版本发布日期距今
0.1.174latest2026/11/07今天
0.1.1732026/11/071 天前
0.1.1722026/11/061 天前
0.1.1712026/11/061 天前
0.1.1702026/11/061 天前
0.1.1692026/11/043 天前

关于 @atscript/db-memory 的常见问题

@atscript/db-memory 是什么?

In-memory adapter for atscript-db.

如何安装 @atscript/db-memory?

在项目目录运行 npm install @atscript/db-memory,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@uniqu/core(^0.2.1)、@atscript/db(^0.1.174)、@atscript/core(^0.2.9)、@atscript/typescript(^0.2.9)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@atscript/db-memory 支持 TypeScript 吗?

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

@atscript/db-memory 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@atscript/db-memory 可以商用吗?

@atscript/db-memory 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@atscript/db-memory 的下载量有多少?

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

@atscript/db-memory 还在维护吗?

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