@effect-dynamodb/schema 1.40.0 → 1.42.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 (104) hide show
  1. package/dist/Aggregate.d.ts.map +1 -1
  2. package/dist/Aggregate.js +4 -1
  3. package/dist/Aggregate.js.map +1 -1
  4. package/dist/DynamoModel.d.ts +35 -7
  5. package/dist/DynamoModel.d.ts.map +1 -1
  6. package/dist/DynamoModel.js +34 -16
  7. package/dist/DynamoModel.js.map +1 -1
  8. package/dist/DynamoSchema.d.ts +20 -0
  9. package/dist/DynamoSchema.d.ts.map +1 -1
  10. package/dist/DynamoSchema.js.map +1 -1
  11. package/dist/Entity.d.ts +18 -6
  12. package/dist/Entity.d.ts.map +1 -1
  13. package/dist/Entity.js +97 -34
  14. package/dist/Entity.js.map +1 -1
  15. package/dist/Errors.d.ts +61 -4
  16. package/dist/Errors.d.ts.map +1 -1
  17. package/dist/Errors.js +83 -6
  18. package/dist/Errors.js.map +1 -1
  19. package/dist/KeyComposer.d.ts +55 -9
  20. package/dist/KeyComposer.d.ts.map +1 -1
  21. package/dist/KeyComposer.js +157 -58
  22. package/dist/KeyComposer.js.map +1 -1
  23. package/dist/VectorIndex.d.ts.map +1 -1
  24. package/dist/VectorIndex.js +5 -4
  25. package/dist/VectorIndex.js.map +1 -1
  26. package/dist/index.d.ts +1 -1
  27. package/dist/index.d.ts.map +1 -1
  28. package/dist/index.js +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/internal/AggregateSchemas.d.ts +131 -6
  31. package/dist/internal/AggregateSchemas.d.ts.map +1 -1
  32. package/dist/internal/AggregateSchemas.js +250 -31
  33. package/dist/internal/AggregateSchemas.js.map +1 -1
  34. package/dist/internal/CompositeCodec.d.ts +26 -4
  35. package/dist/internal/CompositeCodec.d.ts.map +1 -1
  36. package/dist/internal/CompositeCodec.js +127 -25
  37. package/dist/internal/CompositeCodec.js.map +1 -1
  38. package/dist/internal/EffectInstances.d.ts +8 -2
  39. package/dist/internal/EffectInstances.d.ts.map +1 -1
  40. package/dist/internal/EffectInstances.js +33 -27
  41. package/dist/internal/EffectInstances.js.map +1 -1
  42. package/dist/internal/EntityPacked.d.ts.map +1 -1
  43. package/dist/internal/EntityPacked.js +5 -4
  44. package/dist/internal/EntityPacked.js.map +1 -1
  45. package/dist/internal/EntitySchemas.d.ts +163 -3
  46. package/dist/internal/EntitySchemas.d.ts.map +1 -1
  47. package/dist/internal/EntitySchemas.js +972 -85
  48. package/dist/internal/EntitySchemas.js.map +1 -1
  49. package/dist/internal/EntityTypes.d.ts +53 -20
  50. package/dist/internal/EntityTypes.d.ts.map +1 -1
  51. package/dist/internal/InstanceReads.d.ts +7 -0
  52. package/dist/internal/InstanceReads.d.ts.map +1 -1
  53. package/dist/internal/InstanceReads.js +47 -29
  54. package/dist/internal/InstanceReads.js.map +1 -1
  55. package/dist/internal/KeyReadings.d.ts +30 -0
  56. package/dist/internal/KeyReadings.d.ts.map +1 -0
  57. package/dist/internal/KeyReadings.js +99 -0
  58. package/dist/internal/KeyReadings.js.map +1 -0
  59. package/dist/internal/KeyText136.d.ts.map +1 -1
  60. package/dist/internal/KeyText136.js +6 -4
  61. package/dist/internal/KeyText136.js.map +1 -1
  62. package/dist/internal/LostValues.d.ts +50 -0
  63. package/dist/internal/LostValues.d.ts.map +1 -0
  64. package/dist/internal/LostValues.js +465 -0
  65. package/dist/internal/LostValues.js.map +1 -0
  66. package/dist/internal/ObjectKeyText.d.ts +29 -0
  67. package/dist/internal/ObjectKeyText.d.ts.map +1 -0
  68. package/dist/internal/ObjectKeyText.js +50 -0
  69. package/dist/internal/ObjectKeyText.js.map +1 -0
  70. package/dist/internal/OwnNames.d.ts +205 -0
  71. package/dist/internal/OwnNames.d.ts.map +1 -0
  72. package/dist/internal/OwnNames.js +415 -0
  73. package/dist/internal/OwnNames.js.map +1 -0
  74. package/dist/internal/PackedCodec.d.ts +7 -8
  75. package/dist/internal/PackedCodec.d.ts.map +1 -1
  76. package/dist/internal/PackedCodec.js +81 -55
  77. package/dist/internal/PackedCodec.js.map +1 -1
  78. package/dist/internal/RestoredKeyText.d.ts +10 -0
  79. package/dist/internal/RestoredKeyText.d.ts.map +1 -0
  80. package/dist/internal/RestoredKeyText.js +50 -0
  81. package/dist/internal/RestoredKeyText.js.map +1 -0
  82. package/dist/internal/SchemaAccessors.d.ts +132 -12
  83. package/dist/internal/SchemaAccessors.d.ts.map +1 -1
  84. package/dist/internal/SchemaAccessors.js +779 -56
  85. package/dist/internal/SchemaAccessors.js.map +1 -1
  86. package/dist/internal/SchemaIssue.d.ts +21 -0
  87. package/dist/internal/SchemaIssue.d.ts.map +1 -0
  88. package/dist/internal/SchemaIssue.js +34 -0
  89. package/dist/internal/SchemaIssue.js.map +1 -0
  90. package/dist/internal/SharedKeyAttribute.d.ts.map +1 -1
  91. package/dist/internal/SharedKeyAttribute.js +2 -1
  92. package/dist/internal/SharedKeyAttribute.js.map +1 -1
  93. package/dist/internal/StoredForm.d.ts +139 -0
  94. package/dist/internal/StoredForm.d.ts.map +1 -0
  95. package/dist/internal/StoredForm.js +503 -0
  96. package/dist/internal/StoredForm.js.map +1 -0
  97. package/dist/internal/ZOrderCurve.d.ts.map +1 -1
  98. package/dist/internal/ZOrderCurve.js +2 -1
  99. package/dist/internal/ZOrderCurve.js.map +1 -1
  100. package/dist/internal/ZoneKeyText.d.ts +55 -0
  101. package/dist/internal/ZoneKeyText.d.ts.map +1 -0
  102. package/dist/internal/ZoneKeyText.js +264 -0
  103. package/dist/internal/ZoneKeyText.js.map +1 -0
  104. package/package.json +4 -4
@@ -7,8 +7,13 @@
7
7
  */
8
8
  import { DateTime } from "effect";
9
9
  import { applyCasing, composeClusteredSortKey, composeCollectionKey, composeIsolatedSortKey, composeKey, KEY_DELIMITER, } from "./DynamoSchema.js";
10
- import { encodeBigInt, encodeNumber, isStoredNaN } from "./internal/NumericKey.js";
10
+ import { encodeBigInt, encodeNumber, isStoredNaN, KeyValueError } from "./internal/NumericKey.js";
11
+ import { judgeObjectKeyText } from "./internal/ObjectKeyText.js";
12
+ // A record composed from may be a caller's key or query composites: a composite's value is read as
13
+ // a caller's payload is (`given`), so a member every object inherits is no value (#239).
14
+ import { given, PROTO_ENTRY, setOwn } from "./internal/OwnNames.js";
11
15
  import { MAX_SHARD_COUNT, ShardSourceError, shardMarker, shardOf } from "./internal/Shards.js";
16
+ import { zonesAsStored } from "./internal/StoredForm.js";
12
17
  import { concatBytes, interleave, prefixSegment, toBytes, } from "./internal/ZOrderCurve.js";
13
18
  import { declareDimension } from "./internal/ZOrderDimension.js";
14
19
  /** Is this sort key half a Z-order one? */
@@ -177,7 +182,7 @@ const normalizeZOrderHalf = (config, sk) => {
177
182
  * Returns: ["t-1", "a@b.com"]
178
183
  */
179
184
  export const extractComposites = (composite, record) => composite.map((attr) => {
180
- const value = record[attr];
185
+ const value = given(record, attr);
181
186
  if (value === undefined || value === null) {
182
187
  throw new Error(`Missing composite attribute "${attr}" in record`);
183
188
  }
@@ -191,7 +196,7 @@ export const extractComposites = (composite, record) => composite.map((attr) =>
191
196
  export const tryExtractComposites = (composite, record) => {
192
197
  const values = [];
193
198
  for (const attr of composite) {
194
- const value = record[attr];
199
+ const value = given(record, attr);
195
200
  if (value === undefined || value === null)
196
201
  return undefined;
197
202
  values.push(serializeValue(value));
@@ -217,7 +222,29 @@ const serializeWith = (value, number, bigint) => {
217
222
  // Native Date
218
223
  if (value instanceof Date)
219
224
  return value.toISOString();
220
- return String(value);
225
+ // A named zone's text names it as 4.1.2 did, whichever Effect is installed
226
+ return String(zonesAsStored(value));
227
+ };
228
+ /**
229
+ * Whether `value` has key text of its own ({@link serializeValue}): a string, a number, a bigint, a
230
+ * boolean, a `DateTime` or a `Date`. Any other value's text is its `String` form, which says nothing
231
+ * exact about the value: a map read back from a row gives `[object Object]`, where the instance it
232
+ * was stored from gave its own `toString`. Two such texts compared say nothing either way. A map
233
+ * read back from a marshalled `DateTime` carries its type id but isn't one (#133): not exact.
234
+ */
235
+ export const hasExactText = (value) => {
236
+ if (typeof value === "string" ||
237
+ typeof value === "number" ||
238
+ typeof value === "bigint" ||
239
+ typeof value === "boolean" ||
240
+ isStoredNaN(value) ||
241
+ value instanceof Date) {
242
+ return true;
243
+ }
244
+ if (typeof value !== "object" || value === null || !DateTime.isDateTime(value))
245
+ return false;
246
+ const proto = Object.getPrototypeOf(value);
247
+ return proto !== Object.prototype && proto !== null;
221
248
  };
222
249
  /**
223
250
  * Serialize a value for a composite key under `escapeKeys: true`. {@link compositeText} is the
@@ -317,13 +344,29 @@ export const serializeSortKeyValue = (value, casing) => escapeKeyText(serializeV
317
344
  export const keyText = (schema, text, casing) => (schema.escapeKeys === true ? escapeKeyText(text, casing) : applyCasing(text, casing));
318
345
  /** A composite value's text in a key: {@link compositeText}, then {@link keyText}. */
319
346
  export const keyValueText = (schema, value, casing) => keyText(schema, compositeText(schema, value), casing);
320
- /** {@link extractComposites} under the schema's setting, each value's {@link keyValueText}. */
321
- export const extractKeyComposites = (schema, index, composite, record) => composite.map((attr) => {
322
- const value = record[attr];
347
+ /**
348
+ * {@link compositeText} of `attr`'s `value` where a key is composed: `key` is the definition whose
349
+ * key it is and `half` its half. Under a write's own schema (`objectKeys`) an object composing
350
+ * `[object Object]` is refused here (#242, `EDD-9162`), so only a key actually composed refuses: an
351
+ * index a write leaves out (sparse, a half skipped) never composes its composites.
352
+ */
353
+ export const siteCompositeText = (schema, value, attr, key, half) => {
354
+ const text = compositeText(schema, value);
355
+ if (schema.objectKeys !== undefined) {
356
+ judgeObjectKeyText(schema, value, text, () => ({ attr, value, key, half }));
357
+ }
358
+ return text;
359
+ };
360
+ /**
361
+ * {@link extractComposites} under the schema's setting, each value's {@link keyValueText}. `half`
362
+ * says which half of `index` they're composed into, for {@link siteCompositeText}.
363
+ */
364
+ export const extractKeyComposites = (schema, index, composite, record, half) => composite.map((attr) => {
365
+ const value = given(record, attr);
323
366
  if (value === undefined || value === null) {
324
367
  throw new Error(`Missing composite attribute "${attr}" in record`);
325
368
  }
326
- return keyValueText(schema, value, index.casing ?? schema.casing);
369
+ return keyText(schema, siteCompositeText(schema, value, attr, index, half), index.casing ?? schema.casing);
327
370
  });
328
371
  /**
329
372
  * Compose a partition key for an entity index.
@@ -332,7 +375,7 @@ export const extractKeyComposites = (schema, index, composite, record) => compos
332
375
  * For collection indexes: `$schema#v<version>#collectionName#attr1#attr2`
333
376
  */
334
377
  export const composePk = (schema, entityType, index, record) => {
335
- const composites = extractKeyComposites(schema, index, index.pk.composite, record);
378
+ const composites = extractKeyComposites(schema, index, index.pk.composite, record, "pk");
336
379
  return composePkText(schema, entityType, index, composites, index.pk.composite, composedShardMarker(entityType, index, record));
337
380
  };
338
381
  /**
@@ -363,7 +406,7 @@ const composedShardMarker = (entityType, index, record) => {
363
406
  if (shards === undefined)
364
407
  return undefined;
365
408
  const source = boundShardSource(index, shards);
366
- const absent = source.filter((attr) => record[attr] === undefined || record[attr] === null);
409
+ const absent = source.filter((attr) => given(record, attr) === undefined || given(record, attr) === null);
367
410
  if (absent.length > 0) {
368
411
  throw new ShardSourceError({
369
412
  message: `[EDD-9100] Entity "${entityType}": index "${index.index}" is sharded, and its partition ` +
@@ -372,7 +415,7 @@ const composedShardMarker = (entityType, index, record) => {
372
415
  `Include ${absent.length === 1 ? "it" : "them"} in the write.`,
373
416
  });
374
417
  }
375
- return shardMarker(shards.count, shardOf(source.map((attr) => serializeValue(record[attr])), shards.count));
418
+ return shardMarker(shards.count, shardOf(source.map((attr) => serializeValue(given(record, attr))), shards.count));
376
419
  };
377
420
  /**
378
421
  * `index` as its unsharded layout: what a `previous: 1` migration reads, and the
@@ -395,7 +438,7 @@ export const composeStoredPk = (schema, entityType, index, record) => {
395
438
  return undefined;
396
439
  const shards = index.shards;
397
440
  if (shards !== undefined &&
398
- !boundShardSource(index, shards).every((attr) => hasValue(record[attr]))) {
441
+ !boundShardSource(index, shards).every((attr) => hasValue(given(record, attr)))) {
399
442
  return undefined;
400
443
  }
401
444
  return outcome.length === index.pk.composite.length
@@ -448,12 +491,12 @@ const pinsShardSource = (index, source, record) => {
448
491
  const pins = new Set(index.pk.composite);
449
492
  if (!isZOrderHalf(index.sk)) {
450
493
  for (const attr of index.sk.composite) {
451
- if (!hasValue(record[attr]))
494
+ if (!hasValue(given(record, attr)))
452
495
  break;
453
496
  pins.add(attr);
454
497
  }
455
498
  }
456
- return source.every((attr) => pins.has(attr) && hasValue(record[attr]));
499
+ return source.every((attr) => pins.has(attr) && hasValue(given(record, attr)));
457
500
  };
458
501
  /**
459
502
  * The partitions a query of `index` reads for the pinned composites in `record`,
@@ -477,7 +520,7 @@ options) => {
477
520
  const pruned = options?.prune !== false && pinsShardSource(index, source, record);
478
521
  const current = pruned
479
522
  ? [
480
- partition(shards.count, shardOf(source.map((attr) => serializeValue(record[attr])), shards.count)),
523
+ partition(shards.count, shardOf(source.map((attr) => serializeValue(given(record, attr))), shards.count)),
481
524
  ]
482
525
  : layout(shards.count);
483
526
  const unsharded = shards.previous === 1
@@ -508,7 +551,7 @@ options) => {
508
551
  */
509
552
  export const composeSk = (schema, entityType, entityVersion, index, record) => {
510
553
  assertStringHalf(index, "composeSk");
511
- const composites = extractKeyComposites(schema, index, index.sk.composite, record);
554
+ const composites = extractKeyComposites(schema, index, index.sk.composite, record, "sk");
512
555
  const names = [...index.sk.composite];
513
556
  const collection = index.collection;
514
557
  const collectionType = index.type ?? "isolated";
@@ -563,14 +606,14 @@ export const composeZOrderSk = (schema, index, record) => {
563
606
  }
564
607
  const casing = effectiveCasing(schema, index);
565
608
  const segments = prefix.map((part) => {
566
- const value = record[part.field];
609
+ const value = given(record, part.field);
567
610
  if (value === undefined || value === null) {
568
611
  throw new Error(`Missing composite attribute "${part.field}" in record`);
569
612
  }
570
- return prefixSegment(keyValueText(schema, value, casing), part.hash);
613
+ return prefixSegment(keyText(schema, siteCompositeText(schema, value, part.field, index, "sk"), casing), part.hash);
571
614
  });
572
615
  const levels = layout.dimensions.map((dim) => {
573
- const value = record[dim.field];
616
+ const value = given(record, dim.field);
574
617
  if (value === undefined || value === null) {
575
618
  throw new Error(`Missing composite attribute "${dim.field}" in record`);
576
619
  }
@@ -596,13 +639,32 @@ export const composeHistoryItemSegment = (schema, entityType, entityVersion, pri
596
639
  const bare = composeSk(schema, entityType, entityVersion, { ...primary, sk: { ...primary.sk, composite: [] } }, {});
597
640
  return liveSk.startsWith(`${bare}#`) ? liveSk.slice(bare.length + 1) : undefined;
598
641
  };
642
+ /**
643
+ * The attribute an index's `half` is stored under, for a write giving it a value. One named
644
+ * `__proto__` (an index whose composites a row may lack, so its definition loads) is refused
645
+ * (`[EDD-9165]`, a `KeyValueError`): the AWS SDK's marshaller drops it, and its unmarshaller drops
646
+ * one an update stored. A write that gives it no value (the index left out, or its half removed)
647
+ * is as it was.
648
+ */
649
+ const keyAttribute = (index, half) => {
650
+ const field = index[half].field;
651
+ if (field !== "__proto__")
652
+ return field;
653
+ const which = half === "pk" ? "partition" : "sort";
654
+ const named = index.index === undefined ? "the table" : `index "${index.index}"`;
655
+ throw new KeyValueError({
656
+ value: field,
657
+ reason: PROTO_ENTRY,
658
+ message: `The ${which} key attribute of ${named}: ${PROTO_ENTRY} [EDD-9165]`,
659
+ });
660
+ };
599
661
  /**
600
662
  * Compose all key attributes for a single index.
601
663
  * Returns a record mapping field names to composed key values.
602
664
  */
603
665
  export const composeIndexKeys = (schema, entityType, entityVersion, index, record) => ({
604
- [index.pk.field]: composePk(schema, entityType, index, record),
605
- [index.sk.field]: composeSkValue(schema, entityType, entityVersion, index, record),
666
+ [keyAttribute(index, "pk")]: composePk(schema, entityType, index, record),
667
+ [keyAttribute(index, "sk")]: composeSkValue(schema, entityType, entityVersion, index, record),
606
668
  });
607
669
  /**
608
670
  * Non-throwing variant of composeIndexKeys. Returns undefined if any composite
@@ -614,7 +676,7 @@ export const composeIndexKeys = (schema, entityType, entityVersion, index, recor
614
676
  * as text first — a Z-order half's `composite` lists its dimension fields.
615
677
  */
616
678
  export const tryComposeIndexKeys = (schema, entityType, entityVersion, index, record) => {
617
- const present = (composite) => composite.every((attr) => record[attr] !== undefined && record[attr] !== null);
679
+ const present = (composite) => composite.every((attr) => given(record, attr) !== undefined && given(record, attr) !== null);
618
680
  if (!present(index.pk.composite) || !present(index.sk.composite))
619
681
  return undefined;
620
682
  return composeIndexKeys(schema, entityType, entityVersion, index, record);
@@ -626,13 +688,15 @@ export const tryComposeIndexKeys = (schema, entityType, entityVersion, index, re
626
688
  */
627
689
  export const composeKnownHalves = (schema, entityType, entityVersion, index, record) => {
628
690
  const shardSource = index.shards === undefined ? [] : boundShardSource(index, index.shards);
629
- const complete = (composite) => composite.every((attr) => hasValue(record[attr]));
691
+ const complete = (composite) => composite.every((attr) => hasValue(given(record, attr)));
630
692
  return {
631
693
  ...(complete([...index.pk.composite, ...shardSource])
632
- ? { [index.pk.field]: composePk(schema, entityType, index, record) }
694
+ ? { [keyAttribute(index, "pk")]: composePk(schema, entityType, index, record) }
633
695
  : {}),
634
696
  ...(complete(index.sk.composite)
635
- ? { [index.sk.field]: composeSkValue(schema, entityType, entityVersion, index, record) }
697
+ ? {
698
+ [keyAttribute(index, "sk")]: composeSkValue(schema, entityType, entityVersion, index, record),
699
+ }
636
700
  : {}),
637
701
  };
638
702
  };
@@ -667,7 +731,7 @@ export const composeAllKeys = (schema, entityType, entityVersion, indexes, recor
667
731
  */
668
732
  const composePkPrefixUpTo = (schema, entityType, index, record, stopBefore) => {
669
733
  const slice = index.pk.composite.slice(0, stopBefore);
670
- return composePkText(schema, entityType, index, extractKeyComposites(schema, index, slice, record), slice, composedShardMarker(entityType, index, record));
734
+ return composePkText(schema, entityType, index, extractKeyComposites(schema, index, slice, record, "pk"), slice, composedShardMarker(entityType, index, record));
671
735
  };
672
736
  /**
673
737
  * Compose a sort key from the leading prefix `[sk_0, ..., sk_(stopBefore-1)]`
@@ -685,7 +749,7 @@ const composePkPrefixUpTo = (schema, entityType, index, record, stopBefore) => {
685
749
  export const composeSkPrefixUpTo = (schema, entityType, entityVersion, index, record, stopBefore) => {
686
750
  assertStringHalf(index, "composeSkPrefixUpTo");
687
751
  const slice = index.sk.composite.slice(0, stopBefore);
688
- const composites = extractKeyComposites(schema, index, slice, record);
752
+ const composites = extractKeyComposites(schema, index, slice, record, "sk");
689
753
  const collection = index.collection;
690
754
  const collectionType = index.type ?? "isolated";
691
755
  if (collection !== undefined) {
@@ -743,7 +807,7 @@ const classifyHalf = (composites, record, policy, halfHasRemoved) => {
743
807
  }
744
808
  let leadingLen = 0;
745
809
  while (leadingLen < composites.length) {
746
- const v = record[composites[leadingLen]];
810
+ const v = given(record, composites[leadingLen]);
747
811
  if (v === undefined || v === null)
748
812
  break;
749
813
  leadingLen++;
@@ -755,7 +819,7 @@ const classifyHalf = (composites, record, policy, halfHasRemoved) => {
755
819
  // Some absent. Check for hole (a present composite after the absent run).
756
820
  let hasHole = false;
757
821
  for (let j = leadingLen + 1; j < composites.length; j++) {
758
- const v = record[composites[j]];
822
+ const v = given(record, composites[j]);
759
823
  if (v !== undefined && v !== null) {
760
824
  hasHole = true;
761
825
  break;
@@ -783,7 +847,7 @@ const classifyHalf = (composites, record, policy, halfHasRemoved) => {
783
847
  * - otherwise noop (`'preserve'`: the stored address may go stale).
784
848
  */
785
849
  const classifyZOrderHalf = (fields, record, policy, halfHasRemoved) => {
786
- if (fields.every((field) => record[field] !== undefined && record[field] !== null))
850
+ if (fields.every((field) => given(record, field) !== undefined && given(record, field) !== null))
787
851
  return "set";
788
852
  if (policy === "sparse" || halfHasRemoved)
789
853
  return "drop";
@@ -874,7 +938,7 @@ export const judgeIndexHalf = (judging, index, half, stored, record) => {
874
938
  const shardSource = index.shards === undefined ? [] : boundShardSource(index, index.shards);
875
939
  const policy = resolveIndexPolicy(index.indexPolicy)[half];
876
940
  try {
877
- if (composites.every((attr) => hasValue(record[attr]))) {
941
+ if (composites.every((attr) => hasValue(given(record, attr)))) {
878
942
  const full = half === "pk"
879
943
  ? composePk(schema, entityType, index, record)
880
944
  : composeSkValue(schema, entityType, entityVersion, index, record);
@@ -895,7 +959,7 @@ export const judgeIndexHalf = (judging, index, half, stored, record) => {
895
959
  };
896
960
  }
897
961
  // `put` writes an index whole or not at all.
898
- const wholeIndex = [...index.pk.composite, ...index.sk.composite, ...shardSource].every((attr) => hasValue(record[attr]));
962
+ const wholeIndex = [...index.pk.composite, ...index.sk.composite, ...shardSource].every((attr) => hasValue(given(record, attr)));
899
963
  if (!wholeIndex)
900
964
  return FINE;
901
965
  const outsidePrimary = new Set(composites.filter((attr) => !primaryComposites.has(attr) && !stamped.has(attr)));
@@ -926,8 +990,8 @@ export const judgeIndexHalf = (judging, index, half, stored, record) => {
926
990
  * none of them, while the half can't be recomposed from what it knows? Then the
927
991
  * half belongs to some other writer, and its stored key stays as it is.
928
992
  */
929
- const writerLeavesHalf = (composites, updatePayload, keyRecord) => composites.every((attr) => !(attr in updatePayload)) &&
930
- composites.some((attr) => !hasValue(keyRecord[attr]));
993
+ const writerLeavesHalf = (composites, updatePayload, keyRecord) => composites.every((attr) => !Object.hasOwn(updatePayload, attr)) &&
994
+ composites.some((attr) => !hasValue(given(keyRecord, attr)));
931
995
  /**
932
996
  * Whether a write leaves a half holding a value it stamps itself (`DESIGN.md` §7, "Composites the
933
997
  * library stamps"). Every update stamps it, so the stamp says nothing about who owns the half, and
@@ -937,9 +1001,9 @@ const writerLeavesHalf = (composites, updatePayload, keyRecord) => composites.ev
937
1001
  * policy. A composite the writer explicitly clears isn't left: that's the structural rule's.
938
1002
  */
939
1003
  const writerLeavesStampedHalf = (composites, stamped, updatePayload, keyRecord) => {
940
- const others = composites.filter((attr) => !(attr in stamped));
941
- return (others.some((attr) => !hasValue(updatePayload[attr]) && !hasValue(keyRecord[attr])) &&
942
- others.every((attr) => !(attr in updatePayload) || hasValue(updatePayload[attr])));
1004
+ const others = composites.filter((attr) => !Object.hasOwn(stamped, attr));
1005
+ return (others.some((attr) => !hasValue(given(updatePayload, attr)) && !hasValue(given(keyRecord, attr))) &&
1006
+ others.every((attr) => !Object.hasOwn(updatePayload, attr) || hasValue(given(updatePayload, attr))));
943
1007
  };
944
1008
  /**
945
1009
  * The PK half's outcome on a sharded index, from what {@link classifyHalf} says
@@ -960,8 +1024,8 @@ const shardedPkOutcome = (entityType, index, outcome, update) => {
960
1024
  const refuse = () => {
961
1025
  if (update.onMissing === "keep")
962
1026
  return { kind: "noop" };
963
- const absent = boundShardSource(index, shards).filter((attr) => !hasValue(merged[attr]));
964
- const missing = absent.length > 0 ? absent : pkComposites.filter((a) => !hasValue(merged[a]));
1027
+ const absent = boundShardSource(index, shards).filter((attr) => !hasValue(given(merged, attr)));
1028
+ const missing = absent.length > 0 ? absent : pkComposites.filter((a) => !hasValue(given(merged, a)));
965
1029
  throw new ShardSourceError({
966
1030
  message: `[EDD-9100] Entity "${entityType}": this update changes what index "${index.index}" is ` +
967
1031
  `sharded by, and its partition key can't be composed without ` +
@@ -973,12 +1037,12 @@ const shardedPkOutcome = (entityType, index, outcome, update) => {
973
1037
  return { kind: "drop" };
974
1038
  if (outcome.kind === "drop") {
975
1039
  const pkExplicitlyGone = update.pkHasRemoved ||
976
- pkComposites.some((attr) => attr in updatePayload && !hasValue(updatePayload[attr]));
1040
+ pkComposites.some((attr) => Object.hasOwn(updatePayload, attr) && !hasValue(given(updatePayload, attr)));
977
1041
  return update.byTouched && !pkExplicitlyGone ? refuse() : outcome;
978
1042
  }
979
1043
  if (outcome.kind === "noop")
980
1044
  return update.byTouched ? refuse() : outcome;
981
- if (boundShardSource(index, shards).every((attr) => hasValue(merged[attr]))) {
1045
+ if (boundShardSource(index, shards).every((attr) => hasValue(given(merged, attr)))) {
982
1046
  // The shard follows `by`, and the sort key would stay at the old value: no pin
983
1047
  // could reach the row, since each searches the shard its own value hashes to and
984
1048
  // only the old value's text is stored. The update may name the value the row
@@ -986,7 +1050,7 @@ const shardedPkOutcome = (entityType, index, outcome, update) => {
986
1050
  if (update.byTouched && update.skKept) {
987
1051
  if (update.onMissing === "keep")
988
1052
  return { kind: "noop" };
989
- const missing = index.sk.composite.filter((attr) => !hasValue(merged[attr]));
1053
+ const missing = index.sk.composite.filter((attr) => !hasValue(given(merged, attr)));
990
1054
  throw new ShardSourceError({
991
1055
  message: `[EDD-9100] Entity "${entityType}": this update changes what index "${index.index}" is ` +
992
1056
  `sharded by, and its sort key can't be composed without ` +
@@ -1002,7 +1066,7 @@ const shardedPkOutcome = (entityType, index, outcome, update) => {
1002
1066
  return { kind: "drop" };
1003
1067
  // The source is incomplete: an update changing the partition or part of `by`
1004
1068
  // can't compute the shard; any other leaves the stored key alone.
1005
- return update.byTouched || pkComposites.some((attr) => hasValue(updatePayload[attr]))
1069
+ return update.byTouched || pkComposites.some((attr) => hasValue(given(updatePayload, attr)))
1006
1070
  ? refuse()
1007
1071
  : { kind: "noop" };
1008
1072
  };
@@ -1082,7 +1146,7 @@ export const composeGsiKeysForUpdatePolicyAware = (schema, entityType, entityVer
1082
1146
  const removes = [];
1083
1147
  const removedSet = options?.removedSet;
1084
1148
  const stamped = options?.stamped;
1085
- const leavesHalf = (composites) => stamped !== undefined && composites.some((attr) => attr in stamped)
1149
+ const leavesHalf = (composites) => stamped !== undefined && composites.some((attr) => Object.hasOwn(stamped, attr))
1086
1150
  ? writerLeavesStampedHalf(composites, stamped, updatePayload, keyRecord)
1087
1151
  : writerLeavesHalf(composites, updatePayload, keyRecord);
1088
1152
  for (const [indexName, index] of Object.entries(indexes)) {
@@ -1119,13 +1183,13 @@ export const composeGsiKeysForUpdatePolicyAware = (schema, entityType, entityVer
1119
1183
  // touches it: the shard follows its value. Its presence in `keyRecord` is no
1120
1184
  // evidence of a change. The retain path's holds the whole item.
1121
1185
  const by = index.shards?.by ?? [];
1122
- const byTouched = by.some((attr) => hasValue(updatePayload[attr]));
1123
- const byCleared = by.some((attr) => attr in updatePayload && !hasValue(updatePayload[attr]));
1186
+ const byTouched = by.some((attr) => hasValue(given(updatePayload, attr)));
1187
+ const byCleared = by.some((attr) => Object.hasOwn(updatePayload, attr) && !hasValue(given(updatePayload, attr)));
1124
1188
  const byRemoved = removedSet !== undefined && by.some((attr) => removedSet.has(attr));
1125
1189
  // An empty PK half is a constant prefix, unless `by` makes its marker vary.
1126
1190
  const skipPk = (pkComposites.length > 0
1127
1191
  ? !pkHasRemoved && leavesHalf(pkComposites)
1128
- : by.length > 0 && by.some((attr) => !hasValue(keyRecord[attr]))) &&
1192
+ : by.length > 0 && by.some((attr) => !hasValue(given(keyRecord, attr)))) &&
1129
1193
  !byTouched &&
1130
1194
  !byCleared &&
1131
1195
  !byRemoved;
@@ -1144,7 +1208,7 @@ export const composeGsiKeysForUpdatePolicyAware = (schema, entityType, entityVer
1144
1208
  delete merged[k];
1145
1209
  continue;
1146
1210
  }
1147
- merged[k] = v;
1211
+ setOwn(merged, k, v);
1148
1212
  }
1149
1213
  if (removedSet !== undefined) {
1150
1214
  for (const attr of removedSet) {
@@ -1173,7 +1237,7 @@ export const composeGsiKeysForUpdatePolicyAware = (schema, entityType, entityVer
1173
1237
  removes.push(index.pk.field);
1174
1238
  }
1175
1239
  else if (pkOutcome.kind === "set") {
1176
- sets[index.pk.field] =
1240
+ sets[keyAttribute(index, "pk")] =
1177
1241
  pkOutcome.length === pkComposites.length
1178
1242
  ? composePk(schema, entityType, index, merged)
1179
1243
  : composePkPrefixUpTo(schema, entityType, index, merged, pkOutcome.length);
@@ -1187,7 +1251,7 @@ export const composeGsiKeysForUpdatePolicyAware = (schema, entityType, entityVer
1187
1251
  if (outcome === "drop")
1188
1252
  removes.push(index.sk.field);
1189
1253
  else if (outcome === "set")
1190
- sets[index.sk.field] = composeZOrderSk(schema, index, merged);
1254
+ sets[keyAttribute(index, "sk")] = composeZOrderSk(schema, index, merged);
1191
1255
  }
1192
1256
  else if (!skipSk) {
1193
1257
  const skOutcome = classifyHalf(skComposites, merged, policy.sk, skHasRemoved);
@@ -1195,7 +1259,7 @@ export const composeGsiKeysForUpdatePolicyAware = (schema, entityType, entityVer
1195
1259
  removes.push(index.sk.field);
1196
1260
  }
1197
1261
  else if (skOutcome.kind === "set") {
1198
- sets[index.sk.field] =
1262
+ sets[keyAttribute(index, "sk")] =
1199
1263
  skOutcome.length === skComposites.length
1200
1264
  ? composeSk(schema, entityType, entityVersion, index, merged)
1201
1265
  : composeSkPrefixUpTo(schema, entityType, entityVersion, index, merged, skOutcome.length);
@@ -1247,7 +1311,7 @@ export const tryComposeVectorPartition = (schema, entityType, index, record) =>
1247
1311
  * sparse-index outcome.
1248
1312
  */
1249
1313
  export const composeVectorPartition = (schema, entityType, index, record) => {
1250
- const composites = extractKeyComposites(schema, index, index.partition, record);
1314
+ const composites = extractKeyComposites(schema, index, index.partition, record, "pk");
1251
1315
  return composeKey(schema, entityType, composites, {
1252
1316
  casing: index.casing,
1253
1317
  names: [...index.partition],
@@ -1261,7 +1325,7 @@ const availableSkCount = (index, record) => {
1261
1325
  assertStringHalf(index, "availableSkCount");
1262
1326
  let n = 0;
1263
1327
  for (const attr of index.sk.composite) {
1264
- const value = record[attr];
1328
+ const value = given(record, attr);
1265
1329
  if (value === undefined || value === null)
1266
1330
  break;
1267
1331
  n++;
@@ -1308,6 +1372,15 @@ export const composeSortKeyBeginsWith = (schema, entityType, entityVersion, inde
1308
1372
  const complete = availableSkCount(index, record) === index.sk.composite.length;
1309
1373
  return complete ? prefix : `${prefix}${KEY_DELIMITER}`;
1310
1374
  };
1375
+ /**
1376
+ * Whether a `beginsWith` on a composite whose value is `keyForm` (its key form) matches a prefix of
1377
+ * the value's key text. A number's or a bigint's has no text prefix: its key text isn't its digits
1378
+ * (`-3` is `-9999999999999996:`), and an integer's text prefixes its fractions' (3 of 3.14), so a
1379
+ * `beginsWith` on a numeric composite means the whole value, `eq`. Every `beginsWith` on a
1380
+ * composite follows this one rule: an index accessor's `.where()`, a history's `.where()`, and
1381
+ * `Aggregate.list`'s `prefix`.
1382
+ */
1383
+ export const matchesTextPrefix = (keyForm) => typeof keyForm !== "number" && typeof keyForm !== "bigint";
1311
1384
  /**
1312
1385
  * Above every key below a complete sort key prefix: the delimiter is followed by
1313
1386
  * a composite's label, and no label reaches U+FFFF.
@@ -1322,7 +1395,10 @@ export const SORT_KEY_MAX = "\uFFFF";
1322
1395
  * escaped, so text after one can always match another key's. Without `valuesOf`, at the first
1323
1396
  * composite. A composite limited to `values` is their key text under the schema's setting, or
1324
1397
  * with `everyForm` under every setting a row can have been written under (`escapeKeys` on, off,
1325
- * and the 1.37 numeric text): the fixed text is the same under all of them.
1398
+ * and the 1.37 numeric text): the fixed text is the same under all of them. With `walkOpen` the
1399
+ * walk goes on past a composite that can hold the delimiter too, its value any text (`any`), so
1400
+ * the space is exactly the keys the composer writes for values of those kinds, to the end of the
1401
+ * key.
1326
1402
  */
1327
1403
  export const composeKeySpace = (schema, entityType, entityVersion, index, valuesOf = () => "open", options) => {
1328
1404
  const casing = index.casing ?? schema.casing;
@@ -1338,6 +1414,9 @@ export const composeKeySpace = (schema, entityType, entityVersion, index, values
1338
1414
  return { kind: "text" };
1339
1415
  if (values === "number")
1340
1416
  return { kind: "number" };
1417
+ if (values === "date" || values === "zonedDate") {
1418
+ return { kind: "date", zoned: values === "zonedDate" };
1419
+ }
1341
1420
  const texts = new Set();
1342
1421
  for (const value of values.values) {
1343
1422
  for (const form of forms) {
@@ -1359,6 +1438,10 @@ export const composeKeySpace = (schema, entityType, entityVersion, index, values
1359
1438
  for (const name of composite) {
1360
1439
  walked.push(name);
1361
1440
  const of = valuesOf(name);
1441
+ if (of === "open" && options?.walkOpen === true) {
1442
+ values.push({ kind: "any" });
1443
+ continue;
1444
+ }
1362
1445
  if (of === "open") {
1363
1446
  open = true;
1364
1447
  break;
@@ -1385,6 +1468,10 @@ const NOT_DELIMITER = { chars: KEY_DELIMITER, negated: true };
1385
1468
  const NUMBER_FIRST = { chars: "0123456789-:;", negated: false };
1386
1469
  /** Any other character of it: `NumericKey`'s, and 1.36's (`1e+21`, `Infinity`), in any casing. */
1387
1470
  const NUMBER_REST = { chars: "0123456789-+.:;)*eEiInNfFtTyY", negated: false };
1471
+ /** The first character of a date's ISO text: its year's first digit, or the sign of a wide year. */
1472
+ const DATE_FIRST = { chars: "0123456789+-", negated: false };
1473
+ /** Any other character of an instant's ISO text (`+275760-09-13T00:00:00.000Z`), in any casing. */
1474
+ const DATE_REST = { chars: "0123456789-+:.TtZz", negated: false };
1388
1475
  /** Whether some character is in both. */
1389
1476
  const classesMeet = (a, b) => {
1390
1477
  if (a.negated && b.negated)
@@ -1421,10 +1508,10 @@ const keyAutomaton = (space) => {
1421
1508
  space.after.forEach((text, i) => {
1422
1509
  const value = space.values?.[i] ?? { kind: "text" };
1423
1510
  const end = state();
1424
- if (value.kind === "text") {
1511
+ if (value.kind === "text" || value.kind === "any") {
1425
1512
  const loop = state();
1426
1513
  empty[at].push(loop);
1427
- edges[loop].push([NOT_DELIMITER, loop]);
1514
+ edges[loop].push([value.kind === "any" ? ANY_CHARACTER : NOT_DELIMITER, loop]);
1428
1515
  empty[loop].push(end);
1429
1516
  }
1430
1517
  else if (value.kind === "number") {
@@ -1433,6 +1520,12 @@ const keyAutomaton = (space) => {
1433
1520
  edges[digits].push([NUMBER_REST, digits]);
1434
1521
  empty[digits].push(end);
1435
1522
  }
1523
+ else if (value.kind === "date") {
1524
+ const rest = state();
1525
+ edges[at].push([DATE_FIRST, rest]);
1526
+ edges[rest].push([value.zoned ? NOT_DELIMITER : DATE_REST, rest]);
1527
+ empty[rest].push(end);
1528
+ }
1436
1529
  else {
1437
1530
  for (const option of value.texts)
1438
1531
  empty[spell(at, option)].push(end);
@@ -1550,7 +1643,7 @@ export const composeSortKeyPrefix = (schema, entityType, entityVersion, index, r
1550
1643
  const available = [];
1551
1644
  const names = [];
1552
1645
  for (const attr of index.sk.composite) {
1553
- const value = record[attr];
1646
+ const value = given(record, attr);
1554
1647
  if (value === undefined || value === null)
1555
1648
  break;
1556
1649
  available.push(keyValueText(schema, value, casing));
@@ -1595,8 +1688,14 @@ export const composeEventSk = (first, ...rest) => {
1595
1688
  return `${first}#${applyCasing(EVENT_SK_INFIX, casing)}#${escapeKeyText(serializeValue(rest[0]), casing)}`;
1596
1689
  }
1597
1690
  const casing = (rest[2] ?? first.casing);
1598
- return `${rest[0]}#${applyCasing(EVENT_SK_INFIX, casing)}#${keyValueText(first, rest[1], casing)}`;
1691
+ const text = keyText(first, siteCompositeText(first, rest[1], undefined, EVENT_SORT_KEY, "sk"), casing);
1692
+ return `${rest[0]}#${applyCasing(EVENT_SK_INFIX, casing)}#${text}`;
1599
1693
  };
1694
+ /**
1695
+ * The `key` of the site {@link composeEventSk} composes a time-series event's `orderBy` value at
1696
+ * (`DynamoSchema.ObjectKeySite`), which names no attribute.
1697
+ */
1698
+ export const EVENT_SORT_KEY = "timeSeries.orderBy";
1600
1699
  /**
1601
1700
  * The value of the `orderBy` attribute an event's sort key holds: its key form with numeric key
1602
1701
  * text, its encoded value (a `NumberFromString` `"10"` as the string `10`) with `escapeKeys` off,