@atscript/db-memory 0.1.113
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +111 -0
- package/dist/index.cjs +978 -0
- package/dist/index.d.cts +484 -0
- package/dist/index.d.mts +484 -0
- package/dist/index.mjs +972 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://db.atscript.dev/logo.svg" alt="Atscript" width="120" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">@atscript/db-memory</h1>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
8
|
+
<strong>Define your models once</strong> — get TypeScript types, runtime validation, and DB metadata from a single <code>.as</code> model.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://db.atscript.dev">Documentation</a> · <a href="https://db.atscript.dev/adapters">Adapters</a>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
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:
|
|
18
|
+
|
|
19
|
+
- **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 `@InputForm`s) without acquiring a datastore. Writes throw.
|
|
20
|
+
- **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.
|
|
21
|
+
|
|
22
|
+
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](#accepted-limitations-v1)).
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm add @atscript/db-memory
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`@atscript/db` is a peer dependency.
|
|
31
|
+
|
|
32
|
+
## Provider-backed (read-only) — the primary mode
|
|
33
|
+
|
|
34
|
+
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:
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
import { createAdapter, setMemoryProvider } from "@atscript/db-memory";
|
|
38
|
+
import { syncSchema } from "@atscript/db/sync";
|
|
39
|
+
import { Job } from "./models/job.as";
|
|
40
|
+
|
|
41
|
+
// A DbSpace backed by the in-memory adapter — no engine, no I/O.
|
|
42
|
+
const space = createAdapter();
|
|
43
|
+
await syncSchema(space, [Job]);
|
|
44
|
+
|
|
45
|
+
// Make `Job` a READ-ONLY table whose rows come from a runtime closure
|
|
46
|
+
// (e.g. Redis keys zipped with an in-code job registry). Set AFTER syncSchema.
|
|
47
|
+
setMemoryProvider(space, Job, async () => await loadScheduledJobs());
|
|
48
|
+
|
|
49
|
+
// Observe it like any other atscript table.
|
|
50
|
+
const jobs = space.getTable(Job);
|
|
51
|
+
await jobs.findMany({
|
|
52
|
+
filter: { scheduled: { $eq: true } },
|
|
53
|
+
controls: { $sort: { nextRun: 1 }, $limit: 20 },
|
|
54
|
+
});
|
|
55
|
+
// Any write (insert/update/delete) throws: the table is provider-backed.
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
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.
|
|
59
|
+
|
|
60
|
+
## Stored (read-write) — tests and trivial tables
|
|
61
|
+
|
|
62
|
+
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:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { createAdapter } from "@atscript/db-memory";
|
|
66
|
+
import { syncSchema } from "@atscript/db/sync";
|
|
67
|
+
import { User } from "./models/user.as";
|
|
68
|
+
|
|
69
|
+
const space = createAdapter();
|
|
70
|
+
await syncSchema(space, [User]);
|
|
71
|
+
|
|
72
|
+
const users = space.getTable(User);
|
|
73
|
+
await users.insertOne({ id: "u1", name: "Ada" });
|
|
74
|
+
await users.findMany({
|
|
75
|
+
filter: { name: { $regex: "^A" } },
|
|
76
|
+
controls: { $sort: { name: 1 } },
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Public API
|
|
81
|
+
|
|
82
|
+
| Export | Purpose |
|
|
83
|
+
| ------------------------------------ | --------------------------------------------------------------------------------------------------- |
|
|
84
|
+
| `createAdapter()` | Builds a `DbSpace` backed by a fresh `MemoryAdapter` per table. |
|
|
85
|
+
| `setMemoryProvider(space, Type, fn)` | Late-binds a provider closure onto `Type`'s adapter, making that table read-only (provider mode). |
|
|
86
|
+
| `MemoryAdapter` | The adapter class, for constructing a `DbSpace` by hand (`new DbSpace(() => new MemoryAdapter())`). |
|
|
87
|
+
| `MemoryProviderFn` | The provider closure type: `() => Row[] \| Promise<Row[]>`. |
|
|
88
|
+
| `buildMemoryPredicate(filter)` | Compiles a `FilterExpr` into a JS-native `(row) => boolean` predicate (the engine's filter core). |
|
|
89
|
+
|
|
90
|
+
## Accepted limitations (v1)
|
|
91
|
+
|
|
92
|
+
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:
|
|
93
|
+
|
|
94
|
+
- **Regex / null semantics** — JS-native (`RegExp`, `/pat/flags` honored; `$ne`/`$exists` are Mongo-like). Not byte-identical to any SQL engine.
|
|
95
|
+
- **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.
|
|
96
|
+
- **No array-element matching** — the dot-path getter matches scalars and nested-object paths; Mongo-style implicit array-element / `$elemMatch` matching is not provided.
|
|
97
|
+
- **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.)
|
|
98
|
+
- **Provider tables are read-only, with no cross-request pagination stability** — page 1 and page 2 are separate requests over separate snapshots.
|
|
99
|
+
- **No native aggregation** — `$groupBy` throws a typed `INVALID_QUERY` (clean 4xx, not 500).
|
|
100
|
+
- **Relations `$with`** — resolved by core's app-level batch loading, not natively.
|
|
101
|
+
- **No FTS / vector / geo / `$search`** — unsupported.
|
|
102
|
+
- **Not a production datastore** — nothing is persisted or shared across processes.
|
|
103
|
+
|
|
104
|
+
## Documentation
|
|
105
|
+
|
|
106
|
+
- [Full Documentation](https://db.atscript.dev)
|
|
107
|
+
- [Adapters](https://db.atscript.dev/adapters)
|
|
108
|
+
|
|
109
|
+
## License
|
|
110
|
+
|
|
111
|
+
MIT
|