@stonyx/orm 0.3.2-beta.153 → 0.3.2-beta.155
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 +319 -24
- package/dist/index.d.ts +1 -0
- package/dist/main.d.ts +116 -0
- package/dist/main.js +119 -0
- package/dist/manage-record.js +234 -9
- package/dist/orm-request.d.ts +121 -3
- package/dist/orm-request.js +211 -8
- package/dist/setup-rest-server.js +51 -5
- package/dist/standalone-db.js +17 -5
- package/dist/types/orm-types.d.ts +101 -0
- package/dist/utils.d.ts +44 -0
- package/dist/utils.js +47 -0
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/main.ts +123 -0
- package/src/manage-record.ts +253 -9
- package/src/orm-request.ts +218 -13
- package/src/setup-rest-server.ts +59 -6
- package/src/standalone-db.ts +17 -6
- package/src/types/orm-types.ts +106 -0
- package/src/types/stonyx-rest-server.d.ts +14 -1
- package/src/utils.ts +50 -0
package/README.md
CHANGED
|
@@ -381,6 +381,174 @@ export default class GlobalAccess {
|
|
|
381
381
|
}
|
|
382
382
|
```
|
|
383
383
|
|
|
384
|
+
|
|
385
|
+
### The access context (second argument)
|
|
386
|
+
|
|
387
|
+
`access()` is called with **two** arguments:
|
|
388
|
+
|
|
389
|
+
```js
|
|
390
|
+
access(request, { model, operation })
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
The second is the **access context** — the structural facts about the request,
|
|
394
|
+
which the framework already holds at authorization time. Read these instead of
|
|
395
|
+
parsing anything.
|
|
396
|
+
|
|
397
|
+
| Key | Value |
|
|
398
|
+
|---|---|
|
|
399
|
+
| `model` | The model this route was mounted for, as a **model name**: kebab-case, exactly as declared under `config.orm.paths.model` and keyed in the store — `'owner'`, `'animal'`, `'phone-number'`. **Not** the pluralized, dasherized, mount-prefixed *route* name. |
|
|
400
|
+
| `operation` | One of **`'read'`, `'create'`, `'update'`, `'delete'`** — and no second vocabulary *on this path*. Never an HTTP method name like `'GET'`, and **not** the hook vocabulary either (see [below](#operation-is-not-the-hook-operation)). `undefined` when the dispatched method has no entry in the framework's method map. |
|
|
401
|
+
|
|
402
|
+
So a predicate can be written without reference to any URL:
|
|
403
|
+
|
|
404
|
+
```js
|
|
405
|
+
export default class OwnerAccess {
|
|
406
|
+
models = ['owner'];
|
|
407
|
+
|
|
408
|
+
access(request, { model, operation }) {
|
|
409
|
+
if (model === 'owner' && operation === 'read') {
|
|
410
|
+
return record => record.id !== 'angela';
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return ['read'];
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
There is no string to parse, no variant to miss, and no way to fail open through
|
|
419
|
+
a URL shape nobody anticipated. `model` is fixed at mount time and no request
|
|
420
|
+
can influence it — not a mount prefix, not a query string, not a case-varied
|
|
421
|
+
path, not an absolute-form request target.
|
|
422
|
+
|
|
423
|
+
#### What the context does not tell you: which surface
|
|
424
|
+
|
|
425
|
+
It names **which model and which verb**, not **which route**. Measured over the
|
|
426
|
+
live router, six surfaces produce one identical context:
|
|
427
|
+
|
|
428
|
+
```
|
|
429
|
+
GET /owners { model: 'owner', operation: 'read' }
|
|
430
|
+
GET /owners/gina { model: 'owner', operation: 'read' }
|
|
431
|
+
GET /owners/gina/pets { model: 'owner', operation: 'read' }
|
|
432
|
+
GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
|
|
433
|
+
GET /owners/archived { model: 'owner', operation: 'read' }
|
|
434
|
+
GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
So a rule that depends on the **sub-path** still needs `request.path` —
|
|
438
|
+
mount-relative and query-free, and the one read of argument one that
|
|
439
|
+
[Identifying the collection](#identifying-the-collection) sanctions. The sample
|
|
440
|
+
access class shipped with this repo has such a rule: its `/archived` deny
|
|
441
|
+
**cannot be expressed from the context alone**, and a predicate migrated to
|
|
442
|
+
context-only would silently drop it — a deny becoming an allow.
|
|
443
|
+
|
|
444
|
+
Note also that the related-resource and `?include=` surfaces serve *another
|
|
445
|
+
model's* records under `model: 'owner'`, and the context gives a predicate no
|
|
446
|
+
signal that it is authorizing a related-resource route. That is
|
|
447
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196).
|
|
448
|
+
|
|
449
|
+
#### `operation` is not the hook `operation`
|
|
450
|
+
|
|
451
|
+
This module exposes a **second** `operation` vocabulary, on an identically-named
|
|
452
|
+
key of an identically-shaped context object:
|
|
453
|
+
[hook contexts](#hook-context-object) carry `list` / `get` / `create` /
|
|
454
|
+
`update` / `delete`. The access vocabulary collapses `list` and `get` into
|
|
455
|
+
`'read'`, so for one `GET /animals/1` a hook sees `'get'` while `access()` sees
|
|
456
|
+
`'read'` — and a predicate cannot distinguish a collection read from a
|
|
457
|
+
record read.
|
|
458
|
+
|
|
459
|
+
"No second vocabulary" above is a statement about the **access path**, where
|
|
460
|
+
both the context and the permission array come from one method map. It is not a
|
|
461
|
+
statement about the module. Writing `operation === 'get'` in a predicate never
|
|
462
|
+
matches, and a predicate that stops matching falls through to the permission
|
|
463
|
+
array — so the misreading is fail-open shaped. In TypeScript the exported
|
|
464
|
+
`AccessOperation` union makes it a compile error.
|
|
465
|
+
|
|
466
|
+
The four `operation` values are the same four strings the permission-array
|
|
467
|
+
return shape is written in (`['read', 'create', 'update', 'delete']`), because
|
|
468
|
+
both come from one method map inside the framework. The two forms cannot
|
|
469
|
+
disagree about the same request.
|
|
470
|
+
|
|
471
|
+
**`operation` is `undefined`, never defaulted, for an unmapped method.** Express
|
|
472
|
+
delivers `HEAD` to the `GET` handler, so this is reachable. It is deliberately
|
|
473
|
+
not defaulted to `'read'`: a fabricated operation would turn an unclassified
|
|
474
|
+
request into an authorized one. Treat `undefined` as *not classified* and deny.
|
|
475
|
+
|
|
476
|
+
**The second argument is additive.** JavaScript ignores extra arguments, so an
|
|
477
|
+
existing `access(request)` predicate keeps working exactly as it did. Nothing
|
|
478
|
+
needs to be migrated to keep running — but note that argument **one** is still
|
|
479
|
+
the raw request, so the warning in
|
|
480
|
+
[Identifying the collection](#identifying-the-collection) still applies to any
|
|
481
|
+
predicate that reads it.
|
|
482
|
+
|
|
483
|
+
#### `record` is not in the context
|
|
484
|
+
|
|
485
|
+
Deliberately, and it is not an oversight. `auth()` runs after route matching but
|
|
486
|
+
**before any handler executes**, so nothing has been fetched yet. Supplying a
|
|
487
|
+
record would force a pre-fetch on every request — a second store hit, a new
|
|
488
|
+
failure mode, and an ordering change in the middle of an authorization path.
|
|
489
|
+
|
|
490
|
+
It is also unnecessary: the **function** return shape already *is* the
|
|
491
|
+
per-record hook. Return `(record) => boolean` and the handlers apply it to every
|
|
492
|
+
record the request touches. Auth-time and record-time are separate decision
|
|
493
|
+
points, and the contract keeps them separate.
|
|
494
|
+
|
|
495
|
+
#### Reaching another model's predicate
|
|
496
|
+
|
|
497
|
+
The model → predicate map is published on the ORM instance at boot, before any
|
|
498
|
+
route is mounted, so a predicate can be resolved by model name and asked about a
|
|
499
|
+
request routed to a *different* model:
|
|
500
|
+
|
|
501
|
+
```js
|
|
502
|
+
import Orm from '@stonyx/orm';
|
|
503
|
+
|
|
504
|
+
const predicate = Orm.instance.getAccess('animal');
|
|
505
|
+
if (!predicate) return deny;
|
|
506
|
+
|
|
507
|
+
const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
**`undefined` means no predicate could be resolved — not that the model is
|
|
511
|
+
unrestricted. Treat it as deny.** It covers a model with no access class *and* a
|
|
512
|
+
model whose access class failed to **load**: a load failure is caught and warned
|
|
513
|
+
about, and the partial map is published anyway, so a missing key is not evidence
|
|
514
|
+
of an unrestricted model. This is the same rule as `operation === undefined`
|
|
515
|
+
above, and for the same reason.
|
|
516
|
+
|
|
517
|
+
The raw map is `Orm.instance.accessFunctions`, keyed by model name; prefer
|
|
518
|
+
`getAccess()` — it is guarded against inherited `Object.prototype` members and a
|
|
519
|
+
direct index is not. Note that it maps a model name to the predicate of the
|
|
520
|
+
access *class* that claims it, which may claim many models: against this repo's
|
|
521
|
+
sample, `getAccess('owner') === getAccess('animal')`.
|
|
522
|
+
|
|
523
|
+
#### Passing the context makes a model-correct answer *possible*
|
|
524
|
+
|
|
525
|
+
It does not make the answer model-correct on its own. **The resolved predicate
|
|
526
|
+
has to read the context.** Measured against the access class shipped with this
|
|
527
|
+
repo, on a request Express dispatched to `GET /owners/angela`, asked about
|
|
528
|
+
**animals**:
|
|
529
|
+
|
|
530
|
+
```
|
|
531
|
+
getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
532
|
+
-> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
533
|
+
```
|
|
534
|
+
|
|
535
|
+
That is the **owners** filter, and it returns `true` for animal 21 — the record
|
|
536
|
+
hidden on every animal surface. Under a mount that predicate recognizes neither
|
|
537
|
+
way it is worse still: it falls through to
|
|
538
|
+
`['read', 'create', 'update', 'delete']`, a full CRUD grant.
|
|
539
|
+
|
|
540
|
+
Either way the context was supplied and the answer is not the animal answer, and
|
|
541
|
+
it is wrong in the direction that **grants**. That predicate is single-argument
|
|
542
|
+
and identifies its collection from the request, so it answered about the
|
|
543
|
+
collection the request is *addressed to* while being asked about another one.
|
|
544
|
+
Every predicate in this repo, and in every consumer tree, is single-argument on
|
|
545
|
+
the day this ships, and a caller has no supported way to tell which kind it
|
|
546
|
+
resolved. The boot-time arity warning that would surface it is
|
|
547
|
+
[#213](https://github.com/abofs/stonyx-orm/issues/213).
|
|
548
|
+
|
|
549
|
+
So: pass the context, and do not treat a resolved predicate's answer as
|
|
550
|
+
model-specific until that predicate has been migrated to read it.
|
|
551
|
+
|
|
384
552
|
### Return values
|
|
385
553
|
|
|
386
554
|
| `access()` returns | Effect |
|
|
@@ -413,7 +581,7 @@ a write to a *different* collection can still re-parent one. See
|
|
|
413
581
|
| `GET /:models/:id/relationships/{relationship}` | `404` — same |
|
|
414
582
|
| `PATCH /:models/:id` | `404`, no attribute is applied |
|
|
415
583
|
| `DELETE /:models/:id` | `404`, the record is not removed and no SQL `DELETE` is issued |
|
|
416
|
-
| `POST /:models` | `403`, and a record **this request inserted** is rolled back — see [Known limitations](#known-limitations) for the
|
|
584
|
+
| `POST /:models` | `403`, and a record **this request inserted** is rolled back — see [Known limitations](#known-limitations) for why the rollback is conditional |
|
|
417
585
|
|
|
418
586
|
**Denied record-level requests return 404, not 403.** This is deliberate and it
|
|
419
587
|
is the property most easily "improved" away. 403 would confirm that the record
|
|
@@ -447,9 +615,64 @@ chooses the id and learns whether the create succeeded — so under a filter the
|
|
|
447
615
|
caller does not choose the id. The refusal happens before any store lookup, so
|
|
448
616
|
neither the status nor the response time depends on whether the id exists.
|
|
449
617
|
|
|
450
|
-
Let the server assign the id and read it back from the response
|
|
451
|
-
|
|
452
|
-
|
|
618
|
+
Let the server assign the id and read it back from the response — and read it
|
|
619
|
+
back rather than predicting it, because the value it returns is documented but
|
|
620
|
+
not stable across model kinds: a numeric-id model gets `max + 1` (or, at the
|
|
621
|
+
numeric ceiling, the lowest free integer), and a string-id model gets
|
|
622
|
+
`<model>-<n>`. See breaking change 8.
|
|
623
|
+
|
|
624
|
+
**What a server-assigned id is not.** It is not a secret. On a string-id
|
|
625
|
+
collection it is dense and enumerable from `1`, where previously it inherited
|
|
626
|
+
whatever entropy the last-inserted id happened to carry — a UUID-seeded store
|
|
627
|
+
answered a UUID-derived key. If a collection has **no** `access` config its
|
|
628
|
+
record-level routes are ungated, so the id was the only thing standing between
|
|
629
|
+
an unauthenticated caller and `GET`/`PATCH`/`DELETE` on a record. That was never
|
|
630
|
+
a control and must not become one; configure `access`.
|
|
631
|
+
|
|
632
|
+
**And the id itself is an occupancy signal — on both model kinds.**
|
|
633
|
+
`assignRecordId` reads the whole store, not the caller's filtered view — it
|
|
634
|
+
never sees `state.filter` — so the id it returns is a function of records the
|
|
635
|
+
caller may not be permitted to read. **This applies to numeric-id collections
|
|
636
|
+
as well as string-id ones**, and the conditions differ, so read both:
|
|
637
|
+
|
|
638
|
+
- **String-id collections, always.** The assigned `n` is the smallest positive
|
|
639
|
+
integer whose landing key is free, which tells the caller that every key
|
|
640
|
+
below it is taken, hidden or not.
|
|
641
|
+
- **Numeric-id collections, once one record sits at the numeric ceiling.** The
|
|
642
|
+
normal answer is `max + 1`, which discloses only the maximum. But `max + 1`
|
|
643
|
+
is not representable at or above 2^53, so the walk restarts from `1` (see
|
|
644
|
+
breaking change 8) and the assigned id becomes the smallest free integer —
|
|
645
|
+
the same occupancy predicate, now over arbitrary low keys. Each subsequent
|
|
646
|
+
no-id `POST` names the next free one, so a caller can enumerate the holes in
|
|
647
|
+
a range it cannot read.
|
|
648
|
+
|
|
649
|
+
**A ceiling record reaches a filter-protected collection even though `POST`
|
|
650
|
+
refuses caller ids on one.** Breaking change 3 makes
|
|
651
|
+
`POST /animals {"id": 9007199254740992}` answer `403`, but the same id lands
|
|
652
|
+
through a *relationship write on another collection* —
|
|
653
|
+
`POST /owners` carrying `attributes: { pets: [{ "id": 9007199254740992 }] }`
|
|
654
|
+
creates the animal under that key
|
|
655
|
+
([#207](https://github.com/abofs/stonyx-orm/issues/207), the same channel the
|
|
656
|
+
**Known limitations** re-parenting note describes). So the precondition is
|
|
657
|
+
reachable by an unauthenticated caller on exactly the collections `access`
|
|
658
|
+
exists to protect. Measured on the sample fixture, with every animal hidden by
|
|
659
|
+
the `/animals` predicate and keys 4 and 7 deleted:
|
|
660
|
+
|
|
661
|
+
```
|
|
662
|
+
GET /animals -> 200 [] (nothing visible)
|
|
663
|
+
GET /animals/4 -> 404 (free — indistinguishable from hidden)
|
|
664
|
+
POST /animals {"id":4} -> 403 (breaking change 3)
|
|
665
|
+
POST /owners {... pets:[{"id":9007199254740992}]} -> 200 (#207 plants the ceiling record)
|
|
666
|
+
POST /animals (no id) -> 200 id=4 <- names a hole in the hidden range
|
|
667
|
+
POST /animals (no id) -> 200 id=7 <- and the other one
|
|
668
|
+
POST /animals (no id) -> 200 id=13
|
|
669
|
+
POST /animals (no id) -> 200 id=14
|
|
670
|
+
```
|
|
671
|
+
|
|
672
|
+
Closing this requires the assignment to be filter-aware, which is a change to
|
|
673
|
+
the `access` contract rather than a fix; it is stated here rather than left to
|
|
674
|
+
be discovered. Callers with no function-style filter are unaffected — there are
|
|
675
|
+
no hidden records to disclose.
|
|
453
676
|
|
|
454
677
|
### Identifying the collection
|
|
455
678
|
|
|
@@ -512,14 +735,24 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
512
735
|
one collection is writable and another is filtered on a field the first can
|
|
513
736
|
set. Blocking it requires checking animal 21 against the **animal** model's
|
|
514
737
|
predicate while servicing an **owners** route — cross-model access resolution,
|
|
515
|
-
which the
|
|
516
|
-
|
|
517
|
-
`setup-rest-server.ts`
|
|
518
|
-
|
|
519
|
-
[
|
|
520
|
-
(
|
|
521
|
-
|
|
522
|
-
|
|
738
|
+
which the contract could not express before
|
|
739
|
+
[#202](https://github.com/abofs/stonyx-orm/issues/202): `access()` never
|
|
740
|
+
received the model structurally and `setup-rest-server.ts` discarded the
|
|
741
|
+
model→predicate map at boot. **#202 has landed and both halves now exist** —
|
|
742
|
+
see [The access context](#the-access-context-second-argument):
|
|
743
|
+
`Orm.instance.getAccess(modelName)` makes another model's predicate
|
|
744
|
+
**reachable**, and `context.model` makes a **model-correct answer possible** —
|
|
745
|
+
possible, not guaranteed: the resolved predicate has to read the context, and
|
|
746
|
+
every predicate in tree is still single-argument
|
|
747
|
+
([#213](https://github.com/abofs/stonyx-orm/issues/213)), so today it answers
|
|
748
|
+
about the collection the request is addressed to. **The mechanism exists; the
|
|
749
|
+
ORM does not yet use it on this path.** The re-parenting write above is still
|
|
750
|
+
**not refused** — that enforcement is
|
|
751
|
+
[#196](https://github.com/abofs/stonyx-orm/issues/196) and
|
|
752
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207), which were blocked on
|
|
753
|
+
#202 and are now free to proceed. Until they land, do not rely on a filter to
|
|
754
|
+
keep a record unmodifiable; keep the *writable* collections' predicates as tight as
|
|
755
|
+
the hidden ones.
|
|
523
756
|
- **Authorization by identifying the collection is a consumer-side
|
|
524
757
|
reconstruction of information the framework already holds.** `access()`
|
|
525
758
|
receives a transport artifact and is asked to work out which model, which
|
|
@@ -565,9 +798,10 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
565
798
|
transform output differs from its lookup key is the same defect. Filtered
|
|
566
799
|
collections are unaffected — breaking change 3 refuses any client-supplied id
|
|
567
800
|
— so this reaches consumers with **no** function-style filter. Tracked as
|
|
568
|
-
[#205](https://github.com/abofs/stonyx-orm/issues/205)
|
|
569
|
-
[#203](https://github.com/abofs/stonyx-orm/issues/203)
|
|
570
|
-
|
|
801
|
+
[#205](https://github.com/abofs/stonyx-orm/issues/205). Its sibling
|
|
802
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203) — a **server-assigned**
|
|
803
|
+
id landing on an occupied slot — is **fixed**; see breaking change 8. #205 is
|
|
804
|
+
the client-supplied half and is still open.
|
|
571
805
|
- **`context.record` is `undefined` for an after-`create` hook when a string-id
|
|
572
806
|
model is given a numeric-looking id.** The post-create lookup uses the same id
|
|
573
807
|
coercion as every other surface, which resolves `'9107'` to the number `9107`,
|
|
@@ -577,15 +811,18 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
577
811
|
[#209](https://github.com/abofs/stonyx-orm/issues/209).
|
|
578
812
|
- **A denied `POST` rolls back only a record it *inserted*.** The rollback
|
|
579
813
|
requires the store to have grown, because removing by id alone is a write
|
|
580
|
-
primitive keyed by a caller-supplied value.
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
last-*inserted* + 1,
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
814
|
+
primitive keyed by a caller-supplied value. **The reachability condition this
|
|
815
|
+
bullet used to state is gone**: it was
|
|
816
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203) — `assignRecordId`
|
|
817
|
+
returned last-*inserted* + 1, so a server-assigned id could land on an
|
|
818
|
+
occupied slot and `createRecord` would update it in place — and #203 is fixed
|
|
819
|
+
(breaking change 8). A server-assigned create can no longer overwrite, so on a
|
|
820
|
+
collection whose only id channel is `createHandler` this guard has no
|
|
821
|
+
observable effect today. It is kept because a caller-supplied id reaching
|
|
822
|
+
`createRecord` from another route — a relationship write,
|
|
823
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207) — puts the condition
|
|
824
|
+
back, and without the guard a denied `403` would delete a record the request
|
|
825
|
+
did not create.
|
|
589
826
|
|
|
590
827
|
### Breaking changes
|
|
591
828
|
|
|
@@ -646,6 +883,64 @@ they are recorded here.
|
|
|
646
883
|
population breaking changes 3 and 4 explicitly exempt. If you were relying on
|
|
647
884
|
a hex-shaped or whitespace-padded id creating a second record, it never did.
|
|
648
885
|
|
|
886
|
+
8. **Server-assigned ids change value on string-id models, numeric ids stop
|
|
887
|
+
being monotonic at the numeric ceiling, and the create route gains a
|
|
888
|
+
`409`.** Three consumer-visible changes from
|
|
889
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203).
|
|
890
|
+
|
|
891
|
+
**The value.** A `POST` with no `id` against a model declaring
|
|
892
|
+
`id = attr('string')` previously produced the *last-inserted* id with `1`
|
|
893
|
+
concatenated onto it — an owner store holding `['gina', 'bob']` answered
|
|
894
|
+
`'bob1'`. It now answers `'owner-1'`: the model name, a hyphen, and the
|
|
895
|
+
lowest positive integer whose landing key is free. **No test in this repo
|
|
896
|
+
pinned the old value**, so a consumer relying on it gets no failing test, no
|
|
897
|
+
deprecation and no other signal — which is why it is recorded here. Numeric
|
|
898
|
+
id models (`id = attr('number')`, the default) are unaffected in shape: they
|
|
899
|
+
still get an integer, but it is now the **maximum** existing id plus one
|
|
900
|
+
rather than the last-inserted id plus one, which is the defect #203 is about.
|
|
901
|
+
They are **not** unaffected in *sequence* — see the monotonicity half below.
|
|
902
|
+
|
|
903
|
+
The value is deliberately **not** numeric-looking, and that is not cosmetic.
|
|
904
|
+
Every id-bearing surface resolves a numeric-looking string id to a **number**
|
|
905
|
+
(`GET /owners/1` looks up `1`), while a string-id model files its records
|
|
906
|
+
under the **string** key `'1'`. A server-assigned `'1'` would therefore be a
|
|
907
|
+
record that was created successfully and could not be fetched, updated or
|
|
908
|
+
deleted by id, and whose after-`create` hook received
|
|
909
|
+
`context.record === undefined`
|
|
910
|
+
([#209](https://github.com/abofs/stonyx-orm/issues/209)).
|
|
911
|
+
|
|
912
|
+
**Numeric ids are no longer monotonic, and deleted ids can be re-issued.**
|
|
913
|
+
The precondition is narrow but it is reachable, and there is no signal when
|
|
914
|
+
it is met: **one record filed at or above 2^53** (`9007199254740992`). `max
|
|
915
|
+
+ 1` is not representable there, so assignment restarts from `1` and walks
|
|
916
|
+
up to the lowest free key — which means the id of a *deleted* record is
|
|
917
|
+
handed to the next `POST`. Both `dev` and every prior release were strictly
|
|
918
|
+
monotonic and never re-issued a numeric id, so a consumer that relied on
|
|
919
|
+
that — audit rows, cursors, cached authorization decisions, external
|
|
920
|
+
references keyed on the id — now has a stale reference that silently points
|
|
921
|
+
at a **different record, created by a different caller**, rather than at a
|
|
922
|
+
deleted one. Nothing fails; the reference simply resolves to the wrong
|
|
923
|
+
record.
|
|
924
|
+
|
|
925
|
+
The restart is deliberate and is not itself optional: without it, one record
|
|
926
|
+
at the ceiling made every subsequent server-assigned create on that
|
|
927
|
+
collection fail permanently. Re-use is the cost of keeping the collection
|
|
928
|
+
writable. **If you need monotonic ids, assign them yourself** rather than
|
|
929
|
+
letting the server assign, and note that a ceiling record can be planted by
|
|
930
|
+
an unauthenticated caller — see *And the id itself is an occupancy signal*
|
|
931
|
+
under [Filter functions](#filter-functions) for the reachability path.
|
|
932
|
+
String-id models are unaffected by this half: their keys are
|
|
933
|
+
`<model>-<n>` and were never monotonic over an integer sequence.
|
|
934
|
+
|
|
935
|
+
**The status.** `POST /{collection}` can now answer `409` for a reason other
|
|
936
|
+
than a duplicate id: the server could not derive a free id. That requires a
|
|
937
|
+
**non-injective** id transform — one that maps distinct candidates onto the
|
|
938
|
+
same store key, such as `boolean`, or anything you registered on
|
|
939
|
+
`Orm.instance.transforms` and named as an id type. It is a configuration
|
|
940
|
+
fault rather than a request fault; the message is logged through
|
|
941
|
+
`stonyx/log`. Previously this case threw out of the handler and express
|
|
942
|
+
answered `500` with a stack trace.
|
|
943
|
+
|
|
649
944
|
### Include Parameter (Sideloading Relationships)
|
|
650
945
|
|
|
651
946
|
The ORM supports JSON API-compliant relationship sideloading via the `include` query parameter. This reduces the need for multiple API requests by embedding related records in a single response.
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { count, avg, sum, min, max } from './aggregates.js';
|
|
|
9
9
|
export { default } from './main.js';
|
|
10
10
|
export { store, relationships } from './main.js';
|
|
11
11
|
export type { PersistErrorDetail } from './main.js';
|
|
12
|
+
export type { AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js';
|
|
12
13
|
export { Model, View, Serializer };
|
|
13
14
|
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
14
15
|
export { count, avg, sum, min, max };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Store from './store.js';
|
|
2
|
+
import type { AccessFunction } from './types/orm-types.js';
|
|
2
3
|
interface OrmOptions {
|
|
3
4
|
dbType?: string;
|
|
4
5
|
}
|
|
@@ -32,12 +33,127 @@ export default class Orm {
|
|
|
32
33
|
views: Record<string, unknown>;
|
|
33
34
|
transforms: Record<string, (value: unknown) => unknown>;
|
|
34
35
|
warnings: Set<string>;
|
|
36
|
+
/**
|
|
37
|
+
* Model name -> the `access` predicate of the access class that CLAIMS that
|
|
38
|
+
* model (abofs/stonyx-orm#202).
|
|
39
|
+
*
|
|
40
|
+
* Not "that model's own predicate". One access class may claim many models
|
|
41
|
+
* -- `GlobalAccess` in this repo's fixtures declares five, and `models = '*'`
|
|
42
|
+
* claims every model in the store -- and it declares ONE `access` method, so
|
|
43
|
+
* the same function object is registered under every one of those keys.
|
|
44
|
+
* `getAccess('owner') === getAccess('animal')` is `true` there. The
|
|
45
|
+
* one-to-one guarantee below is key -> function, never function -> model,
|
|
46
|
+
* and a caller must not read a resolved predicate as being animal-specific.
|
|
47
|
+
* What makes the ANSWER model-specific is the context the caller passes and
|
|
48
|
+
* the predicate actually reading it -- see {@link Orm#getAccess}.
|
|
49
|
+
*
|
|
50
|
+
* NAMED FOR WHAT IT HOLDS. It was `accessFiles` through review, inherited
|
|
51
|
+
* from the function-local in `setup-rest-server.ts` where the values came
|
|
52
|
+
* straight out of `forEachFileImport` and "files" was defensible. The values
|
|
53
|
+
* are `AccessFunction`s, and the sibling public registries on this class
|
|
54
|
+
* (`models`, `serializers`, `views`, `transforms`) are all plural nouns of
|
|
55
|
+
* the thing held. Renamed here because #202 is the last moment it is free.
|
|
56
|
+
*
|
|
57
|
+
* Populated by `setup-rest-server.ts` at boot, from the access classes under
|
|
58
|
+
* `config.orm.paths.access`, BEFORE any route is mounted -- so it is complete
|
|
59
|
+
* and reachable before the first request can be served. The mapping is
|
|
60
|
+
* one-to-one by construction: setup-rest-server throws if two access classes
|
|
61
|
+
* claim the same model.
|
|
62
|
+
*
|
|
63
|
+
* Keys are model names as declared and stored (kebab-case, e.g.
|
|
64
|
+
* `'phone-number'`), NOT pluralised or mount-prefixed route names.
|
|
65
|
+
*
|
|
66
|
+
* WHY THIS EXISTS AS A FIELD. It used to be a function-local in
|
|
67
|
+
* setup-rest-server that was discarded when that function returned, so at
|
|
68
|
+
* request time there was no way to get from a model name to that model's
|
|
69
|
+
* predicate at all. Each `OrmRequest` held only its OWN model's predicate.
|
|
70
|
+
* That made cross-model authorization -- asking model X's predicate about a
|
|
71
|
+
* request routed to model Y -- inexpressible, which is the capability
|
|
72
|
+
* abofs/stonyx-orm#196 and abofs/stonyx-orm#207 are built on.
|
|
73
|
+
*
|
|
74
|
+
* Empty when the REST server is disabled, and PARTIAL when one access file
|
|
75
|
+
* failed to load (`setup-rest-server.ts` catches, warns and assigns whatever
|
|
76
|
+
* it had). So a missing key does NOT mean the model has no access class.
|
|
77
|
+
* Prefer {@link Orm#getAccess} over indexing this directly -- it is guarded
|
|
78
|
+
* against the prototype chain and this is not.
|
|
79
|
+
*/
|
|
80
|
+
accessFunctions: Record<string, AccessFunction>;
|
|
35
81
|
options: OrmOptions;
|
|
36
82
|
sqlDb?: SqlDb;
|
|
37
83
|
db?: OrmDB | SqlDb;
|
|
38
84
|
private _persistErrorHandler;
|
|
39
85
|
constructor(options?: OrmOptions);
|
|
40
86
|
init(): Promise<void>;
|
|
87
|
+
/**
|
|
88
|
+
* Resolve the `access` predicate registered for a model name
|
|
89
|
+
* (abofs/stonyx-orm#202).
|
|
90
|
+
*
|
|
91
|
+
* This is the supported way to reach another model's predicate while
|
|
92
|
+
* servicing a request routed to a different model. Call it with the model
|
|
93
|
+
* name and invoke the result with the live request and an explicit context
|
|
94
|
+
* naming THAT model:
|
|
95
|
+
*
|
|
96
|
+
* ```js
|
|
97
|
+
* const predicate = Orm.instance.getAccess('animal');
|
|
98
|
+
* if (!predicate) return deny;
|
|
99
|
+
* const verdict = predicate(request, { model: 'animal', operation: 'read' });
|
|
100
|
+
* ```
|
|
101
|
+
*
|
|
102
|
+
* WHAT IT RESOLVES. The predicate of the access CLASS that claims the model,
|
|
103
|
+
* which is not necessarily specific to it: one class may claim many models
|
|
104
|
+
* and declares one `access` method, so
|
|
105
|
+
* `getAccess('owner') === getAccess('animal')` is `true` against this repo's
|
|
106
|
+
* fixture. See {@link Orm#accessFunctions}.
|
|
107
|
+
*
|
|
108
|
+
* `undefined` means NO PREDICATE COULD BE RESOLVED for that name. That
|
|
109
|
+
* includes a model whose access class failed to LOAD -- `setup-rest-server`
|
|
110
|
+
* catches, warns and publishes the partial map -- so it is not the same claim
|
|
111
|
+
* as "this model is unrestricted". Treat it as DENY, the same way
|
|
112
|
+
* `AccessContext.operation === undefined` is treated.
|
|
113
|
+
*
|
|
114
|
+
* PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not, on
|
|
115
|
+
* its own, make the answer model-correct: the resolved predicate has to READ
|
|
116
|
+
* the context. Measured against this repo's shipped access class on a request
|
|
117
|
+
* express dispatched to `GET /owners/angela`, asked about ANIMALS:
|
|
118
|
+
*
|
|
119
|
+
* ```
|
|
120
|
+
* getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
|
|
121
|
+
* -> record => record.id !== 'angela' && record.id !== 'restricted'
|
|
122
|
+
* ```
|
|
123
|
+
*
|
|
124
|
+
* The OWNERS filter, which returns `true` for animal 21 -- the record hidden
|
|
125
|
+
* on every animal surface. Under a mount that predicate recognises neither
|
|
126
|
+
* way it falls through to `['read', 'create', 'update', 'delete']`, a full
|
|
127
|
+
* CRUD grant. Either way: context supplied, answer not the animal answer,
|
|
128
|
+
* wrong in the GRANTING direction, because that predicate is arity-1 and
|
|
129
|
+
* identifies its collection from the request. AC9 asserts the first case on a
|
|
130
|
+
* live dispatch.
|
|
131
|
+
*
|
|
132
|
+
* Every predicate in this repo and in every consumer tree is arity-1 today,
|
|
133
|
+
* and there is no supported way for the caller to tell which kind it got; the
|
|
134
|
+
* boot-time arity warning that would surface it is abofs/stonyx-orm#213. Pass
|
|
135
|
+
* the context, and do not treat a resolved predicate's answer as
|
|
136
|
+
* model-specific until that predicate reads it.
|
|
137
|
+
*
|
|
138
|
+
* OWN PROPERTIES ONLY. A bare `this.accessFunctions[modelName]` walks the
|
|
139
|
+
* prototype chain, so `getAccess('constructor')` resolved `Object` and
|
|
140
|
+
* `getAccess('toString')` resolved `Object.prototype.toString` -- both
|
|
141
|
+
* callable, and the documented `predicate?.(request, ctx)` pattern then
|
|
142
|
+
* returned a TRUTHY value (`Object(request)` is the request), bypassing the
|
|
143
|
+
* `undefined`-means-deny contract entirely. Nothing in the ORM calls
|
|
144
|
+
* `getAccess` yet, so it was not exploitable as shipped -- but #207 takes the
|
|
145
|
+
* model name from the REQUEST BODY (`data.relationships.<key>.data.type`),
|
|
146
|
+
* which would have made a one-field body an authorization bypass. Guarded
|
|
147
|
+
* here at the read point rather than by constructing the map with a null
|
|
148
|
+
* prototype, because the field is public and reassignable and the guard has
|
|
149
|
+
* to hold whatever object it is holding.
|
|
150
|
+
*
|
|
151
|
+
* @param modelName - Model name as declared and stored (kebab-case).
|
|
152
|
+
* @returns The predicate, or `undefined` when no predicate could be resolved
|
|
153
|
+
* for that name. `undefined` is NOT "this model is unrestricted" -- see the
|
|
154
|
+
* note above. Treat it as deny.
|
|
155
|
+
*/
|
|
156
|
+
getAccess(modelName: string): AccessFunction | undefined;
|
|
41
157
|
startup(): Promise<void>;
|
|
42
158
|
shutdown(): Promise<void>;
|
|
43
159
|
static get db(): OrmDB | SqlDb;
|