@stonyx/orm 0.3.2-alpha.77 → 0.3.2-alpha.78
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 +132 -241
- package/dist/orm-request.d.ts +3 -13
- package/dist/orm-request.js +150 -161
- package/dist/types/orm-types.d.ts +4 -30
- package/package.json +1 -1
- package/src/orm-request.ts +152 -164
- package/src/types/orm-types.ts +4 -30
package/dist/orm-request.js
CHANGED
|
@@ -65,19 +65,9 @@
|
|
|
65
65
|
* warning below sanctions. This repo's own fixture has such a rule: its
|
|
66
66
|
* `/archived` deny cannot be expressed from the context alone, and a predicate
|
|
67
67
|
* migrated to context-only would silently drop it, turning a deny into an
|
|
68
|
-
* allow.
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* CORRECTED HERE RATHER THAN DELETED. Both relationship route families resolve
|
|
72
|
-
* the RELATED model's own access class and ask it
|
|
73
|
-
* `{ model: <related>, operation: 'read', recordId: null }`
|
|
74
|
-
* (abofs/stonyx-orm#232), so those surfaces no longer serve another model's
|
|
75
|
-
* records under `model: 'owner'` unexamined. What the context still gives no
|
|
76
|
-
* signal of is WHICH related record is being asked about -- `recordId` is
|
|
77
|
-
* `null` there and `request.params` names a record of a different model. See
|
|
78
|
-
* `AccessContext.recordId` in ./types/orm-types.ts for the full statement of
|
|
79
|
-
* that limit. `?include=` is still unfiltered and is abofs/stonyx-orm#233 /
|
|
80
|
-
* #235.
|
|
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).
|
|
81
71
|
*
|
|
82
72
|
* SUPERSEDED 2026-09-01 BY abofs/stonyx-orm#236/#237, AND KEPT FOR THE
|
|
83
73
|
* CONSTRAINT IT STATES RATHER THAN AS A DESCRIPTION OF THE CODE. The context
|
|
@@ -434,7 +424,7 @@ function normalizeBodyId(id) {
|
|
|
434
424
|
return coerceId(id);
|
|
435
425
|
}
|
|
436
426
|
function buildResponse(data, includeParam, recordOrRecords, options = {}) {
|
|
437
|
-
const { links, baseUrl } = options;
|
|
427
|
+
const { links, baseUrl, linkage } = options;
|
|
438
428
|
const response = { data };
|
|
439
429
|
// Add top-level links
|
|
440
430
|
if (links) {
|
|
@@ -447,14 +437,49 @@ function buildResponse(data, includeParam, recordOrRecords, options = {}) {
|
|
|
447
437
|
return response;
|
|
448
438
|
const includedRecords = collectIncludedRecords(recordOrRecords, includes);
|
|
449
439
|
if (includedRecords.length > 0) {
|
|
450
|
-
//
|
|
451
|
-
//
|
|
452
|
-
//
|
|
453
|
-
//
|
|
454
|
-
//
|
|
455
|
-
//
|
|
456
|
-
//
|
|
457
|
-
|
|
440
|
+
// LINKAGE, NOT MEMBERSHIP -- and the distinction is the whole reason this
|
|
441
|
+
// line is one story's and the line above it is another's
|
|
442
|
+
// (abofs/stonyx-orm#235 and #233 respectively).
|
|
443
|
+
//
|
|
444
|
+
// - WHICH RESOURCES REACH THIS ARRAY is decided by
|
|
445
|
+
// `collectIncludedRecords` on the line above. That is MEMBERSHIP, it is
|
|
446
|
+
// #233's, and it is deliberately untouched here: a hidden owner is
|
|
447
|
+
// still a member of `included` after this change. Pinned green by
|
|
448
|
+
// `[GUARD] #235 X1` so that #235 cannot close #233 incidentally.
|
|
449
|
+
// - WHAT A RECORD ALREADY IN THIS ARRAY MAY NAME in its own
|
|
450
|
+
// `relationships.*.data` is LINKAGE -- the same question #234 answers
|
|
451
|
+
// for the primary document -- and that is what the `linkage` option
|
|
452
|
+
// below decides. Before it, `GET /animals/1?include=owner,owner.pets`
|
|
453
|
+
// filtered the primary document's `owner.data` to `null` and then
|
|
454
|
+
// handed back nine PERMITTED animals in `included` each naming
|
|
455
|
+
// `{"type":"owner","id":"angela"}`. Neither #233 nor #234 closes that.
|
|
456
|
+
//
|
|
457
|
+
// THE FILTER IS THE CALLER'S, PASSED IN, NOT BUILT HERE. Both call sites
|
|
458
|
+
// already hold one for the primary document, and sharing it is what keeps
|
|
459
|
+
// the per-type verdict cache and the per-(type, id) decision cache alive
|
|
460
|
+
// across the primary document AND the sideload -- one verdict resolution
|
|
461
|
+
// per type for the whole response, pinned by `[GUARD] #235 C1`. Building a
|
|
462
|
+
// fresh filter here would resolve the consumer's `access()` once per
|
|
463
|
+
// included record instead.
|
|
464
|
+
//
|
|
465
|
+
// `linkage` IS OPTIONAL IN THE TYPE AND IS NOT OPTIONAL IN PRACTICE.
|
|
466
|
+
// Stating it precisely because the opposite claim stood here in an earlier
|
|
467
|
+
// draft of this change: BOTH of this function's callers supply a filter
|
|
468
|
+
// (`getCollectionHandler` and `getSingleHandler`, the only two), so the
|
|
469
|
+
// `undefined` branch has no live caller in this module today. It is
|
|
470
|
+
// optional so that omitting it degrades to the PRE-#234 document rather
|
|
471
|
+
// than to a denial -- `Record.toJSON` reads an ABSENT option as "no verdict
|
|
472
|
+
// was supplied" and emits linkage in full.
|
|
473
|
+
//
|
|
474
|
+
// WHAT IT MUST NEVER BE HANDED IS A NON-FUNCTION. `toJSON` does NOT read a
|
|
475
|
+
// non-function as absent: `Object.prototype.toString.call(linkage)` must be
|
|
476
|
+
// `'[object Function]'`, and anything else -- `null`, an `AsyncFunction`,
|
|
477
|
+
// and INCLUDING the primitive `true` -- DENIES every relationship on the
|
|
478
|
+
// document and logs once. `toJSON({ linkage: true })` emits `null` linkage.
|
|
479
|
+
// So do not "simplify" this to a boolean, and do not make it default to
|
|
480
|
+
// `true`: both spellings look like "allow everything" and mean the exact
|
|
481
|
+
// opposite (abofs/stonyx-orm#224).
|
|
482
|
+
response.included = includedRecords.map(record => record.toJSON?.({ baseUrl, linkage }));
|
|
458
483
|
}
|
|
459
484
|
return response;
|
|
460
485
|
}
|
|
@@ -636,7 +661,11 @@ export default class OrmRequest extends Request {
|
|
|
636
661
|
const data = recordsToReturn.map(record => record.toJSON?.({ fields: modelFields, baseUrl, linkage }));
|
|
637
662
|
return buildResponse(data, request.query?.include, recordsToReturn, {
|
|
638
663
|
links: { self: `${baseUrl}/${pluralizedModel}` },
|
|
639
|
-
baseUrl
|
|
664
|
+
baseUrl,
|
|
665
|
+
// THE SAME filter object the primary documents above were serialized
|
|
666
|
+
// with, deliberately: it carries the caches, and rebuilding one here
|
|
667
|
+
// would re-resolve every type (abofs/stonyx-orm#235).
|
|
668
|
+
linkage
|
|
640
669
|
});
|
|
641
670
|
};
|
|
642
671
|
const getSingleHandler = async (request, { filter }) => {
|
|
@@ -652,28 +681,28 @@ export default class OrmRequest extends Request {
|
|
|
652
681
|
const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
|
|
653
682
|
const baseUrl = getBaseUrl(request);
|
|
654
683
|
const linkage = createLinkageFilter(request);
|
|
655
|
-
// `buildResponse`
|
|
656
|
-
//
|
|
657
|
-
//
|
|
658
|
-
// - WHETHER A RESOURCE APPEARS in `included` at all is MEMBERSHIP ->
|
|
659
|
-
// abofs/stonyx-orm#233.
|
|
660
|
-
// - What a record already IN `included` may NAME is LINKAGE -- the same
|
|
661
|
-
// question #234 answers for the primary document -- and it is
|
|
662
|
-
// abofs/stonyx-orm#235, which also owns createHandler/updateHandler.
|
|
684
|
+
// `buildResponse` IS given the filter now (abofs/stonyx-orm#235), and it
|
|
685
|
+
// is the SAME object the primary document is serialized with -- one
|
|
686
|
+
// verdict per type for the whole response, sideload included.
|
|
663
687
|
//
|
|
664
|
-
// The
|
|
665
|
-
//
|
|
666
|
-
//
|
|
667
|
-
//
|
|
668
|
-
//
|
|
669
|
-
// `included`. One query parameter deep. Only the PRIMARY document's
|
|
670
|
-
// linkage is filtered here.
|
|
688
|
+
// The boundary that remains, so the next reader does not have to derive
|
|
689
|
+
// it: this closes what a record already in `included` may NAME. WHETHER a
|
|
690
|
+
// resource appears in `included` at all is MEMBERSHIP and it is
|
|
691
|
+
// abofs/stonyx-orm#233's -- a hidden owner is still a member here.
|
|
692
|
+
// Neither question closes the other.
|
|
671
693
|
return buildResponse(record.toJSON?.({ fields: modelFields, baseUrl, linkage }), request.query?.include, record, {
|
|
672
694
|
links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}` },
|
|
673
|
-
baseUrl
|
|
695
|
+
baseUrl,
|
|
696
|
+
linkage
|
|
674
697
|
});
|
|
675
698
|
};
|
|
676
|
-
const createHandler = async (
|
|
699
|
+
const createHandler = async (request, { filter }) => {
|
|
700
|
+
// BOUND, not destructured (abofs/stonyx-orm#235). `HandlerFn` has always
|
|
701
|
+
// delivered the request as argument one; this handler simply discarded
|
|
702
|
+
// the binding, which is why its response document named ids every read
|
|
703
|
+
// surface withholds. `createLinkageFilter` needs the live request and
|
|
704
|
+
// there is no signature change involved in giving it one.
|
|
705
|
+
const { body, query } = request;
|
|
677
706
|
const { type, id, attributes, relationships: rels } = (body?.data || {});
|
|
678
707
|
if (!type)
|
|
679
708
|
return 400; // Bad request
|
|
@@ -912,9 +941,28 @@ export default class OrmRequest extends Request {
|
|
|
912
941
|
}
|
|
913
942
|
return 403;
|
|
914
943
|
}
|
|
915
|
-
|
|
944
|
+
// The filter is built HERE, per invocation, and never hoisted into the
|
|
945
|
+
// OrmRequest constructor where the other per-mount values live: a verdict
|
|
946
|
+
// cached across requests answers a second caller with the first caller's
|
|
947
|
+
// authorization (src/access-verdict.ts says so at the constructor an
|
|
948
|
+
// implementer would reach for).
|
|
949
|
+
//
|
|
950
|
+
// AND IT IS BUILT AFTER `createRecord`, AFTER THE ROLLBACK WINDOW AND
|
|
951
|
+
// AFTER `isDenied`, so the record is in its final form at the call. The
|
|
952
|
+
// filter is lazy per type and per (type, id), so it cannot observe a
|
|
953
|
+
// pre-write state even if it were built earlier.
|
|
954
|
+
//
|
|
955
|
+
// `fields` is passed here and NOT in `updateHandler`: the two handlers
|
|
956
|
+
// are asymmetric on purpose (`updateHandler` has no `fieldsMap` in
|
|
957
|
+
// scope), and a single copy-pasted wiring would drop it from one of them.
|
|
958
|
+
return { data: record.toJSON?.({ fields: modelFields, linkage: createLinkageFilter(request) }) };
|
|
916
959
|
};
|
|
917
|
-
const updateHandler = async (
|
|
960
|
+
const updateHandler = async (request, { filter }) => {
|
|
961
|
+
// Bound rather than destructured, for the reason given in
|
|
962
|
+
// `createHandler` above (abofs/stonyx-orm#235). `PATCH /animals/1`
|
|
963
|
+
// returned 200 naming angela seconds after `GET /animals/1` returned
|
|
964
|
+
// `owner.data: null` for the same record -- one HTTP verb apart.
|
|
965
|
+
const { body, params } = request;
|
|
918
966
|
const found = await store.find(model, getId(params));
|
|
919
967
|
if (!found || !isOrmRecord(found))
|
|
920
968
|
return 404;
|
|
@@ -971,7 +1019,14 @@ export default class OrmRequest extends Request {
|
|
|
971
1019
|
updateRecord(record, relUpdates, { _skipAutoPersist: true });
|
|
972
1020
|
}
|
|
973
1021
|
}
|
|
974
|
-
|
|
1022
|
+
// No `fields` and no `baseUrl`, both unchanged: `updateHandler` has no
|
|
1023
|
+
// `fieldsMap` in scope, and adding `baseUrl` would put `links` on a
|
|
1024
|
+
// document that has never carried them -- an unrelated behaviour change.
|
|
1025
|
+
// #224 AC6's "emits `data: []` WITH links" is a statement about the READ
|
|
1026
|
+
// surfaces; on these two handlers a filtered relationship and a
|
|
1027
|
+
// genuinely-empty one are both a bare `{ data }`, which is what makes
|
|
1028
|
+
// them indistinguishable here too.
|
|
1029
|
+
return { data: record.toJSON?.({ linkage: createLinkageFilter(request) }) };
|
|
975
1030
|
};
|
|
976
1031
|
const deleteHandler = async ({ params }, { filter }) => {
|
|
977
1032
|
// Coerced ONCE. `getId(params)` was evaluated twice here -- once to find
|
|
@@ -1255,102 +1310,21 @@ export default class OrmRequest extends Request {
|
|
|
1255
1310
|
return 404;
|
|
1256
1311
|
const relatedData = record.__relationships[relationshipName];
|
|
1257
1312
|
const baseUrl = getBaseUrl(request);
|
|
1258
|
-
//
|
|
1259
|
-
//
|
|
1260
|
-
//
|
|
1261
|
-
//
|
|
1262
|
-
//
|
|
1263
|
-
// related resource is PRIMARY data on this route, so there is no
|
|
1264
|
-
// linkage-consistency question to answer separately.
|
|
1265
|
-
//
|
|
1266
|
-
// Until #232 this route filtered only the PARENT, so a record its own
|
|
1267
|
-
// model's predicate hides was served in full from another model's
|
|
1268
|
-
// route, at ZERO query parameters. Measured on dev @ 8dda5d6:
|
|
1269
|
-
//
|
|
1270
|
-
// GET /owners/angela -> 404
|
|
1271
|
-
// GET /animals/1/owner -> 200, owner:angela, full attributes
|
|
1272
|
-
// GET /traits/2/tag -> 200, a model NO access class
|
|
1273
|
-
// claims, on a collection that has
|
|
1274
|
-
// no mounted route at all
|
|
1275
|
-
//
|
|
1276
|
-
// ARGUMENT ONE IS THE LIVE REQUEST, NOT A DERIVED ONE. A fabricated
|
|
1277
|
-
// request addressing the RELATED resource was the original design and
|
|
1278
|
-
// it is dropped: #241 removed the shipped fixture's read of argument
|
|
1279
|
-
// one, so a fabricated value changes nothing it could observe.
|
|
1280
|
-
// `createLinkageFilter` is also a published public export
|
|
1281
|
-
// (src/index.ts) whose resolution granularity is per TYPE; supplying a
|
|
1282
|
-
// per-RECORD request would mean widening it, which takes a consumer
|
|
1283
|
-
// `access()` from ~2 calls to ~7 on a plain `GET /animals`. That is a
|
|
1284
|
-
// separate, consumer-visible story.
|
|
1285
|
-
//
|
|
1286
|
-
// GUARDED BY OWN-PROPERTY IDENTITY, NOT BY THE #234 AC13 PIN. That pin
|
|
1287
|
-
// (test/unit/linkage-verdict-test.ts, `strictEqual(seen[0].request,
|
|
1288
|
-
// READ_REQUEST)`) calls `createLinkageFilter` DIRECTLY, so it pins the
|
|
1289
|
-
// function's pass-through and constrains no call site -- an earlier
|
|
1290
|
-
// revision of this comment cited it for this decision and was wrong.
|
|
1291
|
-
// `Object.create(request)` here measured 1015 / 0 with nothing red.
|
|
1292
|
-
// test/integration/orm-test.ts, `#232 AC9`, now asserts that the object
|
|
1293
|
-
// the predicate is handed OWNS `params` (`Object.hasOwn`) and has
|
|
1294
|
-
// nothing request-shaped behind it on the prototype chain. A derived
|
|
1295
|
-
// request inherits `params` -- so it satisfies every value assertion
|
|
1296
|
-
// there -- and reds on those two. Measured: with the derived request in
|
|
1297
|
-
// place, 1014 / 1, and that one is this guard.
|
|
1298
|
-
//
|
|
1299
|
-
// THE RESIDUAL THAT FOLLOWS FROM THAT IS DISCLOSED, NOT PAPERED OVER.
|
|
1300
|
-
// `recordId` is `null` here and the request names a record of a
|
|
1301
|
-
// DIFFERENT model, so a consumer predicate can express a model-level or
|
|
1302
|
-
// a request-level deny for a related resource, but NOT a per-record
|
|
1303
|
-
// one. README.md and docs/usage-patterns.md say so; a ledger assertion
|
|
1304
|
-
// in test/unit/relationship-route-access-test.ts keeps them saying it.
|
|
1313
|
+
// LINKAGE ONLY. This filter decides which ids the emitted documents may
|
|
1314
|
+
// NAME in their own `relationships.*.data`; it does NOT decide whether
|
|
1315
|
+
// the related records themselves are served -- that is the parent-only
|
|
1316
|
+
// filtering this route has done since #190, and widening it to the
|
|
1317
|
+
// related record is abofs/stonyx-orm#196.
|
|
1305
1318
|
const linkage = createLinkageFilter(request);
|
|
1306
|
-
// FAIL CLOSED ON A RECORD WHOSE TYPE CANNOT BE NAMED. `isLinkable` is
|
|
1307
|
-
// keyed on the model name; without one there is no predicate to ask,
|
|
1308
|
-
// and an unidentifiable input must never be the permissive path.
|
|
1309
|
-
const isLinkable = (r) => {
|
|
1310
|
-
const type = r.__model?.__name;
|
|
1311
|
-
return typeof type === 'string' && type !== '' && linkage(type, r);
|
|
1312
|
-
};
|
|
1313
1319
|
let data;
|
|
1314
1320
|
if (info.isArray) {
|
|
1315
|
-
// hasMany - return array
|
|
1316
|
-
// Dropped, never errored: the result is byte-identical to a genuinely
|
|
1317
|
-
// empty relationship, so this route is not an existence oracle.
|
|
1321
|
+
// hasMany - return array
|
|
1318
1322
|
const related = Array.isArray(relatedData) ? relatedData.filter(isOrmRecord) : [];
|
|
1319
|
-
data = related.
|
|
1323
|
+
data = related.map(r => r.toJSON?.({ baseUrl, linkage }));
|
|
1320
1324
|
}
|
|
1321
1325
|
else {
|
|
1322
|
-
// belongsTo - return single or null
|
|
1323
|
-
|
|
1324
|
-
// same reason the hasMany branch above drops rather than errors: this
|
|
1325
|
-
// route must not be an existence oracle for the RELATED record.
|
|
1326
|
-
//
|
|
1327
|
-
// THE OTHER SPELLING WAS 404 AND IT WAS MEASURED AS A DISCLOSURE.
|
|
1328
|
-
// Unauthenticated, zero query parameters, one request each, on `tag`
|
|
1329
|
-
// -- the model with no route mounted at all, which is exactly what
|
|
1330
|
-
// #240 AC5 exists to protect:
|
|
1331
|
-
//
|
|
1332
|
-
// GET /traits/1/tag [ABSENT] -> 200 application/json len 68
|
|
1333
|
-
// GET /traits/2/tag [DENIED] -> 404 text/plain len 9
|
|
1334
|
-
//
|
|
1335
|
-
// and `GET /traits/1` and `GET /traits/2` both report
|
|
1336
|
-
// `relationships.tag = {"data":null}` byte-identical modulo the id,
|
|
1337
|
-
// because #234 closed THAT oracle deliberately. A 404 here would let
|
|
1338
|
-
// a caller ask which of those two nulls was a denial. Under
|
|
1339
|
-
// `data: null` the pair closes completely: 200/200, same
|
|
1340
|
-
// content-type, same content-length, bodies identical modulo the
|
|
1341
|
-
// parent id the caller put in the URL. It opens nothing -- `links`
|
|
1342
|
-
// are entirely parent-derived, there is no `meta` and no counts.
|
|
1343
|
-
//
|
|
1344
|
-
// This is also what README.md's module-wide rule already demanded:
|
|
1345
|
-
// every status on a record route must be identical for filtered-out
|
|
1346
|
-
// and does-not-exist. The route now CONFORMS to that rule rather than
|
|
1347
|
-
// carving an exception out of it.
|
|
1348
|
-
if (!isOrmRecord(relatedData))
|
|
1349
|
-
data = null;
|
|
1350
|
-
else if (!isLinkable(relatedData))
|
|
1351
|
-
data = null;
|
|
1352
|
-
else
|
|
1353
|
-
data = relatedData.toJSON?.({ baseUrl, linkage });
|
|
1326
|
+
// belongsTo - return single or null
|
|
1327
|
+
data = isOrmRecord(relatedData) ? relatedData.toJSON?.({ baseUrl, linkage }) : null;
|
|
1354
1328
|
}
|
|
1355
1329
|
return {
|
|
1356
1330
|
links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}/${dasherizedName}` },
|
|
@@ -1358,6 +1332,47 @@ export default class OrmRequest extends Request {
|
|
|
1358
1332
|
};
|
|
1359
1333
|
};
|
|
1360
1334
|
// Relationship linkage route: GET /:id/relationships/{relationship}
|
|
1335
|
+
//
|
|
1336
|
+
// NO `linkage` FILTER FROM abofs/stonyx-orm#235, AND THAT IS A SCOPE
|
|
1337
|
+
// BOUNDARY RATHER THAN AN OVERSIGHT -- abofs/stonyx-orm#232 OWNS THIS
|
|
1338
|
+
// ROUTE, and PR #247 is IN FLIGHT against it in this same sprint. If you
|
|
1339
|
+
// are reading this after #247 landed, the filtering below is #232's and
|
|
1340
|
+
// this note records why it was never #235's to add.
|
|
1341
|
+
//
|
|
1342
|
+
// The three sites #235 does own (`buildResponse`'s `included`, the
|
|
1343
|
+
// related-resource branch above, and the two write handlers) all reach
|
|
1344
|
+
// the filter through `record.toJSON()`, which is where the `linkage`
|
|
1345
|
+
// OPTION is applied. This branch builds its `{ type, id }` objects BY
|
|
1346
|
+
// HAND and never calls `toJSON` at all, so the `linkage` option cannot
|
|
1347
|
+
// reach it -- whatever this route filters, it has to filter itself, which
|
|
1348
|
+
// is precisely why doing so is a separate change with a separate owner.
|
|
1349
|
+
//
|
|
1350
|
+
// It is also a DIFFERENT QUESTION. Everywhere #235 touches, linkage is
|
|
1351
|
+
// metadata ABOUT a document. Here the linkage IS the primary data, so
|
|
1352
|
+
// dropping an entry is a MEMBERSHIP decision about what this route
|
|
1353
|
+
// serves -- the same class as abofs/stonyx-orm#233 and #196, not the
|
|
1354
|
+
// class #234/#235 close. That is why it is absent from #224 §2a's
|
|
1355
|
+
// seven-site inventory.
|
|
1356
|
+
//
|
|
1357
|
+
// MEASURED, so the next person does not re-derive it. Against this
|
|
1358
|
+
// branch's baseline of 1011/0, wiring `createLinkageFilter` into the
|
|
1359
|
+
// belongsTo branch below takes the suite to 1009/2, reddening
|
|
1360
|
+
// `[GUARD] #235 X2` and the
|
|
1361
|
+
// `GET /animals/:id/relationships/owner returns relationship linkage`
|
|
1362
|
+
// test -- the latter is #232's own reproduction, not a regression.
|
|
1363
|
+
//
|
|
1364
|
+
// THE BASELINE IS QUOTED WITH THE RESULT BECAUSE AN EARLIER REVISION OF
|
|
1365
|
+
// THIS COMMENT SAID 993/2 AND SHIPPED IT. This file lands in consumers'
|
|
1366
|
+
// `node_modules`, so a wrong number here is a wrong number in the
|
|
1367
|
+
// published package. 993+2 = 995 is the DEV baseline, carried over from
|
|
1368
|
+
// a branch on which `[GUARD] #235 X2` does not exist. A pass/fail pair
|
|
1369
|
+
// with no baseline beside it cannot be checked by reading, which is how
|
|
1370
|
+
// it survived three artifacts and a review; the qualitative claim was
|
|
1371
|
+
// right the whole time and only the count was wrong.
|
|
1372
|
+
//
|
|
1373
|
+
// `[GUARD] #235 X2` in test/integration/orm-test.ts pins the OWNERSHIP
|
|
1374
|
+
// BOUNDARY here rather than this route's current answer, so that it
|
|
1375
|
+
// survives #247 landing. Read its comment before changing it.
|
|
1361
1376
|
routes[`/:id/relationships/${dasherizedName}`] = async (request, { filter } = {}) => {
|
|
1362
1377
|
const record = await store.find(model, getId(request.params));
|
|
1363
1378
|
if (!record)
|
|
@@ -1366,43 +1381,17 @@ export default class OrmRequest extends Request {
|
|
|
1366
1381
|
return 404;
|
|
1367
1382
|
const relatedData = record.__relationships[relationshipName];
|
|
1368
1383
|
const baseUrl = getBaseUrl(request);
|
|
1369
|
-
// THE ONE READ SURFACE THAT DOES NOT GO THROUGH `toJSON()`. It builds
|
|
1370
|
-
// `{ type, id }` BY HAND, which is why #234's linkage filter never
|
|
1371
|
-
// reached it and why this half belongs to abofs/stonyx-orm#232 rather
|
|
1372
|
-
// than to #234: on this route the linkage IS the primary data of an
|
|
1373
|
-
// opt-in request, so filtering it changes the route's MEMBERSHIP
|
|
1374
|
-
// semantics, not the ids named inside somebody else's document.
|
|
1375
|
-
//
|
|
1376
|
-
// DELIBERATELY NOT STATED AS A COUNT. README.md's Consumer Contracts
|
|
1377
|
-
// section enumerates the surfaces on which the framework resolves a
|
|
1378
|
-
// verdict and hands it to `toJSON()`, and that enumeration GROWS --
|
|
1379
|
-
// abofs/stonyx-orm#235 adds the two write handlers and the `included`
|
|
1380
|
-
// records. This route is not on that list under any count, because it
|
|
1381
|
-
// never calls `toJSON()`: whatever it filters, it filters here. A
|
|
1382
|
-
// number written into this comment would be false the next time that
|
|
1383
|
-
// list changes, and the README already carries the enumeration.
|
|
1384
|
-
//
|
|
1385
|
-
// Same filter, same argument-one decision, same residual as
|
|
1386
|
-
// `/:id/{relationship}` above -- read the block there.
|
|
1387
|
-
const linkage = createLinkageFilter(request);
|
|
1388
|
-
const isLinkable = (r) => {
|
|
1389
|
-
const type = r.__model?.__name;
|
|
1390
|
-
return typeof type === 'string' && type !== '' && linkage(type, r);
|
|
1391
|
-
};
|
|
1392
1384
|
let data;
|
|
1393
1385
|
if (info.isArray) {
|
|
1394
1386
|
// hasMany - return array of linkage objects
|
|
1395
1387
|
const related = Array.isArray(relatedData) ? relatedData.filter(isOrmRecord) : [];
|
|
1396
1388
|
data = related
|
|
1397
1389
|
.filter((r) => Boolean(r.__model))
|
|
1398
|
-
.filter(isLinkable)
|
|
1399
1390
|
.map(r => ({ type: r.__model.__name, id: r.id }));
|
|
1400
1391
|
}
|
|
1401
1392
|
else {
|
|
1402
|
-
// belongsTo - return single linkage or null
|
|
1403
|
-
|
|
1404
|
-
// the measured oracle in the `/:id/{relationship}` block above.
|
|
1405
|
-
if (isOrmRecord(relatedData) && relatedData.__model && isLinkable(relatedData)) {
|
|
1393
|
+
// belongsTo - return single linkage or null
|
|
1394
|
+
if (isOrmRecord(relatedData) && relatedData.__model) {
|
|
1406
1395
|
data = { type: relatedData.__model.__name, id: relatedData.id };
|
|
1407
1396
|
}
|
|
1408
1397
|
else {
|
|
@@ -340,36 +340,10 @@ export interface AccessContext {
|
|
|
340
340
|
* repaired here. A predicate must not read `undefined` here as "collection",
|
|
341
341
|
* and nothing in this contract makes it safe to read the two keys as one key.
|
|
342
342
|
*
|
|
343
|
-
* IT NAMES WHICH RECORD
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
* `GET /owners/gina/pets` and `GET /owners/gina/relationships/pets` carry
|
|
348
|
-
* `recordId: 'gina'` -- `auth()` reads it off `request.params`.
|
|
349
|
-
*
|
|
350
|
-
* FOR THE ASK ABOUT A RELATED MODEL, IT IS `null`, AND THAT IS A LIMIT ON
|
|
351
|
-
* WHAT A PREDICATE CAN EXPRESS (abofs/stonyx-orm#232). The two relationship
|
|
352
|
-
* route families resolve the RELATED model's own predicate -- `animal` on
|
|
353
|
-
* `GET /owners/gina/pets`, `owner` on `GET /animals/4/owner` -- and that ask
|
|
354
|
-
* carries `recordId: null` while `request.params` names a record of a
|
|
355
|
-
* DIFFERENT model. So a predicate answering about a related model gets the
|
|
356
|
-
* model name, the operation and the request, and CANNOT branch on which
|
|
357
|
-
* related record it is being asked about.
|
|
358
|
-
*
|
|
359
|
-
* The rule, so it is not re-derived wrong: `recordId` may name a record only
|
|
360
|
-
* where the route addresses exactly one record OF THE MODEL BEING ASKED
|
|
361
|
-
* ABOUT. A `hasMany` related-resource route returns many records of one type
|
|
362
|
-
* and the verdict is resolved ONCE PER TYPE, before any record is examined --
|
|
363
|
-
* seeding it from a record would let the first one decide for all of them.
|
|
364
|
-
*
|
|
365
|
-
* What still works, and what does not, is pinned as behaviour by `#232 AC10`
|
|
366
|
-
* in test/integration/orm-test.ts and stated for consumers in README.md:
|
|
367
|
-
* model-level denies work, request-level denies work, and the per-record
|
|
368
|
-
* FILTER shape works because `access()` may return a function and that
|
|
369
|
-
* function receives the whole record. Branching on identity BEFORE returning
|
|
370
|
-
* does not.
|
|
371
|
-
*
|
|
372
|
-
* `?include=` is a separate surface and is abofs/stonyx-orm#233 / #235.
|
|
343
|
+
* IT NAMES WHICH RECORD, NOT WHICH SURFACE. `GET /owners/gina`,
|
|
344
|
+
* `GET /owners/gina/pets` and `GET /owners/gina/relationships/pets` all
|
|
345
|
+
* carry `recordId: 'gina'`; the related-resource gap is abofs/stonyx-orm#196
|
|
346
|
+
* and is untouched by this key.
|
|
373
347
|
*/
|
|
374
348
|
recordId: string | number | null;
|
|
375
349
|
}
|