@stonyx/orm 0.3.2-alpha.8 → 0.3.2-alpha.80

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.
Files changed (65) hide show
  1. package/README.md +1137 -11
  2. package/config/environment.js +8 -0
  3. package/dist/access-verdict.d.ts +85 -0
  4. package/dist/access-verdict.js +284 -0
  5. package/dist/commands.js +34 -0
  6. package/dist/dynamodb/connection.d.ts +31 -0
  7. package/dist/dynamodb/connection.js +28 -0
  8. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  9. package/dist/dynamodb/dynamodb-db.js +596 -0
  10. package/dist/dynamodb/operation-builder.d.ts +76 -0
  11. package/dist/dynamodb/operation-builder.js +116 -0
  12. package/dist/dynamodb/type-map.d.ts +31 -0
  13. package/dist/dynamodb/type-map.js +48 -0
  14. package/dist/hooks.d.ts +15 -1
  15. package/dist/index.d.ts +3 -0
  16. package/dist/index.js +8 -0
  17. package/dist/main.d.ts +116 -0
  18. package/dist/main.js +129 -0
  19. package/dist/manage-record.js +268 -12
  20. package/dist/mysql/connection.d.ts +1 -0
  21. package/dist/mysql/mysql-db.d.ts +8 -0
  22. package/dist/mysql/mysql-db.js +44 -10
  23. package/dist/orm-request.d.ts +264 -3
  24. package/dist/orm-request.js +1138 -61
  25. package/dist/postgres/connection.d.ts +1 -0
  26. package/dist/postgres/connection.js +8 -6
  27. package/dist/postgres/postgres-db.d.ts +8 -0
  28. package/dist/postgres/postgres-db.js +44 -10
  29. package/dist/record.d.ts +16 -0
  30. package/dist/record.js +154 -6
  31. package/dist/relationships.js +1 -1
  32. package/dist/serializer.js +38 -2
  33. package/dist/setup-rest-server.js +51 -5
  34. package/dist/standalone-db.js +17 -5
  35. package/dist/store.d.ts +13 -1
  36. package/dist/store.js +65 -6
  37. package/dist/types/orm-types.d.ts +234 -0
  38. package/dist/utils.d.ts +44 -0
  39. package/dist/utils.js +47 -0
  40. package/package.json +16 -7
  41. package/src/access-verdict.ts +312 -0
  42. package/src/commands.ts +43 -0
  43. package/src/dynamodb/connection.ts +50 -0
  44. package/src/dynamodb/dynamodb-db.ts +811 -0
  45. package/src/dynamodb/operation-builder.ts +202 -0
  46. package/src/dynamodb/type-map.ts +54 -0
  47. package/src/hooks.ts +15 -1
  48. package/src/index.ts +10 -0
  49. package/src/main.ts +133 -0
  50. package/src/manage-record.ts +294 -18
  51. package/src/mysql/connection.ts +1 -0
  52. package/src/mysql/mysql-db.ts +44 -12
  53. package/src/orm-request.ts +1159 -63
  54. package/src/postgres/connection.ts +10 -6
  55. package/src/postgres/postgres-db.ts +44 -12
  56. package/src/record.ts +182 -6
  57. package/src/relationships.ts +1 -1
  58. package/src/serializer.ts +39 -2
  59. package/src/setup-rest-server.ts +59 -6
  60. package/src/standalone-db.ts +17 -6
  61. package/src/store.ts +68 -6
  62. package/src/types/orm-types.ts +242 -1
  63. package/src/types/stonyx-rest-server.d.ts +14 -1
  64. package/src/types/stonyx.d.ts +7 -1
  65. package/src/utils.ts +50 -0
@@ -1,4 +1,266 @@
1
+ /**
2
+ * REST request handling and access enforcement for @stonyx/orm.
3
+ *
4
+ * ---------------------------------------------------------------------------
5
+ * THE `access()` CONTRACT: `access(request, { model, operation })`
6
+ * ---------------------------------------------------------------------------
7
+ * `auth()` calls your predicate with TWO arguments. The second is the access
8
+ * CONTEXT -- the structural facts about the request, which the framework
9
+ * already holds and which you should read INSTEAD of parsing anything:
10
+ *
11
+ * context.model The model this route was mounted for, as a model name:
12
+ * kebab-case, exactly as declared under
13
+ * `config.orm.paths.model` and keyed in the store --
14
+ * `'owner'`, `'animal'`, `'phone-number'`. NOT the
15
+ * pluralised, dasherized, mount-prefixed ROUTE name. It is
16
+ * read from the OrmRequest instance, fixed at mount time,
17
+ * and no request can influence it.
18
+ *
19
+ * context.operation The operation being authorised. Exactly one of the four
20
+ * verbs `'read'`, `'create'`, `'update'`, `'delete'` --
21
+ * no second vocabulary ON THIS PATH, and never an HTTP
22
+ * method name like `'GET'`. These are the same four
23
+ * strings the permission-array return shape is written in
24
+ * (`['read', 'create']`), because both come from the one
25
+ * `methodAccessMap` below.
26
+ *
27
+ * NOT the hook vocabulary. `HookContext.operation`
28
+ * (`src/hooks.ts`, documented under "Hook Context Object"
29
+ * in the README) carries `'list' | 'get' | 'create' |
30
+ * 'update' | 'delete'` on an identically-named key of an
31
+ * identically-shaped context object, and the access
32
+ * vocabulary collapses `list` and `get` into `'read'`. For
33
+ * one `GET /animals/1` a hook sees `'get'` and `access()`
34
+ * sees `'read'`, so a predicate cannot tell a collection
35
+ * read from a record read. `AccessOperation` makes
36
+ * `operation === 'get'` a compile error for a TypeScript
37
+ * consumer, because a predicate that stops matching falls
38
+ * through to the permission array -- the misreading is
39
+ * fail-open shaped.
40
+ *
41
+ * `undefined` when the dispatched method has no entry in
42
+ * that map. Express delivers `HEAD` to the `GET` handler,
43
+ * so this is reachable. It is left undefined rather than
44
+ * defaulted on purpose -- a fabricated `'read'` would turn
45
+ * an unclassified request into an authorised one. Treat
46
+ * `undefined` as "not classified" and deny.
47
+ *
48
+ * So a consumer writes `if (model === 'owner' && operation === 'read')`. There
49
+ * is no string to parse, no variant to miss, and no way to fail open through a
50
+ * URL shape nobody anticipated.
51
+ *
52
+ * WHAT THE CONTEXT DOES NOT TELL YOU: WHICH SURFACE. It names the model and
53
+ * the verb, not the route. Measured over the live router, six surfaces produce
54
+ * one identical context:
55
+ *
56
+ * GET /owners { model: 'owner', operation: 'read' }
57
+ * GET /owners/gina { model: 'owner', operation: 'read' }
58
+ * GET /owners/gina/pets { model: 'owner', operation: 'read' }
59
+ * GET /owners/gina/relationships/pets { model: 'owner', operation: 'read' }
60
+ * GET /owners/archived { model: 'owner', operation: 'read' }
61
+ * GET /owners/gina?include=pets { model: 'owner', operation: 'read' }
62
+ *
63
+ * So a rule that depends on the SUB-PATH still needs `request.path` -- which is
64
+ * mount-relative and query-free, and is the one read of argument one the
65
+ * warning below sanctions. This repo's own fixture has such a rule: its
66
+ * `/archived` deny cannot be expressed from the context alone, and a predicate
67
+ * migrated to context-only would silently drop it, turning a deny into an
68
+ * allow. The related-resource and `?include=` surfaces serve ANOTHER model's
69
+ * records under `model: 'owner'`, and the context gives no signal of that
70
+ * (abofs/stonyx-orm#196).
71
+ *
72
+ * SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237, AND KEPT FOR THE
73
+ * CONSTRAINT IT STATES RATHER THAN AS A DESCRIPTION OF THE CODE. The context
74
+ * now also carries `recordId` -- the DECODED route-parameter id, see
75
+ * `AccessContext.recordId` in ./types/orm-types.ts -- so the fixture's
76
+ * `/archived` deny IS expressible from the context alone, and the shipped
77
+ * sample no longer reads `request.path` at all. Retiring this wording WITH the
78
+ * measurement that retires it, rather than by deletion, is
79
+ * abofs/stonyx-orm#238.
80
+ *
81
+ * `record` IS NOT IN THIS CONTEXT, deliberately. `auth()` runs after route
82
+ * matching but BEFORE any handler executes (`@stonyx/rest-server`
83
+ * `src/request.ts:58-60`), so nothing has been fetched yet -- supplying a
84
+ * record would force a pre-fetch on every request, a second store hit and an
85
+ * ordering change in the middle of an authorization path. It is also
86
+ * unnecessary: the FUNCTION return shape already is the per-record hook. Return
87
+ * `(record) => boolean` and the handlers apply it to every record the request
88
+ * touches. Auth-time and record-time are separate decision points.
89
+ *
90
+ * THE SECOND ARGUMENT IS ADDITIVE. JavaScript ignores extra arguments, so an
91
+ * existing `access(request)` predicate keeps working exactly as before. The
92
+ * warning immediately below is therefore still live: `request` is still
93
+ * argument ONE, and reading it is still how predicates fail open.
94
+ *
95
+ * To reach ANOTHER model's predicate -- e.g. to check an animal while servicing
96
+ * an owners route -- use the boot-time registry:
97
+ *
98
+ * const predicate = Orm.instance.getAccess('animal');
99
+ * if (!predicate) return deny;
100
+ * const verdict = predicate(request, { model: 'animal', operation: 'read' });
101
+ *
102
+ * `undefined` means NO PREDICATE COULD BE RESOLVED for that name -- which
103
+ * includes the case where the model has an access class that failed to load,
104
+ * because `setup-rest-server.ts` catches a load failure, warns, and publishes
105
+ * whatever partial map it had. It does NOT mean the model is unrestricted.
106
+ * Treat it as DENY, the same way `operation === undefined` is treated above.
107
+ *
108
+ * PASSING THE CONTEXT MAKES A MODEL-CORRECT ANSWER POSSIBLE. It does not make
109
+ * the answer model-correct on its own -- the resolved predicate has to READ it.
110
+ * Measured against an ARITY-1 predicate, on a request express dispatched to
111
+ * `GET /owners/angela`, asked about ANIMALS:
112
+ *
113
+ * getAccess('animal')(ownersRequest, { model: 'animal', operation: 'read' })
114
+ * -> record => record.id !== 'angela' && record.id !== 'restricted'
115
+ *
116
+ * That is the OWNERS filter, and it returns `true` for animal 21 -- the record
117
+ * hidden on every animal surface. Under a mount that predicate recognises
118
+ * neither way it is worse: it falls through to
119
+ * `['read', 'create', 'update', 'delete']`, a full CRUD grant. Either way the
120
+ * context was supplied and the answer is not the animal answer, and it is wrong
121
+ * in the GRANTING direction, because that predicate is arity-1 and identifies
122
+ * its collection from the request. (Asserted on a live dispatch by AC9 in
123
+ * test/integration/orm-test.ts, against a deliberately arity-1 predicate.)
124
+ *
125
+ * This repo's own sample access class has since been MIGRATED to read the
126
+ * context (abofs/stonyx-orm#222), so `getAccess('animal')` here now answers
127
+ * with the animal filter. That is not true of a consumer tree: an arity-1
128
+ * predicate keeps working -- the second argument is additive -- and the caller
129
+ * has no supported way to tell which kind it got. The boot-time arity warning
130
+ * that surfaces one is abofs/stonyx-orm#221.
131
+ * So: pass the context, and do not treat a resolved predicate's answer as
132
+ * model-specific until that predicate has been migrated to read the context.
133
+ *
134
+ * ---------------------------------------------------------------------------
135
+ * DO NOT RECONSTRUCT THE REQUEST PATH INSIDE `access()`.
136
+ * ---------------------------------------------------------------------------
137
+ * You do not have to. `auth()` below hands your predicate the ACCESS CONTEXT as
138
+ * argument two, and `context.model` already names the collection -- see the
139
+ * contract section above. Argument ONE is still the raw transport artifact, and
140
+ * everything from here to the end of this banner is the record of what happened
141
+ * when predicates worked the collection out from it. IT IS HISTORY, NOT
142
+ * GUIDANCE: do not write any of it into a new predicate. Every attempt to
143
+ * identify the collection by parsing the request target has failed OPEN. Five
144
+ * distinct variants of the same three-line example have now been found, each
145
+ * after the previous was fixed, by five different people:
146
+ *
147
+ * 1. `request.url` is mount-relative under `RestServer.mountRoute`, so a
148
+ * prefix match against it is ALWAYS false.
149
+ * 2. `request.originalUrl` carries the query string, so an anchored equality
150
+ * check misses `/owners?filter[age]=30`.
151
+ * 3. The router is a bare `express()` (`caseSensitive: false`) while a
152
+ * hand-written matcher is case-SENSITIVE, so `GET /OwNeRs/angela` walks
153
+ * past it. Router-side: abofs/stonyx-rest-server#47.
154
+ * 4. Under a configured `ORM_REST_ROUTE` a hard-coded `/owners` matches
155
+ * nothing -- environment-specifically, which is worse.
156
+ * 5. HTTP/1.1 permits an ABSOLUTE-FORM request-target. Express routes on
157
+ * `parseurl(req).pathname`, but `originalUrl` is the raw target, so
158
+ * `GET http://anything.example/owners/angela` reaches the handler with
159
+ * `originalUrl === 'http://anything.example/owners/angela'`. A `/owners`
160
+ * prefix match is false, `access()` falls through to whatever it returns
161
+ * last, and the record comes back in full. It walks past a hard
162
+ * `return false` deny the same way.
163
+ *
164
+ * The fix is not a sixth rule, and it is not a better string to match. It is to
165
+ * stop identifying the collection at all: read `context.model`. That is a claim
166
+ * about IDENTIFYING THE COLLECTION, not about the sample as a whole -- the
167
+ * `/archived` SUB-PATH rule is still a string match, and abofs/stonyx-orm#228 is
168
+ * a sixth spelling that gets past it.
169
+ *
170
+ * SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237: the `/archived` rule is
171
+ * no longer a string match against the request target -- it compares the
172
+ * decoded `recordId` the framework supplies -- and abofs/stonyx-orm#228 is
173
+ * CLOSED. Retirement of this wording: abofs/stonyx-orm#238.
174
+ *
175
+ * An intermediate revision of the sample read `request.baseUrl` -- the mount
176
+ * Express ACTUALLY MATCHED. That closed all five variants (no query string,
177
+ * not mount-relative, unaffected by absolute-form, already carrying the
178
+ * configured `ORM_REST_ROUTE` prefix), but it was a transport artifact
179
+ * standing in for a structural fact and the sample no longer does it.
180
+ * `context.model` IS the structural fact, so variants 1, 2, 4 and 5 are
181
+ * unconstructible against a migrated predicate rather than handled.
182
+ *
183
+ * VARIANT 3 SURVIVES, and is deliberately not in that list. It is the general
184
+ * shape "a hand-written matcher normalises differently from the router", and a
185
+ * migrated predicate still runs one string comparison for any SUB-PATH rule --
186
+ * in the shipped sample, the `/archived` deny. That comparison folds case but
187
+ * does not decode, so `GET /owners/%61rchived` steps past it. See the
188
+ * normalisation paragraph below and abofs/stonyx-orm#228.
189
+ *
190
+ * SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237. Variant 3 lived in that
191
+ * one string comparison, and the comparison is gone: the sample compares the
192
+ * decoded `recordId`. Left standing rather than edited because the same
193
+ * "variant 3 survives" wording sits at four sites -- this header, README.md
194
+ * twice, and test/sample/access/global-access.ts -- three of which SHIP, so
195
+ * retiring one of four leaves the shipped copies contradicting each other.
196
+ * Retiring all four WITH their measurement is abofs/stonyx-orm#238.
197
+ *
198
+ * ONE READ OF ARGUMENT ONE SURVIVES, AND IT MUST: `request.path`. It is
199
+ * mount-relative and query-free, and it is for rules that distinguish SUB-PATHS
200
+ * beneath the mount. The context names which model and which verb, NOT which
201
+ * route, so the sample's `/archived` deny cannot be expressed from the context
202
+ * alone and a context-ONLY rewrite would silently turn that deny into an allow.
203
+ *
204
+ * SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237: NO read of argument one
205
+ * survives in the shipped sample. `recordId` names WHICH RECORD the route was
206
+ * addressed to, so the `/archived` deny is expressible from the context alone
207
+ * -- and it still must not be dropped; expressible is not optional. Retirement
208
+ * of this wording: abofs/stonyx-orm#238.
209
+ *
210
+ * NORMALISE THE WAY THE ROUTER DOES, AND CASE-FOLDING ALONE IS NOT THAT. The
211
+ * sample lower-cases before comparing, because a matcher stricter than the
212
+ * case-insensitive router can be stepped around. That closes the case gap only.
213
+ * Express sets `request.path` from the RAW, UNDECODED pathname while the router
214
+ * DECODES `:id`, so `GET /owners/%61rchived` reaches a `path === '/archived'`
215
+ * comparison as `/%61rchived` and walks past the deny. That gap is live in the
216
+ * sample and is tracked as abofs/stonyx-orm#228; the `.toLowerCase()` is not a
217
+ * complete normalisation recipe. Compare record ids at their real case.
218
+ *
219
+ * DO NOT FOLLOW THE PARAGRAPH ABOVE. SUPERSEDED 2026-09-01 BY
220
+ * abofs/stonyx-orm#236/#237, and flagged here rather than merely dated because
221
+ * it is an INSTRUCTION, not a stale observation. `.toLowerCase()` on the access
222
+ * path was measured WRONG IN BOTH DIRECTIONS AT ONCE: with a distinct owner
223
+ * seeded at `ARCHIVED`, `GET /owners/ARCHIVED` was a false DENY on the wrong
224
+ * record and `GET /owners/%41RCHIVED` a false ALLOW on that same record. A
225
+ * record id is a VALUE, not a literal route segment, and express's
226
+ * `case sensitive routing` governs literal segments only. Compare
227
+ * `context.recordId` AS IT ARRIVES: do not case-fold it, do not decode it, do
228
+ * not derive it from `request.path`. `AccessContext.recordId` in
229
+ * ./types/orm-types.ts is the contract and says "Do NOT case-fold it"; the same
230
+ * published tarball ships both files, and THIS paragraph is the one that is
231
+ * wrong. Retiring it WITH its measurement is abofs/stonyx-orm#238.
232
+ *
233
+ * `?? ''` is not a defence. It converts an absent request target into an empty
234
+ * string, which matches no collection, which falls through to the permission
235
+ * array -- a total grant. An input you cannot identify must DENY, and that
236
+ * applies to BOTH arguments: since #202 the guard and the read can sit on
237
+ * different objects, and a guard on argument two does not protect a read of
238
+ * argument one. The sample returns `false` for an absent `model` AND for an
239
+ * absent or non-string `request.path`, rather than falling through either way.
240
+ *
241
+ * SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237 as to WHAT is guarded --
242
+ * the principle is unchanged. The sample no longer reads `request.path`, so it
243
+ * returns `false` for an absent `model` AND for an absent `recordId`
244
+ * (`undefined`, the one spelling `auth()` never produces). Retirement of this
245
+ * wording: abofs/stonyx-orm#238.
246
+ *
247
+ * THE REAL FIX IS abofs/stonyx-orm#202: `access()` should receive the model,
248
+ * the operation and the record. Prefer the array shape (`['read']`) or `false`
249
+ * until #202 lands; the function shape is what requires any matching at all.
250
+ *
251
+ * The enforcement gates in this file (GATE 0/1/2, the create rollback, the
252
+ * per-handler `isDenied` re-checks) are correct independently of that -- they
253
+ * enforce whatever predicate you return. The stopgap is the part where YOU have
254
+ * to work out which predicate to return.
255
+ *
256
+ * AND A PREDICATE IS NOT A GUARANTEE THAT A HIDDEN RECORD CANNOT BE MODIFIED.
257
+ * It is evaluated against the record the route is ADDRESSED TO, on that model
258
+ * only. A write to a DIFFERENT collection can still re-parent a hidden record
259
+ * and de-hide it -- abofs/stonyx-orm#207, which is blocked on #202 and #196.
260
+ * See `### Known limitations` in README.
261
+ */
1
262
  import { Request } from '@stonyx/rest-server';
263
+ import type { AccessFunction } from './types/orm-types.js';
2
264
  interface OrmRequest$ extends Request {
3
265
  protocol?: string;
4
266
  method: string;
@@ -13,13 +275,12 @@ interface OrmRequest$ extends Request {
13
275
  };
14
276
  get(header: string): string;
15
277
  }
16
- type AccessMethod = string | boolean | string[] | ((record: unknown) => boolean);
17
278
  type HandlerFn = (request: OrmRequest$, state: {
18
279
  [key: string]: unknown;
19
280
  }) => unknown | Promise<unknown>;
20
281
  export default class OrmRequest extends Request {
21
282
  model: string;
22
- access: (request: unknown) => AccessMethod;
283
+ access: AccessFunction;
23
284
  handlers: {
24
285
  [key: string]: {
25
286
  [key: string]: HandlerFn;
@@ -27,7 +288,7 @@ export default class OrmRequest extends Request {
27
288
  };
28
289
  constructor({ model, access }: {
29
290
  model: string;
30
- access: (request: unknown) => AccessMethod;
291
+ access: AccessFunction;
31
292
  });
32
293
  private _withHooks;
33
294
  private _generateRelationshipRoutes;