@stonyx/orm 0.3.2-alpha.61 → 0.3.2-alpha.63
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 +73 -16
- package/dist/manage-record.js +168 -38
- package/dist/orm-request.js +60 -7
- package/dist/standalone-db.js +17 -5
- package/dist/utils.d.ts +44 -0
- package/dist/utils.js +47 -0
- package/package.json +1 -1
- package/src/manage-record.ts +180 -41
- package/src/orm-request.ts +62 -7
- package/src/standalone-db.ts +17 -6
- package/src/utils.ts +50 -0
package/README.md
CHANGED
|
@@ -581,7 +581,7 @@ a write to a *different* collection can still re-parent one. See
|
|
|
581
581
|
| `GET /:models/:id/relationships/{relationship}` | `404` — same |
|
|
582
582
|
| `PATCH /:models/:id` | `404`, no attribute is applied |
|
|
583
583
|
| `DELETE /:models/:id` | `404`, the record is not removed and no SQL `DELETE` is issued |
|
|
584
|
-
| `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 |
|
|
585
585
|
|
|
586
586
|
**Denied record-level requests return 404, not 403.** This is deliberate and it
|
|
587
587
|
is the property most easily "improved" away. 403 would confirm that the record
|
|
@@ -615,9 +615,29 @@ chooses the id and learns whether the create succeeded — so under a filter the
|
|
|
615
615
|
caller does not choose the id. The refusal happens before any store lookup, so
|
|
616
616
|
neither the status nor the response time depends on whether the id exists.
|
|
617
617
|
|
|
618
|
-
Let the server assign the id and read it back from the response
|
|
619
|
-
|
|
620
|
-
|
|
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.** `assignRecordId` reads the whole
|
|
633
|
+
store, not the caller's filtered view — it never sees `state.filter` — so the id
|
|
634
|
+
it returns is a function of records the caller may not be permitted to read. On
|
|
635
|
+
a string-id collection the assigned `n` is the smallest positive integer whose
|
|
636
|
+
key is free, which tells the caller that every key below it is taken, hidden or
|
|
637
|
+
not. Closing that requires the assignment to be filter-aware, which is a change
|
|
638
|
+
to the `access` contract rather than a fix; it is stated here rather than left
|
|
639
|
+
to be discovered. Callers with no function-style filter are unaffected — there
|
|
640
|
+
are no hidden records to disclose.
|
|
621
641
|
|
|
622
642
|
### Identifying the collection
|
|
623
643
|
|
|
@@ -743,9 +763,10 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
743
763
|
transform output differs from its lookup key is the same defect. Filtered
|
|
744
764
|
collections are unaffected — breaking change 3 refuses any client-supplied id
|
|
745
765
|
— so this reaches consumers with **no** function-style filter. Tracked as
|
|
746
|
-
[#205](https://github.com/abofs/stonyx-orm/issues/205)
|
|
747
|
-
[#203](https://github.com/abofs/stonyx-orm/issues/203)
|
|
748
|
-
|
|
766
|
+
[#205](https://github.com/abofs/stonyx-orm/issues/205). Its sibling
|
|
767
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203) — a **server-assigned**
|
|
768
|
+
id landing on an occupied slot — is **fixed**; see breaking change 8. #205 is
|
|
769
|
+
the client-supplied half and is still open.
|
|
749
770
|
- **`context.record` is `undefined` for an after-`create` hook when a string-id
|
|
750
771
|
model is given a numeric-looking id.** The post-create lookup uses the same id
|
|
751
772
|
coercion as every other surface, which resolves `'9107'` to the number `9107`,
|
|
@@ -755,15 +776,18 @@ sub-paths beneath the mount, as the `/archived` deny above does.
|
|
|
755
776
|
[#209](https://github.com/abofs/stonyx-orm/issues/209).
|
|
756
777
|
- **A denied `POST` rolls back only a record it *inserted*.** The rollback
|
|
757
778
|
requires the store to have grown, because removing by id alone is a write
|
|
758
|
-
primitive keyed by a caller-supplied value.
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
last-*inserted* + 1,
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
779
|
+
primitive keyed by a caller-supplied value. **The reachability condition this
|
|
780
|
+
bullet used to state is gone**: it was
|
|
781
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203) — `assignRecordId`
|
|
782
|
+
returned last-*inserted* + 1, so a server-assigned id could land on an
|
|
783
|
+
occupied slot and `createRecord` would update it in place — and #203 is fixed
|
|
784
|
+
(breaking change 8). A server-assigned create can no longer overwrite, so on a
|
|
785
|
+
collection whose only id channel is `createHandler` this guard has no
|
|
786
|
+
observable effect today. It is kept because a caller-supplied id reaching
|
|
787
|
+
`createRecord` from another route — a relationship write,
|
|
788
|
+
[#207](https://github.com/abofs/stonyx-orm/issues/207) — puts the condition
|
|
789
|
+
back, and without the guard a denied `403` would delete a record the request
|
|
790
|
+
did not create.
|
|
767
791
|
|
|
768
792
|
### Breaking changes
|
|
769
793
|
|
|
@@ -824,6 +848,39 @@ they are recorded here.
|
|
|
824
848
|
population breaking changes 3 and 4 explicitly exempt. If you were relying on
|
|
825
849
|
a hex-shaped or whitespace-padded id creating a second record, it never did.
|
|
826
850
|
|
|
851
|
+
8. **Server-assigned ids change value on string-id models, and the create route
|
|
852
|
+
gains a `409`.** Two consumer-visible changes from
|
|
853
|
+
[#203](https://github.com/abofs/stonyx-orm/issues/203).
|
|
854
|
+
|
|
855
|
+
**The value.** A `POST` with no `id` against a model declaring
|
|
856
|
+
`id = attr('string')` previously produced the *last-inserted* id with `1`
|
|
857
|
+
concatenated onto it — an owner store holding `['gina', 'bob']` answered
|
|
858
|
+
`'bob1'`. It now answers `'owner-1'`: the model name, a hyphen, and the
|
|
859
|
+
lowest positive integer whose landing key is free. **No test in this repo
|
|
860
|
+
pinned the old value**, so a consumer relying on it gets no failing test, no
|
|
861
|
+
deprecation and no other signal — which is why it is recorded here. Numeric
|
|
862
|
+
id models (`id = attr('number')`, the default) are unaffected in shape: they
|
|
863
|
+
still get an integer, but it is now the **maximum** existing id plus one
|
|
864
|
+
rather than the last-inserted id plus one, which is the defect #203 is about.
|
|
865
|
+
|
|
866
|
+
The value is deliberately **not** numeric-looking, and that is not cosmetic.
|
|
867
|
+
Every id-bearing surface resolves a numeric-looking string id to a **number**
|
|
868
|
+
(`GET /owners/1` looks up `1`), while a string-id model files its records
|
|
869
|
+
under the **string** key `'1'`. A server-assigned `'1'` would therefore be a
|
|
870
|
+
record that was created successfully and could not be fetched, updated or
|
|
871
|
+
deleted by id, and whose after-`create` hook received
|
|
872
|
+
`context.record === undefined`
|
|
873
|
+
([#209](https://github.com/abofs/stonyx-orm/issues/209)).
|
|
874
|
+
|
|
875
|
+
**The status.** `POST /{collection}` can now answer `409` for a reason other
|
|
876
|
+
than a duplicate id: the server could not derive a free id. That requires a
|
|
877
|
+
**non-injective** id transform — one that maps distinct candidates onto the
|
|
878
|
+
same store key, such as `boolean`, or anything you registered on
|
|
879
|
+
`Orm.instance.transforms` and named as an id type. It is a configuration
|
|
880
|
+
fault rather than a request fault; the message is logged through
|
|
881
|
+
`stonyx/log`. Previously this case threw out of the handler and express
|
|
882
|
+
answered `500` with a stack trace.
|
|
883
|
+
|
|
827
884
|
### Include Parameter (Sideloading Relationships)
|
|
828
885
|
|
|
829
886
|
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/manage-record.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Orm, { store } from '@stonyx/orm';
|
|
2
2
|
import OrmRecord from './record.js';
|
|
3
3
|
import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
|
|
4
|
-
import { isOrmRecord } from './utils.js';
|
|
4
|
+
import { isOrmRecord, maxNumericId, NO_FREE_ID_ERROR } from './utils.js';
|
|
5
5
|
const defaultOptions = {
|
|
6
6
|
isDbRecord: false,
|
|
7
7
|
serialize: true,
|
|
@@ -185,9 +185,15 @@ function assignRecordId(modelName, rawData) {
|
|
|
185
185
|
// the one string that means "no id". `parseInt('')` is `NaN`, a record CAN be
|
|
186
186
|
// held under the key `NaN`, and orm-request.ts's body-id normalisation relies
|
|
187
187
|
// on `''` staying absent — otherwise `POST {"id":""}` answers 409 against a
|
|
188
|
-
// record it never named.
|
|
189
|
-
//
|
|
190
|
-
//
|
|
188
|
+
// record it never named.
|
|
189
|
+
//
|
|
190
|
+
// WHAT WIDENING THIS TO `!== undefined` ACTUALLY DOES, measured rather than
|
|
191
|
+
// asserted: `{id: ''}` early-returns, `parseInt('')` NaNs it, and the record
|
|
192
|
+
// lands on the store's `NaN` slot and OVERWRITES whatever is there — #203's
|
|
193
|
+
// own defect class. It does NOT turn access-filter-enforcement-test.ts
|
|
194
|
+
// assertion 44 red; an earlier revision of this comment claimed it did, which
|
|
195
|
+
// converted an unknown into a false assurance. AC6's BOUNDARY assertions are
|
|
196
|
+
// what catch it, and they only do so because they seed the `NaN` slot first.
|
|
191
197
|
if (rawData.id || rawData.id === 0)
|
|
192
198
|
return;
|
|
193
199
|
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
@@ -208,53 +214,142 @@ function assignRecordId(modelName, rawData) {
|
|
|
208
214
|
if (!storeMap)
|
|
209
215
|
throw new Error(`Cannot assign record ID: model "${modelName}" not found in store`);
|
|
210
216
|
const modelStore = Array.from(storeMap.values()).filter(isOrmRecord);
|
|
211
|
-
//
|
|
212
|
-
//
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
//
|
|
216
|
-
|
|
217
|
-
// that slot and overwrite it — exactly the defect being fixed, in a new
|
|
218
|
-
// disguise. This reduce cannot: non-numbers are skipped, and `NaN > max` is
|
|
219
|
-
// `false`. Pinned by AC2.
|
|
220
|
-
const maxId = modelStore.reduce((max, record) => {
|
|
221
|
-
const recordId = record.id;
|
|
222
|
-
return typeof recordId === 'number' && recordId > max ? recordId : max;
|
|
223
|
-
}, 0);
|
|
217
|
+
// ONE COPY of the max-numeric-id reduce, in src/utils.ts. There were three
|
|
218
|
+
// (here, StandaloneDB.create, and the #203 test helper) and `docs/
|
|
219
|
+
// improvements.md`'s WET Code category prescribes the extraction. What that
|
|
220
|
+
// helper must NOT be is `Math.max(...ids)`; the reason is measured and it is
|
|
221
|
+
// documented at the helper rather than duplicated here. Pinned by AC2.
|
|
222
|
+
const maxId = maxNumericId(modelStore);
|
|
224
223
|
// THE OCCUPANCY CHECK RUNS ON THE LANDING KEY, NOT ON THE RAW CANDIDATE, and
|
|
225
224
|
// the difference is a silent data loss rather than a nicety.
|
|
226
225
|
//
|
|
227
226
|
// `createRecord` looks the record up under `rawData.id` (:50) but WRITES it
|
|
228
227
|
// under `record.id` (:69) — the value after the model's declared id transform
|
|
229
|
-
// has run inside `serialize`.
|
|
230
|
-
//
|
|
231
|
-
//
|
|
232
|
-
//
|
|
233
|
-
//
|
|
234
|
-
// #205's lookup-key/landing-key divergence reappearing inside
|
|
235
|
-
// which is why AC4 exists and why `rawData.id` is set to the
|
|
236
|
-
//
|
|
228
|
+
// has run inside `serialize`. When the transform is not the identity those two
|
|
229
|
+
// differ, so a guard written as `storeMap.has(rawData.id)` checks a key the
|
|
230
|
+
// record will never occupy, misses an occupied slot and overwrites it —
|
|
231
|
+
// measured on an `uppercase`-id model: the guard checks `owner-1`, the record
|
|
232
|
+
// lands under `OWNER-1`, store size unchanged, no error. That is
|
|
233
|
+
// abofs/stonyx-orm#205's lookup-key/landing-key divergence reappearing inside
|
|
234
|
+
// #203's own fix, which is why AC4 exists and why `rawData.id` is set to the
|
|
235
|
+
// LANDING key below.
|
|
237
236
|
//
|
|
238
|
-
//
|
|
239
|
-
//
|
|
240
|
-
//
|
|
241
|
-
//
|
|
237
|
+
// THE SCOPE OF THAT CLAIM, stated rather than implied. Setting `rawData.id` to
|
|
238
|
+
// the landing key makes :50 and :69 agree for every IDEMPOTENT id transform —
|
|
239
|
+
// `number`, `float`, `string`, `passthrough`, `uppercase`, `trim`. It does NOT
|
|
240
|
+
// make them agree for `date` or `timestamp`: `transforms.date` returns a NEW
|
|
241
|
+
// object every call and a `Map` keys by identity, so `storeMap.has(landingKey)`
|
|
242
|
+
// is always `false` there and the occupancy check is vacuous. `dev` is broken
|
|
243
|
+
// for those types too — this is not a regression — but no comment here may
|
|
244
|
+
// claim a property it was not measured to have (#212 § AC5).
|
|
242
245
|
//
|
|
243
|
-
// Resolved ONCE, outside the
|
|
246
|
+
// Resolved ONCE, outside the walk: `getIdType` instantiates the model class,
|
|
244
247
|
// so resolving it per candidate would put a model construction on every
|
|
245
248
|
// iteration of a loop that exists to walk past occupied slots.
|
|
246
249
|
const toStoreKey = storeKeyDeriver(modelName);
|
|
247
|
-
|
|
248
|
-
|
|
250
|
+
// DOES THIS MODEL FILE ITS RECORDS UNDER STRING KEYS? Decided by RUNNING the
|
|
251
|
+
// model's own id transform once, not by matching a type NAME against a list:
|
|
252
|
+
// `Orm.instance.transforms` (main.ts:70) is a public, MUTABLE instance
|
|
253
|
+
// property, so any enumeration of "the string-ish types" written here would be
|
|
254
|
+
// wrong the moment a consumer registers one.
|
|
255
|
+
const stringKeyed = typeof toStoreKey(maxId + 1) === 'string';
|
|
256
|
+
// THE CANDIDATE FOR A STRING-KEYED MODEL IS NOT A BARE NUMBER, and this is
|
|
257
|
+
// abofs/stonyx-orm#209 — which is REOPENED — not aesthetics.
|
|
258
|
+
//
|
|
259
|
+
// `orm-request.ts`'s `coerceId` (:322) resolves a NUMERIC-LOOKING string to a
|
|
260
|
+
// NUMBER on every id-bearing surface, while a model declaring
|
|
261
|
+
// `id = attr('string')` files under the STRING key. So a server-assigned `'1'`
|
|
262
|
+
// produces a record that is created and then NOT ADDRESSABLE. Measured over
|
|
263
|
+
// the route, owner store `{'1': ...}`:
|
|
264
|
+
//
|
|
265
|
+
// GET /owners/1 -> 404 (the record exists)
|
|
266
|
+
// DELETE /owners/1 -> 404
|
|
267
|
+
// GET /owners/owner-1 -> 200
|
|
268
|
+
//
|
|
269
|
+
// and `_withHooks` (:1185) hands an after-`create` hook
|
|
270
|
+
// `context.record === undefined` for the same reason. `dev` assigned `'bob1'`,
|
|
271
|
+
// which is not numeric-looking, so `dev` has neither problem: a bare-number
|
|
272
|
+
// candidate would move #209 from "a caller supplied a numeric-looking id" onto
|
|
273
|
+
// the DEFAULT path for every server-assigned create on every string-id model.
|
|
274
|
+
// Prefixing with the model name keeps #209's population exactly as narrow as
|
|
275
|
+
// it already was, without touching the one shared coercion or the assertion
|
|
276
|
+
// that pins #209 open. Pinned by AC3.
|
|
277
|
+
const toCandidate = stringKeyed
|
|
278
|
+
? (value) => `${modelName}-${value}`
|
|
279
|
+
: (value) => value;
|
|
280
|
+
// `maxId + 1` is the id AC1 pins: strictly greater than every numeric key
|
|
281
|
+
// present. IT IS NOT ALWAYS AVAILABLE, and that gap was a live denial of
|
|
282
|
+
// service. Float64 has no integer successor at or above 2^53, so
|
|
283
|
+
// `maxId + 1 === maxId` for every `maxId >= 9007199254740992` and `+ 1` inside
|
|
284
|
+
// the walk is a NO-OP there. One record filed under that key — which an
|
|
285
|
+
// unauthenticated `POST {"id":9007199254740992}` puts there, and which reaches
|
|
286
|
+
// even a filter-protected collection through has-many.ts:65 (#207), a channel
|
|
287
|
+
// GATE 0 does not cover — made the walk unable to advance, so it exhausted its
|
|
288
|
+
// budget and threw on EVERY subsequent server-assigned create, permanently,
|
|
289
|
+
// until that record was deleted. Measured over the route: 200, then 500 for
|
|
290
|
+
// every no-id create. `dev` answers 200.
|
|
291
|
+
//
|
|
292
|
+
// So a store holding one adversarial record must not disable its collection.
|
|
293
|
+
// When "above the max" is not a usable strategy the walk RESTARTS FROM 1: the
|
|
294
|
+
// store holds at most `size` keys, so one of `1 .. size + 1` is always free
|
|
295
|
+
// under an injective id transform. Pinned by AC7.
|
|
296
|
+
const start = maxId + 1;
|
|
297
|
+
let landingKey = firstFreeKey(storeMap, toStoreKey, toCandidate, start);
|
|
298
|
+
// THE RESTART, and it is the whole of the ceiling fix. Killing mutation:
|
|
299
|
+
// delete this block -> AC7 goes red (the route answers 409 instead of the
|
|
300
|
+
// created resource).
|
|
301
|
+
if (landingKey === NO_FREE_KEY && start !== 1) {
|
|
302
|
+
landingKey = firstFreeKey(storeMap, toStoreKey, toCandidate, 1);
|
|
303
|
+
}
|
|
304
|
+
if (landingKey === NO_FREE_KEY) {
|
|
305
|
+
// Reachable only with a NON-INJECTIVE id transform — see `firstFreeKey`.
|
|
306
|
+
// `createHandler` matches this message and answers 409 rather than letting it
|
|
307
|
+
// reach express's default handler, which serialises a stack trace with
|
|
308
|
+
// absolute install paths outside NODE_ENV=production (the hazard
|
|
309
|
+
// orm-request.ts:553-558 exists to name). Pinned by AC8.
|
|
310
|
+
throw new Error(`${NO_FREE_ID_ERROR} for model "${modelName}"`);
|
|
311
|
+
}
|
|
312
|
+
rawData.id = landingKey;
|
|
313
|
+
}
|
|
314
|
+
// Returned instead of a key so that "no key" cannot be confused with a transform
|
|
315
|
+
// that legitimately produced `undefined` or `null`.
|
|
316
|
+
const NO_FREE_KEY = Symbol('no free store key');
|
|
317
|
+
/**
|
|
318
|
+
* The first store key at or above `start` that no record occupies, walking
|
|
319
|
+
* candidate ids upward, or `NO_FREE_KEY` if the walk cannot reach one.
|
|
320
|
+
*/
|
|
321
|
+
function firstFreeKey(storeMap, toStoreKey, toCandidate, start) {
|
|
322
|
+
let candidate = start;
|
|
323
|
+
let landingKey = toStoreKey(toCandidate(candidate));
|
|
249
324
|
let attempts = 0;
|
|
250
325
|
while (storeMap.has(landingKey)) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
326
|
+
// THE BOUND IS EXACTLY TIGHT, not conservative: this walk tries
|
|
327
|
+
// `storeMap.size + 1` DISTINCT candidates against at most `storeMap.size`
|
|
328
|
+
// occupied keys, so under an injective `toStoreKey` it provably cannot fire.
|
|
329
|
+
// Under a non-injective one it provably terminates — and that is a reachable
|
|
330
|
+
// consumer state rather than a hypothesis: `transforms.boolean`
|
|
331
|
+
// (transforms.ts:4) collapses every candidate onto `true`/`false`, and
|
|
332
|
+
// `Orm.instance.transforms` (main.ts:70) is public and MUTABLE, so a consumer
|
|
333
|
+
// can register an arbitrary non-injective transform and name it as an id
|
|
334
|
+
// type. Without this, a no-id create spins forever inside a synchronous store
|
|
335
|
+
// walk and pins a worker, which is worse than either collision policy. Its
|
|
336
|
+
// EXISTENCE and its THRESHOLD are both pinned by AC8: deleting it makes AC8
|
|
337
|
+
// HANG rather than fail, and weakening it to fire on the first collision
|
|
338
|
+
// makes AC8.1 red.
|
|
339
|
+
if (++attempts > storeMap.size)
|
|
340
|
+
return NO_FREE_KEY;
|
|
341
|
+
// NOTE FOR ANYONE ADDING A SECOND EXIT HERE. A `candidate + 1 === candidate`
|
|
342
|
+
// float-saturation check was written, measured, and REMOVED: with the
|
|
343
|
+
// restart-from-1 above in place, deleting the saturation check leaves the
|
|
344
|
+
// whole suite green, because the budget reaches the same `NO_FREE_KEY` one
|
|
345
|
+
// pass later and the restart still answers. An unkillable guard in a change
|
|
346
|
+
// whose deliverable is falsifiable coverage is exactly what this story exists
|
|
347
|
+
// to stop shipping. `+ 1` being a no-op at 2^53 costs `size` extra `Map.has`
|
|
348
|
+
// calls on that one path and changes no outcome.
|
|
254
349
|
candidate += 1;
|
|
255
|
-
landingKey = toStoreKey(candidate);
|
|
350
|
+
landingKey = toStoreKey(toCandidate(candidate));
|
|
256
351
|
}
|
|
257
|
-
|
|
352
|
+
return landingKey;
|
|
258
353
|
}
|
|
259
354
|
/**
|
|
260
355
|
* Returns the derivation that maps an id VALUE to the store KEY a record
|
|
@@ -264,9 +359,44 @@ function assignRecordId(modelName, rawData) {
|
|
|
264
359
|
function storeKeyDeriver(modelName) {
|
|
265
360
|
const idType = getIdType(modelName);
|
|
266
361
|
const transform = idType ? Orm.instance?.transforms?.[idType] : undefined;
|
|
362
|
+
// SURVIVOR, RETAINED, with its reachability condition stated rather than left
|
|
363
|
+
// silent — `docs/project-structure.md` § "unkillable code reads as coverage and
|
|
364
|
+
// is not" is the standing rule and it applies outside orm-request.ts too.
|
|
365
|
+
//
|
|
366
|
+
// No mutation in this repo can kill this branch, and that is STRUCTURAL rather
|
|
367
|
+
// than an untested gap: `Model` declares `id = attr('number')` (model.ts:15) so
|
|
368
|
+
// every registered model has an id type; `ModelProperty` refuses a type with no
|
|
369
|
+
// registered transform (model-property.ts:4) so a declared type always
|
|
370
|
+
// resolves; and `getIdType` can therefore only return `undefined` when
|
|
371
|
+
// `getRecordClasses` yields no `modelClass` — in which case `createRecord`
|
|
372
|
+
// throws at :62 a few lines later regardless, so no record is ever filed
|
|
373
|
+
// through this branch.
|
|
374
|
+
//
|
|
375
|
+
// BECOMES REACHABLE if a model can be declared without an `id` property, if a
|
|
376
|
+
// store map can exist for a model with no registered class, or if
|
|
377
|
+
// `createRecord` stops constructing the model class. Kept rather than deleted
|
|
378
|
+
// because the alternative on that path is a `transform is not a function`
|
|
379
|
+
// TypeError, and because identity is exactly what `createRecord` would file
|
|
380
|
+
// under when no transform exists — the two agree, which is the property AC4 is
|
|
381
|
+
// about.
|
|
267
382
|
if (typeof transform !== 'function')
|
|
268
383
|
return value => value;
|
|
269
|
-
return value =>
|
|
384
|
+
return value => {
|
|
385
|
+
try {
|
|
386
|
+
return transform(value);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// `uppercase` and `trim` (transforms.ts:11-12) call a string method on the
|
|
390
|
+
// value directly, so a NUMERIC candidate throws `value?.toUpperCase is not
|
|
391
|
+
// a function`. On `dev` they never saw one — `lastRecord.id + 1` on a
|
|
392
|
+
// string id is a string — so feeding them a number here would regress a
|
|
393
|
+
// legal, registered id type into an uncaught 500. The retry feeds the
|
|
394
|
+
// string form, which is the shape an id actually arrives in off a JSON body
|
|
395
|
+
// or a URL param. A transform that throws on BOTH shapes still propagates.
|
|
396
|
+
// Pinned by AC9.
|
|
397
|
+
return transform(String(value));
|
|
398
|
+
}
|
|
399
|
+
};
|
|
270
400
|
}
|
|
271
401
|
function getIdType(modelName) {
|
|
272
402
|
const modelClass = Orm.instance?.getRecordClasses(modelName).modelClass;
|
package/dist/orm-request.js
CHANGED
|
@@ -183,7 +183,7 @@ import { getPluralName } from './plural-registry.js';
|
|
|
183
183
|
import { getBeforeHooks, getAfterHooks } from './hooks.js';
|
|
184
184
|
import config from 'stonyx/config';
|
|
185
185
|
import log from 'stonyx/log';
|
|
186
|
-
import { isOrmRecord } from './utils.js';
|
|
186
|
+
import { isOrmRecord, NO_FREE_ID_ERROR } from './utils.js';
|
|
187
187
|
const methodAccessMap = {
|
|
188
188
|
GET: 'read',
|
|
189
189
|
POST: 'create',
|
|
@@ -676,7 +676,39 @@ export default class OrmRequest extends Request {
|
|
|
676
676
|
// is true for a record the request did not create. The map's size is the
|
|
677
677
|
// only O(1) signal that distinguishes an insert from an overwrite.
|
|
678
678
|
const slotsBefore = store.get(model)?.size ?? 0;
|
|
679
|
-
|
|
679
|
+
// THE ONE `createRecord` FAILURE THIS ROUTE ANSWERS RATHER THAN
|
|
680
|
+
// PROPAGATES, and it is narrow on purpose.
|
|
681
|
+
//
|
|
682
|
+
// `assignRecordId` throws when it cannot derive a free store key for a
|
|
683
|
+
// server-assigned id. Unguarded that rejection is auto-forwarded -- there
|
|
684
|
+
// is no catch here, none in @stonyx/rest-server's dispatcher
|
|
685
|
+
// (dist/request.js:41-70), and express 5 hands it to its default error
|
|
686
|
+
// handler, which serialises the STACK, with absolute install paths and the
|
|
687
|
+
// internal module graph, to an unauthenticated caller outside
|
|
688
|
+
// NODE_ENV=production. That is the hazard :553-558 already names in this
|
|
689
|
+
// file, and every sibling refusal in this handler returns an integer
|
|
690
|
+
// status instead. So this one returns 409, matching the client-duplicate
|
|
691
|
+
// refusal at :713: the caller asked for a record and the collection has no
|
|
692
|
+
// id to give it.
|
|
693
|
+
//
|
|
694
|
+
// MATCHED ON THE SHARED PREFIX, not on a literal, and NOT by catching
|
|
695
|
+
// everything: `createRecord` also throws for "ORM is not ready", a
|
|
696
|
+
// read-only view and an unregistered model store, and turning any of those
|
|
697
|
+
// into a 409 would report a configuration fault as a conflict. Anything
|
|
698
|
+
// else is re-thrown unchanged.
|
|
699
|
+
let created;
|
|
700
|
+
try {
|
|
701
|
+
created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
|
|
702
|
+
}
|
|
703
|
+
catch (error) {
|
|
704
|
+
if (!(error instanceof Error) || !error.message.startsWith(NO_FREE_ID_ERROR))
|
|
705
|
+
throw error;
|
|
706
|
+
// Not silently. A collection that can no longer assign an id is a
|
|
707
|
+
// configuration fault (a non-injective id transform), and a bare 409
|
|
708
|
+
// with no diagnostic is indistinguishable from an ordinary duplicate.
|
|
709
|
+
log.error?.(`[@stonyx/orm] ${error.message}`);
|
|
710
|
+
return 409; // Conflict
|
|
711
|
+
}
|
|
680
712
|
const record = isOrmRecord(created) ? created : null;
|
|
681
713
|
if (!record)
|
|
682
714
|
return 500;
|
|
@@ -700,11 +732,32 @@ export default class OrmRequest extends Request {
|
|
|
700
732
|
//
|
|
701
733
|
// Both conditions are required and neither implies the other:
|
|
702
734
|
// createdNewSlot -- the store grew, so this request inserted rather
|
|
703
|
-
// than overwrote.
|
|
704
|
-
//
|
|
705
|
-
// last-INSERTED + 1,
|
|
706
|
-
//
|
|
707
|
-
//
|
|
735
|
+
// than overwrote. SURVIVOR AS OF #203, AND THAT IS
|
|
736
|
+
// WHAT THIS NOTE IS FOR. It used to be killable:
|
|
737
|
+
// `assignRecordId` returned last-INSERTED + 1, so a
|
|
738
|
+
// server-assigned id could land on an occupied slot,
|
|
739
|
+
// `createRecord` updated in place, and removing this
|
|
740
|
+
// half turned access-filter-enforcement-test.ts
|
|
741
|
+
// assertion 31 red. #203 closed that: the
|
|
742
|
+
// server-assigned path now walks past occupied keys,
|
|
743
|
+
// so no create reaching here can overwrite. Measured
|
|
744
|
+
// -- delete `createdNewSlot &&` below: `dev` gives
|
|
745
|
+
// 55 pass / 1 fail with assertion 31 RED, this tree
|
|
746
|
+
// gives 56 pass / 0 fail, GREEN.
|
|
747
|
+
// KEPT ANYWAY, AND NOT FOR THE OLD REASON. Without
|
|
748
|
+
// it a denied create becomes `store.remove` on a key
|
|
749
|
+
// the caller may have influenced, which :815-820
|
|
750
|
+
// records as having been an unauthenticated deletion
|
|
751
|
+
// primitive across the whole id space. BECOMES
|
|
752
|
+
// KILLABLE AGAIN the moment any caller-supplied id
|
|
753
|
+
// can reach `createRecord` from this handler --
|
|
754
|
+
// which is exactly what has-many.ts:65 and
|
|
755
|
+
// belongs-to.ts:45 already do for ANOTHER model's
|
|
756
|
+
// store (abofs/stonyx-orm#207), and what a third
|
|
757
|
+
// un-stripped id channel would do for this one
|
|
758
|
+
// (#204). Do not delete it on the strength of #203
|
|
759
|
+
// being closed; that is the reasoning :862-867 warns
|
|
760
|
+
// about, one level up.
|
|
708
761
|
// identity -- the slot still holds the object we just created,
|
|
709
762
|
// so nothing between createRecord and here replaced
|
|
710
763
|
// it. Deleting this half SURVIVES the suite, and it
|
package/dist/standalone-db.js
CHANGED
|
@@ -7,6 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import fs from 'fs/promises';
|
|
9
9
|
import path from 'path';
|
|
10
|
+
// `./utils.js` pulls in `@stonyx/utils/string` and nothing else -- no ORM
|
|
11
|
+
// bootstrap, no `@stonyx/orm` index, no side effects -- so the "no framework
|
|
12
|
+
// dependencies" property above still holds.
|
|
13
|
+
import { maxNumericId } from './utils.js';
|
|
10
14
|
export default class StandaloneDB {
|
|
11
15
|
mode;
|
|
12
16
|
dbPath;
|
|
@@ -102,11 +106,19 @@ export default class StandaloneDB {
|
|
|
102
106
|
async create(collection, data) {
|
|
103
107
|
const records = await this.readCollection(collection);
|
|
104
108
|
if (!data.id) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
// SHARED WITH `assignRecordId` (src/manage-record.ts), which is the other
|
|
110
|
+
// place this repo picks a server-assigned id. It was a second copy of the
|
|
111
|
+
// reduce, and nothing here pointed at it — a maintainer editing this
|
|
112
|
+
// method could not discover the other existed. See `maxNumericId` for why
|
|
113
|
+
// it is not `Math.max` (abofs/stonyx-orm#203).
|
|
114
|
+
//
|
|
115
|
+
// THE TWO ARE NOT THE SAME FUNCTION beyond this line, deliberately.
|
|
116
|
+
// `StandaloneDB` has no model, id-type or transform concept, so `maxId + 1`
|
|
117
|
+
// IS its store key; `assignRecordId` has to map the candidate through the
|
|
118
|
+
// model's declared id transform first, and then walk past occupied keys.
|
|
119
|
+
// Transplanting this method's remaining logic into the ORM reproduces
|
|
120
|
+
// #203's landing-key defect exactly — which is what AC4 pins.
|
|
121
|
+
data.id = maxNumericId(records) + 1;
|
|
110
122
|
}
|
|
111
123
|
// Check for duplicate id
|
|
112
124
|
const existing = records.find(r => r.id === data.id);
|
package/dist/utils.d.ts
CHANGED
|
@@ -5,3 +5,47 @@ export declare function isDbError(error: unknown): error is {
|
|
|
5
5
|
};
|
|
6
6
|
export declare function isOrmRecord(value: unknown): value is OrmRecord;
|
|
7
7
|
export declare function pluralize(word: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* The highest NUMERIC id held by a set of records, or `0` when there is none.
|
|
10
|
+
*
|
|
11
|
+
* ONE COPY, and the duplication it replaces is the reason it lives here. Three
|
|
12
|
+
* near-identical reduces existed at once: `assignRecordId` (server-assigned id
|
|
13
|
+
* selection), `StandaloneDB.create` (src/standalone-db.ts) and the #203 test
|
|
14
|
+
* helper. `docs/improvements.md`'s standing WET Code category prescribes
|
|
15
|
+
* exactly this remedy -- extract into the module that already acts as the
|
|
16
|
+
* shared utility -- and `assignRecordId` already imported `isOrmRecord` from
|
|
17
|
+
* here.
|
|
18
|
+
*
|
|
19
|
+
* NON-NUMBERS ARE SKIPPED RATHER THAN COERCED TO `0`, AND THAT IS STYLISTIC.
|
|
20
|
+
* `StandaloneDB`'s shape mapped them to `0`, which can never beat a seed of
|
|
21
|
+
* `0`. Measured over eleven input classes (`[]`, `1`, `NaN`, `'5'`, `'abc'`,
|
|
22
|
+
* `-3`, `0`, `null`, `undefined`, `Infinity`, and mixed arrays) the two shapes
|
|
23
|
+
* produce IDENTICAL output on every one. In particular `typeof NaN` is
|
|
24
|
+
* `'number'`, so NEITHER shape coerces `NaN` -- both reject it on `NaN > max`,
|
|
25
|
+
* which is `false`. An earlier revision of this code asserted that the skip was
|
|
26
|
+
* what made the `NaN` case work; it is not, the comparison is, and that claim
|
|
27
|
+
* has been removed rather than left standing.
|
|
28
|
+
*
|
|
29
|
+
* WHAT IS LOAD-BEARING is that this is not `Math.max(...ids)`. `Math.max`
|
|
30
|
+
* returns `NaN` if any operand is `NaN`, and a record CAN be held under the key
|
|
31
|
+
* `NaN` -- so the obvious fix assigns `NaN`, lands on that slot and overwrites
|
|
32
|
+
* it, which is abofs/stonyx-orm#203 in a new disguise. Pinned by
|
|
33
|
+
* test/unit/assign-record-id-test.ts AC2; before that file existed the whole
|
|
34
|
+
* suite scored 951/0 under exactly that fix.
|
|
35
|
+
*/
|
|
36
|
+
export declare function maxNumericId(records: {
|
|
37
|
+
id?: unknown;
|
|
38
|
+
}[]): number;
|
|
39
|
+
/**
|
|
40
|
+
* The message prefix `assignRecordId` throws with when no free id can be
|
|
41
|
+
* derived for a model, and the ONE string `createHandler` matches on to answer
|
|
42
|
+
* `409` instead of letting the rejection reach express's default handler.
|
|
43
|
+
*
|
|
44
|
+
* It lives here rather than in either file because both need it and neither
|
|
45
|
+
* should own a copy: a literal in two places is how the two id coercions in
|
|
46
|
+
* orm-request.ts drifted apart (see `coerceId`). The repo has no error codes
|
|
47
|
+
* and no custom error classes -- 24 bare `throw new Error` sites across `src/`
|
|
48
|
+
* -- so a shared prefix is the narrowest way to make ONE failure distinguishable
|
|
49
|
+
* without inventing an error taxonomy this codebase does not use.
|
|
50
|
+
*/
|
|
51
|
+
export declare const NO_FREE_ID_ERROR = "Cannot assign record ID: no free id available";
|
package/dist/utils.js
CHANGED
|
@@ -15,3 +15,50 @@ export function pluralize(word) {
|
|
|
15
15
|
}
|
|
16
16
|
return basePluralize(word);
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* The highest NUMERIC id held by a set of records, or `0` when there is none.
|
|
20
|
+
*
|
|
21
|
+
* ONE COPY, and the duplication it replaces is the reason it lives here. Three
|
|
22
|
+
* near-identical reduces existed at once: `assignRecordId` (server-assigned id
|
|
23
|
+
* selection), `StandaloneDB.create` (src/standalone-db.ts) and the #203 test
|
|
24
|
+
* helper. `docs/improvements.md`'s standing WET Code category prescribes
|
|
25
|
+
* exactly this remedy -- extract into the module that already acts as the
|
|
26
|
+
* shared utility -- and `assignRecordId` already imported `isOrmRecord` from
|
|
27
|
+
* here.
|
|
28
|
+
*
|
|
29
|
+
* NON-NUMBERS ARE SKIPPED RATHER THAN COERCED TO `0`, AND THAT IS STYLISTIC.
|
|
30
|
+
* `StandaloneDB`'s shape mapped them to `0`, which can never beat a seed of
|
|
31
|
+
* `0`. Measured over eleven input classes (`[]`, `1`, `NaN`, `'5'`, `'abc'`,
|
|
32
|
+
* `-3`, `0`, `null`, `undefined`, `Infinity`, and mixed arrays) the two shapes
|
|
33
|
+
* produce IDENTICAL output on every one. In particular `typeof NaN` is
|
|
34
|
+
* `'number'`, so NEITHER shape coerces `NaN` -- both reject it on `NaN > max`,
|
|
35
|
+
* which is `false`. An earlier revision of this code asserted that the skip was
|
|
36
|
+
* what made the `NaN` case work; it is not, the comparison is, and that claim
|
|
37
|
+
* has been removed rather than left standing.
|
|
38
|
+
*
|
|
39
|
+
* WHAT IS LOAD-BEARING is that this is not `Math.max(...ids)`. `Math.max`
|
|
40
|
+
* returns `NaN` if any operand is `NaN`, and a record CAN be held under the key
|
|
41
|
+
* `NaN` -- so the obvious fix assigns `NaN`, lands on that slot and overwrites
|
|
42
|
+
* it, which is abofs/stonyx-orm#203 in a new disguise. Pinned by
|
|
43
|
+
* test/unit/assign-record-id-test.ts AC2; before that file existed the whole
|
|
44
|
+
* suite scored 951/0 under exactly that fix.
|
|
45
|
+
*/
|
|
46
|
+
export function maxNumericId(records) {
|
|
47
|
+
return records.reduce((max, record) => {
|
|
48
|
+
const { id } = record;
|
|
49
|
+
return typeof id === 'number' && id > max ? id : max;
|
|
50
|
+
}, 0);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The message prefix `assignRecordId` throws with when no free id can be
|
|
54
|
+
* derived for a model, and the ONE string `createHandler` matches on to answer
|
|
55
|
+
* `409` instead of letting the rejection reach express's default handler.
|
|
56
|
+
*
|
|
57
|
+
* It lives here rather than in either file because both need it and neither
|
|
58
|
+
* should own a copy: a literal in two places is how the two id coercions in
|
|
59
|
+
* orm-request.ts drifted apart (see `coerceId`). The repo has no error codes
|
|
60
|
+
* and no custom error classes -- 24 bare `throw new Error` sites across `src/`
|
|
61
|
+
* -- so a shared prefix is the narrowest way to make ONE failure distinguishable
|
|
62
|
+
* without inventing an error taxonomy this codebase does not use.
|
|
63
|
+
*/
|
|
64
|
+
export const NO_FREE_ID_ERROR = 'Cannot assign record ID: no free id available';
|
package/package.json
CHANGED
package/src/manage-record.ts
CHANGED
|
@@ -2,7 +2,7 @@ import Orm, { store } from '@stonyx/orm';
|
|
|
2
2
|
import OrmRecord from './record.js';
|
|
3
3
|
import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
|
|
4
4
|
import type Serializer from './serializer.js';
|
|
5
|
-
import { isOrmRecord } from './utils.js';
|
|
5
|
+
import { isOrmRecord, maxNumericId, NO_FREE_ID_ERROR } from './utils.js';
|
|
6
6
|
|
|
7
7
|
interface CreateRecordOptions {
|
|
8
8
|
isDbRecord?: boolean;
|
|
@@ -227,9 +227,15 @@ function assignRecordId(modelName: string, rawData: { [key: string]: unknown }):
|
|
|
227
227
|
// the one string that means "no id". `parseInt('')` is `NaN`, a record CAN be
|
|
228
228
|
// held under the key `NaN`, and orm-request.ts's body-id normalisation relies
|
|
229
229
|
// on `''` staying absent — otherwise `POST {"id":""}` answers 409 against a
|
|
230
|
-
// record it never named.
|
|
231
|
-
//
|
|
232
|
-
//
|
|
230
|
+
// record it never named.
|
|
231
|
+
//
|
|
232
|
+
// WHAT WIDENING THIS TO `!== undefined` ACTUALLY DOES, measured rather than
|
|
233
|
+
// asserted: `{id: ''}` early-returns, `parseInt('')` NaNs it, and the record
|
|
234
|
+
// lands on the store's `NaN` slot and OVERWRITES whatever is there — #203's
|
|
235
|
+
// own defect class. It does NOT turn access-filter-enforcement-test.ts
|
|
236
|
+
// assertion 44 red; an earlier revision of this comment claimed it did, which
|
|
237
|
+
// converted an unknown into a false assurance. AC6's BOUNDARY assertions are
|
|
238
|
+
// what catch it, and they only do so because they seed the `NaN` slot first.
|
|
233
239
|
if (rawData.id || rawData.id === 0) return;
|
|
234
240
|
|
|
235
241
|
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
@@ -251,59 +257,158 @@ function assignRecordId(modelName: string, rawData: { [key: string]: unknown }):
|
|
|
251
257
|
if (!storeMap) throw new Error(`Cannot assign record ID: model "${modelName}" not found in store`);
|
|
252
258
|
const modelStore = Array.from(storeMap.values()).filter(isOrmRecord);
|
|
253
259
|
|
|
254
|
-
//
|
|
255
|
-
//
|
|
256
|
-
//
|
|
257
|
-
//
|
|
258
|
-
//
|
|
259
|
-
|
|
260
|
-
// that slot and overwrite it — exactly the defect being fixed, in a new
|
|
261
|
-
// disguise. This reduce cannot: non-numbers are skipped, and `NaN > max` is
|
|
262
|
-
// `false`. Pinned by AC2.
|
|
263
|
-
const maxId = modelStore.reduce((max: number, record) => {
|
|
264
|
-
const recordId = record.id as unknown;
|
|
265
|
-
|
|
266
|
-
return typeof recordId === 'number' && recordId > max ? recordId : max;
|
|
267
|
-
}, 0);
|
|
260
|
+
// ONE COPY of the max-numeric-id reduce, in src/utils.ts. There were three
|
|
261
|
+
// (here, StandaloneDB.create, and the #203 test helper) and `docs/
|
|
262
|
+
// improvements.md`'s WET Code category prescribes the extraction. What that
|
|
263
|
+
// helper must NOT be is `Math.max(...ids)`; the reason is measured and it is
|
|
264
|
+
// documented at the helper rather than duplicated here. Pinned by AC2.
|
|
265
|
+
const maxId = maxNumericId(modelStore);
|
|
268
266
|
|
|
269
267
|
// THE OCCUPANCY CHECK RUNS ON THE LANDING KEY, NOT ON THE RAW CANDIDATE, and
|
|
270
268
|
// the difference is a silent data loss rather than a nicety.
|
|
271
269
|
//
|
|
272
270
|
// `createRecord` looks the record up under `rawData.id` (:50) but WRITES it
|
|
273
271
|
// under `record.id` (:69) — the value after the model's declared id transform
|
|
274
|
-
// has run inside `serialize`.
|
|
275
|
-
//
|
|
276
|
-
//
|
|
277
|
-
//
|
|
278
|
-
//
|
|
279
|
-
// #205's lookup-key/landing-key divergence reappearing inside
|
|
280
|
-
// which is why AC4 exists and why `rawData.id` is set to the
|
|
281
|
-
//
|
|
272
|
+
// has run inside `serialize`. When the transform is not the identity those two
|
|
273
|
+
// differ, so a guard written as `storeMap.has(rawData.id)` checks a key the
|
|
274
|
+
// record will never occupy, misses an occupied slot and overwrites it —
|
|
275
|
+
// measured on an `uppercase`-id model: the guard checks `owner-1`, the record
|
|
276
|
+
// lands under `OWNER-1`, store size unchanged, no error. That is
|
|
277
|
+
// abofs/stonyx-orm#205's lookup-key/landing-key divergence reappearing inside
|
|
278
|
+
// #203's own fix, which is why AC4 exists and why `rawData.id` is set to the
|
|
279
|
+
// LANDING key below.
|
|
282
280
|
//
|
|
283
|
-
//
|
|
284
|
-
//
|
|
285
|
-
//
|
|
286
|
-
//
|
|
281
|
+
// THE SCOPE OF THAT CLAIM, stated rather than implied. Setting `rawData.id` to
|
|
282
|
+
// the landing key makes :50 and :69 agree for every IDEMPOTENT id transform —
|
|
283
|
+
// `number`, `float`, `string`, `passthrough`, `uppercase`, `trim`. It does NOT
|
|
284
|
+
// make them agree for `date` or `timestamp`: `transforms.date` returns a NEW
|
|
285
|
+
// object every call and a `Map` keys by identity, so `storeMap.has(landingKey)`
|
|
286
|
+
// is always `false` there and the occupancy check is vacuous. `dev` is broken
|
|
287
|
+
// for those types too — this is not a regression — but no comment here may
|
|
288
|
+
// claim a property it was not measured to have (#212 § AC5).
|
|
287
289
|
//
|
|
288
|
-
// Resolved ONCE, outside the
|
|
290
|
+
// Resolved ONCE, outside the walk: `getIdType` instantiates the model class,
|
|
289
291
|
// so resolving it per candidate would put a model construction on every
|
|
290
292
|
// iteration of a loop that exists to walk past occupied slots.
|
|
291
293
|
const toStoreKey = storeKeyDeriver(modelName);
|
|
292
294
|
|
|
293
|
-
|
|
294
|
-
|
|
295
|
+
// DOES THIS MODEL FILE ITS RECORDS UNDER STRING KEYS? Decided by RUNNING the
|
|
296
|
+
// model's own id transform once, not by matching a type NAME against a list:
|
|
297
|
+
// `Orm.instance.transforms` (main.ts:70) is a public, MUTABLE instance
|
|
298
|
+
// property, so any enumeration of "the string-ish types" written here would be
|
|
299
|
+
// wrong the moment a consumer registers one.
|
|
300
|
+
const stringKeyed = typeof toStoreKey(maxId + 1) === 'string';
|
|
301
|
+
|
|
302
|
+
// THE CANDIDATE FOR A STRING-KEYED MODEL IS NOT A BARE NUMBER, and this is
|
|
303
|
+
// abofs/stonyx-orm#209 — which is REOPENED — not aesthetics.
|
|
304
|
+
//
|
|
305
|
+
// `orm-request.ts`'s `coerceId` (:322) resolves a NUMERIC-LOOKING string to a
|
|
306
|
+
// NUMBER on every id-bearing surface, while a model declaring
|
|
307
|
+
// `id = attr('string')` files under the STRING key. So a server-assigned `'1'`
|
|
308
|
+
// produces a record that is created and then NOT ADDRESSABLE. Measured over
|
|
309
|
+
// the route, owner store `{'1': ...}`:
|
|
310
|
+
//
|
|
311
|
+
// GET /owners/1 -> 404 (the record exists)
|
|
312
|
+
// DELETE /owners/1 -> 404
|
|
313
|
+
// GET /owners/owner-1 -> 200
|
|
314
|
+
//
|
|
315
|
+
// and `_withHooks` (:1185) hands an after-`create` hook
|
|
316
|
+
// `context.record === undefined` for the same reason. `dev` assigned `'bob1'`,
|
|
317
|
+
// which is not numeric-looking, so `dev` has neither problem: a bare-number
|
|
318
|
+
// candidate would move #209 from "a caller supplied a numeric-looking id" onto
|
|
319
|
+
// the DEFAULT path for every server-assigned create on every string-id model.
|
|
320
|
+
// Prefixing with the model name keeps #209's population exactly as narrow as
|
|
321
|
+
// it already was, without touching the one shared coercion or the assertion
|
|
322
|
+
// that pins #209 open. Pinned by AC3.
|
|
323
|
+
const toCandidate = stringKeyed
|
|
324
|
+
? (value: number) => `${modelName}-${value}`
|
|
325
|
+
: (value: number) => value;
|
|
326
|
+
|
|
327
|
+
// `maxId + 1` is the id AC1 pins: strictly greater than every numeric key
|
|
328
|
+
// present. IT IS NOT ALWAYS AVAILABLE, and that gap was a live denial of
|
|
329
|
+
// service. Float64 has no integer successor at or above 2^53, so
|
|
330
|
+
// `maxId + 1 === maxId` for every `maxId >= 9007199254740992` and `+ 1` inside
|
|
331
|
+
// the walk is a NO-OP there. One record filed under that key — which an
|
|
332
|
+
// unauthenticated `POST {"id":9007199254740992}` puts there, and which reaches
|
|
333
|
+
// even a filter-protected collection through has-many.ts:65 (#207), a channel
|
|
334
|
+
// GATE 0 does not cover — made the walk unable to advance, so it exhausted its
|
|
335
|
+
// budget and threw on EVERY subsequent server-assigned create, permanently,
|
|
336
|
+
// until that record was deleted. Measured over the route: 200, then 500 for
|
|
337
|
+
// every no-id create. `dev` answers 200.
|
|
338
|
+
//
|
|
339
|
+
// So a store holding one adversarial record must not disable its collection.
|
|
340
|
+
// When "above the max" is not a usable strategy the walk RESTARTS FROM 1: the
|
|
341
|
+
// store holds at most `size` keys, so one of `1 .. size + 1` is always free
|
|
342
|
+
// under an injective id transform. Pinned by AC7.
|
|
343
|
+
const start = maxId + 1;
|
|
344
|
+
let landingKey = firstFreeKey(storeMap, toStoreKey, toCandidate, start);
|
|
345
|
+
|
|
346
|
+
// THE RESTART, and it is the whole of the ceiling fix. Killing mutation:
|
|
347
|
+
// delete this block -> AC7 goes red (the route answers 409 instead of the
|
|
348
|
+
// created resource).
|
|
349
|
+
if (landingKey === NO_FREE_KEY && start !== 1) {
|
|
350
|
+
landingKey = firstFreeKey(storeMap, toStoreKey, toCandidate, 1);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (landingKey === NO_FREE_KEY) {
|
|
354
|
+
// Reachable only with a NON-INJECTIVE id transform — see `firstFreeKey`.
|
|
355
|
+
// `createHandler` matches this message and answers 409 rather than letting it
|
|
356
|
+
// reach express's default handler, which serialises a stack trace with
|
|
357
|
+
// absolute install paths outside NODE_ENV=production (the hazard
|
|
358
|
+
// orm-request.ts:553-558 exists to name). Pinned by AC8.
|
|
359
|
+
throw new Error(`${NO_FREE_ID_ERROR} for model "${modelName}"`);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
rawData.id = landingKey;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// Returned instead of a key so that "no key" cannot be confused with a transform
|
|
366
|
+
// that legitimately produced `undefined` or `null`.
|
|
367
|
+
const NO_FREE_KEY = Symbol('no free store key');
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* The first store key at or above `start` that no record occupies, walking
|
|
371
|
+
* candidate ids upward, or `NO_FREE_KEY` if the walk cannot reach one.
|
|
372
|
+
*/
|
|
373
|
+
function firstFreeKey(
|
|
374
|
+
storeMap: Map<number | string, unknown>,
|
|
375
|
+
toStoreKey: (value: number | string) => number | string,
|
|
376
|
+
toCandidate: (value: number) => number | string,
|
|
377
|
+
start: number
|
|
378
|
+
): number | string | typeof NO_FREE_KEY {
|
|
379
|
+
let candidate = start;
|
|
380
|
+
let landingKey = toStoreKey(toCandidate(candidate));
|
|
295
381
|
let attempts = 0;
|
|
296
382
|
|
|
297
383
|
while (storeMap.has(landingKey)) {
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
384
|
+
// THE BOUND IS EXACTLY TIGHT, not conservative: this walk tries
|
|
385
|
+
// `storeMap.size + 1` DISTINCT candidates against at most `storeMap.size`
|
|
386
|
+
// occupied keys, so under an injective `toStoreKey` it provably cannot fire.
|
|
387
|
+
// Under a non-injective one it provably terminates — and that is a reachable
|
|
388
|
+
// consumer state rather than a hypothesis: `transforms.boolean`
|
|
389
|
+
// (transforms.ts:4) collapses every candidate onto `true`/`false`, and
|
|
390
|
+
// `Orm.instance.transforms` (main.ts:70) is public and MUTABLE, so a consumer
|
|
391
|
+
// can register an arbitrary non-injective transform and name it as an id
|
|
392
|
+
// type. Without this, a no-id create spins forever inside a synchronous store
|
|
393
|
+
// walk and pins a worker, which is worse than either collision policy. Its
|
|
394
|
+
// EXISTENCE and its THRESHOLD are both pinned by AC8: deleting it makes AC8
|
|
395
|
+
// HANG rather than fail, and weakening it to fire on the first collision
|
|
396
|
+
// makes AC8.1 red.
|
|
397
|
+
if (++attempts > storeMap.size) return NO_FREE_KEY;
|
|
398
|
+
|
|
399
|
+
// NOTE FOR ANYONE ADDING A SECOND EXIT HERE. A `candidate + 1 === candidate`
|
|
400
|
+
// float-saturation check was written, measured, and REMOVED: with the
|
|
401
|
+
// restart-from-1 above in place, deleting the saturation check leaves the
|
|
402
|
+
// whole suite green, because the budget reaches the same `NO_FREE_KEY` one
|
|
403
|
+
// pass later and the restart still answers. An unkillable guard in a change
|
|
404
|
+
// whose deliverable is falsifiable coverage is exactly what this story exists
|
|
405
|
+
// to stop shipping. `+ 1` being a no-op at 2^53 costs `size` extra `Map.has`
|
|
406
|
+
// calls on that one path and changes no outcome.
|
|
302
407
|
candidate += 1;
|
|
303
|
-
landingKey = toStoreKey(candidate);
|
|
408
|
+
landingKey = toStoreKey(toCandidate(candidate));
|
|
304
409
|
}
|
|
305
410
|
|
|
306
|
-
|
|
411
|
+
return landingKey;
|
|
307
412
|
}
|
|
308
413
|
|
|
309
414
|
/**
|
|
@@ -311,13 +416,47 @@ function assignRecordId(modelName: string, rawData: { [key: string]: unknown }):
|
|
|
311
416
|
* carrying it will actually be filed under — the model's declared id transform,
|
|
312
417
|
* the same one `serialize` runs at createRecord:68 before the `.set` at :69.
|
|
313
418
|
*/
|
|
314
|
-
function storeKeyDeriver(modelName: string): (value: number) => number | string {
|
|
419
|
+
function storeKeyDeriver(modelName: string): (value: number | string) => number | string {
|
|
315
420
|
const idType = getIdType(modelName);
|
|
316
421
|
const transform = idType ? Orm.instance?.transforms?.[idType] : undefined;
|
|
317
422
|
|
|
423
|
+
// SURVIVOR, RETAINED, with its reachability condition stated rather than left
|
|
424
|
+
// silent — `docs/project-structure.md` § "unkillable code reads as coverage and
|
|
425
|
+
// is not" is the standing rule and it applies outside orm-request.ts too.
|
|
426
|
+
//
|
|
427
|
+
// No mutation in this repo can kill this branch, and that is STRUCTURAL rather
|
|
428
|
+
// than an untested gap: `Model` declares `id = attr('number')` (model.ts:15) so
|
|
429
|
+
// every registered model has an id type; `ModelProperty` refuses a type with no
|
|
430
|
+
// registered transform (model-property.ts:4) so a declared type always
|
|
431
|
+
// resolves; and `getIdType` can therefore only return `undefined` when
|
|
432
|
+
// `getRecordClasses` yields no `modelClass` — in which case `createRecord`
|
|
433
|
+
// throws at :62 a few lines later regardless, so no record is ever filed
|
|
434
|
+
// through this branch.
|
|
435
|
+
//
|
|
436
|
+
// BECOMES REACHABLE if a model can be declared without an `id` property, if a
|
|
437
|
+
// store map can exist for a model with no registered class, or if
|
|
438
|
+
// `createRecord` stops constructing the model class. Kept rather than deleted
|
|
439
|
+
// because the alternative on that path is a `transform is not a function`
|
|
440
|
+
// TypeError, and because identity is exactly what `createRecord` would file
|
|
441
|
+
// under when no transform exists — the two agree, which is the property AC4 is
|
|
442
|
+
// about.
|
|
318
443
|
if (typeof transform !== 'function') return value => value;
|
|
319
444
|
|
|
320
|
-
return value =>
|
|
445
|
+
return value => {
|
|
446
|
+
try {
|
|
447
|
+
return transform(value) as number | string;
|
|
448
|
+
} catch {
|
|
449
|
+
// `uppercase` and `trim` (transforms.ts:11-12) call a string method on the
|
|
450
|
+
// value directly, so a NUMERIC candidate throws `value?.toUpperCase is not
|
|
451
|
+
// a function`. On `dev` they never saw one — `lastRecord.id + 1` on a
|
|
452
|
+
// string id is a string — so feeding them a number here would regress a
|
|
453
|
+
// legal, registered id type into an uncaught 500. The retry feeds the
|
|
454
|
+
// string form, which is the shape an id actually arrives in off a JSON body
|
|
455
|
+
// or a URL param. A transform that throws on BOTH shapes still propagates.
|
|
456
|
+
// Pinned by AC9.
|
|
457
|
+
return transform(String(value)) as number | string;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
321
460
|
}
|
|
322
461
|
|
|
323
462
|
function getIdType(modelName: string): string | undefined {
|
package/src/orm-request.ts
CHANGED
|
@@ -185,7 +185,7 @@ import type { HookContext } from './hooks.js';
|
|
|
185
185
|
import config from 'stonyx/config';
|
|
186
186
|
import log from 'stonyx/log';
|
|
187
187
|
import type { OrmRecord, AccessContext, AccessFunction, AccessMethod, AccessOperation } from './types/orm-types.js';
|
|
188
|
-
import { isOrmRecord } from './utils.js';
|
|
188
|
+
import { isOrmRecord, NO_FREE_ID_ERROR } from './utils.js';
|
|
189
189
|
|
|
190
190
|
interface OrmRequest$ extends Request {
|
|
191
191
|
protocol?: string;
|
|
@@ -762,7 +762,41 @@ export default class OrmRequest extends Request {
|
|
|
762
762
|
// only O(1) signal that distinguishes an insert from an overwrite.
|
|
763
763
|
const slotsBefore = store.get(model)?.size ?? 0;
|
|
764
764
|
|
|
765
|
-
|
|
765
|
+
// THE ONE `createRecord` FAILURE THIS ROUTE ANSWERS RATHER THAN
|
|
766
|
+
// PROPAGATES, and it is narrow on purpose.
|
|
767
|
+
//
|
|
768
|
+
// `assignRecordId` throws when it cannot derive a free store key for a
|
|
769
|
+
// server-assigned id. Unguarded that rejection is auto-forwarded -- there
|
|
770
|
+
// is no catch here, none in @stonyx/rest-server's dispatcher
|
|
771
|
+
// (dist/request.js:41-70), and express 5 hands it to its default error
|
|
772
|
+
// handler, which serialises the STACK, with absolute install paths and the
|
|
773
|
+
// internal module graph, to an unauthenticated caller outside
|
|
774
|
+
// NODE_ENV=production. That is the hazard :553-558 already names in this
|
|
775
|
+
// file, and every sibling refusal in this handler returns an integer
|
|
776
|
+
// status instead. So this one returns 409, matching the client-duplicate
|
|
777
|
+
// refusal at :713: the caller asked for a record and the collection has no
|
|
778
|
+
// id to give it.
|
|
779
|
+
//
|
|
780
|
+
// MATCHED ON THE SHARED PREFIX, not on a literal, and NOT by catching
|
|
781
|
+
// everything: `createRecord` also throws for "ORM is not ready", a
|
|
782
|
+
// read-only view and an unregistered model store, and turning any of those
|
|
783
|
+
// into a 409 would report a configuration fault as a conflict. Anything
|
|
784
|
+
// else is re-thrown unchanged.
|
|
785
|
+
let created;
|
|
786
|
+
|
|
787
|
+
try {
|
|
788
|
+
created = createRecord(model, recordAttributes as { [key: string]: unknown }, { serialize: false, _skipAutoPersist: true });
|
|
789
|
+
} catch (error) {
|
|
790
|
+
if (!(error instanceof Error) || !error.message.startsWith(NO_FREE_ID_ERROR)) throw error;
|
|
791
|
+
|
|
792
|
+
// Not silently. A collection that can no longer assign an id is a
|
|
793
|
+
// configuration fault (a non-injective id transform), and a bare 409
|
|
794
|
+
// with no diagnostic is indistinguishable from an ordinary duplicate.
|
|
795
|
+
log.error?.(`[@stonyx/orm] ${error.message}`);
|
|
796
|
+
|
|
797
|
+
return 409; // Conflict
|
|
798
|
+
}
|
|
799
|
+
|
|
766
800
|
const record = isOrmRecord(created) ? created : null;
|
|
767
801
|
if (!record) return 500;
|
|
768
802
|
|
|
@@ -787,11 +821,32 @@ export default class OrmRequest extends Request {
|
|
|
787
821
|
//
|
|
788
822
|
// Both conditions are required and neither implies the other:
|
|
789
823
|
// createdNewSlot -- the store grew, so this request inserted rather
|
|
790
|
-
// than overwrote.
|
|
791
|
-
//
|
|
792
|
-
// last-INSERTED + 1,
|
|
793
|
-
//
|
|
794
|
-
//
|
|
824
|
+
// than overwrote. SURVIVOR AS OF #203, AND THAT IS
|
|
825
|
+
// WHAT THIS NOTE IS FOR. It used to be killable:
|
|
826
|
+
// `assignRecordId` returned last-INSERTED + 1, so a
|
|
827
|
+
// server-assigned id could land on an occupied slot,
|
|
828
|
+
// `createRecord` updated in place, and removing this
|
|
829
|
+
// half turned access-filter-enforcement-test.ts
|
|
830
|
+
// assertion 31 red. #203 closed that: the
|
|
831
|
+
// server-assigned path now walks past occupied keys,
|
|
832
|
+
// so no create reaching here can overwrite. Measured
|
|
833
|
+
// -- delete `createdNewSlot &&` below: `dev` gives
|
|
834
|
+
// 55 pass / 1 fail with assertion 31 RED, this tree
|
|
835
|
+
// gives 56 pass / 0 fail, GREEN.
|
|
836
|
+
// KEPT ANYWAY, AND NOT FOR THE OLD REASON. Without
|
|
837
|
+
// it a denied create becomes `store.remove` on a key
|
|
838
|
+
// the caller may have influenced, which :815-820
|
|
839
|
+
// records as having been an unauthenticated deletion
|
|
840
|
+
// primitive across the whole id space. BECOMES
|
|
841
|
+
// KILLABLE AGAIN the moment any caller-supplied id
|
|
842
|
+
// can reach `createRecord` from this handler --
|
|
843
|
+
// which is exactly what has-many.ts:65 and
|
|
844
|
+
// belongs-to.ts:45 already do for ANOTHER model's
|
|
845
|
+
// store (abofs/stonyx-orm#207), and what a third
|
|
846
|
+
// un-stripped id channel would do for this one
|
|
847
|
+
// (#204). Do not delete it on the strength of #203
|
|
848
|
+
// being closed; that is the reasoning :862-867 warns
|
|
849
|
+
// about, one level up.
|
|
795
850
|
// identity -- the slot still holds the object we just created,
|
|
796
851
|
// so nothing between createRecord and here replaced
|
|
797
852
|
// it. Deleting this half SURVIVES the suite, and it
|
package/src/standalone-db.ts
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
|
|
9
9
|
import fs from 'fs/promises';
|
|
10
10
|
import path from 'path';
|
|
11
|
+
// `./utils.js` pulls in `@stonyx/utils/string` and nothing else -- no ORM
|
|
12
|
+
// bootstrap, no `@stonyx/orm` index, no side effects -- so the "no framework
|
|
13
|
+
// dependencies" property above still holds.
|
|
14
|
+
import { maxNumericId } from './utils.js';
|
|
11
15
|
|
|
12
16
|
interface StandaloneDBOptions {
|
|
13
17
|
dbPath?: string;
|
|
@@ -131,12 +135,19 @@ export default class StandaloneDB {
|
|
|
131
135
|
const records = await this.readCollection(collection);
|
|
132
136
|
|
|
133
137
|
if (!data.id) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
138
|
+
// SHARED WITH `assignRecordId` (src/manage-record.ts), which is the other
|
|
139
|
+
// place this repo picks a server-assigned id. It was a second copy of the
|
|
140
|
+
// reduce, and nothing here pointed at it — a maintainer editing this
|
|
141
|
+
// method could not discover the other existed. See `maxNumericId` for why
|
|
142
|
+
// it is not `Math.max` (abofs/stonyx-orm#203).
|
|
143
|
+
//
|
|
144
|
+
// THE TWO ARE NOT THE SAME FUNCTION beyond this line, deliberately.
|
|
145
|
+
// `StandaloneDB` has no model, id-type or transform concept, so `maxId + 1`
|
|
146
|
+
// IS its store key; `assignRecordId` has to map the candidate through the
|
|
147
|
+
// model's declared id transform first, and then walk past occupied keys.
|
|
148
|
+
// Transplanting this method's remaining logic into the ORM reproduces
|
|
149
|
+
// #203's landing-key defect exactly — which is what AC4 pins.
|
|
150
|
+
data.id = maxNumericId(records) + 1;
|
|
140
151
|
}
|
|
141
152
|
|
|
142
153
|
// Check for duplicate id
|
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';
|