@stonyx/orm 0.3.2-alpha.7 → 0.3.2-alpha.71

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 (63) hide show
  1. package/README.md +968 -11
  2. package/config/environment.js +8 -0
  3. package/dist/access-verdict.d.ts +59 -0
  4. package/dist/access-verdict.js +222 -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/index.d.ts +3 -0
  15. package/dist/index.js +8 -0
  16. package/dist/main.d.ts +116 -0
  17. package/dist/main.js +129 -0
  18. package/dist/manage-record.js +268 -12
  19. package/dist/mysql/connection.d.ts +1 -0
  20. package/dist/mysql/mysql-db.d.ts +8 -0
  21. package/dist/mysql/mysql-db.js +44 -10
  22. package/dist/orm-request.d.ts +216 -3
  23. package/dist/orm-request.js +924 -55
  24. package/dist/postgres/connection.d.ts +1 -0
  25. package/dist/postgres/connection.js +8 -6
  26. package/dist/postgres/postgres-db.d.ts +8 -0
  27. package/dist/postgres/postgres-db.js +44 -10
  28. package/dist/record.d.ts +16 -0
  29. package/dist/record.js +62 -6
  30. package/dist/relationships.js +1 -1
  31. package/dist/serializer.js +38 -2
  32. package/dist/setup-rest-server.js +51 -5
  33. package/dist/standalone-db.js +17 -5
  34. package/dist/store.d.ts +13 -1
  35. package/dist/store.js +65 -6
  36. package/dist/types/orm-types.d.ts +139 -0
  37. package/dist/utils.d.ts +44 -0
  38. package/dist/utils.js +47 -0
  39. package/package.json +16 -7
  40. package/src/access-verdict.ts +248 -0
  41. package/src/commands.ts +43 -0
  42. package/src/dynamodb/connection.ts +50 -0
  43. package/src/dynamodb/dynamodb-db.ts +811 -0
  44. package/src/dynamodb/operation-builder.ts +202 -0
  45. package/src/dynamodb/type-map.ts +54 -0
  46. package/src/index.ts +10 -0
  47. package/src/main.ts +133 -0
  48. package/src/manage-record.ts +294 -18
  49. package/src/mysql/connection.ts +1 -0
  50. package/src/mysql/mysql-db.ts +44 -12
  51. package/src/orm-request.ts +944 -56
  52. package/src/postgres/connection.ts +10 -6
  53. package/src/postgres/postgres-db.ts +44 -12
  54. package/src/record.ts +82 -6
  55. package/src/relationships.ts +1 -1
  56. package/src/serializer.ts +39 -2
  57. package/src/setup-rest-server.ts +59 -6
  58. package/src/standalone-db.ts +17 -6
  59. package/src/store.ts +68 -6
  60. package/src/types/orm-types.ts +146 -1
  61. package/src/types/stonyx-rest-server.d.ts +14 -1
  62. package/src/types/stonyx.d.ts +7 -1
  63. package/src/utils.ts +50 -0
@@ -18,6 +18,7 @@ export interface OrmMysqlConfig {
18
18
  connectionLimit?: number;
19
19
  migrationsDir?: string;
20
20
  migrationsTable?: string;
21
+ autoMigrate?: boolean;
21
22
  [key: string]: unknown;
22
23
  }
23
24
 
@@ -30,6 +31,7 @@ export interface OrmPostgresConfig {
30
31
  connectionLimit?: number;
31
32
  migrationsDir?: string;
32
33
  migrationsTable?: string;
34
+ autoMigrate?: boolean;
33
35
  [key: string]: unknown;
34
36
  }
35
37
 
@@ -48,6 +50,13 @@ export interface OrmRestServerConfig {
48
50
  metaRoute: boolean;
49
51
  }
50
52
 
53
+ export interface OrmDynamoDBConfig {
54
+ region?: string;
55
+ endpoint?: string;
56
+ tablePrefix?: string;
57
+ [key: string]: unknown;
58
+ }
59
+
51
60
  export interface OrmSection {
52
61
  db: OrmDbConfig;
53
62
  paths: OrmPaths;
@@ -55,6 +64,9 @@ export interface OrmSection {
55
64
  mysql?: OrmMysqlConfig;
56
65
  postgres?: OrmPostgresConfig;
57
66
  timescale?: OrmPostgresConfig;
67
+ dynamodb?: OrmDynamoDBConfig;
68
+ logColor?: string;
69
+ logMethod?: string;
58
70
  [key: string]: unknown;
59
71
  }
60
72
 
@@ -77,7 +89,15 @@ export interface OrmRecord {
77
89
  __model?: { __name: string };
78
90
  __data: Record<string, unknown> & { id?: string | number; __pendingSqlId?: boolean };
79
91
  __relationships: Record<string, unknown>;
80
- toJSON?(options?: { fields?: Set<string>; baseUrl?: string }): Record<string, unknown>;
92
+ /**
93
+ * `linkage` is an ALREADY-RESOLVED decision supplied by a caller that holds
94
+ * the request (abofs/stonyx-orm#234): return `false` for a related record and
95
+ * its `{ type, id }` is dropped from `relationships.*.data`. Omitting it is
96
+ * the default, and the default is the pre-#234 document unchanged -- this
97
+ * method is also the `JSON.stringify` hook, so an implicit caller has no
98
+ * syntactic place to pass it (abofs/stonyx-orm#230).
99
+ */
100
+ toJSON?(options?: { fields?: Set<string>; baseUrl?: string; linkage?: LinkageFilter }): Record<string, unknown>;
81
101
  [key: string]: unknown;
82
102
  }
83
103
 
@@ -156,3 +176,128 @@ export interface SnapshotEntry {
156
176
  source?: string;
157
177
  viewQuery?: string;
158
178
  }
179
+
180
+ /**
181
+ * The shapes a consumer `access()` predicate may return.
182
+ *
183
+ * - `false` (or any falsy value) -- deny, 403.
184
+ * - `true` -- allow, with no per-record filter.
185
+ * - a permission string or array of them, drawn from the same four verbs as
186
+ * {@link AccessContext.operation}. A BARE STRING IS ONE PERMISSION, not a
187
+ * grant of all four.
188
+ * - a `(record) => boolean` predicate -- allow, and filter every record the
189
+ * request touches through it.
190
+ *
191
+ * Anything else fails CLOSED. See `src/orm-request.ts` `auth()`.
192
+ */
193
+ export type AccessMethod = string | boolean | string[] | ((record: unknown) => boolean);
194
+
195
+ /**
196
+ * The closed vocabulary `AccessContext.operation` is drawn from
197
+ * (abofs/stonyx-orm#202).
198
+ *
199
+ * A literal union rather than `string`, so the guarantee the prose makes is the
200
+ * one the compiler enforces: a consumer who writes `operation === 'GET'` or
201
+ * `operation === 'get'` -- the hook vocabulary, see below -- gets a compile
202
+ * error instead of a comparison that never matches. A predicate that stops
203
+ * matching falls through to the permission array, so the misreading is
204
+ * fail-open shaped.
205
+ *
206
+ * In-repo precedent: `PersistErrorDetail.operation` in `src/main.ts`.
207
+ */
208
+ export type AccessOperation = 'read' | 'create' | 'update' | 'delete';
209
+
210
+ /**
211
+ * The structural facts about the request being authorised, handed to a consumer
212
+ * `access()` predicate as its SECOND argument (abofs/stonyx-orm#202).
213
+ *
214
+ * These are the facts the framework already holds at authorisation time. Before
215
+ * #202 a consumer had to reconstruct both of them by string-matching a URL, and
216
+ * five independent fail-open variants of that reconstruction were found in one
217
+ * three-line documented example -- each one wrong in the direction that GRANTS
218
+ * access. Read these instead; there is nothing to parse and no variant to miss.
219
+ *
220
+ * `record` is deliberately NOT a member. `auth()` runs after route matching but
221
+ * before any handler executes (`@stonyx/rest-server` `src/request.ts:58-60`),
222
+ * so nothing has been fetched yet -- carrying a record here would force a
223
+ * pre-fetch on every request. It is also unnecessary: the `(record) => boolean`
224
+ * return shape of {@link AccessMethod} already IS the per-record hook, applied
225
+ * by the handlers. Auth-time and record-time are separate decision points.
226
+ */
227
+ export interface AccessContext {
228
+ /**
229
+ * The model this route was mounted for, e.g. `'owner'` or `'phone-number'`.
230
+ *
231
+ * Model names are kebab-case, as declared under `config.orm.paths.model` and
232
+ * keyed in the store -- NOT the pluralised, mount-prefixed route name. It is
233
+ * read from the `OrmRequest` instance and is never derived from the request
234
+ * target, so a mount prefix, a case-varied path, a query string or an
235
+ * absolute-form request-target cannot change it.
236
+ */
237
+ model: string;
238
+
239
+ /**
240
+ * The operation being authorised. Exactly one of the four {@link
241
+ * AccessOperation} verbs, or `undefined`. These are exactly the values of
242
+ * `methodAccessMap` in `src/orm-request.ts`, which is also what the
243
+ * permission-array return shape is matched against -- so the two forms cannot
244
+ * disagree.
245
+ *
246
+ * NOT the hook vocabulary. `HookContext.operation` (`src/hooks.ts`) carries
247
+ * `'list' | 'get' | 'create' | 'update' | 'delete'` on an identically-named
248
+ * key of an identically-shaped context object, and the access vocabulary
249
+ * collapses `list` and `get` into `'read'`. For one `GET /animals/1` a hook
250
+ * sees `'get'` and `access()` sees `'read'`. "No second vocabulary" is a
251
+ * statement about the ACCESS path only.
252
+ *
253
+ * `undefined` when the dispatched method has no entry in that map. Express
254
+ * delivers `HEAD` to the `GET` handler, so this is reachable. It is left
255
+ * undefined rather than defaulted on purpose: a fabricated `'read'` would
256
+ * turn an unclassified request into an authorised one.
257
+ *
258
+ * The KEY is required even though the value may be undefined: `auth()` always
259
+ * sets it, and a context that simply omitted it would be indistinguishable
260
+ * from one that classified the request and found nothing.
261
+ */
262
+ operation: AccessOperation | undefined;
263
+ }
264
+
265
+ /**
266
+ * A consumer `access()` predicate.
267
+ *
268
+ * The second argument is ADDITIVE: JavaScript ignores extra arguments, so every
269
+ * pre-#202 single-argument predicate keeps working untouched. Changing the
270
+ * FIRST argument instead would have been the breaking form, and a predicate
271
+ * that can no longer identify its collection falls through to a full CRUD
272
+ * grant -- so the "safer" breaking change would have converted every unmigrated
273
+ * predicate into a fail-open.
274
+ *
275
+ * `context` is nonetheless REQUIRED in the type, and that costs back-compat
276
+ * nothing. TypeScript already lets a fewer-parameter implementation satisfy a
277
+ * more-parameter signature, so an arity-1 predicate assigns to this type
278
+ * cleanly -- measured under `--strict`. What the `?` bought was the opposite of
279
+ * safety: it silently permitted `getAccess('animal')?.(request)` at the CALL
280
+ * site, i.e. exactly the omission {@link AccessContext} exists to prevent, and
281
+ * that call gets the model-wrong answer. Required, a caller that drops the
282
+ * context gets `TS2554: Expected 2 arguments, but got 1`.
283
+ */
284
+ export type AccessFunction = (request: unknown, context: AccessContext) => AccessMethod;
285
+
286
+ /**
287
+ * A resolved, request-scoped linkage decision: may `record` of model `type` be
288
+ * NAMED, by id, inside another model's document (abofs/stonyx-orm#234)?
289
+ *
290
+ * Arity is `(type, record)` and not `(type, id)` because the per-record filter
291
+ * a consumer returns is handed the RECORD -- this repo's own fixture reads
292
+ * `record.owner?.id`, not just `record.id`. The `(type, id)` pair is the CACHE
293
+ * key inside `createLinkageFilter`, not the input.
294
+ *
295
+ * DECLARED HERE, with the rest of the access vocabulary, and imported by every
296
+ * site that names it. It had three structurally-identical hand-written copies
297
+ * (`access-verdict.ts`, `record.ts`, `OrmRecord.toJSON` below) bridged to each
298
+ * other by nothing, so a drift in nullability or a widening of `type` would
299
+ * have landed on one and not the others -- which is the same "second,
300
+ * unreviewed vocabulary" failure `src/access-verdict.ts` exists to prevent, one
301
+ * level up in the type system.
302
+ */
303
+ export type LinkageFilter = (type: string, record: unknown) => boolean;
@@ -5,7 +5,20 @@ declare module '@stonyx/rest-server' {
5
5
 
6
6
  interface RouteOptions {
7
7
  name: string;
8
- options?: { model: string; access: (request: unknown) => unknown } | Record<string, unknown>;
8
+ /**
9
+ * `access` is the two-argument post-#202 shape. This is the THIRD place the
10
+ * contract is declared (`AccessInstance.access` in
11
+ * `src/setup-rest-server.ts` and `OrmRequest.access` in
12
+ * `src/orm-request.ts` are the other two) and it is the one `mountRoute` is
13
+ * actually called through, at `src/setup-rest-server.ts`. It kept the
14
+ * pre-#202 single-argument signature after the other two migrated; the
15
+ * union with `Record<string, unknown>` meant nothing broke, which is
16
+ * exactly why it would have drifted silently.
17
+ *
18
+ * Spelled structurally rather than as `AccessFunction`: an ambient
19
+ * `declare module` block cannot carry an `import type`.
20
+ */
21
+ options?: { model: string; access: (request: unknown, context: { model: string; operation: string | undefined }) => unknown } | Record<string, unknown>;
9
22
  }
10
23
 
11
24
  export default class RestServer {
@@ -5,7 +5,13 @@ declare module 'stonyx/config' {
5
5
  }
6
6
 
7
7
  declare module 'stonyx/log' {
8
- const log: Record<string, ((...args: unknown[]) => void) | undefined>;
8
+ interface Log {
9
+ db(message: string): void;
10
+ error(message: string, ...args: unknown[]): void;
11
+ defineType(type: string, setting: string, options?: Record<string, unknown> | null): void;
12
+ [key: string]: ((...args: unknown[]) => void) | undefined;
13
+ }
14
+ const log: Log;
9
15
  export default log;
10
16
  }
11
17
 
package/src/utils.ts CHANGED
@@ -20,3 +20,53 @@ export function pluralize(word: string): string {
20
20
 
21
21
  return basePluralize(word);
22
22
  }
23
+
24
+ /**
25
+ * The highest NUMERIC id held by a set of records, or `0` when there is none.
26
+ *
27
+ * ONE COPY, and the duplication it replaces is the reason it lives here. Three
28
+ * near-identical reduces existed at once: `assignRecordId` (server-assigned id
29
+ * selection), `StandaloneDB.create` (src/standalone-db.ts) and the #203 test
30
+ * helper. `docs/improvements.md`'s standing WET Code category prescribes
31
+ * exactly this remedy -- extract into the module that already acts as the
32
+ * shared utility -- and `assignRecordId` already imported `isOrmRecord` from
33
+ * here.
34
+ *
35
+ * NON-NUMBERS ARE SKIPPED RATHER THAN COERCED TO `0`, AND THAT IS STYLISTIC.
36
+ * `StandaloneDB`'s shape mapped them to `0`, which can never beat a seed of
37
+ * `0`. Measured over eleven input classes (`[]`, `1`, `NaN`, `'5'`, `'abc'`,
38
+ * `-3`, `0`, `null`, `undefined`, `Infinity`, and mixed arrays) the two shapes
39
+ * produce IDENTICAL output on every one. In particular `typeof NaN` is
40
+ * `'number'`, so NEITHER shape coerces `NaN` -- both reject it on `NaN > max`,
41
+ * which is `false`. An earlier revision of this code asserted that the skip was
42
+ * what made the `NaN` case work; it is not, the comparison is, and that claim
43
+ * has been removed rather than left standing.
44
+ *
45
+ * WHAT IS LOAD-BEARING is that this is not `Math.max(...ids)`. `Math.max`
46
+ * returns `NaN` if any operand is `NaN`, and a record CAN be held under the key
47
+ * `NaN` -- so the obvious fix assigns `NaN`, lands on that slot and overwrites
48
+ * it, which is abofs/stonyx-orm#203 in a new disguise. Pinned by
49
+ * test/unit/assign-record-id-test.ts AC2; before that file existed the whole
50
+ * suite scored 951/0 under exactly that fix.
51
+ */
52
+ export function maxNumericId(records: { id?: unknown }[]): number {
53
+ return records.reduce((max: number, record) => {
54
+ const { id } = record;
55
+
56
+ return typeof id === 'number' && id > max ? id : max;
57
+ }, 0);
58
+ }
59
+
60
+ /**
61
+ * The message prefix `assignRecordId` throws with when no free id can be
62
+ * derived for a model, and the ONE string `createHandler` matches on to answer
63
+ * `409` instead of letting the rejection reach express's default handler.
64
+ *
65
+ * It lives here rather than in either file because both need it and neither
66
+ * should own a copy: a literal in two places is how the two id coercions in
67
+ * orm-request.ts drifted apart (see `coerceId`). The repo has no error codes
68
+ * and no custom error classes -- 24 bare `throw new Error` sites across `src/`
69
+ * -- so a shared prefix is the narrowest way to make ONE failure distinguishable
70
+ * without inventing an error taxonomy this codebase does not use.
71
+ */
72
+ export const NO_FREE_ID_ERROR = 'Cannot assign record ID: no free id available';