@owlmeans/mongo-resource 0.1.18-rc.2 → 0.1.18-rc.20
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 +51 -16
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/mongo-resource/SKILL.md +80 -30
- package/build/consts.d.ts +8 -4
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +8 -4
- package/build/consts.js.map +1 -1
- package/build/declarations.d.ts.map +1 -1
- package/build/declarations.js +5 -6
- package/build/declarations.js.map +1 -1
- package/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -0
- package/build/index.js.map +1 -1
- package/build/resource.d.ts +2 -2
- package/build/resource.d.ts.map +1 -1
- package/build/resource.js +91 -118
- package/build/resource.js.map +1 -1
- package/build/types.d.ts +16 -5
- package/build/types.d.ts.map +1 -1
- package/build/utils/criteria.d.ts +24 -0
- package/build/utils/criteria.d.ts.map +1 -0
- package/build/utils/criteria.js +221 -0
- package/build/utils/criteria.js.map +1 -0
- package/build/utils/index.d.ts +1 -0
- package/build/utils/index.d.ts.map +1 -1
- package/build/utils/index.js +1 -0
- package/build/utils/index.js.map +1 -1
- package/build/utils/migrations.d.ts.map +1 -1
- package/build/utils/migrations.js +16 -1
- package/build/utils/migrations.js.map +1 -1
- package/build/utils/refs.d.ts +5 -3
- package/build/utils/refs.d.ts.map +1 -1
- package/build/utils/refs.js +4 -1
- package/build/utils/refs.js.map +1 -1
- package/package.json +6 -6
- package/src/consts.ts +8 -4
- package/src/declarations.ts +5 -6
- package/src/index.ts +1 -0
- package/src/resource.ts +115 -144
- package/src/types.ts +17 -5
- package/src/utils/criteria.ts +248 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/migrations.ts +17 -2
- package/src/utils/refs.ts +8 -6
- package/tests/criteria.spec.ts +114 -0
- package/build/.gitkeep +0 -0
package/README.md
CHANGED
|
@@ -4,9 +4,9 @@ MongoDB-backed `Resource<T>` implementation — the primary database resource fo
|
|
|
4
4
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
|
-
- `makeMongoResource<R, T>(alias, dbAlias?, serviceAlias?,
|
|
7
|
+
- `makeMongoResource<R, T>(alias, dbAlias?, serviceAlias?, collectionName?)` — factory for MongoDB resources
|
|
8
8
|
- `MongoResource<T>` — extends `Resource<T>` with MongoDB collection, indexing, field encryption, code migrations and ObjectId references
|
|
9
|
-
- Supports CRUD,
|
|
9
|
+
- Supports CRUD, criteria queries, sorting and paging, AJV schema validation ($jsonSchema collection validators), and field-level locking (encryption)
|
|
10
10
|
- Declared references convert between the string ids records carry and the `ObjectId`s the collection stores — the same way `_id` already does
|
|
11
11
|
- Code migrations run automatically at resource initialization, tracked in a per-database `_owlmeans_migrations` ledger
|
|
12
12
|
- Used for all persistent data models in server applications
|
|
@@ -14,7 +14,7 @@ MongoDB-backed `Resource<T>` implementation — the primary database resource fo
|
|
|
14
14
|
## Installation
|
|
15
15
|
|
|
16
16
|
```bash
|
|
17
|
-
bun add @owlmeans/mongo-resource
|
|
17
|
+
bun add @owlmeans/mongo-resource@^0.1.18-rc.11
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
## Usage
|
|
@@ -29,9 +29,7 @@ import type { ResourceMaker } from '@owlmeans/resource'
|
|
|
29
29
|
export interface ProjectResource extends MongoResource<ProjectRecord> {}
|
|
30
30
|
|
|
31
31
|
export const makeProjectResource: ResourceMaker<ProjectRecord, ProjectResource> = (dbAlias, serviceAlias) => {
|
|
32
|
-
const resource = makeMongoResource<ProjectRecord>(
|
|
33
|
-
RES_PROJECT, dbAlias, serviceAlias, makeProjectResource
|
|
34
|
-
)
|
|
32
|
+
const resource = makeMongoResource<ProjectRecord, ProjectResource>(RES_PROJECT, dbAlias, serviceAlias)
|
|
35
33
|
resource.schema = ProjectSchema
|
|
36
34
|
resource.index('entity', { entityId: 1 })
|
|
37
35
|
resource.index('alias', { alias: 1 })
|
|
@@ -50,29 +48,55 @@ Use in a handler:
|
|
|
50
48
|
```typescript
|
|
51
49
|
const projects = context.resource<ProjectResource>(RES_PROJECT)
|
|
52
50
|
const record = await projects.create({ entityId, alias, title })
|
|
53
|
-
const
|
|
51
|
+
const one = await projects.load({ entityId, alias })
|
|
52
|
+
const page = await projects.list({ entityId }, { page: 0, size: 20, sort: ['createdAt'] })
|
|
53
|
+
const open = await projects.count({ status: ['draft', 'active'] })
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
## API
|
|
57
57
|
|
|
58
|
-
### `makeMongoResource<R, T>(alias, dbAlias?, serviceAlias?,
|
|
58
|
+
### `makeMongoResource<R, T>(alias, dbAlias?, serviceAlias?, collectionName?): T`
|
|
59
59
|
|
|
60
60
|
Creates a MongoDB resource. `dbAlias` defaults to `DEFAULT_DB_ALIAS` (`'mongo'`).
|
|
61
61
|
`collectionName` overrides the physical collection name (otherwise `resourcePrefix + alias`).
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
`migration()` and `reference()` are kept in module-scope declarations keyed by alias, so a
|
|
63
|
+
maker that runs more than once for the same alias re-declares the same entries and loses
|
|
64
|
+
nothing.
|
|
64
65
|
|
|
65
66
|
### `MongoResource<T>`
|
|
66
67
|
|
|
67
|
-
Extends `Resource<T>`
|
|
68
|
+
Extends `Resource<T>` with the shared `MigratableResource<MongoTx, MongoResource<T>>` and
|
|
69
|
+
`LockableResource<T>` capabilities, plus:
|
|
68
70
|
- `collection: Collection` — MongoDB collection
|
|
69
71
|
- `db(): Promise<Db>` / `client(): Promise<MongoClient>`
|
|
70
72
|
- `index(name, spec, options?): this` — define a collection index
|
|
71
73
|
- `reference(field, targetAlias?): this` / `references()` — declare that a field stores another record's id (see below)
|
|
72
|
-
- `migration(name, apply, stage?)
|
|
74
|
+
- `migration(name, apply, stage?)` / `migrations()` — register a code migration (see below)
|
|
73
75
|
- `lock(record, fields?)` / `unlock(record, fields?)` — encrypt/decrypt secure fields
|
|
74
76
|
- `getDefaults(): Partial<T>` — default values derived from schema
|
|
75
77
|
|
|
78
|
+
### Criteria, sorting and paging
|
|
79
|
+
|
|
80
|
+
`Criteria<T>` is the portable query shape from [`@owlmeans/resource`](../resource): a bare value
|
|
81
|
+
is equality, a bare array is "any of these", `null` asks for the absence of a value and
|
|
82
|
+
`undefined` is skipped so an untouched filter never empties a list. Every operator in the shared
|
|
83
|
+
vocabulary — `$eq $ne $gt $gte $lt $lte $in $nin $exists $null $like $ilike $regex $startsWith
|
|
84
|
+
$endsWith $between $contains $contained $overlaps`, plus `$and`/`$or`/`$not` — is translated into
|
|
85
|
+
the mongo expression that answers the same question, so one criteria object means the same thing
|
|
86
|
+
here as it does against Postgres or an in-memory store. `$exists` and `$null` both ask whether the
|
|
87
|
+
field *has a value*, not whether the key is present. An operator outside the vocabulary raises
|
|
88
|
+
`UnsupportedArgumentError`.
|
|
89
|
+
|
|
90
|
+
Paging is per call: `list(where, { page, size, sort })`. Mongo pages by default —
|
|
91
|
+
`DEFAULT_PAGE_SIZE` records when no `size` is given — because an unbounded read of a collection is
|
|
92
|
+
not something a caller should get by omission. `list(where, { size: 0 })` asks for the whole
|
|
93
|
+
result set explicitly. `ListResult.total` is always filled; `page` and `size` come back only when
|
|
94
|
+
a limit was applied. `sort` takes field names (ascending) or `{ field, order: 'desc' }`, and `id`
|
|
95
|
+
addresses `_id`.
|
|
96
|
+
|
|
97
|
+
`criteriaToFilter(criteria, refs)` and `sortToMongo(sort)` are exported for code driving
|
|
98
|
+
`resource.collection` directly.
|
|
99
|
+
|
|
76
100
|
### ObjectId references
|
|
77
101
|
|
|
78
102
|
`reference(field, targetAlias?)` declares that a record field references another record's id.
|
|
@@ -100,7 +124,7 @@ The resource then treats the field exactly like `_id`:
|
|
|
100
124
|
|
|
101
125
|
`migration(name, apply, stage?)` registers a code migration, applied once per database in
|
|
102
126
|
declaration order and recorded in the `_owlmeans_migrations` collection (one ledger per
|
|
103
|
-
database
|
|
127
|
+
database, so each database tracks its own).
|
|
104
128
|
|
|
105
129
|
- `MigrationStage.Pre` runs before the validator/index update, `Post` after. On a collection
|
|
106
130
|
created by this very boot, registered migrations are **baselined** (recorded, not run).
|
|
@@ -112,12 +136,23 @@ database — an Entity-layer database tracks its own).
|
|
|
112
136
|
|
|
113
137
|
### `Resource<T>` methods (all implemented)
|
|
114
138
|
|
|
115
|
-
`get`, `load`, `create`, `update`, `save`, `delete`, `
|
|
139
|
+
`get`, `load`, `list`, `count`, `create`, `update`, `save`, `delete`, `take`, `purge`
|
|
140
|
+
|
|
141
|
+
- `get`/`load` take either an id or a `Criteria<T>` with an optional `{ sort }`; `get` throws
|
|
142
|
+
`UnknownRecordError` where `load` answers `null`. An id that is not a mongo id finds nothing
|
|
143
|
+
rather than raising a driver error.
|
|
144
|
+
- `update` replaces the whole record addressed by its `id`; `save` creates when the record
|
|
145
|
+
carries no id and replaces otherwise.
|
|
146
|
+
- `delete(id)` and `take(id)` remove atomically through `findOneAndDelete` and hand the record
|
|
147
|
+
back — `take` throws `UnknownRecordError` on absence where `delete` answers `null`.
|
|
148
|
+
- `purge(where)` is the bulk delete; it refuses an empty criteria object rather than emptying
|
|
149
|
+
the collection.
|
|
150
|
+
- `ttl` is refused with `UnsupportedArgumentError` — a collection has no per-record expiry.
|
|
116
151
|
|
|
117
152
|
### Constants
|
|
118
153
|
|
|
119
154
|
- `DEFAULT_DB_ALIAS` — `'mongo'`
|
|
120
|
-
- `DEFAULT_PAGE_SIZE` — `
|
|
155
|
+
- `DEFAULT_PAGE_SIZE` — `100`
|
|
121
156
|
- `DEF_MIGRATIONS_COLLECTION` — `'_owlmeans_migrations'`
|
|
122
157
|
|
|
123
158
|
## Related Packages
|
|
@@ -133,7 +168,7 @@ This package ships embedded agent skills under `agent-meta/`. After installing y
|
|
|
133
168
|
your project's skill store (`.agents/skills/`):
|
|
134
169
|
|
|
135
170
|
```sh
|
|
136
|
-
npx @owlmeans/agent-skills
|
|
171
|
+
npx @owlmeans/agent-skills@^0.1.18-rc.20
|
|
137
172
|
```
|
|
138
173
|
|
|
139
174
|
The embedded files are version-matched to this package release. Do not edit them
|
package/agent-meta/manifest.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schemaVersion": 2,
|
|
3
3
|
"package": "@owlmeans/mongo-resource",
|
|
4
|
-
"version": "0.1.18-rc.
|
|
5
|
-
"generatedAt": "2026-
|
|
4
|
+
"version": "0.1.18-rc.20",
|
|
5
|
+
"generatedAt": "2026-09-12T14:21:25.463Z",
|
|
6
6
|
"canonicalRepo": "https://github.com/owlmeans/common",
|
|
7
7
|
"entries": [
|
|
8
8
|
{
|
|
@@ -8,7 +8,7 @@ user-invocable: false
|
|
|
8
8
|
# @owlmeans/mongo-resource
|
|
9
9
|
|
|
10
10
|
**Layer:** Infra
|
|
11
|
-
**Install:** `"@owlmeans/mongo-resource": "^0.1.18-rc.
|
|
11
|
+
**Install:** `"@owlmeans/mongo-resource": "^0.1.18-rc.20"` in `dependencies` (peers `mongodb`, `ajv`)
|
|
12
12
|
|
|
13
13
|
The Mongo counterpart of [[postgres-resource]]. A collection has no structure of its own, so
|
|
14
14
|
here the resource layer owns the *validator* (`$jsonSchema` from the AJV schema), the indexes,
|
|
@@ -19,16 +19,21 @@ reference**.
|
|
|
19
19
|
|
|
20
20
|
| Export | Description |
|
|
21
21
|
|--------|-------------|
|
|
22
|
-
| `makeMongoResource<R, T>(alias, dbAlias?, serviceAlias?,
|
|
23
|
-
| `MongoResource<T>` | `Resource<T>` + `collection`, `index`, `reference`/`references`, `migration`/`migrations` (the shared `MigratableResource` capability), `lock`/`unlock`, `getDefaults
|
|
22
|
+
| `makeMongoResource<R, T>(alias, dbAlias?, serviceAlias?, collectionName?)` | The resource factory. Aliases default to `DEFAULT_DB_ALIAS` (`'mongo'`). `collectionName` overrides the collection (else `resourcePrefix + alias`). |
|
|
23
|
+
| `MongoResource<T>` | `Resource<T>` + `collection`, `db()`/`client()`, `index`/`indexes`, `reference`/`references`, `migration`/`migrations` (the shared `MigratableResource` capability), `lock`/`unlock`, `getDefaults`, and the `dbAlias`/`serviceAlias` it was registered against. Anything naming a collection on another resource's behalf reads *that* resource's `dbAlias`, since two resources in one database can carry different `resourcePrefix`es. |
|
|
24
24
|
| `MongoDbService`, `MongoTx` | Service contract implemented by `@owlmeans/mongo`; the façade handed to migrations (`db`, `collection`, `use(alias)`, `ref(alias)`). |
|
|
25
25
|
| `MongoReference`, `MongoRefOptions` | A declared ObjectId reference and the `reference()` options (`resource`, `noIndex`). |
|
|
26
|
-
| `marshalReference`, `demarshalReference`, `marshalCriteria`, `identityCriteria`, `isObjectIdHex` | The conversion layer — reuse these wherever raw driver access bypasses the resource. |
|
|
27
|
-
| `
|
|
26
|
+
| `marshalReference`, `demarshalReference`, `demarshalRefs`, `marshalCriteria`, `identityCriteria`, `isObjectIdHex` | The conversion layer — reuse these wherever raw driver access bypasses the resource. |
|
|
27
|
+
| `criteriaToFilter`, `sortToMongo` | `Criteria<T>` → a mongo filter (references converted, every shared operator rewritten into a mongo expression) and `Sort<T>` → a mongo sort spec. |
|
|
28
|
+
| `convertReferenceField`, `makeRefMigration`, `reconcileReferences`, `refMigrationName` | The system reference migration's machinery. |
|
|
28
29
|
| `makeMongoTx`, `makeMongoMigrationStore` | The migration store (ledger) implementation. |
|
|
29
|
-
| `getDeclaration`, `resetDeclarations` | Module-scope migration/reference declarations, keyed by alias. |
|
|
30
|
-
| `
|
|
31
|
-
| `DEFAULT_DB_ALIAS`, `DEFAULT_PAGE_SIZE`, `DEF_MIGRATIONS_COLLECTION` | Constants. |
|
|
30
|
+
| `getDeclaration`, `resetDeclarations`, `MongoDeclaration` | Module-scope migration/reference declarations, keyed by alias; `resetDeclarations(alias?)` is the testing seam. |
|
|
31
|
+
| `getSchemaSecureFeilds` | The `secure: true` schema properties `lock`/`unlock` operate on when the caller names no fields. |
|
|
32
|
+
| `DEFAULT_DB_ALIAS`, `DEFAULT_PAGE_SIZE`, `DEF_MIGRATIONS_COLLECTION`, `DEF_MIGRATION_WAIT`, `DEF_MIGRATION_POLL`, `MONGO_DUPLICATE_KEY` | Constants. |
|
|
33
|
+
|
|
34
|
+
Validator compilation and collection naming stay inside the package — the entry point exports
|
|
35
|
+
nothing to reach them with, so a consumer shapes a collection by assigning `resource.schema` and
|
|
36
|
+
declaring indexes, never by calling the compiler.
|
|
32
37
|
|
|
33
38
|
## Usage — the maker pattern
|
|
34
39
|
|
|
@@ -36,7 +41,7 @@ reference**.
|
|
|
36
41
|
export const makeProjectStoryResource: ResourceMaker<ProjectStoryRecord, ProjectStoryResource> =
|
|
37
42
|
(dbAlias, serviceAlias) => {
|
|
38
43
|
const resource = makeMongoResource<ProjectStoryRecord, ProjectStoryResource>(
|
|
39
|
-
RES_PROJECT_STORY, dbAlias, serviceAlias
|
|
44
|
+
RES_PROJECT_STORY, dbAlias, serviceAlias
|
|
40
45
|
)
|
|
41
46
|
resource.schema = ProjectStorySchema
|
|
42
47
|
resource.reference('projectId', RES_PROJECT)
|
|
@@ -48,10 +53,9 @@ export const makeProjectStoryResource: ResourceMaker<ProjectStoryRecord, Project
|
|
|
48
53
|
context.registerResource(makeProjectStoryResource())
|
|
49
54
|
```
|
|
50
55
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
silently loses a data transformation.
|
|
56
|
+
`migration()` and `reference()` live in module-scope declarations keyed by alias, so a maker
|
|
57
|
+
that runs more than once for the same alias (a custom maker, a test) re-declares the same
|
|
58
|
+
entries and loses nothing — losing one would silently lose a data transformation.
|
|
55
59
|
|
|
56
60
|
## ObjectId references
|
|
57
61
|
|
|
@@ -59,18 +63,18 @@ A field that stores **another record's id** is declared with `reference(field, t
|
|
|
59
63
|
The resource then behaves for that field exactly as it does for `_id`:
|
|
60
64
|
|
|
61
65
|
- **Records and criteria carry strings; the collection stores `ObjectId`s.** Conversion is
|
|
62
|
-
automatic in `create`/`update`/`save` (write), `get`/`load`/`list`/`delete`/`
|
|
63
|
-
lookup), and in
|
|
64
|
-
`$
|
|
65
|
-
alone.
|
|
66
|
+
automatic in `create`/`update`/`save` (write), `get`/`load`/`list`/`count`/`delete`/`take`/
|
|
67
|
+
`purge` (read and lookup), and in every criteria object — including operator objects,
|
|
68
|
+
`$and`/`$or`/`$not` branches, and arrays of ids (elementwise). `$regex`-style operands are
|
|
69
|
+
left alone.
|
|
66
70
|
- **Writes are strict**: storing a non-24-hex value in a declared reference throws
|
|
67
71
|
`MisshapedRecord('ref:<field>')` — a silent string would reintroduce the mixed-type state.
|
|
68
72
|
Reads and criteria are tolerant: an unconverted legacy string comes back as-is; a non-id
|
|
69
73
|
criteria value simply matches nothing (the auth `userId ?? profileId` fallback relies on
|
|
70
74
|
this).
|
|
71
75
|
- **`id` criteria address `_id`.** Documents never store an `id` field, so `list({ id })` and
|
|
72
|
-
`load(
|
|
73
|
-
|
|
76
|
+
`load(id)` map onto `_id` with conversion; a criteria key naming a declared reference converts
|
|
77
|
+
the same way.
|
|
74
78
|
- **The field is indexed** automatically (`ref_<field>`), unless `noIndex: true` or the
|
|
75
79
|
resource already declares an index with the identical key pattern (mongo forbids two
|
|
76
80
|
indexes over the same keys; a declared unique index wins).
|
|
@@ -96,7 +100,10 @@ version suffix** in `refMigrationName`, or every already-applied ledger raises
|
|
|
96
100
|
|
|
97
101
|
Only fields assigned from another record's `.id` qualify. Known traps from the live codebase:
|
|
98
102
|
|
|
99
|
-
- `entityId` / `
|
|
103
|
+
- `entityId` / `entitySlug` — the **organization entity**, never a document in this database.
|
|
104
|
+
`entitySlug` is the renameable name that travels on tokens and URLs; `entityId` is the stable id
|
|
105
|
+
the organization registry minted for it, and a deployment with no resolver stores the slug in the
|
|
106
|
+
same field. Neither is an `ObjectId`, and the field carries both shapes across deployments
|
|
100
107
|
- `profileId` — composite key `"{type}:{accountId}"`; `credentials.userId` — external
|
|
101
108
|
provider key `"{type}:{service}:{sub}"` (while `profile.userId` **is** a reference)
|
|
102
109
|
- Stripe ids (`externalId`, `productId`, `taxId`), Cloudflare `providerId`, GitHub numeric ids
|
|
@@ -115,10 +122,22 @@ with `marshalReference(field, value)` and convert read-back documents' reference
|
|
|
115
122
|
|
|
116
123
|
`resource.migration(name, apply, stage?)` — the shared `MigratableResource` capability from
|
|
117
124
|
[[resource]]. Applied once per database, in declaration order, ledgered in
|
|
118
|
-
`_owlmeans_migrations` (one ledger per database, so
|
|
125
|
+
`_owlmeans_migrations` (one ledger per database, so each database tracks its own).
|
|
119
126
|
|
|
120
127
|
- `Pre` runs **before** the validator is updated and indexes reconcile; `Post` after. A `Pre`
|
|
121
128
|
body writes shapes the *old* validator allows; a `Post` body the *new* one.
|
|
129
|
+
- **A `Pre` body that removes an indexed field must drop that index itself.** Indexes reconcile
|
|
130
|
+
*after* `Pre`, so the old index is still live and still enforcing while the body writes, whatever
|
|
131
|
+
the declaration now says. `$unset`ing an indexed field collapses every document onto
|
|
132
|
+
`{ <field>: null, … }`, and on a `unique` index the second one dies with E11000 — mid-migration,
|
|
133
|
+
after earlier collections were already rewritten, leaving the database half-migrated and the boot
|
|
134
|
+
aborting on every restart. Drop the stale index by name in the body first (guard on the key spec
|
|
135
|
+
so the drop is idempotent) and let reconciliation recreate it from the declaration.
|
|
136
|
+
- **Index reconciliation matches by name and recreates on any difference.** A live index whose key
|
|
137
|
+
pattern or options differ from the declaration is dropped and created again, so a renamed field
|
|
138
|
+
does converge — after `Pre`, which is why the bullet above exists. Text indexes are the one
|
|
139
|
+
exception: a live index carrying `weights` is left as it is, and changing one means dropping it
|
|
140
|
+
yourself.
|
|
122
141
|
- On a collection this boot just created, every registered migration is **baselined**
|
|
123
142
|
(recorded, not run) — a fresh collection is born at head.
|
|
124
143
|
- **No transactions**: a standalone `mongod` (the dev/CI target) rejects multi-document
|
|
@@ -140,25 +159,56 @@ with `marshalReference(field, value)` and convert read-back documents' reference
|
|
|
140
159
|
2. absent → baseline all migrations; present → run `Pre` (system `$ref:` first if declared before app migrations)
|
|
141
160
|
3. create collection (validator + indexes) or update validator + reconcile indexes
|
|
142
161
|
4. present → run `Post`
|
|
143
|
-
5. reconcile declared references (probe + repair — the double check)
|
|
162
|
+
5. present → reconcile declared references (probe + repair — the double check)
|
|
163
|
+
|
|
164
|
+
Steps 4 and 5 belong to the existing-collection path alone: a collection this boot created is
|
|
165
|
+
already at head and carries no legacy strings to repair.
|
|
144
166
|
|
|
145
167
|
## Method semantics worth remembering
|
|
146
168
|
|
|
147
169
|
| Method | Semantics |
|
|
148
170
|
|---|---|
|
|
149
|
-
| `create` | refuses a caller-supplied id (`RecordExists`) |
|
|
150
|
-
| `update` | **replaces** the whole record (no merge) |
|
|
151
|
-
| `
|
|
152
|
-
| `load
|
|
171
|
+
| `create` | refuses a caller-supplied id (`RecordExists`); rejects `opts.ttl` (`UnsupportedArgumentError`) |
|
|
172
|
+
| `update` | **replaces** the whole record (no merge), keeping the document's `_id` |
|
|
173
|
+
| `load(id)` / `get(id)` | a string that is not a 24-hex id matches nothing — `load` answers `null` and `get` throws, where handing it to `ObjectId` would raise a driver error at a call site that only asked whether the record exists |
|
|
174
|
+
| `load(where, { sort })` / `get(where, { sort })` | `findOne` with the sort applied, so "the newest matching record" is one round trip |
|
|
153
175
|
| `list` | criteria go through reference conversion; documents never store `id` — use `id` criteria freely, they map to `_id` |
|
|
176
|
+
| `delete` / `take` | one `findOneAndDelete`: the record is handed back by the very operation that removed it, so two callers can never be given the same record. `take` **deletes** and throws `UnknownRecordError` on a miss |
|
|
177
|
+
| `purge` | `deleteMany`; refuses an empty criteria object (`UnsupportedArgumentError('purge:no-criteria')`) rather than emptying the collection |
|
|
154
178
|
| `lock`/`unlock` | encrypt/decrypt `secure: true` schema fields via the db service |
|
|
155
179
|
|
|
180
|
+
## Paging
|
|
181
|
+
|
|
182
|
+
Mongo is **PAGED**: `DEFAULT_PAGE_SIZE` is `100`, so `list(where)` with no `size` returns the first
|
|
183
|
+
100 matches — a collection is unbounded and an unpaged read is an incident waiting for the document
|
|
184
|
+
count to grow. `ListResult.total` counts every match regardless of the window, and
|
|
185
|
+
`list(where, { size: 0 })` is the explicit, greppable ask for the whole result set. Asking for a
|
|
186
|
+
page while lifting the limit contradicts itself and raises
|
|
187
|
+
`UnsupportedArgumentError('page-without-size')` rather than quietly answering page 0.
|
|
188
|
+
|
|
189
|
+
## Criteria against a collection
|
|
190
|
+
|
|
191
|
+
`criteriaToFilter` rewrites the shared vocabulary ([[resource]]) into mongo expressions so one
|
|
192
|
+
criteria object selects the same records here as it does in SQL and in memory. Two rewrites are
|
|
193
|
+
worth knowing:
|
|
194
|
+
|
|
195
|
+
- **`$exists` and `$null` compare against `null`**, not mongo's own `$exists`. The shared question
|
|
196
|
+
is whether a field *has a value*, which the other stores answer as `IS NULL` / `value == null`;
|
|
197
|
+
mongo's `$exists` answers whether the key is present, and a key present but null would part the
|
|
198
|
+
three stores over one object.
|
|
199
|
+
- **`$like`/`$ilike` become anchored regular expressions** with `%` as any run and `_` as one
|
|
200
|
+
character; `$between` becomes `$gte`/`$lte`; and `$contains`/`$contained`/`$overlaps` mean over
|
|
201
|
+
an array field exactly what the postgres operators `@>`, `<@` and `&&` mean. An operator mongo
|
|
202
|
+
cannot answer raises `UnsupportedArgumentError` rather than being dropped.
|
|
203
|
+
|
|
156
204
|
## Tests
|
|
157
205
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
206
|
+
`bun test ./tests` in this package runs the conversion-layer unit specs (`criteria.spec.ts`,
|
|
207
|
+
`refs.spec.ts`) ungated, plus `crud.spec.ts`, which needs a real collection and self-skips behind
|
|
208
|
+
the same `MONGO_URL` gate as the integration suites. Specs that build a real `ServerContext` live in
|
|
209
|
+
`@owlmeans/mongo` (`crud.spec.ts`, `migration.spec.ts`, `references.spec.ts`), also gated on
|
|
210
|
+
`MONGO_URL` — see [[testing-integration]]; a dev port-forward to the cluster mongo fills the
|
|
211
|
+
`MONGO_URL` the repo's `.env.example` describes.
|
|
162
212
|
|
|
163
213
|
## Depends On
|
|
164
214
|
|
package/build/consts.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export declare const DEFAULT_DB_ALIAS = "mongo";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Page size a `list` call gets when it asks for none. Mongo cannot afford an unbounded read
|
|
4
|
+
* of a collection by omission, so paging is the default here — `list(where, { size: 0 })`
|
|
5
|
+
* asks for the whole result set, explicitly and greppably.
|
|
6
|
+
*/
|
|
7
|
+
export declare const DEFAULT_PAGE_SIZE = 100;
|
|
3
8
|
/**
|
|
4
9
|
* Collection that records which code-registered migrations have already been applied.
|
|
5
10
|
*
|
|
6
|
-
* One ledger per database, which is the right boundary:
|
|
7
|
-
*
|
|
8
|
-
* data and dropping the database drops the ledger with it.
|
|
11
|
+
* One ledger per database, which is the right boundary: it sits beside the data its
|
|
12
|
+
* migrations changed, so dropping the database drops the ledger with it.
|
|
9
13
|
*/
|
|
10
14
|
export declare const DEF_MIGRATIONS_COLLECTION = "_owlmeans_migrations";
|
|
11
15
|
/**
|
package/build/consts.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,gBAAgB,UAAU,CAAA;AAEvC,eAAO,MAAM,iBAAiB,
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,gBAAgB,UAAU,CAAA;AAEvC;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,MAAM,CAAA;AAEpC;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,yBAAyB,CAAA;AAE/D;;;GAGG;AACH,eAAO,MAAM,kBAAkB,QAAQ,CAAA;AAEvC,eAAO,MAAM,kBAAkB,MAAM,CAAA;AAErC,yFAAyF;AACzF,eAAO,MAAM,mBAAmB,QAAQ,CAAA"}
|
package/build/consts.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export const DEFAULT_DB_ALIAS = 'mongo';
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* Page size a `list` call gets when it asks for none. Mongo cannot afford an unbounded read
|
|
4
|
+
* of a collection by omission, so paging is the default here — `list(where, { size: 0 })`
|
|
5
|
+
* asks for the whole result set, explicitly and greppably.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_PAGE_SIZE = 100;
|
|
3
8
|
/**
|
|
4
9
|
* Collection that records which code-registered migrations have already been applied.
|
|
5
10
|
*
|
|
6
|
-
* One ledger per database, which is the right boundary:
|
|
7
|
-
*
|
|
8
|
-
* data and dropping the database drops the ledger with it.
|
|
11
|
+
* One ledger per database, which is the right boundary: it sits beside the data its
|
|
12
|
+
* migrations changed, so dropping the database drops the ledger with it.
|
|
9
13
|
*/
|
|
10
14
|
export const DEF_MIGRATIONS_COLLECTION = '_owlmeans_migrations';
|
|
11
15
|
/**
|
package/build/consts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAAO,CAAA;AAEvC,MAAM,CAAC,MAAM,iBAAiB,GAAG,
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"","sources":["../src/consts.ts"],"names":[],"mappings":"AACA,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAAO,CAAA;AAEvC;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,GAAG,CAAA;AAEpC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,sBAAsB,CAAA;AAE/D;;;GAGG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,CAAA;AAEvC,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAErC,yFAAyF;AACzF,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"declarations.d.ts","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAE3D,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzD,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAA;IACtC,2FAA2F;IAC3F,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;CACxC;
|
|
1
|
+
{"version":3,"file":"declarations.d.ts","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAE3D,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzD,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE,iBAAiB,CAAC,OAAO,CAAC,CAAA;IACtC,2FAA2F;IAC3F,UAAU,EAAE,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAAA;CACxC;AAaD,eAAO,MAAM,cAAc,UAAW,MAAM,KAAG,gBAQ9C,CAAA;AAED,8FAA8F;AAC9F,eAAO,MAAM,iBAAiB,WAAY,MAAM,KAAG,IAOlD,CAAA"}
|
package/build/declarations.js
CHANGED
|
@@ -2,12 +2,11 @@ import { createMigrationRegistry } from '@owlmeans/resource';
|
|
|
2
2
|
/**
|
|
3
3
|
* Per-alias migration store, held at module scope rather than on the resource object.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* by alias makes the declarations outlive any number of context switches.
|
|
5
|
+
* A maker may run more than once for the same alias — a custom maker wrapping the built-in
|
|
6
|
+
* one, a maker called again by an app or a spec. Keying the declarations by alias makes
|
|
7
|
+
* that a no-op: every run reads and extends the same registry, so nothing a caller declared
|
|
8
|
+
* by chaining onto an earlier resource object is lost. Losing a migration is silent — the
|
|
9
|
+
* data transformation simply never runs — which is why the store cannot live on the object.
|
|
11
10
|
*/
|
|
12
11
|
const declarations = new Map();
|
|
13
12
|
export const getDeclaration = (alias) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"declarations.js","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAW5D
|
|
1
|
+
{"version":3,"file":"declarations.js","sourceRoot":"","sources":["../src/declarations.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAW5D;;;;;;;;GAQG;AACH,MAAM,YAAY,GAAkC,IAAI,GAAG,EAAE,CAAA;AAE7D,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,KAAa,EAAoB,EAAE;IAChE,IAAI,WAAW,GAAG,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IACzC,IAAI,WAAW,IAAI,IAAI,EAAE,CAAC;QACxB,WAAW,GAAG,EAAE,UAAU,EAAE,uBAAuB,EAAW,EAAE,UAAU,EAAE,IAAI,GAAG,EAAE,EAAE,CAAA;QACvF,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,WAAW,CAAC,CAAA;IACtC,CAAC;IAED,OAAO,WAAW,CAAA;AACpB,CAAC,CAAA;AAED,8FAA8F;AAC9F,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,KAAc,EAAQ,EAAE;IACxD,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAClB,YAAY,CAAC,KAAK,EAAE,CAAA;QAEpB,OAAM;IACR,CAAC;IACD,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAC5B,CAAC,CAAA"}
|
package/build/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export * from './consts.js';
|
|
|
3
3
|
export * from './declarations.js';
|
|
4
4
|
export * from './utils/migrations.js';
|
|
5
5
|
export * from './utils/refs.js';
|
|
6
|
+
export * from './utils/criteria.js';
|
|
6
7
|
export * from './resource.js';
|
|
7
8
|
export * from './helper.js';
|
|
8
9
|
//# sourceMappingURL=index.d.ts.map
|
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,mBAAmB,YAAY,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA"}
|
package/build/index.js
CHANGED
|
@@ -2,6 +2,7 @@ export * from './consts.js';
|
|
|
2
2
|
export * from './declarations.js';
|
|
3
3
|
export * from './utils/migrations.js';
|
|
4
4
|
export * from './utils/refs.js';
|
|
5
|
+
export * from './utils/criteria.js';
|
|
5
6
|
export * from './resource.js';
|
|
6
7
|
export * from './helper.js';
|
|
7
8
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AACjC,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,qBAAqB,CAAA;AACnC,cAAc,eAAe,CAAA;AAC7B,cAAc,aAAa,CAAA"}
|
package/build/resource.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ResourceRecord } from '@owlmeans/resource';
|
|
2
2
|
import type { MongoResource } from './types.js';
|
|
3
|
-
export declare const makeMongoResource: <R extends ResourceRecord, T extends MongoResource<R> = MongoResource<R>>(alias: string, dbAlias?: string, serviceAlias?: string,
|
|
3
|
+
export declare const makeMongoResource: <R extends ResourceRecord, T extends MongoResource<R> = MongoResource<R>>(alias: string, dbAlias?: string, serviceAlias?: string, collectionName?: string) => T;
|
|
4
4
|
//# sourceMappingURL=resource.d.ts.map
|
package/build/resource.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../src/resource.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resource.d.ts","sourceRoot":"","sources":["../src/resource.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACuC,cAAc,EAChE,MAAM,oBAAoB,CAAA;AAE3B,OAAO,KAAK,EAAmD,aAAa,EAAW,MAAM,YAAY,CAAA;AAkBzG,eAAO,MAAM,iBAAiB,GAC5B,CAAC,SAAS,cAAc,EAAE,CAAC,SAAS,aAAa,CAAC,CAAC,CAAC,GAAG,aAAa,CAAC,CAAC,CAAC,SAEhE,MAAM,YAAW,MAAM,iBAAmC,MAAM,mBACtD,MAAM,KACtB,CAsRF,CAAA"}
|