@owlmeans/resource 0.1.18-rc.8 → 0.1.18
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 +336 -135
- package/agent-meta/manifest.json +87 -4
- package/agent-meta/skills/resource/SKILL.md +36 -104
- package/agent-meta/skills/resource/parts/capabilities.md +23 -0
- package/agent-meta/skills/resource/parts/criteria.md +60 -0
- package/agent-meta/skills/resource/parts/memory-engine.md +27 -0
- package/agent-meta/skills/resource/parts/migrations.md +35 -0
- package/agent-meta/skills/resource-choice/SKILL.md +271 -0
- package/build/consts.d.ts +0 -3
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +0 -23
- package/build/consts.js.map +1 -1
- package/build/errors.d.ts +13 -0
- package/build/errors.d.ts.map +1 -1
- package/build/errors.js +15 -1
- package/build/errors.js.map +1 -1
- package/build/helper.d.ts.map +1 -1
- package/build/index.d.ts +2 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/migration.d.ts.map +1 -1
- package/build/migration.js +0 -0
- package/build/migration.js.map +1 -1
- package/build/service.d.ts.map +1 -1
- package/build/types.d.ts +3 -1
- package/build/types.d.ts.map +1 -1
- package/build/utils/consts.local.d.ts +2 -0
- package/build/utils/consts.local.d.ts.map +1 -0
- package/build/utils/consts.local.js +2 -0
- package/build/utils/consts.local.js.map +1 -0
- package/build/utils/name.d.ts.map +1 -1
- package/build/utils/name.js.map +1 -1
- package/build/utils/query/types.d.ts +35 -0
- package/build/utils/query/types.d.ts.map +1 -0
- package/build/utils/query/types.js +2 -0
- package/build/utils/query/types.js.map +1 -0
- package/build/utils/query.d.ts +8 -20
- package/build/utils/query.d.ts.map +1 -1
- package/build/utils/query.js +97 -103
- package/build/utils/query.js.map +1 -1
- package/build/utils.d.ts +4 -0
- package/build/utils.d.ts.map +1 -0
- package/build/utils.js +24 -0
- package/build/utils.js.map +1 -0
- package/package.json +7 -5
- package/src/consts.ts +0 -25
- package/src/errors.ts +16 -1
- package/src/index.ts +2 -0
- package/src/migration.ts +0 -0
- package/src/types.ts +1 -1
- package/src/utils/consts.local.ts +1 -0
- package/src/utils/name.ts +2 -2
- package/src/utils/query/types.ts +37 -0
- package/src/utils/query.ts +106 -98
- package/src/utils.ts +26 -0
- package/tests/errors.spec.ts +60 -0
- package/tests/tsconfig.json +17 -0
- package/tsconfig.json +1 -1
package/agent-meta/manifest.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
|
-
"schemaVersion":
|
|
2
|
+
"schemaVersion": 3,
|
|
3
3
|
"package": "@owlmeans/resource",
|
|
4
|
-
"version": "0.1.18
|
|
5
|
-
"generatedAt": "2026-
|
|
4
|
+
"version": "0.1.18",
|
|
5
|
+
"generatedAt": "2026-10-28T00:31:34.719Z",
|
|
6
6
|
"canonicalRepo": "https://github.com/owlmeans/common",
|
|
7
7
|
"entries": [
|
|
8
8
|
{
|
|
@@ -10,7 +10,90 @@
|
|
|
10
10
|
"name": "resource",
|
|
11
11
|
"category": "package-specific",
|
|
12
12
|
"file": "skills/resource/SKILL.md",
|
|
13
|
-
"canonicalPath": ".agents/skills/resource/SKILL.md"
|
|
13
|
+
"canonicalPath": ".agents/skills/resource/SKILL.md",
|
|
14
|
+
"parts": [
|
|
15
|
+
{
|
|
16
|
+
"name": "capabilities",
|
|
17
|
+
"file": "skills/resource/parts/capabilities.md",
|
|
18
|
+
"description": "The optional capability interfaces a concrete resource composes beside Resource (pub/sub, watch, stream, lock, migrations).",
|
|
19
|
+
"purposes": [
|
|
20
|
+
"data"
|
|
21
|
+
],
|
|
22
|
+
"mentions": [
|
|
23
|
+
"PubSubResource",
|
|
24
|
+
"WatchableResource",
|
|
25
|
+
"StreamResource",
|
|
26
|
+
"LockableResource",
|
|
27
|
+
"MigratableResource",
|
|
28
|
+
"SubscribeOptions"
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"name": "criteria",
|
|
33
|
+
"file": "skills/resource/parts/criteria.md",
|
|
34
|
+
"description": "The criteria language (equality, any-of arrays, operators, branches, nested keys), sort and where absent values land, and paging per backend.",
|
|
35
|
+
"purposes": [
|
|
36
|
+
"query",
|
|
37
|
+
"list"
|
|
38
|
+
],
|
|
39
|
+
"mentions": [
|
|
40
|
+
"Criteria",
|
|
41
|
+
"FieldCriteria",
|
|
42
|
+
"FieldOperators",
|
|
43
|
+
"$or",
|
|
44
|
+
"$in",
|
|
45
|
+
"$ilike",
|
|
46
|
+
"$contains",
|
|
47
|
+
"$overlaps",
|
|
48
|
+
"Sort",
|
|
49
|
+
"ListOptions",
|
|
50
|
+
"ListResult",
|
|
51
|
+
"DEFAULT_PAGE_SIZE",
|
|
52
|
+
"page-without-size"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"name": "memory-engine",
|
|
57
|
+
"file": "skills/resource/parts/memory-engine.md",
|
|
58
|
+
"description": "recordQueryHelper, the shared in-memory engine every store without a query engine of its own filters, sorts and pages through.",
|
|
59
|
+
"purposes": [
|
|
60
|
+
"test"
|
|
61
|
+
],
|
|
62
|
+
"mentions": [
|
|
63
|
+
"recordQueryHelper",
|
|
64
|
+
"matchCriteria",
|
|
65
|
+
"filterRecords",
|
|
66
|
+
"sortRecords",
|
|
67
|
+
"firstMatch",
|
|
68
|
+
"applyQuery"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"name": "migrations",
|
|
73
|
+
"file": "skills/resource/parts/migrations.md",
|
|
74
|
+
"description": "The migration framework — automatic on init — and the five steps of adding migration support to a new database backend.",
|
|
75
|
+
"purposes": [
|
|
76
|
+
"migration"
|
|
77
|
+
],
|
|
78
|
+
"mentions": [
|
|
79
|
+
"MigratableResource",
|
|
80
|
+
"MigrationStore",
|
|
81
|
+
"createMigrationRegistry",
|
|
82
|
+
"runMigrations",
|
|
83
|
+
"MigrationStage",
|
|
84
|
+
"mongoDeclarationHelper",
|
|
85
|
+
"pgDeclarationHelper",
|
|
86
|
+
"_owlmeans_migrations"
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
"kind": "skill",
|
|
93
|
+
"name": "resource-choice",
|
|
94
|
+
"category": "multi-package",
|
|
95
|
+
"file": "skills/resource-choice/SKILL.md",
|
|
96
|
+
"canonicalPath": ".agents/skills/resource-choice/SKILL.md"
|
|
14
97
|
}
|
|
15
98
|
]
|
|
16
99
|
}
|
|
@@ -8,26 +8,29 @@ user-invocable: false
|
|
|
8
8
|
# @owlmeans/resource
|
|
9
9
|
|
|
10
10
|
**Layer:** Core
|
|
11
|
-
**Install:** `"@owlmeans/resource": "^0.1.18
|
|
11
|
+
**Install:** `"@owlmeans/resource": "^0.1.18"` in `dependencies`
|
|
12
12
|
|
|
13
13
|
## Key Exports
|
|
14
14
|
|
|
15
15
|
| Export | Description |
|
|
16
16
|
|--------|-------------|
|
|
17
|
+
| `ResourceRecord` | `{ id?: string }` — the constraint every record type satisfies, and the only field the contract itself knows about. |
|
|
17
18
|
| `Resource<T>` | Generic resource contract: `get`/`load`/`list`/`count`/`create`/`update`/`save`/`delete`/`take`/`purge`. Reads take **an id or a criteria object**; `list(where?, opts?)` takes `{ page, size, sort }` flat in the second argument. |
|
|
18
19
|
| `Criteria<T>`, `FieldCriteria<V>`, `FieldOperators<V>` | The query language, typed by the record — a mistyped field is a compile error. |
|
|
19
20
|
| `Sort<T>`, `SortField<T>`, `FirstOptions<T>`, `ListOptions<T>`, `ListQuery<T>`, `ListResult<T>` | Ordering, the read options, the one-object form an API carries over the wire, and the answer shape `{ items, total, page?, size? }`. |
|
|
20
21
|
| `WriteOptions`, `Ttl` | `{ ttl }` on `create`/`update`/`save`; seconds from now, or the instant to expire at. Backends without expiry refuse it. |
|
|
21
|
-
| `matchCriteria`, `filterRecords`, `sortRecords`, `firstMatch`, `applyQuery` | The shared in-memory engine — one criteria object means the same thing in a browser store as it does in SQL. |
|
|
22
|
-
| `PubSubResource<T>`, `WatchableResource<T>`, `StreamResource<T>`, `LockableResource<T>` | **Optional capabilities**, composed into a concrete resource interface alongside `Resource<T>`. |
|
|
22
|
+
| `recordQueryHelper` — `matchCriteria`, `filterRecords`, `sortRecords`, `firstMatch`, `applyQuery` | The shared in-memory engine — one criteria object means the same thing in a browser store as it does in SQL. |
|
|
23
|
+
| `PubSubResource<T>`, `WatchableResource<T>`, `StreamResource<T>`, `LockableResource<T>` | **Optional capabilities**, composed into a concrete resource interface alongside `Resource<T>`. `SubscribeOptions` (`{ channel, once, ttl }`) and `Unsubscribe` come with the first two. |
|
|
23
24
|
| `MigratableResource<Tx, Self>` | **Optional migration capability** — `migration(name, apply, stage?)` (chainable) + `migrations()`. Optional the way pub/sub is on redis resources: mongo and postgres extend it, backends with nothing to migrate don't. |
|
|
24
25
|
| `Migration`, `MigrationRegistry`, `MigrationStore`, `MigrationReport`, `MigrationRunOptions` | The framework's contracts. `MigrationStore` is the *migration register* a database implements to track applied migrations (`ensure`/`applied`/`baseline`/`run`). |
|
|
25
26
|
| `createMigrationRegistry<Tx>()` | Storage-agnostic ledger of code-registered migrations per alias. Declaration order = application order; re-registering an identical body is a no-op, a changed body under a used name throws `MigrationConflict`. |
|
|
26
27
|
| `runMigrations(alias, registry, store, opts?)` | Apply one stage's pending migrations through a store. `baseline: true` records instead of running (fresh structures); `strictChecksum` (default on) rejects edited applied bodies. |
|
|
27
28
|
| `MigrationStage` | `Pre` (before structure reconciliation — renames, casts, rescues) / `Post` (after — backfills into new structure). |
|
|
28
|
-
| `createDbService` | Base for `ResourceDbService` implementations (config/alias/name/client plumbing). `
|
|
29
|
-
| `
|
|
30
|
-
|
|
|
29
|
+
| `createDbService` | Base for `ResourceDbService` implementations (config/alias/name/client plumbing). The service's `name(alias?)` resolves to `config.schema ?? config.alias ?? service.alias`. |
|
|
30
|
+
| `ResourceDbService<Db, Client>`, `DbLocker<T>` | What a connection service implements: `db`/`client`/`clients`/`config`/`name`/`ensureConfigAlias`/`initialize`, plus `lock`/`unlock` when the backend encrypts fields. |
|
|
31
|
+
| `ResourceMaker<R, T>` | `(dbAlias?, serviceAlias?) => T` — the signature every resource maker is typed with, so an app registers `makeXResource()` without repeating the argument list. |
|
|
32
|
+
| `filterObject(obj, keep?)`, `createListSchema` | Drop null and undefined properties, keeping the names listed in `keep`; the AJV schema for a `ListResult<T>` envelope. |
|
|
33
|
+
| Errors | `ResourceError`, `UnknownRecordError` (404), `MisshapedRecord`, `RecordExists` (409), `RecordUpdateFailed`, `UnsupportedArgumentError`, `UnsupportedMethodError`, `MigrationError`, `MigrationConflict` — the unmarked ones are faults (500). `tests/errors.spec.ts` refuses an exported class whose status was not decided. `UnknownRecordError.id` is everything after the first `/`, so an id or criteria text holding one survives. |
|
|
31
34
|
| `DbConfig`, `Config`, `Context` | Database config (`cfg.dbs`) types. |
|
|
32
35
|
|
|
33
36
|
## Usage
|
|
@@ -82,112 +85,41 @@ else; the criteria overload takes `{ sort }` to say which match is "the" one:
|
|
|
82
85
|
Every method returns the record(s) themselves, never a driver result object — there is no
|
|
83
86
|
`rowsAffected`/`rowCount` anywhere on this contract.
|
|
84
87
|
|
|
85
|
-
###
|
|
88
|
+
### What an escaped storage error answers
|
|
86
89
|
|
|
87
|
-
|
|
88
|
-
|
|
90
|
+
The two refusals declare their HTTP status (`static httpStatus`, the `error` skill's rule), so an
|
|
91
|
+
`@owlmeans/server-api` handler that lets one escape answers it: `UnknownRecordError` **404**,
|
|
92
|
+
`RecordExists` **409** (a unique-key violation, a taken id, and a `create` handed an id —
|
|
93
|
+
`id-present` — alike). Every other class of the family is a fault and answers 500. A development
|
|
94
|
+
body rebuilds into the same class on the client; a production body is the incident id alone and
|
|
95
|
+
arrives as `ApiStatusError` with that status (`@owlmeans/api`).
|
|
89
96
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
| `{ status: ['open', 'done'] }` | **any of these** — a bare array is never exact-array equality (that is `{ $eq: [...] }`) |
|
|
94
|
-
| `{ archivedAt: null }` | the absence of a value |
|
|
95
|
-
| `{ status: undefined }` | **skipped** — an untouched filter must never empty a list |
|
|
96
|
-
| `{ 'profile.city': 'Kraków' }` | reach into a nested value or a jsonb column |
|
|
97
|
-
|
|
98
|
-
Operators go in an object under the field: `$eq $ne $gt $gte $lt $lte $in $nin $exists $null
|
|
99
|
-
$like $ilike $regex $startsWith $endsWith $between $contains $contained $overlaps`. Branches
|
|
100
|
-
combine with `$and` / `$or` / `$not`, each taking criteria of the same shape.
|
|
101
|
-
|
|
102
|
-
```typescript
|
|
103
|
-
await projects.list({
|
|
104
|
-
entityId: 'abc',
|
|
105
|
-
createdAt: { $gte: since },
|
|
106
|
-
$or: [{ name: { $ilike: 'owl%' } }, { tags: { $overlaps: ['pinned'] } }]
|
|
107
|
-
})
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
Every backend answers the same object the same way — that is the point of the vocabulary. An
|
|
111
|
-
operator a store cannot express raises `UnsupportedArgumentError` rather than being quietly
|
|
112
|
-
dropped.
|
|
113
|
-
|
|
114
|
-
`Sort<T>` is a bare field name (ascending) or `{ field, order: 'asc' | 'desc' }`, and `sort` takes
|
|
115
|
-
a list of them. An absent value sorts last ascending, everywhere.
|
|
116
|
-
|
|
117
|
-
### Paging is a property of the backend
|
|
118
|
-
|
|
119
|
-
`ListResult<T>` is `{ items, total, page?, size? }` — `total` always counts every match. Whether a
|
|
120
|
-
missing `size` means "everything" depends on what the store can afford:
|
|
121
|
-
|
|
122
|
-
| Backend | `list(where)` with no `size` |
|
|
123
|
-
|---|---|
|
|
124
|
-
| mongo, postgres | a page of `DEFAULT_PAGE_SIZE` (100) — an unbounded read of a growing table is an incident waiting to happen |
|
|
125
|
-
| redis, static, client-resource, config, state | every match — answering the criteria already read the whole namespace, so an implied window would only hide records without saving work |
|
|
126
|
-
|
|
127
|
-
`size: 0` means **no limit** on every backend — the explicit, greppable way to ask for a whole
|
|
128
|
-
result set. `page` without `size` against an unpaged backend throws
|
|
129
|
-
`UnsupportedArgumentError('page-without-size')`: there is no implied default to take a window of.
|
|
130
|
-
|
|
131
|
-
### The shared in-memory engine
|
|
132
|
-
|
|
133
|
-
Every store without a query engine of its own — redis after its SCAN, the client and static
|
|
134
|
-
stores, state — filters through the same helpers, so a criteria object written for an endpoint
|
|
135
|
-
selects the same records when a screen applies it locally:
|
|
136
|
-
|
|
137
|
-
| Helper | Answers |
|
|
138
|
-
|---|---|
|
|
139
|
-
| `matchCriteria(record, where)` | does this one record match |
|
|
140
|
-
| `filterRecords(records, where)` | every match, in insertion order |
|
|
141
|
-
| `sortRecords(records, sort)` | a sorted copy |
|
|
142
|
-
| `firstMatch(records, where, { sort })` | the record `load(where)` / `get(where)` return |
|
|
143
|
-
| `applyQuery(records, where, opts)` | the whole `ListResult` — filter, sort and page in one call |
|
|
144
|
-
|
|
145
|
-
Reach for them when implementing a backend, and when narrowing a list already in hand rather than
|
|
146
|
-
going back to the store.
|
|
147
|
-
|
|
148
|
-
### Optional capabilities
|
|
149
|
-
|
|
150
|
-
A concrete resource interface composes the capabilities its backend actually has beside
|
|
151
|
-
`Resource<T>`; a consumer types the **resource** once and gets exactly those methods.
|
|
152
|
-
|
|
153
|
-
| Interface | Surface |
|
|
154
|
-
|---|---|
|
|
155
|
-
| `PubSubResource<T>` | `publish(value, channel?)` · `subscribe(handler, { channel?, once?, ttl? })` |
|
|
156
|
-
| `WatchableResource<T>` | `watch(id, handler, { once?, ttl? })` — changes to ONE record |
|
|
157
|
-
| `StreamResource<T>` | `stream(key, value)` · `consume(key, { group?, consumer?, block? })` |
|
|
158
|
-
| `LockableResource<T>` | `lock`/`unlock` — field level encryption at rest |
|
|
159
|
-
| `MigratableResource<Tx, Self>` | `migration(name, apply, stage?)` · `migrations()` |
|
|
160
|
-
|
|
161
|
-
## The migration framework
|
|
162
|
-
|
|
163
|
-
Migrations are **automatic on app setup**: a backend runs them inside the resource's
|
|
164
|
-
`init()` — registering one is all an app ever does. Every migration self-describes when it
|
|
165
|
-
applies: the registry skips names the store has recorded, `baseline` covers structures
|
|
166
|
-
created already at head, and bodies are written idempotent so an interrupted run can repeat.
|
|
167
|
-
|
|
168
|
-
Adding migration support to a new database backend:
|
|
169
|
-
|
|
170
|
-
1. Extend the concrete resource interface with `MigratableResource<YourTx>` and design the
|
|
171
|
-
`Tx` façade a migration receives.
|
|
172
|
-
2. Keep registrations in a **module-scope declaration keyed by alias** (see either
|
|
173
|
-
implementation's `declarations.ts`) — a maker that runs more than once for the same alias
|
|
174
|
-
(a custom maker, a test) then re-declares the same entries and loses nothing, where a
|
|
175
|
-
registry held on the resource object would silently lose data transformations.
|
|
176
|
-
3. Implement `MigrationStore` over a durable ledger when the database can store one
|
|
177
|
-
(`_owlmeans_migrations` in both mongo and postgres); a store-less backend may run
|
|
178
|
-
migrations unconditionally if its bodies are self-checking.
|
|
179
|
-
4. In the resource's `init()`: probe structure → absent: `runMigrations(..., { baseline: true })`;
|
|
180
|
-
present: run `Pre` → reconcile structure → run `Post`.
|
|
181
|
-
5. Make `run` atomic with the ledger write where the database allows it (postgres: same
|
|
182
|
-
transaction). Where it doesn't (mongo: no multi-doc transactions on standalone), use
|
|
183
|
-
claim-then-complete on a unique `(alias, name)` key and require idempotent bodies.
|
|
97
|
+
So `get` only what the REQUEST addressed. A missing row it did not name — a configuration record,
|
|
98
|
+
a session, an internal lookup — is not the caller's 404: `load` it and throw the fault or the
|
|
99
|
+
refusal that fits (an auth path throws `AuthorizationError`).
|
|
184
100
|
|
|
185
101
|
## Depends On
|
|
186
102
|
|
|
187
103
|
- `@owlmeans/context` — service/resource lifecycle
|
|
188
|
-
- `@
|
|
104
|
+
- `@owlmeans/error` — `ResilientError`, the base every error class above extends
|
|
105
|
+
- peer `ajv` — `createListSchema` builds a `JSONSchemaType`; install it when you use that schema
|
|
106
|
+
|
|
107
|
+
The migration framework checksums bodies with `@noble/hashes` and `@scure/base`, and the package
|
|
108
|
+
declares neither. Install both alongside it in any project that loads `createMigrationRegistry` or
|
|
109
|
+
`runMigrations`, or the import fails at load time.
|
|
189
110
|
|
|
190
111
|
## Related
|
|
191
112
|
|
|
192
113
|
- [[mongo-resource]] · [[postgres-resource]] — the two `MigratableResource` implementations
|
|
193
114
|
- [[redis-resource]] — a non-migratable backend (pub/sub capability instead)
|
|
115
|
+
|
|
116
|
+
## Parts
|
|
117
|
+
|
|
118
|
+
Load a part on top of this core when its job is in play.
|
|
119
|
+
|
|
120
|
+
| Part | Load it when |
|
|
121
|
+
|---|---|
|
|
122
|
+
| [criteria](parts/criteria.md) | Writing a `where` (operators, `$or`, nested keys), a `sort`, or paging a `list` (`size`, `page`, `size: 0`) |
|
|
123
|
+
| [memory-engine](parts/memory-engine.md) | Implementing a store without a query engine, or narrowing a list already in hand (`recordQueryHelper`) |
|
|
124
|
+
| [capabilities](parts/capabilities.md) | Typing a resource with pub/sub, watch, stream, lock or migration capabilities |
|
|
125
|
+
| [migrations](parts/migrations.md) | Registering a migration or adding migration support to a database backend |
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: capabilities
|
|
3
|
+
description: The optional capability interfaces a concrete resource composes beside Resource (pub/sub, watch, stream, lock, migrations).
|
|
4
|
+
metadata:
|
|
5
|
+
purposes: data
|
|
6
|
+
mentions: PubSubResource, WatchableResource, StreamResource, LockableResource, MigratableResource, SubscribeOptions
|
|
7
|
+
---
|
|
8
|
+
<!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
|
|
9
|
+
|
|
10
|
+
# @owlmeans/resource — Optional capabilities
|
|
11
|
+
|
|
12
|
+
## Optional capabilities
|
|
13
|
+
|
|
14
|
+
A concrete resource interface composes the capabilities its backend actually has beside
|
|
15
|
+
`Resource<T>`; a consumer types the **resource** once and gets exactly those methods.
|
|
16
|
+
|
|
17
|
+
| Interface | Surface |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `PubSubResource<T>` | `publish(value, channel?)` · `subscribe(handler, { channel?, once?, ttl? })` |
|
|
20
|
+
| `WatchableResource<T>` | `watch(id, handler, { once?, ttl? })` — changes to ONE record |
|
|
21
|
+
| `StreamResource<T>` | `stream(key, value)` · `consume(key, { group?, consumer?, block? })` |
|
|
22
|
+
| `LockableResource<T>` | `lock`/`unlock` — field level encryption at rest |
|
|
23
|
+
| `MigratableResource<Tx, Self>` | `migration(name, apply, stage?)` · `migrations()` |
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: criteria
|
|
3
|
+
description: The criteria language (equality, any-of arrays, operators, branches, nested keys), sort and where absent values land, and paging per backend.
|
|
4
|
+
metadata:
|
|
5
|
+
purposes: query, list
|
|
6
|
+
mentions: Criteria, FieldCriteria, FieldOperators, $or, $in, $ilike, $contains, $overlaps, Sort, ListOptions, ListResult, DEFAULT_PAGE_SIZE, page-without-size
|
|
7
|
+
---
|
|
8
|
+
<!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
|
|
9
|
+
|
|
10
|
+
# @owlmeans/resource — Criteria, sort and paging
|
|
11
|
+
|
|
12
|
+
## The criteria language
|
|
13
|
+
|
|
14
|
+
`Criteria<T>` is keyed by the record's own fields, so a typo is a compile error. A dotted key
|
|
15
|
+
reaches into a nested value (or a jsonb column) and stays open.
|
|
16
|
+
|
|
17
|
+
| Written as | Means |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `{ status: 'open' }` | equality |
|
|
20
|
+
| `{ status: ['open', 'done'] }` | **any of these** — a bare array is never exact-array equality (that is `{ $eq: [...] }`) |
|
|
21
|
+
| `{ archivedAt: null }` | the absence of a value |
|
|
22
|
+
| `{ status: undefined }` | **skipped** — an untouched filter must never empty a list |
|
|
23
|
+
| `{ 'profile.city': 'Kraków' }` | reach into a nested value or a jsonb column |
|
|
24
|
+
|
|
25
|
+
Operators go in an object under the field: `$eq $ne $gt $gte $lt $lte $in $nin $exists $null
|
|
26
|
+
$like $ilike $regex $startsWith $endsWith $between $contains $contained $overlaps`. Branches
|
|
27
|
+
combine with `$and` / `$or` / `$not`, each taking criteria of the same shape.
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
await projects.list({
|
|
31
|
+
entityId: 'abc',
|
|
32
|
+
createdAt: { $gte: since },
|
|
33
|
+
$or: [{ name: { $ilike: 'owl%' } }, { tags: { $overlaps: ['pinned'] } }]
|
|
34
|
+
})
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Every backend answers the same object the same way — that is the point of the vocabulary. An
|
|
38
|
+
operator a store cannot express raises `UnsupportedArgumentError` rather than being quietly
|
|
39
|
+
dropped.
|
|
40
|
+
|
|
41
|
+
`Sort<T>` is a bare field name (ascending) or `{ field, order: 'asc' | 'desc' }`, and `sort` takes
|
|
42
|
+
a list of them. Where an **absent** value lands is the store's own rule, not the vocabulary's: the
|
|
43
|
+
in-memory sorter puts it last ascending, Postgres emits a bare `ASC` and so leaves it last (the
|
|
44
|
+
server's `NULLS LAST` default), and Mongo hands the sort document to the driver untouched, so BSON
|
|
45
|
+
ordering puts null and missing first. Sort on a field the schema requires when the position of an
|
|
46
|
+
absent value matters.
|
|
47
|
+
|
|
48
|
+
## Paging is a property of the backend
|
|
49
|
+
|
|
50
|
+
`ListResult<T>` is `{ items, total, page?, size? }` — `total` always counts every match. Whether a
|
|
51
|
+
missing `size` means "everything" depends on what the store can afford:
|
|
52
|
+
|
|
53
|
+
| Backend | `list(where)` with no `size` |
|
|
54
|
+
|---|---|
|
|
55
|
+
| mongo, postgres | a page of `DEFAULT_PAGE_SIZE` (100) — an unbounded read of a growing table is an incident waiting to happen |
|
|
56
|
+
| redis, static, client-resource, config, state | every match — answering the criteria already read the whole namespace, so an implied window would only hide records without saving work |
|
|
57
|
+
|
|
58
|
+
`size: 0` means **no limit** on every backend — the explicit, greppable way to ask for a whole
|
|
59
|
+
result set. `page` without `size` against an unpaged backend throws
|
|
60
|
+
`UnsupportedArgumentError('page-without-size')`: there is no implied default to take a window of.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: memory-engine
|
|
3
|
+
description: recordQueryHelper, the shared in-memory engine every store without a query engine of its own filters, sorts and pages through.
|
|
4
|
+
metadata:
|
|
5
|
+
purposes: test
|
|
6
|
+
mentions: recordQueryHelper, matchCriteria, filterRecords, sortRecords, firstMatch, applyQuery
|
|
7
|
+
---
|
|
8
|
+
<!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
|
|
9
|
+
|
|
10
|
+
# @owlmeans/resource — The shared in-memory engine
|
|
11
|
+
|
|
12
|
+
## The shared in-memory engine
|
|
13
|
+
|
|
14
|
+
Every store without a query engine of its own — redis after its SCAN, the client and static stores,
|
|
15
|
+
state — filters through the same helper, `recordQueryHelper`, so a criteria object written for an
|
|
16
|
+
endpoint selects the same records when a screen applies it locally:
|
|
17
|
+
|
|
18
|
+
| Member | Answers |
|
|
19
|
+
|---|---|
|
|
20
|
+
| `recordQueryHelper.matchCriteria(record, where)` | does this one record match |
|
|
21
|
+
| `recordQueryHelper.filterRecords(records, where)` | every match, in insertion order |
|
|
22
|
+
| `recordQueryHelper.sortRecords(records, sort)` | a sorted copy |
|
|
23
|
+
| `recordQueryHelper.firstMatch(records, where, { sort })` | the record `load(where)` / `get(where)` return |
|
|
24
|
+
| `recordQueryHelper.applyQuery(records, where, opts)` | the whole `ListResult` — filter, sort and page in one call |
|
|
25
|
+
|
|
26
|
+
Reach for them when implementing a backend, and when narrowing a list already in hand rather than
|
|
27
|
+
going back to the store.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: migrations
|
|
3
|
+
description: The migration framework — automatic on init — and the five steps of adding migration support to a new database backend.
|
|
4
|
+
metadata:
|
|
5
|
+
purposes: migration
|
|
6
|
+
mentions: MigratableResource, MigrationStore, createMigrationRegistry, runMigrations, MigrationStage, mongoDeclarationHelper, pgDeclarationHelper, _owlmeans_migrations
|
|
7
|
+
---
|
|
8
|
+
<!-- AUTO-GENERATED — do not edit. Regenerate via sync-agent-meta. -->
|
|
9
|
+
|
|
10
|
+
# @owlmeans/resource — Migrations
|
|
11
|
+
|
|
12
|
+
## The migration framework
|
|
13
|
+
|
|
14
|
+
Migrations are **automatic on app setup**: a backend runs them inside the resource's
|
|
15
|
+
`init()` — registering one is all an app ever does. Every migration self-describes when it
|
|
16
|
+
applies: the registry skips names the store has recorded, `baseline` covers structures
|
|
17
|
+
created already at head, and bodies are written idempotent so an interrupted run can repeat.
|
|
18
|
+
|
|
19
|
+
Adding migration support to a new database backend:
|
|
20
|
+
|
|
21
|
+
1. Extend the concrete resource interface with `MigratableResource<YourTx>` and design the
|
|
22
|
+
`Tx` façade a migration receives.
|
|
23
|
+
2. Keep registrations in a **module-scope declaration keyed by alias** — the shape mongo and
|
|
24
|
+
postgres both expose as `mongoDeclarationHelper` / `pgDeclarationHelper` with
|
|
25
|
+
`.getDeclaration(alias)` / `.resetDeclarations(alias?)`. A maker that runs more than once for the
|
|
26
|
+
same alias (a custom maker, a test) then re-declares the same entries and loses nothing, where a
|
|
27
|
+
registry held on the resource object would silently lose data transformations.
|
|
28
|
+
3. Implement `MigrationStore` over a durable ledger when the database can store one
|
|
29
|
+
(`_owlmeans_migrations` in both mongo and postgres); a store-less backend may run
|
|
30
|
+
migrations unconditionally if its bodies are self-checking.
|
|
31
|
+
4. In the resource's `init()`: probe structure → absent: `runMigrations(..., { baseline: true })`;
|
|
32
|
+
present: run `Pre` → reconcile structure → run `Post`.
|
|
33
|
+
5. Make `run` atomic with the ledger write where the database allows it (postgres: same
|
|
34
|
+
transaction). Where it doesn't (mongo: no multi-doc transactions on standalone), use
|
|
35
|
+
claim-then-complete on a unique `(alias, name)` key and require idempotent bodies.
|