@effect-dynamodb/schema 1.42.1 → 1.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/DynamoModel.d.ts +227 -30
  2. package/dist/DynamoModel.d.ts.map +1 -1
  3. package/dist/DynamoModel.js +153 -38
  4. package/dist/DynamoModel.js.map +1 -1
  5. package/dist/Entity.d.ts +38 -10
  6. package/dist/Entity.d.ts.map +1 -1
  7. package/dist/Entity.js +32 -2
  8. package/dist/Entity.js.map +1 -1
  9. package/dist/Errors.d.ts +58 -0
  10. package/dist/Errors.d.ts.map +1 -1
  11. package/dist/Errors.js +24 -0
  12. package/dist/Errors.js.map +1 -1
  13. package/dist/index.d.ts +3 -2
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/internal/AggregateSchemas.d.ts.map +1 -1
  18. package/dist/internal/AggregateSchemas.js +1 -20
  19. package/dist/internal/AggregateSchemas.js.map +1 -1
  20. package/dist/internal/BlobPointer.d.ts +157 -0
  21. package/dist/internal/BlobPointer.d.ts.map +1 -0
  22. package/dist/internal/BlobPointer.js +471 -0
  23. package/dist/internal/BlobPointer.js.map +1 -0
  24. package/dist/internal/Crc64Nvme.d.ts +42 -0
  25. package/dist/internal/Crc64Nvme.d.ts.map +1 -0
  26. package/dist/internal/Crc64Nvme.js +128 -0
  27. package/dist/internal/Crc64Nvme.js.map +1 -0
  28. package/dist/internal/EntityConfig.d.ts +30 -0
  29. package/dist/internal/EntityConfig.d.ts.map +1 -1
  30. package/dist/internal/EntityOffload.d.ts +112 -0
  31. package/dist/internal/EntityOffload.d.ts.map +1 -0
  32. package/dist/internal/EntityOffload.js +260 -0
  33. package/dist/internal/EntityOffload.js.map +1 -0
  34. package/dist/internal/EntityPacked.d.ts +11 -10
  35. package/dist/internal/EntityPacked.d.ts.map +1 -1
  36. package/dist/internal/EntityPacked.js +6 -5
  37. package/dist/internal/EntityPacked.js.map +1 -1
  38. package/dist/internal/EntitySchemas.d.ts +92 -5
  39. package/dist/internal/EntitySchemas.d.ts.map +1 -1
  40. package/dist/internal/EntitySchemas.js +353 -69
  41. package/dist/internal/EntitySchemas.js.map +1 -1
  42. package/dist/internal/EntityTypes.d.ts +45 -6
  43. package/dist/internal/EntityTypes.d.ts.map +1 -1
  44. package/dist/internal/PackedCodec.d.ts +82 -27
  45. package/dist/internal/PackedCodec.d.ts.map +1 -1
  46. package/dist/internal/PackedCodec.js +175 -44
  47. package/dist/internal/PackedCodec.js.map +1 -1
  48. package/dist/internal/SchemaAccessors.d.ts +13 -4
  49. package/dist/internal/SchemaAccessors.d.ts.map +1 -1
  50. package/dist/internal/SchemaAccessors.js +26 -4
  51. package/dist/internal/SchemaAccessors.js.map +1 -1
  52. package/dist/internal/Sha256.d.ts +34 -0
  53. package/dist/internal/Sha256.d.ts.map +1 -0
  54. package/dist/internal/Sha256.js +144 -0
  55. package/dist/internal/Sha256.js.map +1 -0
  56. package/package.json +1 -1
@@ -4,12 +4,12 @@
4
4
  * Extracted from Entity.ts for decomposition. Not part of the public API.
5
5
  */
6
6
  import { DateTime, Effect, Exit, Option, Redacted, Schema, SchemaAST, SchemaGetter, SchemaIssue, SchemaTransformation, } from "effect";
7
- import { getEncoding, isConfiguredModel, isHidden, } from "../DynamoModel.js";
7
+ import { getEncoding, isConfiguredModel, isHidden, isRef, } from "../DynamoModel.js";
8
8
  import { reviveMarshalledDateTime } from "./EffectInstances.js";
9
9
  import { readingInstances } from "./InstanceReads.js";
10
10
  import { hasOwnName, ownValue, PROTO_ENTRY, setOwn } from "./OwnNames.js";
11
11
  import { BETA_DATE_MARKER, DATE_MARKER } from "./PackedCodec.js";
12
- import { absentStored, firstTypeParameterAst, getSchemaFields, holdsAbsentValue, holdsPlainBigint, holdsUndefined, isDecodingDefault, isSchemaLike, isSetDeclaration, keyRenamesOf, lackDecision, membersNamedExactly, numericTypeWithStringEncoding, ReadsAbsentAs, rowCanLack, storedList, transformWireKind as transformWireKindImpl, } from "./SchemaAccessors.js";
12
+ import { absentStored, firstTypeParameterAst, getSchemaFields, holdsAbsentValue, holdsPlainBigint, holdsUndefined, isDecodingDefault, isSameSchemaNode, isSchemaLike, isSetDeclaration, keyRenamesOf, lackDecision, membersNamedExactly, numericTypeWithStringEncoding, ReadsAbsentAs, rowCanLack, storedList, transformWireKind as transformWireKindImpl, } from "./SchemaAccessors.js";
13
13
  import { CANT_SAY, issueOf } from "./SchemaIssue.js";
14
14
  import { zonedIsoText } from "./StoredForm.js";
15
15
  import { parseZonedIso } from "./ZonedIso.js";
@@ -961,6 +961,28 @@ export const optionalField = (field) => {
961
961
  rewrap: (s) => Schema.optionalKey(s),
962
962
  };
963
963
  };
964
+ /**
965
+ * @internal Whether a ref's declaration takes `null` (`Schema.NullOr`, a union with `Schema.Null`)
966
+ * or, as a member of its own union, `undefined` (`Schema.UndefinedOr`), beneath an optional wrapper
967
+ * or not. `Schema.optional`'s own `undefined` is the wrapper's, not the ref's. Neither where the
968
+ * union holds no member a walk takes for a ref to `model` ({@link unionHoldsRef}): the ref's target
969
+ * replaces such a union whole, as up to 1.42.1, and holds neither.
970
+ */
971
+ export const refNullish = (declared, model) => {
972
+ if (declared === undefined)
973
+ return { nullable: false, undefinable: false };
974
+ const optional = optionalField(declared);
975
+ const inner = optional?.union ?? optional?.inner ?? declared;
976
+ // A union among the members is one with them (`Schema.UndefinedOr(Schema.NullOr(Team))`).
977
+ const flat = (ast) => SchemaAST.isUnion(ast) ? ast.types.flatMap(flat) : [ast];
978
+ const members = SchemaAST.isUnion(inner.ast) && (model === undefined || unionHoldsRef(inner, model))
979
+ ? flat(inner.ast)
980
+ : [];
981
+ return {
982
+ nullable: members.some((member) => SchemaAST.isNull(member)),
983
+ undefinable: optional === undefined && members.some((member) => SchemaAST.isUndefined(member)),
984
+ };
985
+ };
964
986
  /** The options a nested schema inherits: everything but the per-position ones. */
965
987
  const descend = (opts) => opts?.tolerantTransforms ||
966
988
  opts?.resolveRef ||
@@ -1126,6 +1148,11 @@ const childOptions = (container, deeper, index, opts) => {
1126
1148
  ...(opts?.unionDateOverride && opts.readsDeclaredStorage ? { readsDeclaredStorage: true } : {}),
1127
1149
  // A nullable ref (`NullOr(Team)`): its struct member holds what the ref's entity holds.
1128
1150
  ...(opts?.configuredStorage ? { configuredStorage: opts.configuredStorage } : {}),
1151
+ ...(opts?.heldRefs ? { heldRefs: opts.heldRefs } : {}),
1152
+ // ...where the union is walked member by member (`unionHoldsRef`); else as declared, as 1.42.1.
1153
+ ...(opts?.heldRef && membersHoldRef(container.children, opts.heldRef.model)
1154
+ ? { heldRef: opts.heldRef }
1155
+ : {}),
1129
1156
  };
1130
1157
  };
1131
1158
  /** The options an optional wrapper hands its inner schema: it is transparent. */
@@ -1194,7 +1221,9 @@ const throughOptional = (opts) => {
1194
1221
  return opts?.unionDateOverride ||
1195
1222
  opts?.collidingKinds ||
1196
1223
  opts?.dateEncodingOverride ||
1197
- opts?.configuredStorage
1224
+ opts?.configuredStorage ||
1225
+ opts?.heldRefs ||
1226
+ opts?.heldRef
1198
1227
  ? {
1199
1228
  ...deeper,
1200
1229
  ...(opts.unionDateOverride ? { unionDateOverride: opts.unionDateOverride } : {}),
@@ -1202,6 +1231,8 @@ const throughOptional = (opts) => {
1202
1231
  ...(opts.dateEncodingOverride ? { dateEncodingOverride: opts.dateEncodingOverride } : {}),
1203
1232
  ...(opts.readsDeclaredStorage ? { readsDeclaredStorage: true } : {}),
1204
1233
  ...(opts.configuredStorage ? { configuredStorage: opts.configuredStorage } : {}),
1234
+ ...(opts.heldRefs ? { heldRefs: opts.heldRefs } : {}),
1235
+ ...(opts.heldRef ? { heldRef: opts.heldRef } : {}),
1205
1236
  }
1206
1237
  : deeper;
1207
1238
  };
@@ -1519,6 +1550,10 @@ const refusesUndefinedPositions = (schema, opts) => opts?.enforceChecks === true
1519
1550
  const needsDeepSubstitution = (schema, opts) => {
1520
1551
  if (schema == null || schema.ast == null)
1521
1552
  return false;
1553
+ const held = heldMemberOf(schema, opts);
1554
+ if (held !== undefined) {
1555
+ return held.member !== schema || needsDeepSubstitution(held.member, held.options);
1556
+ }
1522
1557
  // A ref held as less than its class (`refHeldTarget`) is another schema than the field it
1523
1558
  // re-points, whatever its leaves.
1524
1559
  if (heldStructs.has(schema))
@@ -1592,7 +1627,10 @@ const needsDeepSubstitution = (schema, opts) => {
1592
1627
  // Re-point ref edge fields at their target; optionality is handled when the
1593
1628
  // field itself is recursed into (optional unwrap at the entry above).
1594
1629
  const refTarget = opts?.resolveRef?.(name, field);
1595
- return needsDeepSubstitution(refTarget ?? field, deeper);
1630
+ if (refTarget !== undefined)
1631
+ return needsDeepSubstitution(refTarget, deeper);
1632
+ const held = ownValue(opts?.heldRefs, name);
1633
+ return needsDeepSubstitution(field, held === undefined ? deeper : { ...deeper, heldRef: held });
1596
1634
  });
1597
1635
  }
1598
1636
  return liftsBigInts(opts) && holdsUnwalkedBigInt(schema);
@@ -1675,6 +1713,10 @@ const holdsDeclaration = (ast, seen) => {
1675
1713
  * @internal
1676
1714
  */
1677
1715
  export const substituteSchemaDeep = (schema, opts) => {
1716
+ // The member of a nested ref (`heldRef`), beneath the wrappers walked to reach it.
1717
+ const held = heldMemberOf(schema, opts);
1718
+ if (held !== undefined)
1719
+ return substituteSchemaDeep(held.member, held.options);
1678
1720
  if (!needsDeepSubstitution(schema, opts))
1679
1721
  return schema;
1680
1722
  // `tolerantTransforms`, `resolveRef`, `strictWireKind` and `legacyReads`
@@ -1887,7 +1929,11 @@ export const substituteSchemaDeep = (schema, opts) => {
1887
1929
  setOwn(subFields, name, fieldNamed(name, fieldOpt ? fieldOpt.rewrap(sub) : sub, field));
1888
1930
  }
1889
1931
  else {
1890
- const sub = substituteSchemaDeep(field, withConfiguredStorage(atField(deeper, opts, name), name, field, ownValue(opts?.configuredStorage?.fields, name), ownValue(opts?.configuredStorage?.refs, name)));
1932
+ // A ref of the model's entity (`heldRefs`): its member is what a read of the ref's entity
1933
+ // returns, beneath the wrappers the model declares around it (`heldRef`).
1934
+ const held = ownValue(opts?.heldRefs, name);
1935
+ const options = withConfiguredStorage(atField(deeper, opts, name), name, field, ownValue(opts?.configuredStorage?.fields, name), ownValue(opts?.configuredStorage?.refs, name));
1936
+ const sub = substituteSchemaDeep(field, held === undefined ? options : { ...options, heldRef: held });
1891
1937
  setOwn(subFields, name, name === "__proto__"
1892
1938
  ? fieldNamed(name, sub, field)
1893
1939
  : opts?.absentReads
@@ -2246,19 +2292,126 @@ export const configuredStorageOf = (model) => {
2246
2292
  };
2247
2293
  /** Each entity's {@link HeldStorage}, by its derived schemas (shared by its pure and runtime forms). */
2248
2294
  const heldStorages = new WeakMap();
2295
+ /** Each entity's {@link HeldRefs}, by its derived schemas, as {@link heldStorages}. */
2296
+ const heldRefsBySchemas = new WeakMap();
2249
2297
  /**
2250
2298
  * @internal Record what an entity holds (`fields`, the storage its schemas apply to its own fields;
2251
- * `refs`, by ref field, each entity it refs), when it's made. An entity can only ref one made
2252
- * before it, so the tree is finite.
2299
+ * `refs`, by ref field, each entity it refs, and that entity's model and refs), when it's made. An
2300
+ * entity can only ref one made before it, so each tree is finite, and is fixed when it's made.
2253
2301
  */
2254
2302
  export const holdStorage = (schemas, fields, refs) => {
2255
2303
  const held = {};
2304
+ const models = {};
2256
2305
  for (const ref of refs) {
2257
2306
  const storage = heldStorageOf(ref.refEntity);
2258
2307
  if (storage !== undefined)
2259
2308
  setOwn(held, ref.fieldName, storage);
2309
+ const model = ref.refEntity?.model;
2310
+ if (model !== undefined) {
2311
+ setOwn(models, ref.fieldName, {
2312
+ model: (isConfiguredModel(model) ? model.model : model),
2313
+ refs: entityRefsOf(ref.refEntity),
2314
+ });
2315
+ }
2260
2316
  }
2261
2317
  heldStorages.set(schemas, { fields: fields ?? {}, refs: held });
2318
+ heldRefsBySchemas.set(schemas, models);
2319
+ };
2320
+ /** @internal The {@link HeldRefs} of a ref's entity: none for an entity this module didn't make. */
2321
+ export const entityRefsOf = (entity) => {
2322
+ const { schemas } = (entity ?? {});
2323
+ return ((typeof schemas === "object" && schemas !== null
2324
+ ? heldRefsBySchemas.get(schemas)
2325
+ : undefined) ?? {});
2326
+ };
2327
+ /** `options` with the refs of the model a field holds ({@link HeldRef}), where it has any. */
2328
+ const withHeldRefs = (options, held) => held === undefined || Object.keys(held.refs).length === 0
2329
+ ? options
2330
+ : { ...options, heldRefs: held.refs };
2331
+ /**
2332
+ * @internal The member of a nested ref's declaration that holds the ref, as a walk below the ref
2333
+ * takes it, or `undefined` for anything else: an optional wrapper, a union (`NullOr`, one with other
2334
+ * members), a decoding default, which the walk goes through to the member beneath, kept as declared,
2335
+ * and a member that isn't the ref (`null`, a string). The member is the model of the ref's entity, a
2336
+ * copy of it that changes only its annotations or checks (`Team.annotate({ description })`,
2337
+ * `isSameSchemaNode`), a `Schema.suspend` of it (a suspend of a suspend too), or a
2338
+ * `DynamoModel.ref` of any of these. It is what a read of that entity returns (`refHeldTarget`)
2339
+ * wherever its own schema can't be walked (`DynamoModel.ref`, `.annotate` and `Schema.suspend` hide
2340
+ * a class's fields) or holds more than that read; elsewhere the member as declared, as 1.42.1
2341
+ * walked it.
2342
+ */
2343
+ export const heldRefMember = (member, model) => {
2344
+ const ast = member.ast;
2345
+ let suspended = ast;
2346
+ while (SchemaAST.isSuspend(suspended))
2347
+ suspended = suspended.thunk();
2348
+ if (optionalField(member) !== undefined ||
2349
+ SchemaAST.isUnion(ast) ||
2350
+ decodingDefaultField(member) !== undefined ||
2351
+ !(member === model || isRef(member) || isSameSchemaNode(suspended, model.ast))) {
2352
+ return undefined;
2353
+ }
2354
+ const held = refHeldTarget(model);
2355
+ return schemaFieldsOf(member) !== undefined && held === model ? member : held;
2356
+ };
2357
+ /**
2358
+ * Whether a member of a ref's union can't take an object, so can't take the ref's value before the
2359
+ * ref's own member does: `null`, `undefined`, `void`, a primitive keyword, a literal, an enum, a
2360
+ * template literal, or a union of these, none with an encoding of its own. Anything else (a struct,
2361
+ * another class, a record, an array, `unknown`, an object keyword, a declaration, a suspend, a
2362
+ * transformation) might.
2363
+ */
2364
+ const takesNoObject = (ast) => ast.encoding === undefined &&
2365
+ (SchemaAST.isUnion(ast)
2366
+ ? ast.types.every(takesNoObject)
2367
+ : SchemaAST.isNull(ast) ||
2368
+ SchemaAST.isUndefined(ast) ||
2369
+ SchemaAST.isVoid(ast) ||
2370
+ SchemaAST.isString(ast) ||
2371
+ SchemaAST.isNumber(ast) ||
2372
+ SchemaAST.isBoolean(ast) ||
2373
+ SchemaAST.isBigInt(ast) ||
2374
+ SchemaAST.isSymbol(ast) ||
2375
+ SchemaAST.isLiteral(ast) ||
2376
+ SchemaAST.isEnum(ast) ||
2377
+ SchemaAST.isTemplateLiteral(ast));
2378
+ /**
2379
+ * Whether a union's members, through unions among them, hold a member the walk takes for the ref
2380
+ * ({@link heldRefMember}) and nothing else that can take an object ({@link takesNoObject}).
2381
+ */
2382
+ const membersHoldRef = (members, model) => {
2383
+ let holds = false;
2384
+ const onlyRefOrNoObject = (all) => all.every((member) => {
2385
+ if (heldRefMember(member, model) !== undefined) {
2386
+ holds = true;
2387
+ return true;
2388
+ }
2389
+ const inner = member.members;
2390
+ return SchemaAST.isUnion(member.ast) && member.ast.encoding === undefined && inner
2391
+ ? onlyRefOrNoObject(inner)
2392
+ : takesNoObject(member.ast);
2393
+ });
2394
+ return onlyRefOrNoObject(members) && holds;
2395
+ };
2396
+ /**
2397
+ * @internal Whether a ref's union is walked member by member: it holds a member the walk takes for
2398
+ * the ref ({@link heldRefMember}), and every other member, through unions among them, can't take
2399
+ * an object (`null`, `undefined`, a string, a literal, …; {@link takesNoObject}), so a value of the
2400
+ * ref's target decodes as that member alone. Otherwise (another class or a struct beside the ref,
2401
+ * or no member the walk takes for it) the ref's target replaces the union whole, as up to 1.42.1,
2402
+ * and the ref's id takes no `null`; where 1.42.1 walked the union as declared (a ref nested in a
2403
+ * ref's target, an aggregate edge entity's ref), it still is.
2404
+ */
2405
+ export const unionHoldsRef = (union, model) => membersHoldRef(union.members ?? [], model);
2406
+ /** The walk's {@link heldRefMember} at `schema` (`heldRef`), with what it walks that member with. */
2407
+ const heldMemberOf = (schema, opts) => {
2408
+ const held = opts?.heldRef;
2409
+ if (held === undefined)
2410
+ return undefined;
2411
+ const member = heldRefMember(schema, held.model);
2412
+ return member === undefined
2413
+ ? undefined
2414
+ : { member, options: withHeldRefs({ ...opts, heldRef: undefined }, held) };
2262
2415
  };
2263
2416
  /**
2264
2417
  * @internal The {@link HeldStorage} of a ref's entity, or `undefined` where it holds every field in
@@ -2394,6 +2547,25 @@ const transformWireKind = (schema) => transformWireKindImpl(schema);
2394
2547
  export const hiddenFieldNamesOf = (fields) => new Set(Object.keys(fields).filter((name) => isHidden(fields[name])));
2395
2548
  /** The targets {@link refHeldTarget} decided, each once. */
2396
2549
  const refHeldTargets = new WeakMap();
2550
+ /** {@link refHeldTarget}'s decisions for an aggregate edge, which keeps the offloaded fields. */
2551
+ const edgeHeldTargets = new WeakMap();
2552
+ /** The fields an entity of each model offloads to the blob store (#179), by its unconfigured model. */
2553
+ const offloadedByModel = new WeakMap();
2554
+ /**
2555
+ * @internal Record that an entity of `model` (its unconfigured model) offloads `fields` (#179): a
2556
+ * read of that entity's ref holds none of them, as `refDataOf` stores none ({@link refHeldTarget}).
2557
+ * Called by each entity definition before any entity referencing it can be made. Two entities of one
2558
+ * model that offload different fields leave the union out of a ref to either.
2559
+ */
2560
+ export const registerOffloadedFields = (model, fields) => {
2561
+ const prior = offloadedByModel.get(model) ?? new Set();
2562
+ const next = new Set([...prior, ...fields]);
2563
+ if (next.size === prior.size)
2564
+ return;
2565
+ offloadedByModel.set(model, next);
2566
+ refHeldTargets.delete(model);
2567
+ edgeHeldTargets.delete(model);
2568
+ };
2397
2569
  /** The schemas {@link refHeldTarget} built: a target's fields without its hidden ones. */
2398
2570
  const heldStructs = new WeakSet();
2399
2571
  /**
@@ -2402,19 +2574,32 @@ const heldStructs = new WeakSet();
2402
2574
  * what a read of that entity returns, which put-time hydration and a cascade store, both through
2403
2575
  * `refDataOf` (the entity's own read schema encoding the item it read). A read leaves out the
2404
2576
  * target's `DynamoModel.Hidden` fields ({@link hiddenFieldNamesOf}), so the stored ref holds none
2405
- * of them. Where a hidden field read absent fails, as a read decodes it (`Missing key`), the ref is
2577
+ * of them, and a ref never stores a field the entity offloads ({@link registerOffloadedFields}), so
2578
+ * those are left out the same way — except at an aggregate's edge (`edge`), whose row stores them as
2579
+ * its own uploads (#179). Where a hidden field read absent fails, as a read decodes it (`Missing key`), the ref is
2406
2580
  * the target's other fields, a plain object as the entity's own read decodes it: up to 1.40.0 it
2407
2581
  * was the whole target, so the referencing row failed every read, after its write. Every other
2408
2582
  * target is held as declared, a class read back as its instance, as up to 1.40.0: one whose hidden
2409
2583
  * fields are optional, or read absent as a value (`Schema.Unknown`, `Schema.Any`,
2410
2584
  * `Schema.UndefinedOr`, a decoding default).
2411
2585
  */
2412
- export const refHeldTarget = (target) => {
2413
- const decided = refHeldTargets.get(target);
2586
+ export const refHeldTarget = (target, edge = false) => {
2587
+ const cache = edge ? edgeHeldTargets : refHeldTargets;
2588
+ const decided = cache.get(target);
2414
2589
  if (decided !== undefined)
2415
2590
  return decided;
2416
2591
  const fields = getSchemaFields(target);
2417
- const hidden = fields === undefined ? new Set() : hiddenFieldNamesOf(fields);
2592
+ // Its hidden fields, and the fields it offloads (#179), which a ref never stores either. An
2593
+ // aggregate's edge (`edge`) keeps the offloaded ones: its own row stores them, uploaded afresh
2594
+ // under its own key (design §6.9), never a pointer copied from the entity's row.
2595
+ const hidden = fields === undefined
2596
+ ? new Set()
2597
+ : new Set([
2598
+ ...hiddenFieldNamesOf(fields),
2599
+ ...(edge
2600
+ ? []
2601
+ : [...(offloadedByModel.get(target) ?? [])].filter((name) => hasOwnName(fields, name))),
2602
+ ]);
2418
2603
  // A hidden field read absent, as a read of the referencing row decodes it.
2419
2604
  const failsAbsent = (name) => Option.isNone(Schema.decodeUnknownOption(substituteSchemaDeep(Schema.Struct({ [name]: fields[name] }), {
2420
2605
  legacyReads: true,
@@ -2425,7 +2610,7 @@ export const refHeldTarget = (target) => {
2425
2610
  : target;
2426
2611
  if (held !== target)
2427
2612
  heldStructs.add(held);
2428
- refHeldTargets.set(target, held);
2613
+ cache.set(target, held);
2429
2614
  return held;
2430
2615
  };
2431
2616
  /** @internal {@link refHeldTarget}, substituted as a read or a write takes it (`options`). */
@@ -2470,20 +2655,37 @@ protoRenamed = false) => {
2470
2655
  : targetModel;
2471
2656
  // As optional as the model declares it (#241): a row an optional ref was left out of, or
2472
2657
  // removed from, reads without it. Up to 1.40.0 the target replaced the declaration whole.
2473
- const optional = optionalField(ownValue(modelFields, ref.fieldName));
2658
+ const declared = ownValue(modelFields, ref.fieldName);
2659
+ const optional = optionalField(declared);
2474
2660
  const asDeclared = (target) => optional === undefined ? target : optional.rewrap(target);
2475
2661
  // In the storage the target's `DynamoModel.configure` gives its fields, which the ref holds them
2476
2662
  // in (`refDataOf`), and its refs' fields in theirs. Up to 1.41.0 the default: a date stored as
2477
2663
  // epoch seconds read back as milliseconds, and a condition or `pathSet` on it sent ISO text.
2478
2664
  const storage = heldStorageOf(ref.refEntity);
2479
- const configured = storage === undefined ? {} : { configuredStorage: storage };
2480
- // What a read of the target returns (`refHeldSchema`): its required hidden fields left out.
2481
- setOwn(fields, ref.fieldName, asDeclared(refHeldSchema(resolved, { enforceChecks: true, ...configured })));
2482
- setOwn(readFields, ref.fieldName, asDeclared(refHeldSchema(resolved, {
2483
- legacyReads: true,
2484
- absentReads: true,
2485
- ...configured,
2486
- })));
2665
+ // ...and the target's own refs as what a read of each of their entities returns, however the
2666
+ // target's model declares them. Up to 1.42.1 a `DynamoModel.ref` field of the target hid its
2667
+ // model's fields, so its dates were read as the ref holds no row (`Expected DateTime.Utc`).
2668
+ const nested = entityRefsOf(ref.refEntity);
2669
+ const configured = {
2670
+ ...(storage === undefined ? {} : { configuredStorage: storage }),
2671
+ ...(Object.keys(nested).length === 0 ? {} : { heldRefs: nested }),
2672
+ };
2673
+ // What a read of the target returns (`refHeldSchema`): its required hidden fields left out. A
2674
+ // ref the model declares in a union (`Schema.NullOr`, `Schema.UndefinedOr`, one with other
2675
+ // members) keeps it: the walk re-points the ref's member alone (`heldRef`), as it does a ref
2676
+ // nested in a ref's target, so a row holding `null` for it reads. Up to 1.42.1 the target
2677
+ // replaced the union, and a `null` failed every read (`Expected object`). A union holding no
2678
+ // member the walk takes for the ref (`unionHoldsRef`) is still replaced, as 1.42.1 replaced it.
2679
+ const union = optional?.union ?? optional?.inner ?? declared;
2680
+ const inUnion = SchemaAST.isUnion(union.ast) && unionHoldsRef(union, resolved);
2681
+ const held = (options) => inUnion
2682
+ ? substituteSchemaDeep(declared, { ...options, heldRef: { model: resolved, refs: nested } })
2683
+ : asDeclared(refHeldSchema(resolved, options));
2684
+ setOwn(fields, ref.fieldName, held({ enforceChecks: true, ...configured }));
2685
+ const read = held({ legacyReads: true, absentReads: true, ...configured });
2686
+ // ...and, as every other field of a read, an absent attribute as the value it stands for (a
2687
+ // `Schema.UndefinedOr` ref left out reads as `undefined`).
2688
+ setOwn(readFields, ref.fieldName, inUnion ? readingAbsent(read) : read);
2487
2689
  }
2488
2690
  // Each of Effect's look-alike declarations (a `BigDecimal`, a `Duration`, an address, a date in
2489
2691
  // an `Option`, ...) reads back as a real instance, not the map DynamoDB hands back for it. A field
@@ -2555,11 +2757,21 @@ protoRenamed = false) => {
2555
2757
  const refFieldSet = new Set(resolvedRefs.map((r) => r.fieldName));
2556
2758
  // A ref's id is taken as the model takes the ref (#241): required where it's required, and left
2557
2759
  // out (`Schema.optionalKey`) or `undefined` too (`Schema.optional`) where the model declares the
2558
- // ref optional, which then stores no ref. Up to 1.40.0 the id was always required.
2760
+ // ref optional, which then stores no ref. Up to 1.40.0 the id was always required. Where the model
2761
+ // declares the ref nullable (`Schema.NullOr`) the id takes `null` too, which stores `null` for the
2762
+ // ref and fetches nothing, and `undefined` where it declares `Schema.UndefinedOr`.
2763
+ const refModelOf = (ref) => {
2764
+ const model = ref.refEntity?.model;
2765
+ return isConfiguredModel(model) ? model.model : model;
2766
+ };
2559
2767
  const refIdInput = (ref) => {
2560
2768
  const declared = modelFields[ref.fieldName];
2561
2769
  const optional = declared === undefined ? undefined : optionalField(declared);
2562
- return optional === undefined ? ref.keySchema : optional.rewrap(ref.keySchema);
2770
+ const { nullable, undefinable } = refNullish(declared, refModelOf(ref));
2771
+ const id = nullable ? Schema.NullOr(ref.keySchema) : ref.keySchema;
2772
+ if (optional !== undefined)
2773
+ return optional.rewrap(id);
2774
+ return undefinable ? Schema.UndefinedOr(id) : id;
2563
2775
  };
2564
2776
  const buildInputFieldsAcc = () => {
2565
2777
  const acc = {};
@@ -2634,7 +2846,10 @@ protoRenamed = false) => {
2634
2846
  continue;
2635
2847
  if (immutableFields.has(ref.fieldName))
2636
2848
  continue;
2637
- setOwn(acc, ref.idFieldName, Schema.optionalKey(ref.keySchema));
2849
+ // `null` detaches a ref the model declares nullable, as `.remove()` an optional one.
2850
+ const nullable = refNullish(ownValue(modelFields, ref.fieldName), refModelOf(ref)).nullable;
2851
+ const id = nullable ? Schema.NullOr(ref.keySchema) : ref.keySchema;
2852
+ setOwn(acc, ref.idFieldName, Schema.optionalKey(id));
2638
2853
  }
2639
2854
  return acc;
2640
2855
  };
@@ -2745,10 +2960,13 @@ export const resolveUniqueFields = (def) => Array.isArray(def) ? def : def.field
2745
2960
  // ---------------------------------------------------------------------------
2746
2961
  // Path-addressed values (#133)
2747
2962
  // ---------------------------------------------------------------------------
2748
- /** The schema one path segment addresses inside `schema`, or `undefined`. */
2749
- const childAtSegment = (schema, segment) => {
2963
+ /**
2964
+ * The schema one path segment addresses inside `schema`, or `undefined`. `member`: what the walk
2965
+ * takes each member beneath an optional wrapper or a union as (a nested ref's, {@link heldRefMember}).
2966
+ */
2967
+ const childAtSegment = (schema, segment, member = (schema) => schema) => {
2750
2968
  const opt = optionalField(schema);
2751
- const inner = opt?.union ?? opt?.inner ?? schema;
2969
+ const inner = member(opt?.union ?? opt?.inner ?? schema);
2752
2970
  const s = inner;
2753
2971
  const fields = schemaFieldsOf(inner);
2754
2972
  if (fields !== undefined)
@@ -2764,8 +2982,8 @@ const childAtSegment = (schema, segment) => {
2764
2982
  if (SchemaAST.isUnion(inner.ast)) {
2765
2983
  // `NullOr(Stamp)` and friends: the first member the segment resolves in.
2766
2984
  const members = Array.isArray(s.members) ? s.members : [];
2767
- for (const member of members) {
2768
- const child = childAtSegment(member, segment);
2985
+ for (const each of members) {
2986
+ const child = childAtSegment(each, segment, member);
2769
2987
  if (child !== undefined)
2770
2988
  return child;
2771
2989
  }
@@ -2910,9 +3128,11 @@ const dynamoTypeOf = (value) => typeof value === "string" ? "S" : typeof value =
2910
3128
  * the operand as (`5` where a `NumberFromString` stores `"5"`). Text where text is stored could be
2911
3129
  * the stored text itself, a substring for `contains` or a prefix, which the library can't tell
2912
3130
  * apart, so it is sent as 1.41.0 sent it; so is a number where a number is stored, and a primitive
2913
- * where what's stored is no primitive.
3131
+ * where what's stored is no primitive. An aggregate's root reads it too.
3132
+ *
3133
+ * @internal
2914
3134
  */
2915
- const keepsAsGiven = (operand, stored) => isPrimitive(operand) && (!isPrimitive(stored) || dynamoTypeOf(stored) === dynamoTypeOf(operand));
3135
+ export const keepsAsGiven = (operand, stored) => isPrimitive(operand) && (!isPrimitive(stored) || dynamoTypeOf(stored) === dynamoTypeOf(operand));
2916
3136
  /** A plain object or class instance whose fields a path value can be walked by. */
2917
3137
  const isWalkableObject = (value) => value !== null &&
2918
3138
  typeof value === "object" &&
@@ -3054,7 +3274,12 @@ readFields = {},
3054
3274
  * What each ref's entity holds its fields in (`heldStorageOf`), its own refs' included, keyed by
3055
3275
  * ref field: the ref holds them in it, as its schemas do.
3056
3276
  */
3057
- refStorage = {}) => {
3277
+ refStorage = {},
3278
+ /**
3279
+ * The entity's refs ({@link HeldRefs}): a path below a ref follows a ref of its target's entity,
3280
+ * at any depth, as what the ref holds there ({@link heldRefMember}), as the ref's schemas do.
3281
+ */
3282
+ heldRefs = {}) => {
3058
3283
  const recordFields = writeFields;
3059
3284
  const cache = new Map();
3060
3285
  const encoderFor = (key, resolve) => {
@@ -3195,6 +3420,34 @@ refStorage = {}) => {
3195
3420
  }
3196
3421
  return out;
3197
3422
  };
3423
+ /**
3424
+ * The ref a path ends at below a ref (`path` naming a ref of each ref's target in turn), or
3425
+ * `undefined`: a ref nested in a ref's target, at any depth ({@link HeldRefs}).
3426
+ */
3427
+ const nestedRefAt = (path) => {
3428
+ let held;
3429
+ for (const [index, segment] of path.entries()) {
3430
+ if (typeof segment !== "string")
3431
+ return undefined;
3432
+ held = ownValue(index === 0 ? heldRefs : held?.refs, segment);
3433
+ if (held === undefined)
3434
+ return undefined;
3435
+ }
3436
+ return path.length < 2 ? undefined : held;
3437
+ };
3438
+ /**
3439
+ * What a walk takes a member of the schema at `path` as: where a ref nested in a ref's target is,
3440
+ * its member is what the ref holds there ({@link heldRefMember}), beneath the wrappers the model
3441
+ * declares around it; anything else as it is.
3442
+ */
3443
+ const memberAt = (path) => {
3444
+ const held = nestedRefAt(path);
3445
+ return held === undefined
3446
+ ? (member) => member
3447
+ : (member) => heldRefMember(member, held.model) ?? member;
3448
+ };
3449
+ /** {@link childAtSegment}, at `path` (the segments up to and including `segment`). */
3450
+ const childAt = (schema, segment, path) => childAtSegment(schema, segment, memberAt(path.slice(0, -1)));
3198
3451
  /**
3199
3452
  * The model's own schema at a path, before any substitution, and whether the path goes through a
3200
3453
  * later member of an optional union, which is written as declared ({@link writesFirstMemberOnly}).
@@ -3205,14 +3458,15 @@ refStorage = {}) => {
3205
3458
  return undefined;
3206
3459
  let current = ownValue(refTargets, head) ?? ownValue(modelFields, head);
3207
3460
  let asDeclared = false;
3208
- for (const segment of rest) {
3461
+ for (const [index, segment] of rest.entries()) {
3209
3462
  if (current === undefined)
3210
3463
  return undefined;
3211
3464
  const opt = optionalField(current);
3212
- if (opt?.union !== undefined && childAtSegment(opt.inner, segment) === undefined) {
3465
+ if (opt?.union !== undefined &&
3466
+ childAtSegment(opt.inner, segment, memberAt(segments.slice(0, index + 1))) === undefined) {
3213
3467
  asDeclared = true;
3214
3468
  }
3215
- current = childAtSegment(current, segment);
3469
+ current = childAt(current, segment, segments.slice(0, index + 2));
3216
3470
  }
3217
3471
  return current === undefined ? undefined : { schema: current, asDeclared };
3218
3472
  };
@@ -3220,7 +3474,7 @@ refStorage = {}) => {
3220
3474
  * `original`, the model's schema at `segments` (below a field), as a write stores it: a field of a
3221
3475
  * ref's target in the storage its `DynamoModel.configure` gives it, which the ref holds it in
3222
3476
  * (`refStorage`), as the ref's own schemas do, and a field of a ref that target holds in turn in
3223
- * its entity's.
3477
+ * its entity's, that ref held as its entity's read returns it (`heldRef`).
3224
3478
  */
3225
3479
  const storedSchema = (segments, original) => {
3226
3480
  const name = segments[segments.length - 1];
@@ -3229,7 +3483,9 @@ refStorage = {}) => {
3229
3483
  held = typeof segment === "string" ? ownValue(held?.refs, segment) : undefined;
3230
3484
  }
3231
3485
  const named = typeof name === "string";
3232
- return substituteSchemaDeep(original, withConfiguredStorage({ enforceChecks: true }, name, original, named ? ownValue(held?.fields, name) : undefined, named ? ownValue(held?.refs, name) : undefined));
3486
+ const options = withConfiguredStorage({ enforceChecks: true }, name, original, named ? ownValue(held?.fields, name) : undefined, named ? ownValue(held?.refs, name) : undefined);
3487
+ const heldRef = nestedRefAt(segments);
3488
+ return substituteSchemaDeep(original, heldRef === undefined ? options : { ...options, heldRef });
3233
3489
  };
3234
3490
  const schemaAt = (segments) => {
3235
3491
  const at = originalAt(segments);
@@ -3301,26 +3557,27 @@ refStorage = {}) => {
3301
3557
  const members = union.members;
3302
3558
  return Array.isArray(members) ? members : undefined;
3303
3559
  };
3304
- const through = (members, segment, asDeclared) => {
3560
+ const through = (members, segment, asDeclared, path) => {
3305
3561
  const out = [];
3306
3562
  for (const [index, member] of members.entries()) {
3307
- const reached = reach(member, segment, asDeclared(index));
3563
+ const reached = reach(member, segment, asDeclared(index), path);
3308
3564
  if (reached === undefined)
3309
3565
  return undefined;
3310
3566
  out.push(...reached);
3311
3567
  }
3312
3568
  return out;
3313
3569
  };
3314
- const reach = (schema, segment, asDeclared) => {
3570
+ /** The positions below `schema` at `segment`, `path` the segments up to and including it. */
3571
+ const reach = (schema, segment, asDeclared, path) => {
3315
3572
  const opt = optionalField(schema);
3316
3573
  if (opt?.union !== undefined) {
3317
3574
  // An optional union writes its first member substituted and the later ones as declared.
3318
3575
  const members = membersOf(opt.union)?.filter((m) => m.ast._tag !== "Undefined");
3319
3576
  if (members === undefined)
3320
3577
  return undefined;
3321
- return through(members, segment, (index) => asDeclared || index > 0);
3578
+ return through(members, segment, (index) => asDeclared || index > 0, path);
3322
3579
  }
3323
- const inner = opt?.inner ?? schema;
3580
+ const inner = memberAt(path.slice(0, -1))(opt?.inner ?? schema);
3324
3581
  const fields = schemaFieldsOf(inner);
3325
3582
  if (fields !== undefined) {
3326
3583
  const child = typeof segment === "string" ? fields[segment] : undefined;
@@ -3331,7 +3588,7 @@ refStorage = {}) => {
3331
3588
  return [];
3332
3589
  if (SchemaAST.isUnion(ast)) {
3333
3590
  const members = membersOf(inner);
3334
- return members === undefined ? undefined : through(members, segment, () => asDeclared);
3591
+ return members === undefined ? undefined : through(members, segment, () => asDeclared, path);
3335
3592
  }
3336
3593
  if (SchemaAST.isArrays(ast)) {
3337
3594
  if (typeof segment !== "number")
@@ -3352,10 +3609,11 @@ refStorage = {}) => {
3352
3609
  return undefined;
3353
3610
  };
3354
3611
  let positions = [{ schema: top, asDeclared: false }];
3355
- for (const segment of rest) {
3612
+ for (const [index, segment] of rest.entries()) {
3356
3613
  const next = [];
3614
+ const path = segments.slice(0, index + 2);
3357
3615
  for (const position of positions) {
3358
- const reached = reach(position.schema, segment, position.asDeclared);
3616
+ const reached = reach(position.schema, segment, position.asDeclared, path);
3359
3617
  if (reached === undefined)
3360
3618
  return undefined;
3361
3619
  for (const p of reached) {
@@ -3445,7 +3703,10 @@ refStorage = {}) => {
3445
3703
  const ways = [];
3446
3704
  const walk = (schema, depth, choices, asDeclared, parent) => {
3447
3705
  if (depth === rest.length) {
3448
- ways.push({ choices, end: { _tag: "At", original: schema, asDeclared, parent } });
3706
+ ways.push({
3707
+ choices,
3708
+ end: { _tag: "At", original: schema, asDeclared, parent, path: segments },
3709
+ });
3449
3710
  return;
3450
3711
  }
3451
3712
  const segment = rest[depth];
@@ -3455,28 +3716,40 @@ refStorage = {}) => {
3455
3716
  // `originalAt` writes it ({@link writesFirstMemberOnly}).
3456
3717
  const first = opt?.union === undefined ? undefined : new Set(membersOf(opt.inner));
3457
3718
  const at = JSON.stringify(segments.slice(0, depth + 1));
3719
+ const held = memberAt(segments.slice(0, depth + 1));
3458
3720
  for (const member of members) {
3459
3721
  const chosen = members.length > 1 ? new Map(choices).set(at, member) : choices;
3460
3722
  const declared = asDeclared || (first !== undefined && !first.has(member));
3461
- const child = childAtSegment(member, segment);
3723
+ const child = childAt(member, segment, segments.slice(0, depth + 2));
3462
3724
  if (child !== undefined)
3463
- walk(child, depth + 1, chosen, declared, parentOf(member));
3725
+ walk(child, depth + 1, chosen, declared, parentOf(held(member)));
3464
3726
  else
3465
- ways.push({ choices: chosen, end: missingAt(member, segment) });
3727
+ ways.push({ choices: chosen, end: missingAt(held(member), segment) });
3466
3728
  }
3467
3729
  };
3468
3730
  walk(start, 0, new Map(), false, "other");
3469
3731
  return ways;
3470
3732
  };
3471
3733
  const wayStored = new Map();
3734
+ /**
3735
+ * What a row holds at a way's end: below a field, as a write stores it there ({@link storedSchema}:
3736
+ * a ref's target's fields in the storage its `configure` gives them, at any depth). Up to 1.42.1 a
3737
+ * way's end was taken in the default storage, so a whole ref written below a union on the way
3738
+ * (`pathSet(["a", "b", "team"], team)` through a `NullOr`) failed in epoch storage.
3739
+ */
3472
3740
  const wayDeclared = (way) => {
3473
3741
  if (way.asDeclared)
3474
3742
  return way.original;
3475
- const found = wayStored.get(way.original);
3743
+ const byPath = wayStored.get(way.original) ?? new Map();
3744
+ wayStored.set(way.original, byPath);
3745
+ const key = JSON.stringify(way.path);
3746
+ const found = byPath.get(key);
3476
3747
  if (found !== undefined)
3477
3748
  return found;
3478
- const stored = substituteSchemaDeep(way.original, { enforceChecks: true });
3479
- wayStored.set(way.original, stored);
3749
+ const stored = way.path.length > 1
3750
+ ? storedSchema(way.path, way.original)
3751
+ : substituteSchemaDeep(way.original, { enforceChecks: true });
3752
+ byPath.set(key, stored);
3480
3753
  return stored;
3481
3754
  };
3482
3755
  const wayValidators = new Map();
@@ -3554,10 +3827,16 @@ refStorage = {}) => {
3554
3827
  * clause below it, the member taken alike for every clause under the same union. Anything the
3555
3828
  * schema can't judge is taken as read, and a member holding nothing there as not reading it.
3556
3829
  */
3557
- const judge = (schema, below, asDeclared, parent, strict) => {
3830
+ const judge = (schema, below, asDeclared, parent, strict, path) => {
3558
3831
  const ending = below.filter(({ rest }) => rest.length === 0);
3559
3832
  const further = bySegment(below);
3560
- const at = { _tag: "At", original: schema, asDeclared, parent };
3833
+ const at = {
3834
+ _tag: "At",
3835
+ original: schema,
3836
+ asDeclared,
3837
+ parent,
3838
+ path,
3839
+ };
3561
3840
  if (strict && ending.length > 0 && (ending.length > 1 || further.length > 0))
3562
3841
  return false;
3563
3842
  for (const { clause } of ending) {
@@ -3574,7 +3853,7 @@ refStorage = {}) => {
3574
3853
  // writes it ({@link writesFirstMemberOnly}).
3575
3854
  const first = opt?.union === undefined ? undefined : new Set(membersOf(opt.inner));
3576
3855
  const declaredFor = (member) => asDeclared || (first !== undefined && !first.has(member));
3577
- const reads = (member) => memberReads(member, further, declaredFor(member), strict);
3856
+ const reads = (member) => memberReads(member, further, declaredFor(member), strict, path);
3578
3857
  if (members.length === 1)
3579
3858
  return reads(members[0]);
3580
3859
  // Members that don't name every segment alike are written as 1.40.0 wrote them, by the names
@@ -3584,14 +3863,18 @@ refStorage = {}) => {
3584
3863
  if (!strict)
3585
3864
  return members.some(reads);
3586
3865
  return (members.every(reads) ||
3587
- members.some((member) => fixes(member, further, declaredFor(member)) && reads(member)));
3866
+ members.some((member) => fixes(member, further, declaredFor(member), path) && reads(member)));
3588
3867
  };
3589
- /** {@link judge} under one member, for the clauses going further, by segment. */
3590
- const memberReads = (member, further, asDeclared, strict) => further.every(([segment, below]) => {
3591
- const read = readAs(member);
3592
- const child = childAtSegment(read, segment);
3868
+ /**
3869
+ * {@link judge} under one member, for the clauses going further, by segment. `path`: the segments
3870
+ * up to the member.
3871
+ */
3872
+ const memberReads = (member, further, asDeclared, strict, path) => further.every(([segment, below]) => {
3873
+ const read = readAs(memberAt(path)(member));
3874
+ const at = [...path, segment];
3875
+ const child = childAt(read, segment, at);
3593
3876
  if (child !== undefined)
3594
- return judge(child, below, asDeclared, parentOf(read), strict);
3877
+ return judge(child, below, asDeclared, parentOf(read), strict, at);
3595
3878
  const end = missingAt(read, segment);
3596
3879
  if (strict)
3597
3880
  return end._tag !== "Unknown";
@@ -3607,20 +3890,21 @@ refStorage = {}) => {
3607
3890
  * leave. What the member holds after the update is then all theirs, whatever the row held. A
3608
3891
  * member that isn't a plain struct isn't known to be.
3609
3892
  */
3610
- const fixes = (member, further, asDeclared) => {
3611
- const read = readAs(member);
3893
+ const fixes = (member, further, asDeclared, path) => {
3894
+ const read = readAs(memberAt(path)(member));
3612
3895
  if (parentOf(read) !== "struct")
3613
3896
  return false;
3614
3897
  return read.ast.propertySignatures.every((property) => {
3615
3898
  const group = further.find(([segment]) => segment === property.name);
3616
3899
  if (group === undefined)
3617
3900
  return false;
3618
- const child = childAtSegment(read, property.name);
3619
- return child !== undefined && fixedBy(child, group[1], asDeclared);
3901
+ const at = [...path, property.name];
3902
+ const child = childAt(read, property.name, at);
3903
+ return child !== undefined && fixedBy(child, group[1], asDeclared, at);
3620
3904
  });
3621
3905
  };
3622
3906
  /** Whether the clauses below a node set all of it ({@link fixes}). */
3623
- const fixedBy = (schema, below, asDeclared) => {
3907
+ const fixedBy = (schema, below, asDeclared, path) => {
3624
3908
  const ending = below.filter(({ rest }) => rest.length === 0);
3625
3909
  if (ending.length > 0) {
3626
3910
  return ending.length === 1 && below.length === 1 && ending[0].clause._tag !== "Other";
@@ -3633,8 +3917,8 @@ refStorage = {}) => {
3633
3917
  return false;
3634
3918
  return members.some((member) => {
3635
3919
  const declared = asDeclared || (first !== undefined && !first.has(member));
3636
- return (fixes(member, further, declared) &&
3637
- (members.length === 1 || memberReads(member, further, declared, true)));
3920
+ return (fixes(member, further, declared, path) &&
3921
+ (members.length === 1 || memberReads(member, further, declared, true, path)));
3638
3922
  });
3639
3923
  };
3640
3924
  const reads = (clauses, strict) => {
@@ -3647,7 +3931,7 @@ refStorage = {}) => {
3647
3931
  return !strict;
3648
3932
  }
3649
3933
  const below = clauses.map((clause) => ({ rest: clause.segments.slice(1), clause }));
3650
- return judge(start, below, false, "other", strict);
3934
+ return judge(start, below, false, "other", strict, [head]);
3651
3935
  };
3652
3936
  const readDecoders = new WeakMap();
3653
3937
  const readDecoder = (schema) => {