@effect-dynamodb/schema 1.38.0 → 1.39.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 (40) hide show
  1. package/dist/Aggregate.d.ts.map +1 -1
  2. package/dist/Aggregate.js +12 -0
  3. package/dist/Aggregate.js.map +1 -1
  4. package/dist/DynamoSchema.d.ts +28 -17
  5. package/dist/DynamoSchema.d.ts.map +1 -1
  6. package/dist/DynamoSchema.js +19 -13
  7. package/dist/DynamoSchema.js.map +1 -1
  8. package/dist/Entity.d.ts +64 -14
  9. package/dist/Entity.d.ts.map +1 -1
  10. package/dist/Entity.js +59 -0
  11. package/dist/Entity.js.map +1 -1
  12. package/dist/Errors.d.ts +0 -27
  13. package/dist/Errors.d.ts.map +1 -1
  14. package/dist/Errors.js +0 -24
  15. package/dist/Errors.js.map +1 -1
  16. package/dist/KeyComposer.d.ts +212 -12
  17. package/dist/KeyComposer.d.ts.map +1 -1
  18. package/dist/KeyComposer.js +488 -86
  19. package/dist/KeyComposer.js.map +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +1 -1
  23. package/dist/index.js.map +1 -1
  24. package/dist/internal/KeyText136.d.ts +56 -131
  25. package/dist/internal/KeyText136.d.ts.map +1 -1
  26. package/dist/internal/KeyText136.js +123 -264
  27. package/dist/internal/KeyText136.js.map +1 -1
  28. package/dist/internal/NumericKey.d.ts +8 -0
  29. package/dist/internal/NumericKey.d.ts.map +1 -1
  30. package/dist/internal/NumericKey.js +45 -0
  31. package/dist/internal/NumericKey.js.map +1 -1
  32. package/dist/internal/Shards.d.ts +47 -0
  33. package/dist/internal/Shards.d.ts.map +1 -0
  34. package/dist/internal/Shards.js +60 -0
  35. package/dist/internal/Shards.js.map +1 -0
  36. package/dist/internal/SharedKeyAttribute.d.ts +48 -0
  37. package/dist/internal/SharedKeyAttribute.d.ts.map +1 -0
  38. package/dist/internal/SharedKeyAttribute.js +173 -0
  39. package/dist/internal/SharedKeyAttribute.js.map +1 -0
  40. package/package.json +2 -1
@@ -0,0 +1,47 @@
1
+ /**
2
+ * @internal The shard function and marker of a sharded index (#183,
3
+ * `docs/designs/gsi-write-sharding.md` §2–§3).
4
+ *
5
+ * Golden vectors pin both: stored index keys depend on them, so changing either
6
+ * is a breaking change.
7
+ */
8
+ /** The largest `shards` count (`[EDD-9099]`, design §10.1). */
9
+ export declare const MAX_SHARD_COUNT = 256;
10
+ declare const ShardSourceError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
11
+ readonly _tag: "ShardSourceError";
12
+ } & Readonly<A>;
13
+ /**
14
+ * `[EDD-9100]`: a sharded index's partition key half can't be composed because
15
+ * the shard source is missing. There is no unsharded fallback, so the write is
16
+ * not made.
17
+ */
18
+ export declare class ShardSourceError extends ShardSourceError_base<{
19
+ readonly message: string;
20
+ }> {
21
+ }
22
+ /** Is `u` a {@link ShardSourceError}? By its tag, as `isKeyValueError` is. */
23
+ export declare const isShardSourceError: (u: unknown) => u is ShardSourceError;
24
+ /**
25
+ * The case fold of a source value's text: every spelling the key text treats as
26
+ * one value, under either casing, folds to the same text (design §3).
27
+ * `toLowerCase` first: `toUpperCase` first maps `ß` to `SS` and `ẞ` to `ẞ`,
28
+ * which lowercase to `ss` and `ß`, though both of them are one key.
29
+ */
30
+ export declare const foldShardText: (text: string) => string;
31
+ /**
32
+ * FNV-1a-32 over the source values' folded UTF-8, in source order, each pair
33
+ * separated by byte `0xFF`.
34
+ */
35
+ export declare const shardHash: (texts: ReadonlyArray<string>) => number;
36
+ /** The shard `texts` land on, of `count`. */
37
+ export declare const shardOf: (texts: ReadonlyArray<string>, count: number) => number;
38
+ /**
39
+ * The marker a sharded partition key carries right after its entity type or
40
+ * collection name: `!<count>.<shard>`, the shard zero-padded to the width of
41
+ * `count - 1`. Before every value, it can't be spelled by one: an unsharded
42
+ * key's fourth segment always holds `<label>_`, and the count in it keeps two
43
+ * layouts' shards apart (design §2.1).
44
+ */
45
+ export declare const shardMarker: (count: number, shard: number) => string;
46
+ export {};
47
+ //# sourceMappingURL=Shards.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Shards.d.ts","sourceRoot":"","sources":["../../src/internal/Shards.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,+DAA+D;AAC/D,eAAO,MAAM,eAAe,MAAM,CAAA;;;;AAElC;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,sBAAqC;IACzE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CACzB,CAAC;CAAG;AAEL,8EAA8E;AAC9E,eAAO,MAAM,kBAAkB,GAAI,GAAG,OAAO,KAAG,CAAC,IAAI,gBACyC,CAAA;AAI9F;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM,MAAM,KAAG,MAA0C,CAAA;AAOvF;;;GAGG;AACH,eAAO,MAAM,SAAS,GAAI,OAAO,aAAa,CAAC,MAAM,CAAC,KAAG,MAaxD,CAAA;AAED,6CAA6C;AAC7C,eAAO,MAAM,OAAO,GAAI,OAAO,aAAa,CAAC,MAAM,CAAC,EAAE,OAAO,MAAM,KAAG,MAC5C,CAAA;AAE1B;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,EAAE,OAAO,MAAM,KAAG,MACW,CAAA"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @internal The shard function and marker of a sharded index (#183,
3
+ * `docs/designs/gsi-write-sharding.md` §2–§3).
4
+ *
5
+ * Golden vectors pin both: stored index keys depend on them, so changing either
6
+ * is a breaking change.
7
+ */
8
+ import { Data } from "effect";
9
+ /** The largest `shards` count (`[EDD-9099]`, design §10.1). */
10
+ export const MAX_SHARD_COUNT = 256;
11
+ /**
12
+ * `[EDD-9100]`: a sharded index's partition key half can't be composed because
13
+ * the shard source is missing. There is no unsharded fallback, so the write is
14
+ * not made.
15
+ */
16
+ export class ShardSourceError extends Data.TaggedError("ShardSourceError") {
17
+ }
18
+ /** Is `u` a {@link ShardSourceError}? By its tag, as `isKeyValueError` is. */
19
+ export const isShardSourceError = (u) => typeof u === "object" && u !== null && u._tag === "ShardSourceError";
20
+ const encoder = new TextEncoder();
21
+ /**
22
+ * The case fold of a source value's text: every spelling the key text treats as
23
+ * one value, under either casing, folds to the same text (design §3).
24
+ * `toLowerCase` first: `toUpperCase` first maps `ß` to `SS` and `ẞ` to `ẞ`,
25
+ * which lowercase to `ss` and `ß`, though both of them are one key.
26
+ */
27
+ export const foldShardText = (text) => text.toLowerCase().toUpperCase();
28
+ const FNV_OFFSET = 0x811c9dc5;
29
+ const FNV_PRIME = 0x01000193;
30
+ /** Never occurs in UTF-8, so values can't run into each other. */
31
+ const SEPARATOR = 0xff;
32
+ /**
33
+ * FNV-1a-32 over the source values' folded UTF-8, in source order, each pair
34
+ * separated by byte `0xFF`.
35
+ */
36
+ export const shardHash = (texts) => {
37
+ let hash = FNV_OFFSET;
38
+ texts.forEach((text, i) => {
39
+ if (i > 0) {
40
+ hash ^= SEPARATOR;
41
+ hash = Math.imul(hash, FNV_PRIME) >>> 0;
42
+ }
43
+ for (const byte of encoder.encode(foldShardText(text))) {
44
+ hash ^= byte;
45
+ hash = Math.imul(hash, FNV_PRIME) >>> 0;
46
+ }
47
+ });
48
+ return hash >>> 0;
49
+ };
50
+ /** The shard `texts` land on, of `count`. */
51
+ export const shardOf = (texts, count) => shardHash(texts) % count;
52
+ /**
53
+ * The marker a sharded partition key carries right after its entity type or
54
+ * collection name: `!<count>.<shard>`, the shard zero-padded to the width of
55
+ * `count - 1`. Before every value, it can't be spelled by one: an unsharded
56
+ * key's fourth segment always holds `<label>_`, and the count in it keeps two
57
+ * layouts' shards apart (design §2.1).
58
+ */
59
+ export const shardMarker = (count, shard) => `!${count}.${String(shard).padStart(String(count - 1).length, "0")}`;
60
+ //# sourceMappingURL=Shards.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C,EAAE,CAAC;YACvD,IAAI,IAAI,IAAI,CAAA;YACZ,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,KAAK,CAAC,CAAA;QACzC,CAAC;IACH,CAAC,CAAC,CAAA;IACF,OAAO,IAAI,KAAK,CAAC,CAAA;AACnB,CAAC,CAAA;AAED,6CAA6C;AAC7C,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAA4B,EAAE,KAAa,EAAU,EAAE,CAC7E,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,CAAA;AAE1B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAE,KAAa,EAAU,EAAE,CAClE,IAAI,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,CAAA"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * What an entity's key definitions may not do (#183).
3
+ *
4
+ * `[EDD-9106]`: two key halves of one entity stored in one attribute (D61). A row can't
5
+ * show which of them owns the attribute: whichever is written last overwrites the other,
6
+ * and a later update corrupts a row that only ever used one. So they may share it only with
7
+ * the same fields, in the same order and casing, and a shard marker only with the same marker.
8
+ * One rule composes every key's values (`KeyComposer.keyText`), so such halves build the
9
+ * same text whichever role each plays: an inverted index needs no case of its own. The
10
+ * check runs per entity, so one index overloaded across entity types is unaffected.
11
+ *
12
+ * `[EDD-9108]`: one entity using one physical index twice. An item has one key in an index,
13
+ * so the second accessor can't have one of its own.
14
+ *
15
+ * `[EDD-9109]` and `[EDD-9112]`: a value stored in a table key attribute. The library never
16
+ * sets any part of the primary key: a `Put` carrying it replaces the item's key with the
17
+ * value, and DynamoDB refuses an update that names it.
18
+ */
19
+ import * as DynamoSchema from "../DynamoSchema.js";
20
+ import { type IndexDefinition } from "../KeyComposer.js";
21
+ /**
22
+ * `Entity.make`, which knows no schema: refuse what no schema can reconcile. Two halves
23
+ * that differ only by a casing one of them leaves to the schema wait for `Table.make`.
24
+ */
25
+ export declare const refuseSharedKeyAttributeOnEverySchema: (entityType: string, indexes: Readonly<Record<string, IndexDefinition>>) => void;
26
+ /**
27
+ * `Table.make`, which knows the schema, and so what a casing left to it resolves to. An
28
+ * entity registered on two tables is judged by each table's schema.
29
+ */
30
+ export declare const refuseSharedKeyAttribute: (schema: DynamoSchema.DynamoSchema, entityType: string, indexes: Readonly<Record<string, IndexDefinition>>) => void;
31
+ /**
32
+ * `[EDD-9108]`: `Entity.make` refuses one entity naming one physical index twice, even over
33
+ * the same fields. Different entities sharing an index stay fine.
34
+ */
35
+ export declare const refuseRepeatedIndex: (entityType: string, indexes: Readonly<Record<string, IndexDefinition>>) => void;
36
+ /**
37
+ * `[EDD-9109]`: `Entity.make` refuses an attribute it would store in a table key attribute: a
38
+ * model field, under a `field:` rename or by its own name, or a system timestamp or version
39
+ * field.
40
+ */
41
+ export declare const refuseStoredInTableKey: (entityType: string, primary: IndexDefinition, stored: ReadonlyArray<readonly [name: string, attribute: string]>) => void;
42
+ /**
43
+ * `[EDD-9112]`: `Aggregate.make` refuses a key it would store in a table key attribute: its
44
+ * list key, or its collection sort key. Aggregate rows are `Put`s, so one would replace the
45
+ * row's own key.
46
+ */
47
+ export declare const refuseAggregateKeyInTableKey: (aggregate: string, tableKeys: ReadonlyArray<string | undefined>, keys: ReadonlyArray<readonly [what: string, attribute: string]>) => void;
48
+ //# sourceMappingURL=SharedKeyAttribute.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SharedKeyAttribute.d.ts","sourceRoot":"","sources":["../../src/internal/SharedKeyAttribute.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAGL,KAAK,eAAe,EAGrB,MAAM,mBAAmB,CAAA;AA0H1B;;;GAGG;AACH,eAAO,MAAM,qCAAqC,GAChD,YAAY,MAAM,EAClB,SAAS,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,KACjD,IAMF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,wBAAwB,GACnC,QAAQ,YAAY,CAAC,YAAY,EACjC,YAAY,MAAM,EAClB,SAAS,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,KACjD,IAMF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,mBAAmB,GAC9B,YAAY,MAAM,EAClB,SAAS,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC,KACjD,IAeF,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,GACjC,YAAY,MAAM,EAClB,SAAS,eAAe,EACxB,QAAQ,aAAa,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC,KAChE,IAWF,CAAA;AAED;;;;GAIG;AACH,eAAO,MAAM,4BAA4B,GACvC,WAAW,MAAM,EACjB,WAAW,aAAa,CAAC,MAAM,GAAG,SAAS,CAAC,EAC5C,MAAM,aAAa,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC,KAC9D,IASF,CAAA"}
@@ -0,0 +1,173 @@
1
+ /**
2
+ * What an entity's key definitions may not do (#183).
3
+ *
4
+ * `[EDD-9106]`: two key halves of one entity stored in one attribute (D61). A row can't
5
+ * show which of them owns the attribute: whichever is written last overwrites the other,
6
+ * and a later update corrupts a row that only ever used one. So they may share it only with
7
+ * the same fields, in the same order and casing, and a shard marker only with the same marker.
8
+ * One rule composes every key's values (`KeyComposer.keyText`), so such halves build the
9
+ * same text whichever role each plays: an inverted index needs no case of its own. The
10
+ * check runs per entity, so one index overloaded across entity types is unaffected.
11
+ *
12
+ * `[EDD-9108]`: one entity using one physical index twice. An item has one key in an index,
13
+ * so the second accessor can't have one of its own.
14
+ *
15
+ * `[EDD-9109]` and `[EDD-9112]`: a value stored in a table key attribute. The library never
16
+ * sets any part of the primary key: a `Put` carrying it replaces the item's key with the
17
+ * value, and DynamoDB refuses an update that names it.
18
+ */
19
+ import * as DynamoSchema from "../DynamoSchema.js";
20
+ import { composePk, composeSk, isZOrderHalf, unshardedView, } from "../KeyComposer.js";
21
+ const describe = ({ owner, half }) => `${owner === "primary" ? "the primary key" : `index "${owner}"`}'s ${half === "pk" ? "partition" : "sort"} key`;
22
+ const isZOrder = ({ half, index }) => half === "sk" && isZOrderHalf(index.sk);
23
+ /** The hash a sharded partition key's marker depends on; nothing on any other half. */
24
+ const shardForm = ({ half, index }) => half === "pk" && index.shards !== undefined
25
+ ? JSON.stringify([index.shards.count, index.shards.source])
26
+ : undefined;
27
+ /**
28
+ * A half's fields in order, and the label a collection or its absence gives the key. The
29
+ * fields alone can't say: an isolated collection's sort key and a plain one, over the same
30
+ * fields, compose different text. So this composes each value as a stand-in, through the
31
+ * composers, so it can't drift from them. A stand-in is unique to the exact field (`tenant`
32
+ * and `Tenant` have one label between them, and different values).
33
+ */
34
+ const textForm = (schema, entityType, standIns, { half, index }) => {
35
+ const record = {};
36
+ for (const attr of half === "pk" ? index.pk.composite : index.sk.composite) {
37
+ if (!standIns.has(attr))
38
+ standIns.set(attr, String.fromCharCode(0xe000 + standIns.size));
39
+ record[attr] = standIns.get(attr);
40
+ }
41
+ return half === "pk"
42
+ ? composePk(schema, entityType, unshardedView(index), record)
43
+ : composeSk(schema, entityType, 1, index, record);
44
+ };
45
+ /** A Z-order half's fields: each prefix composite with its hash, and every dimension's options. */
46
+ const zOrderForm = ({ index }) => {
47
+ if (!isZOrderHalf(index.sk))
48
+ return "";
49
+ const { prefix, dimensions } = index.sk.zorderHalf;
50
+ return JSON.stringify({
51
+ prefix: prefix.map(({ field, hash }) => ({ field, hash: hash ?? null })),
52
+ // A date's `from` is held normalized, so two spellings of one instant are the same.
53
+ dimensions,
54
+ });
55
+ };
56
+ /** Do two halves have the same fields, order and casing, under this schema casing? */
57
+ const sameDefinition = (casing, entityType, a, b) => {
58
+ if ((a.index.casing ?? casing) !== (b.index.casing ?? casing))
59
+ return false;
60
+ if (isZOrder(a) !== isZOrder(b) || shardForm(a) !== shardForm(b))
61
+ return false;
62
+ if (isZOrder(a))
63
+ return zOrderForm(a) === zOrderForm(b);
64
+ const schema = DynamoSchema.make({ name: "form", version: 1, casing });
65
+ const standIns = new Map();
66
+ return textForm(schema, entityType, standIns, a) === textForm(schema, entityType, standIns, b);
67
+ };
68
+ /** Every pair of an entity's key halves stored in one attribute. */
69
+ const sharedHalves = (indexes) => {
70
+ const halves = Object.entries(indexes).flatMap(([owner, index]) => [
71
+ { owner, half: "pk", index, field: index.pk.field },
72
+ { owner, half: "sk", index, field: index.sk.field },
73
+ ]);
74
+ return halves.flatMap((a, i) => halves
75
+ .slice(i + 1)
76
+ .filter((b) => a.field === b.field)
77
+ .map((b) => [a, b]));
78
+ };
79
+ const refusal = (entityType, a, b, casing) => {
80
+ const sharded = [a, b].find((half) => shardForm(half) !== undefined);
81
+ const casingA = a.index.casing ?? casing;
82
+ const casingB = b.index.casing ?? casing;
83
+ const why = shardForm(a) !== shardForm(b)
84
+ ? `${describe(sharded)} holds a shard marker, and is sharded differently from anything ` +
85
+ `else in that attribute`
86
+ : isZOrder(a) !== isZOrder(b)
87
+ ? "one is a binary Z-order address and the other text"
88
+ : casingA !== undefined && casingB !== undefined && casingA !== casingB
89
+ ? `they have the casings "${casingA}" and "${casingB}"`
90
+ : isZOrder(a)
91
+ ? "their Z-order layouts differ"
92
+ : "they have different fields, a different order, or a collection's different label";
93
+ return new Error(`[EDD-9106] Entity "${entityType}": ${describe(a)} and ${describe(b)} are both stored in the ` +
94
+ `attribute "${a.field}", but ${why}. Two key halves may share an attribute only with the ` +
95
+ `same fields, in the same order and casing, and a sharded partition key only with one ` +
96
+ `sharded the same way. ` +
97
+ `A row can't show which half owns the attribute: the one written last overwrites the ` +
98
+ `other, and an update corrupts even a row that only used one, so an item would be ` +
99
+ `unreachable through the other key. Give each its own attribute.`);
100
+ };
101
+ const CASINGS = ["lowercase", "uppercase", "preserve"];
102
+ /**
103
+ * `Entity.make`, which knows no schema: refuse what no schema can reconcile. Two halves
104
+ * that differ only by a casing one of them leaves to the schema wait for `Table.make`.
105
+ */
106
+ export const refuseSharedKeyAttributeOnEverySchema = (entityType, indexes) => {
107
+ for (const [a, b] of sharedHalves(indexes)) {
108
+ if (!CASINGS.some((casing) => sameDefinition(casing, entityType, a, b))) {
109
+ throw refusal(entityType, a, b, undefined);
110
+ }
111
+ }
112
+ };
113
+ /**
114
+ * `Table.make`, which knows the schema, and so what a casing left to it resolves to. An
115
+ * entity registered on two tables is judged by each table's schema.
116
+ */
117
+ export const refuseSharedKeyAttribute = (schema, entityType, indexes) => {
118
+ for (const [a, b] of sharedHalves(indexes)) {
119
+ if (!sameDefinition(schema.casing, entityType, a, b)) {
120
+ throw refusal(entityType, a, b, schema.casing);
121
+ }
122
+ }
123
+ };
124
+ /**
125
+ * `[EDD-9108]`: `Entity.make` refuses one entity naming one physical index twice, even over
126
+ * the same fields. Different entities sharing an index stay fine.
127
+ */
128
+ export const refuseRepeatedIndex = (entityType, indexes) => {
129
+ const seen = new Map();
130
+ for (const [owner, index] of Object.entries(indexes)) {
131
+ if (index.index === undefined)
132
+ continue;
133
+ const first = seen.get(index.index);
134
+ if (first !== undefined) {
135
+ throw new Error(`[EDD-9108] Entity "${entityType}": indexes "${first}" and "${owner}" both use the ` +
136
+ `physical index "${index.index}". One entity can use a physical index once: an item ` +
137
+ `has one key in it, so the second accessor couldn't have its own. Give each its own ` +
138
+ `index. Different entities can share one.`);
139
+ }
140
+ seen.set(index.index, owner);
141
+ }
142
+ };
143
+ /**
144
+ * `[EDD-9109]`: `Entity.make` refuses an attribute it would store in a table key attribute: a
145
+ * model field, under a `field:` rename or by its own name, or a system timestamp or version
146
+ * field.
147
+ */
148
+ export const refuseStoredInTableKey = (entityType, primary, stored) => {
149
+ for (const [name, attribute] of stored) {
150
+ if (attribute !== primary.pk.field && attribute !== primary.sk.field)
151
+ continue;
152
+ throw new Error(`[EDD-9109] Entity "${entityType}": "${name}" would be stored in "${attribute}", an ` +
153
+ "attribute of the table's primary key. The library never sets any part of the primary " +
154
+ `key: a write would replace the item's key with the value. Store "${name}" under ` +
155
+ "another attribute with `DynamoModel.configure(model, { field: { field: ... } })`, or " +
156
+ "name the key attributes something else.");
157
+ }
158
+ };
159
+ /**
160
+ * `[EDD-9112]`: `Aggregate.make` refuses a key it would store in a table key attribute: its
161
+ * list key, or its collection sort key. Aggregate rows are `Put`s, so one would replace the
162
+ * row's own key.
163
+ */
164
+ export const refuseAggregateKeyInTableKey = (aggregate, tableKeys, keys) => {
165
+ for (const [what, attribute] of keys) {
166
+ if (!tableKeys.includes(attribute))
167
+ continue;
168
+ throw new Error(`[EDD-9112] Aggregate "${aggregate}": ${what} is stored in "${attribute}", an attribute of ` +
169
+ "the table's primary key. The library never sets any part of the primary key: each " +
170
+ "row's write would replace its key with this one. Store it in an attribute of its own.");
171
+ }
172
+ };
173
+ //# sourceMappingURL=SharedKeyAttribute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SharedKeyAttribute.js","sourceRoot":"","sources":["../../src/internal/SharedKeyAttribute.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,YAAY,MAAM,oBAAoB,CAAA;AAClD,OAAO,EACL,SAAS,EACT,SAAS,EAET,YAAY,EACZ,aAAa,GACd,MAAM,mBAAmB,CAAA;AAS1B,MAAM,QAAQ,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,EAAW,EAAU,EAAE,CACpD,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,UAAU,KAAK,GAAG,MAC7D,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,M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AK,CAAC,CAAA;IAC9B,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,UAAkB,EAClB,OAAwB,EACxB,MAAiE,EAC3D,EAAE;IACR,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,MAAM,EAAE,CAAC;QACvC,IAAI,SAAS,KAAK,OAAO,CAAC,EAAE,CAAC,KAAK,IAAI,SAAS,KAAK,OAAO,CAAC,EAAE,CAAC,KAAK;YAAE,SAAQ;QAC9E,MAAM,IAAI,KAAK,CACb,sBAAsB,UAAU,OAAO,IAAI,yBAAyB,SAAS,QAAQ;YACnF,uFAAuF;YACvF,oEAAoE,IAAI,UAAU;YAClF,uFAAuF;YACvF,yCAAyC,CAC5C,CAAA;IACH,CAAC;AACH,CAAC,CAAA;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAC1C,SAAiB,EACjB,SAA4C,EAC5C,IAA+D,EACzD,EAAE;IACR,KAAK,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,IAAI,IAAI,EAAE,CAAC;QACrC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,SAAS,CAAC;YAAE,SAAQ;QAC5C,MAAM,IAAI,KAAK,CACb,yBAAyB,SAAS,MAAM,IAAI,kBAAkB,SAAS,qBAAqB;YAC1F,oFAAoF;YACpF,uFAAuF,CAC1F,CAAA;IACH,CAAC;AACH,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@effect-dynamodb/schema",
3
- "version": "1.38.0",
3
+ "version": "1.39.0",
4
4
  "description": "Pure Effect Schema-driven entity/aggregate modeling and relationship derivation for effect-dynamodb — importable without @aws-sdk",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -62,6 +62,7 @@
62
62
  "scripts": {
63
63
  "build": "tsc",
64
64
  "test": "vitest run",
65
+ "test:escape-on": "vitest run --config vitest.escape-on.ts",
65
66
  "test:watch": "vitest",
66
67
  "check": "tsc --noEmit && tsc --noEmit -p tsconfig.test.json",
67
68
  "lint": "biome check .",