effect-dynamodb 0.1.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 (171) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -0
  3. package/dist/Aggregate.d.ts +210 -0
  4. package/dist/Aggregate.d.ts.map +1 -0
  5. package/dist/Aggregate.js +1169 -0
  6. package/dist/Aggregate.js.map +1 -0
  7. package/dist/Batch.d.ts +62 -0
  8. package/dist/Batch.d.ts.map +1 -0
  9. package/dist/Batch.js +218 -0
  10. package/dist/Batch.js.map +1 -0
  11. package/dist/Collection.d.ts +58 -0
  12. package/dist/Collection.d.ts.map +1 -0
  13. package/dist/Collection.js +195 -0
  14. package/dist/Collection.js.map +1 -0
  15. package/dist/Collections.d.ts +121 -0
  16. package/dist/Collections.d.ts.map +1 -0
  17. package/dist/Collections.js +121 -0
  18. package/dist/Collections.js.map +1 -0
  19. package/dist/DynamoClient.d.ts +317 -0
  20. package/dist/DynamoClient.d.ts.map +1 -0
  21. package/dist/DynamoClient.js +653 -0
  22. package/dist/DynamoClient.js.map +1 -0
  23. package/dist/DynamoModel.d.ts +278 -0
  24. package/dist/DynamoModel.d.ts.map +1 -0
  25. package/dist/DynamoModel.js +453 -0
  26. package/dist/DynamoModel.js.map +1 -0
  27. package/dist/DynamoSchema.d.ts +137 -0
  28. package/dist/DynamoSchema.d.ts.map +1 -0
  29. package/dist/DynamoSchema.js +188 -0
  30. package/dist/DynamoSchema.js.map +1 -0
  31. package/dist/Entity.d.ts +628 -0
  32. package/dist/Entity.d.ts.map +1 -0
  33. package/dist/Entity.js +2371 -0
  34. package/dist/Entity.js.map +1 -0
  35. package/dist/Errors.d.ts +227 -0
  36. package/dist/Errors.d.ts.map +1 -0
  37. package/dist/Errors.js +93 -0
  38. package/dist/Errors.js.map +1 -0
  39. package/dist/EventStore.d.ts +208 -0
  40. package/dist/EventStore.d.ts.map +1 -0
  41. package/dist/EventStore.js +333 -0
  42. package/dist/EventStore.js.map +1 -0
  43. package/dist/Expression.d.ts +50 -0
  44. package/dist/Expression.d.ts.map +1 -0
  45. package/dist/Expression.js +170 -0
  46. package/dist/Expression.js.map +1 -0
  47. package/dist/KeyComposer.d.ts +125 -0
  48. package/dist/KeyComposer.d.ts.map +1 -0
  49. package/dist/KeyComposer.js +257 -0
  50. package/dist/KeyComposer.js.map +1 -0
  51. package/dist/Marshaller.d.ts +30 -0
  52. package/dist/Marshaller.d.ts.map +1 -0
  53. package/dist/Marshaller.js +115 -0
  54. package/dist/Marshaller.js.map +1 -0
  55. package/dist/Projection.d.ts +28 -0
  56. package/dist/Projection.d.ts.map +1 -0
  57. package/dist/Projection.js +39 -0
  58. package/dist/Projection.js.map +1 -0
  59. package/dist/Query.d.ts +214 -0
  60. package/dist/Query.d.ts.map +1 -0
  61. package/dist/Query.js +397 -0
  62. package/dist/Query.js.map +1 -0
  63. package/dist/Table.d.ts +178 -0
  64. package/dist/Table.d.ts.map +1 -0
  65. package/dist/Table.js +211 -0
  66. package/dist/Table.js.map +1 -0
  67. package/dist/Transaction.d.ts +79 -0
  68. package/dist/Transaction.d.ts.map +1 -0
  69. package/dist/Transaction.js +215 -0
  70. package/dist/Transaction.js.map +1 -0
  71. package/dist/_AggregateCursor.d.ts +44 -0
  72. package/dist/_AggregateCursor.d.ts.map +1 -0
  73. package/dist/_AggregateCursor.js +18 -0
  74. package/dist/_AggregateCursor.js.map +1 -0
  75. package/dist/_AggregateEdges.d.ts +77 -0
  76. package/dist/_AggregateEdges.d.ts.map +1 -0
  77. package/dist/_AggregateEdges.js +41 -0
  78. package/dist/_AggregateEdges.js.map +1 -0
  79. package/dist/_AggregateSchemas.d.ts +69 -0
  80. package/dist/_AggregateSchemas.d.ts.map +1 -0
  81. package/dist/_AggregateSchemas.js +194 -0
  82. package/dist/_AggregateSchemas.js.map +1 -0
  83. package/dist/_AggregateTypes.d.ts +94 -0
  84. package/dist/_AggregateTypes.d.ts.map +1 -0
  85. package/dist/_AggregateTypes.js +7 -0
  86. package/dist/_AggregateTypes.js.map +1 -0
  87. package/dist/_EntityCombinators.d.ts +155 -0
  88. package/dist/_EntityCombinators.d.ts.map +1 -0
  89. package/dist/_EntityCombinators.js +158 -0
  90. package/dist/_EntityCombinators.js.map +1 -0
  91. package/dist/_EntityConfig.d.ts +100 -0
  92. package/dist/_EntityConfig.d.ts.map +1 -0
  93. package/dist/_EntityConfig.js +7 -0
  94. package/dist/_EntityConfig.js.map +1 -0
  95. package/dist/_EntityOps.d.ts +205 -0
  96. package/dist/_EntityOps.d.ts.map +1 -0
  97. package/dist/_EntityOps.js +137 -0
  98. package/dist/_EntityOps.js.map +1 -0
  99. package/dist/_EntitySchemas.d.ts +57 -0
  100. package/dist/_EntitySchemas.d.ts.map +1 -0
  101. package/dist/_EntitySchemas.js +298 -0
  102. package/dist/_EntitySchemas.js.map +1 -0
  103. package/dist/_EntityTypes.d.ts +81 -0
  104. package/dist/_EntityTypes.d.ts.map +1 -0
  105. package/dist/_EntityTypes.js +7 -0
  106. package/dist/_EntityTypes.js.map +1 -0
  107. package/dist/_sparse_check.d.ts +2 -0
  108. package/dist/_sparse_check.d.ts.map +1 -0
  109. package/dist/_sparse_check.js +193 -0
  110. package/dist/_sparse_check.js.map +1 -0
  111. package/dist/index.d.ts +35 -0
  112. package/dist/index.d.ts.map +1 -0
  113. package/dist/index.js +20 -0
  114. package/dist/index.js.map +1 -0
  115. package/dist/internal/AggregateCursor.d.ts +44 -0
  116. package/dist/internal/AggregateCursor.d.ts.map +1 -0
  117. package/dist/internal/AggregateCursor.js +18 -0
  118. package/dist/internal/AggregateCursor.js.map +1 -0
  119. package/dist/internal/AggregateEdges.d.ts +77 -0
  120. package/dist/internal/AggregateEdges.d.ts.map +1 -0
  121. package/dist/internal/AggregateEdges.js +41 -0
  122. package/dist/internal/AggregateEdges.js.map +1 -0
  123. package/dist/internal/AggregateSchemas.d.ts +69 -0
  124. package/dist/internal/AggregateSchemas.d.ts.map +1 -0
  125. package/dist/internal/AggregateSchemas.js +193 -0
  126. package/dist/internal/AggregateSchemas.js.map +1 -0
  127. package/dist/internal/AggregateTypes.d.ts +94 -0
  128. package/dist/internal/AggregateTypes.d.ts.map +1 -0
  129. package/dist/internal/AggregateTypes.js +7 -0
  130. package/dist/internal/AggregateTypes.js.map +1 -0
  131. package/dist/internal/BoundQuery.d.ts +107 -0
  132. package/dist/internal/BoundQuery.d.ts.map +1 -0
  133. package/dist/internal/BoundQuery.js +111 -0
  134. package/dist/internal/BoundQuery.js.map +1 -0
  135. package/dist/internal/EntityCombinators.d.ts +173 -0
  136. package/dist/internal/EntityCombinators.d.ts.map +1 -0
  137. package/dist/internal/EntityCombinators.js +183 -0
  138. package/dist/internal/EntityCombinators.js.map +1 -0
  139. package/dist/internal/EntityConfig.d.ts +100 -0
  140. package/dist/internal/EntityConfig.d.ts.map +1 -0
  141. package/dist/internal/EntityConfig.js +7 -0
  142. package/dist/internal/EntityConfig.js.map +1 -0
  143. package/dist/internal/EntityOps.d.ts +268 -0
  144. package/dist/internal/EntityOps.d.ts.map +1 -0
  145. package/dist/internal/EntityOps.js +153 -0
  146. package/dist/internal/EntityOps.js.map +1 -0
  147. package/dist/internal/EntitySchemas.d.ts +57 -0
  148. package/dist/internal/EntitySchemas.d.ts.map +1 -0
  149. package/dist/internal/EntitySchemas.js +299 -0
  150. package/dist/internal/EntitySchemas.js.map +1 -0
  151. package/dist/internal/EntityTypes.d.ts +128 -0
  152. package/dist/internal/EntityTypes.d.ts.map +1 -0
  153. package/dist/internal/EntityTypes.js +7 -0
  154. package/dist/internal/EntityTypes.js.map +1 -0
  155. package/dist/internal/Expr.d.ts +141 -0
  156. package/dist/internal/Expr.d.ts.map +1 -0
  157. package/dist/internal/Expr.js +332 -0
  158. package/dist/internal/Expr.js.map +1 -0
  159. package/dist/internal/PathBuilder.d.ts +88 -0
  160. package/dist/internal/PathBuilder.d.ts.map +1 -0
  161. package/dist/internal/PathBuilder.js +100 -0
  162. package/dist/internal/PathBuilder.js.map +1 -0
  163. package/dist/internal/TransactableOps.d.ts +25 -0
  164. package/dist/internal/TransactableOps.d.ts.map +1 -0
  165. package/dist/internal/TransactableOps.js +58 -0
  166. package/dist/internal/TransactableOps.js.map +1 -0
  167. package/dist/internal/sparse_check.d.ts +2 -0
  168. package/dist/internal/sparse_check.d.ts.map +1 -0
  169. package/dist/internal/sparse_check.js +196 -0
  170. package/dist/internal/sparse_check.js.map +1 -0
  171. package/package.json +71 -0
@@ -0,0 +1,453 @@
1
+ /**
2
+ * DynamoModel — Schema annotations, date schemas, and model configuration for effect-dynamodb.
3
+ *
4
+ * Provides:
5
+ * - `configure` — per-field DynamoDB overrides (field rename, storage encoding, immutable, identifier, ref)
6
+ * - `Hidden` annotation — marks fields as excluded from default decode (asModel/asRecord)
7
+ * - `DynamoEncoding` annotation — controls how date fields are stored in DynamoDB
8
+ * - Date schemas — wire ↔ domain ↔ storage transformations for DateTime.Utc, DateTime.Zoned, Date
9
+ * - `storedAs` modifier — overrides DynamoDB storage format via annotation
10
+ *
11
+ * See docs/design-dynamo-model.md for the three-layer model (wire → domain → storage).
12
+ */
13
+ import { DateTime, Effect, Option, Schema, SchemaAST, SchemaGetter, SchemaIssue } from "effect";
14
+ // ---------------------------------------------------------------------------
15
+ // Identifier annotation
16
+ // ---------------------------------------------------------------------------
17
+ /** Unique symbol for the Identifier annotation */
18
+ const IdentifierId = Symbol.for("effect-dynamodb/Identifier");
19
+ /**
20
+ * Mark a schema field as the primary identifier for ref resolution.
21
+ *
22
+ * Entities that can be referenced via `DynamoModel.ref` must annotate exactly
23
+ * one field with `identifier`. The annotated field should be a string or
24
+ * branded string type.
25
+ */
26
+ export const identifier = (schema) => schema.pipe(Schema.annotate({ [IdentifierId]: true }));
27
+ /**
28
+ * Check if a schema field has the Identifier annotation.
29
+ */
30
+ export const isIdentifier = (schema) => SchemaAST.resolve(schema.ast)?.[IdentifierId] ?? false;
31
+ /**
32
+ * Find the identifier field in a schema with `.fields` (Schema.Class or Schema.Struct).
33
+ * Checks both schema-level `DynamoModel.identifier` annotations and ConfiguredModel
34
+ * `identifier: true` overrides.
35
+ *
36
+ * Returns the field name and schema, or undefined if no field has the identifier annotation.
37
+ */
38
+ export const getIdentifierField = (model) => {
39
+ // Check ConfiguredModel attributes first (configure-level overrides take precedence)
40
+ if (isConfiguredModel(model)) {
41
+ const configured = model;
42
+ for (const [name, attr] of Object.entries(configured.attributes)) {
43
+ if (attr.identifier) {
44
+ const rawModel = configured.model;
45
+ if ("fields" in rawModel &&
46
+ typeof rawModel.fields === "object") {
47
+ const fields = rawModel
48
+ .fields;
49
+ const fieldSchema = fields[name];
50
+ if (fieldSchema)
51
+ return { name, schema: fieldSchema };
52
+ }
53
+ // Fallback: return with a generic string schema
54
+ return { name, schema: Schema.String };
55
+ }
56
+ }
57
+ // Fall through to check schema-level annotations on the inner model
58
+ return getIdentifierField(configured.model);
59
+ }
60
+ if (!("fields" in model) ||
61
+ typeof model.fields !== "object")
62
+ return undefined;
63
+ const fields = model.fields;
64
+ for (const [name, fieldSchema] of Object.entries(fields)) {
65
+ if (isIdentifier(fieldSchema))
66
+ return { name, schema: fieldSchema };
67
+ }
68
+ return undefined;
69
+ };
70
+ // ---------------------------------------------------------------------------
71
+ // Ref annotation
72
+ // ---------------------------------------------------------------------------
73
+ /** Unique symbol for the Ref annotation */
74
+ const RefId = Symbol.for("effect-dynamodb/Ref");
75
+ /**
76
+ * Mark a schema field as a denormalized reference to another entity.
77
+ *
78
+ * Ref fields:
79
+ * - In `Entity.Input`, become ID fields (e.g., `team: Team` → `teamId: string`)
80
+ * - In `Entity.Record`, remain the full entity domain type
81
+ * - In `Entity.Update`, become optional ID fields
82
+ * - In DynamoDB, store the entity's core domain data as an embedded map
83
+ *
84
+ * The referenced entity's model must have exactly one `DynamoModel.identifier`-annotated field.
85
+ */
86
+ export const ref = (schema) => {
87
+ // Extract Schema.Class identifier if available (e.g., "Team" from Schema.Class<Team>("Team"))
88
+ // In Effect v4, Schema.Class exposes `identifier` (not `_tag`) as the class name
89
+ const s = schema;
90
+ const refSchemaId = "identifier" in schema && typeof s.identifier === "string"
91
+ ? s.identifier
92
+ : "_tag" in schema && typeof s._tag === "string"
93
+ ? s._tag
94
+ : undefined;
95
+ return schema.pipe(Schema.annotate({ [RefId]: { _tag: "Ref", refSchemaId } }));
96
+ };
97
+ /**
98
+ * Check if a schema field has the Ref annotation.
99
+ */
100
+ export const isRef = (schema) => {
101
+ const ann = SchemaAST.resolve(schema.ast)?.[RefId];
102
+ return ann != null && typeof ann === "object" && ann._tag === "Ref";
103
+ };
104
+ /**
105
+ * Check if a field is a ref, considering both schema-level annotations and
106
+ * ConfiguredModel overrides. Use this when you have access to the configured model.
107
+ */
108
+ export const isRefField = (fieldName, model) => {
109
+ // Check ConfiguredModel attributes
110
+ if (isConfiguredModel(model)) {
111
+ const configured = model;
112
+ if (configured.attributes[fieldName]?.ref)
113
+ return true;
114
+ // Fall through to check schema-level annotation on the inner model
115
+ return isRefField(fieldName, configured.model);
116
+ }
117
+ // Check schema-level annotation
118
+ if ("fields" in model &&
119
+ typeof model.fields === "object") {
120
+ const fields = model.fields;
121
+ const fieldSchema = fields[fieldName];
122
+ if (fieldSchema)
123
+ return isRef(fieldSchema);
124
+ }
125
+ return false;
126
+ };
127
+ /**
128
+ * Get the full Ref annotation metadata from a schema field.
129
+ * Returns undefined if the field is not ref-annotated.
130
+ */
131
+ export const getRefAnnotation = (schema) => {
132
+ const ann = SchemaAST.resolve(schema.ast)?.[RefId];
133
+ if (ann != null && typeof ann === "object" && ann._tag === "Ref") {
134
+ return ann;
135
+ }
136
+ return undefined;
137
+ };
138
+ // ---------------------------------------------------------------------------
139
+ // Hidden annotation
140
+ // ---------------------------------------------------------------------------
141
+ /** Unique symbol for the Hidden annotation */
142
+ const HiddenId = Symbol.for("effect-dynamodb/Hidden");
143
+ /**
144
+ * Mark a schema field as hidden from default decode modes (asModel, asRecord).
145
+ *
146
+ * Hidden fields:
147
+ * - Stored in DynamoDB normally
148
+ * - Visible in asItem and asNative decode modes
149
+ * - Stripped from asModel and asRecord decode modes
150
+ *
151
+ * Use for internal/infrastructure fields that shouldn't leak into application code.
152
+ */
153
+ export const Hidden = (schema) => schema.pipe(Schema.annotate({ [HiddenId]: true }));
154
+ /**
155
+ * Check if a schema field has the Hidden annotation.
156
+ */
157
+ export const isHidden = (schema) => SchemaAST.resolve(schema.ast)?.[HiddenId] ?? false;
158
+ // ---------------------------------------------------------------------------
159
+ // DynamoEncoding annotation
160
+ // ---------------------------------------------------------------------------
161
+ /** Unique symbol for the DynamoEncoding annotation */
162
+ export const DynamoEncodingKey = Symbol.for("effect-dynamodb/DynamoEncoding");
163
+ /**
164
+ * Read the DynamoEncoding annotation from a schema, if present.
165
+ */
166
+ export const getEncoding = (schema) => SchemaAST.resolve(schema.ast)?.[DynamoEncodingKey];
167
+ // ---------------------------------------------------------------------------
168
+ // Date schemas — DateTime.Utc domain
169
+ // ---------------------------------------------------------------------------
170
+ /**
171
+ * Wire: ISO 8601 string ↔ Domain: DateTime.Utc
172
+ *
173
+ * Default storage: ISO string (`S`).
174
+ * This is a thin wrapper around `Schema.DateTimeUtcFromString` with the DynamoEncoding annotation.
175
+ */
176
+ export const DateString = Schema.DateTimeUtcFromString.pipe(Schema.annotate({
177
+ [DynamoEncodingKey]: { storage: "string", domain: "DateTime.Utc" },
178
+ }));
179
+ /**
180
+ * Wire: epoch milliseconds (number) ↔ Domain: DateTime.Utc
181
+ *
182
+ * Default storage: epoch ms (`N`).
183
+ */
184
+ export const DateEpochMs = Schema.Number.pipe(Schema.decodeTo(Schema.DateTimeUtc, {
185
+ decode: SchemaGetter.transform((n) => DateTime.makeUnsafe(n)),
186
+ encode: SchemaGetter.transform((dt) => DateTime.toEpochMillis(dt)),
187
+ }), Schema.annotate({
188
+ [DynamoEncodingKey]: { storage: "epochMs", domain: "DateTime.Utc" },
189
+ }));
190
+ /**
191
+ * Wire: epoch seconds (number) ↔ Domain: DateTime.Utc
192
+ *
193
+ * Default storage: epoch seconds (`N`).
194
+ * DynamoDB TTL requires this format.
195
+ */
196
+ export const DateEpochSeconds = Schema.Number.pipe(Schema.decodeTo(Schema.DateTimeUtc, {
197
+ decode: SchemaGetter.transform((n) => DateTime.makeUnsafe(n * 1000)),
198
+ encode: SchemaGetter.transform((dt) => Math.floor(DateTime.toEpochMillis(dt) / 1000)),
199
+ }), Schema.annotate({
200
+ [DynamoEncodingKey]: {
201
+ storage: "epochSeconds",
202
+ domain: "DateTime.Utc",
203
+ },
204
+ }));
205
+ /**
206
+ * Wire: auto-detect epoch ms or seconds ↔ Domain: DateTime.Utc
207
+ *
208
+ * Uses a minimum threshold to disambiguate: if the value interpreted as ms
209
+ * produces a date before `minimum`, it tries seconds instead.
210
+ *
211
+ * @param options.minimum - Threshold date for disambiguation
212
+ * @param options.encode - Epoch schema for wire encoding (default: DateEpochMs)
213
+ */
214
+ export const DateEpoch = (options) => {
215
+ const minEpochMs = typeof options.minimum === "string"
216
+ ? new Date(options.minimum).getTime()
217
+ : DateTime.toEpochMillis(DateTime.makeUnsafe(options.minimum));
218
+ const encodeSchema = options.encode ?? DateEpochMs;
219
+ const encodeEncoding = getEncoding(encodeSchema);
220
+ return Schema.Number.pipe(Schema.decodeTo(Schema.DateTimeUtc, {
221
+ decode: SchemaGetter.transformOrFail((n) => {
222
+ // Try milliseconds first
223
+ try {
224
+ const asMs = DateTime.makeUnsafe(n);
225
+ if (DateTime.toEpochMillis(asMs) >= minEpochMs) {
226
+ return Effect.succeed(asMs);
227
+ }
228
+ }
229
+ catch {
230
+ // fall through
231
+ }
232
+ // Fall back to seconds
233
+ try {
234
+ const asSec = DateTime.makeUnsafe(n * 1000);
235
+ if (DateTime.toEpochMillis(asSec) >= minEpochMs) {
236
+ return Effect.succeed(asSec);
237
+ }
238
+ }
239
+ catch {
240
+ // fall through
241
+ }
242
+ return Effect.fail(new SchemaIssue.InvalidType(Schema.Number.ast, Option.some(n)));
243
+ }),
244
+ encode: SchemaGetter.transform((dt) => Schema.encodeSync(encodeSchema)(dt)),
245
+ }), Schema.annotate({
246
+ [DynamoEncodingKey]: {
247
+ storage: encodeEncoding?.storage ?? "epochMs",
248
+ domain: "DateTime.Utc",
249
+ },
250
+ }));
251
+ };
252
+ // ---------------------------------------------------------------------------
253
+ // Date schemas — DateTime.Zoned domain
254
+ // ---------------------------------------------------------------------------
255
+ /**
256
+ * Wire: ISO 8601 string with offset/zone ↔ Domain: DateTime.Zoned
257
+ *
258
+ * Default storage: extended ISO with zone (`S`).
259
+ * Format: `2024-01-01T15:00:00+09:00[Asia/Tokyo]`
260
+ *
261
+ * Cannot use epoch storage — timezone information would be lost (enforced at type level via storedAs).
262
+ */
263
+ export const DateTimeZoned = Schema.String.pipe(Schema.decodeTo(Schema.DateTimeZoned, {
264
+ decode: SchemaGetter.transformOrFail((s) => {
265
+ // Parse extended ISO format: "2024-01-01T15:00:00+09:00[Asia/Tokyo]"
266
+ const match = s.match(/^(.+)\[(.+)\]$/);
267
+ if (match) {
268
+ const dateStr = match[1];
269
+ const tzName = match[2];
270
+ try {
271
+ const utc = DateTime.makeUnsafe(dateStr);
272
+ return Effect.succeed(DateTime.makeZonedUnsafe(utc, { timeZone: tzName }));
273
+ }
274
+ catch {
275
+ return Effect.fail(new SchemaIssue.InvalidType(Schema.String.ast, Option.some(s)));
276
+ }
277
+ }
278
+ // Try parsing as offset-only: "2024-01-01T15:00:00+09:00"
279
+ try {
280
+ const utc = DateTime.makeUnsafe(s);
281
+ return Effect.succeed(DateTime.makeZonedUnsafe(utc, { timeZone: "UTC" }));
282
+ }
283
+ catch {
284
+ return Effect.fail(new SchemaIssue.InvalidType(Schema.String.ast, Option.some(s)));
285
+ }
286
+ }),
287
+ encode: SchemaGetter.transform((dt) => DateTime.formatIsoZoned(dt)),
288
+ }), Schema.annotate({
289
+ [DynamoEncodingKey]: { storage: "string", domain: "DateTime.Zoned" },
290
+ }));
291
+ // ---------------------------------------------------------------------------
292
+ // Unsafe date schemas — native Date domain
293
+ // ---------------------------------------------------------------------------
294
+ /**
295
+ * Wire: ISO 8601 string ↔ Domain: native Date (mutable)
296
+ *
297
+ * Default storage: ISO string (`S`).
298
+ * For interop with non-Effect code that expects native JS Date objects.
299
+ */
300
+ export const UnsafeDateString = Schema.String.pipe(Schema.decodeTo(Schema.DateValid, {
301
+ decode: SchemaGetter.transform((s) => new Date(s)),
302
+ encode: SchemaGetter.transform((d) => d.toISOString()),
303
+ }), Schema.annotate({
304
+ [DynamoEncodingKey]: { storage: "string", domain: "Date" },
305
+ }));
306
+ /**
307
+ * Wire: epoch milliseconds (number) ↔ Domain: native Date (mutable)
308
+ *
309
+ * Default storage: epoch ms (`N`).
310
+ */
311
+ export const UnsafeDateEpochMs = Schema.Number.pipe(Schema.decodeTo(Schema.DateValid, {
312
+ decode: SchemaGetter.transform((ms) => new Date(ms)),
313
+ encode: SchemaGetter.transform((d) => d.getTime()),
314
+ }), Schema.annotate({
315
+ [DynamoEncodingKey]: { storage: "epochMs", domain: "Date" },
316
+ }));
317
+ /**
318
+ * Wire: epoch seconds (number) ↔ Domain: native Date (mutable)
319
+ *
320
+ * Default storage: epoch seconds (`N`).
321
+ */
322
+ export const UnsafeDateEpochSeconds = Schema.Number.pipe(Schema.decodeTo(Schema.DateValid, {
323
+ decode: SchemaGetter.transform((s) => new Date(s * 1000)),
324
+ encode: SchemaGetter.transform((d) => Math.floor(d.getTime() / 1000)),
325
+ }), Schema.annotate({
326
+ [DynamoEncodingKey]: {
327
+ storage: "epochSeconds",
328
+ domain: "Date",
329
+ },
330
+ }));
331
+ // ---------------------------------------------------------------------------
332
+ // TTL alias
333
+ // ---------------------------------------------------------------------------
334
+ /**
335
+ * Alias for `DateEpochSeconds`. DynamoDB TTL requires epoch seconds.
336
+ * A named alias makes intent clear at the call site.
337
+ */
338
+ export const TTL = DateEpochSeconds;
339
+ // ---------------------------------------------------------------------------
340
+ // storedAs modifier
341
+ // ---------------------------------------------------------------------------
342
+ /**
343
+ * Override the DynamoDB storage format for a date field.
344
+ *
345
+ * `storedAs` reads the DynamoEncoding annotation from the target storage schema
346
+ * and applies its storage format to the field schema. The domain type must match
347
+ * (enforced at the type level via the `A` constraint).
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * // Wire: ISO string, DynamoDB: epoch seconds (for TTL)
352
+ * DynamoModel.DateString.pipe(DynamoModel.storedAs(DynamoModel.DateEpochSeconds))
353
+ *
354
+ * // Wire: epoch ms, DynamoDB: ISO string
355
+ * DynamoModel.DateEpochMs.pipe(DynamoModel.storedAs(DynamoModel.DateString))
356
+ * ```
357
+ *
358
+ * Type-safe: incompatible domain types produce a compile error:
359
+ * ```typescript
360
+ * // Type error — DateTime.Zoned ≠ DateTime.Utc
361
+ * DynamoModel.DateTimeZoned.pipe(DynamoModel.storedAs(DynamoModel.DateEpochMs))
362
+ * ```
363
+ */
364
+ export const storedAs = (storageSchema) => {
365
+ const storageEncoding = getEncoding(storageSchema);
366
+ if (!storageEncoding) {
367
+ throw new Error("storedAs: target schema has no DynamoEncoding annotation");
368
+ }
369
+ return (fieldSchema) => {
370
+ const currentEncoding = getEncoding(fieldSchema);
371
+ return fieldSchema.pipe(Schema.annotate({
372
+ [DynamoEncodingKey]: {
373
+ storage: storageEncoding.storage,
374
+ domain: currentEncoding?.domain ?? storageEncoding.domain,
375
+ },
376
+ }));
377
+ };
378
+ };
379
+ // ---------------------------------------------------------------------------
380
+ // ConfiguredModel
381
+ // ---------------------------------------------------------------------------
382
+ /** Unique symbol identifying a ConfiguredModel wrapper */
383
+ export const ConfiguredModelTag = Symbol.for("effect-dynamodb/ConfiguredModel");
384
+ /**
385
+ * Check if a value is a ConfiguredModel.
386
+ */
387
+ export const isConfiguredModel = (value) => typeof value === "object" &&
388
+ value !== null &&
389
+ value[ConfiguredModelTag] === true;
390
+ /**
391
+ * Create a configured model with per-field DynamoDB overrides.
392
+ *
393
+ * Separates DynamoDB infrastructure (storage format, field renaming) from
394
+ * the domain model, enabling pure models that work across storage backends.
395
+ *
396
+ * @example
397
+ * ```typescript
398
+ * class Order extends Schema.Class<Order>("Order")({
399
+ * orderId: Schema.String,
400
+ * placedAt: Schema.DateTimeUtcFromString,
401
+ * expiresAt: Schema.DateTimeUtcFromString,
402
+ * }) {}
403
+ *
404
+ * const OrderModel = DynamoModel.configure(Order, {
405
+ * expiresAt: { field: "ttl", storedAs: DynamoModel.DateEpochSeconds },
406
+ * })
407
+ * ```
408
+ */
409
+ export const configure = (model, attributes) => {
410
+ const resolved = {};
411
+ for (const [key, config] of Object.entries(attributes)) {
412
+ const entry = {};
413
+ if (config.field)
414
+ entry.field = config.field;
415
+ if (config.immutable)
416
+ entry.immutable = true;
417
+ if (config.identifier)
418
+ entry.identifier = true;
419
+ if (config.ref)
420
+ entry.ref = true;
421
+ if (config.storedAs) {
422
+ const encoding = getEncoding(config.storedAs);
423
+ if (!encoding) {
424
+ throw new Error(`DynamoModel.configure: storedAs schema for "${key}" has no DynamoEncoding annotation`);
425
+ }
426
+ entry.encoding = encoding;
427
+ }
428
+ resolved[key] = entry;
429
+ }
430
+ // Validate: no field rename targets collide with existing model field names
431
+ const modelFields = new Set();
432
+ if ("fields" in model &&
433
+ typeof model.fields === "object") {
434
+ for (const fieldName of Object.keys(model.fields)) {
435
+ modelFields.add(fieldName);
436
+ }
437
+ }
438
+ if (modelFields.size > 0) {
439
+ for (const [sourceField, config] of Object.entries(resolved)) {
440
+ if (config.field && config.field !== sourceField && modelFields.has(config.field)) {
441
+ throw new Error(`[EDD-9007] DynamoModel.configure: renaming "${sourceField}" to "${config.field}" collides with existing model field "${config.field}". ` +
442
+ `This would cause both fields to map to the same DynamoDB attribute.`);
443
+ }
444
+ }
445
+ }
446
+ return {
447
+ [ConfiguredModelTag]: true,
448
+ model,
449
+ _attributes: attributes,
450
+ attributes: resolved,
451
+ };
452
+ };
453
+ //# sourceMappingURL=DynamoModel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DynamoModel.js","sourceRoot":"","sources":["../src/DynamoModel.ts"],"names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AAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC7D,IAAI,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,KAAK,KAAK,WAAW,IAAI,WAAW,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;gBAClF,MAAM,IAAI,KAAK,CACb,+CAA+C,WAAW,SAAS,MAAM,CAAC,KAAK,yCAAyC,MAAM,CAAC,KAAK,KAAK;oBACvI,qEAAqE,CACxE,CAAA;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO;QACL,CAAC,kBAAkB,CAAC,EAAE,IAAa;QACnC,KAAK;QACL,WAAW,EAAE,UAAU;QACvB,UAAU,EAAE,QAAQ;KACI,CAAA;AAC5B,CAAC,CAAA"}
@@ -0,0 +1,137 @@
1
+ /**
2
+ * DynamoSchema — Application namespace for key prefixing.
3
+ *
4
+ * Every generated DynamoDB key starts with `$<schema>#v<version>#` followed by
5
+ * entity-specific parts. The `$` sentinel identifies ORM-managed keys.
6
+ */
7
+ export type Casing = "lowercase" | "uppercase" | "preserve";
8
+ export interface DynamoSchema {
9
+ readonly name: string;
10
+ readonly version: number;
11
+ readonly casing: Casing;
12
+ }
13
+ /**
14
+ * Create a DynamoSchema instance.
15
+ */
16
+ export declare const make: (config: {
17
+ readonly name: string;
18
+ readonly version: number;
19
+ readonly casing?: Casing | undefined;
20
+ }) => DynamoSchema;
21
+ /**
22
+ * Apply casing to a key part (structural or composite value).
23
+ *
24
+ * Matches ElectroDB behavior: casing is applied to the entire composed key,
25
+ * including composite attribute values. With the default `"lowercase"` casing,
26
+ * `"Male"` becomes `"male"` in the key, ensuring case-insensitive key matching.
27
+ */
28
+ export declare const applyCasing: (value: string, casing: Casing) => string;
29
+ /**
30
+ * Build the schema prefix: `$<schema>#v<version>`
31
+ * This is prepended to every generated key.
32
+ */
33
+ export declare const prefix: (schema: DynamoSchema) => string;
34
+ /**
35
+ * Compose a full key from schema, entity type, and composite attribute values.
36
+ *
37
+ * Format: `$<schema>#v<version>#<entityType>#<attr1>#<attr2>`
38
+ *
39
+ * Both the entityType and composite values are cased according to the schema's casing setting.
40
+ * This matches ElectroDB behavior where the entire key is uniformly cased.
41
+ */
42
+ export declare const composeKey: (schema: DynamoSchema, entityType: string, composites: ReadonlyArray<string>, options?: {
43
+ readonly casing?: Casing | undefined;
44
+ readonly names?: ReadonlyArray<string> | undefined;
45
+ } | undefined) => string;
46
+ /**
47
+ * Compose a collection key prefix (for clustered collections).
48
+ *
49
+ * Format: `$<schema>#v<version>#<collectionName>#<attr1>#<attr2>`
50
+ */
51
+ export declare const composeCollectionKey: (schema: DynamoSchema, collectionName: string, composites: ReadonlyArray<string>, options?: {
52
+ readonly casing?: Casing | undefined;
53
+ readonly names?: ReadonlyArray<string> | undefined;
54
+ } | undefined) => string;
55
+ /**
56
+ * Compose a sort key for clustered collection indexes.
57
+ *
58
+ * Format (single collection):
59
+ * `$<schema>#v<version>#<collectionName>#<entityType>_<entityVersion>#<attr1>#<attr2>`
60
+ *
61
+ * Format (sub-collection — when `collectionName` is an array):
62
+ * `$<schema>#v<version>#<parent>#<child>#<entityType>_<entityVersion>#<attr1>#<attr2>`
63
+ *
64
+ * The hierarchy elements are joined with `#` so a `begins_with` query at any level
65
+ * matches every entity in that subtree.
66
+ *
67
+ * The entityVersion is the schema version for the entity type within the collection.
68
+ * This allows different entity types in a collection to have their own sort key namespace.
69
+ */
70
+ export declare const composeClusteredSortKey: (schema: DynamoSchema, collectionName: string | ReadonlyArray<string>, entityType: string, entityVersion: number, composites: ReadonlyArray<string>, options?: {
71
+ readonly casing?: Casing | undefined;
72
+ readonly names?: ReadonlyArray<string> | undefined;
73
+ } | undefined) => string;
74
+ /**
75
+ * Compose a sort key for isolated indexes.
76
+ *
77
+ * Format: `$<schema>#v<version>#<entityType>_<entityVersion>#<attr1>#<attr2>`
78
+ */
79
+ export declare const composeIsolatedSortKey: (schema: DynamoSchema, entityType: string, entityVersion: number, composites: ReadonlyArray<string>, options?: {
80
+ readonly casing?: Casing | undefined;
81
+ readonly names?: ReadonlyArray<string> | undefined;
82
+ } | undefined) => string;
83
+ /**
84
+ * Compose a unique constraint sentinel key.
85
+ *
86
+ * PK format: `$<schema>#v<version>#<entityType>.<constraintName>#<fieldValues>`
87
+ * SK format: `$<schema>#v<version>#<entityType>.<constraintName>`
88
+ */
89
+ export declare const composeUniqueKey: (schema: DynamoSchema, entityType: string, constraintName: string, fieldValues: ReadonlyArray<string>, options?: {
90
+ readonly casing?: Casing | undefined;
91
+ } | undefined) => {
92
+ readonly pk: string;
93
+ readonly sk: string;
94
+ };
95
+ /**
96
+ * Compose a version snapshot sort key.
97
+ *
98
+ * Format: `$<schema>#v<version>#<entityType>#v#<zeroPaddedVersion>`
99
+ */
100
+ export declare const composeVersionKey: (schema: DynamoSchema, entityType: string, version: number, options?: {
101
+ readonly casing?: Casing | undefined;
102
+ } | undefined) => string;
103
+ /**
104
+ * Compose a soft-deleted item sort key.
105
+ *
106
+ * Format: `$<schema>#v<version>#<entityType>#deleted#<isoTimestamp>`
107
+ */
108
+ export declare const composeDeletedKey: (schema: DynamoSchema, entityType: string, timestamp: string, options?: {
109
+ readonly casing?: Casing | undefined;
110
+ } | undefined) => string;
111
+ /**
112
+ * Compose the version key prefix for `begins_with` queries.
113
+ *
114
+ * Format: `$<schema>#v<version>#<entityType>#v#`
115
+ */
116
+ export declare const composeVersionKeyPrefix: (schema: DynamoSchema, entityType: string, options?: {
117
+ readonly casing?: Casing | undefined;
118
+ } | undefined) => string;
119
+ /**
120
+ * Compose the deleted key prefix for `begins_with` queries.
121
+ *
122
+ * Format: `$<schema>#v<version>#<entityType>#deleted#`
123
+ */
124
+ export declare const composeDeletedKeyPrefix: (schema: DynamoSchema, entityType: string, options?: {
125
+ readonly casing?: Casing | undefined;
126
+ } | undefined) => string;
127
+ /**
128
+ * Compose an event version sort key (10-digit zero-padded).
129
+ *
130
+ * Format: `$<schema>#v<version>#<entityType>_1#<zeroPaddedVersion>`
131
+ *
132
+ * Uses isolated sort key format with 10-digit padding (supports ~10B events per stream).
133
+ */
134
+ export declare const composeEventVersionKey: (schema: DynamoSchema, entityType: string, version: number, options?: {
135
+ readonly casing?: Casing | undefined;
136
+ } | undefined) => string;
137
+ //# sourceMappingURL=DynamoSchema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"DynamoSchema.d.ts","sourceRoot":"","sources":["../src/DynamoSchema.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,MAAM,GAAG,WAAW,GAAG,WAAW,GAAG,UAAU,CAAA;AAE3D,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CACxB;AAED;;GAEG;AACH,eAAO,MAAM,IAAI,GAAI,QAAQ;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CACrC,KAAG,YAIF,CAAA;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAI,OAAO,MAAM,EAAE,QAAQ,MAAM,KAAG,MAS3D,CAAA;AAyBD;;;GAGG;AACH,eAAO,MAAM,MAAM,GAAI,QAAQ,YAAY,KAAG,MAG7C,CAAA;AAcD;;;;;;;GAOG;AACH,eAAO,MAAM,UAAU,GACrB,QAAQ,YAAY,EACpB,YAAY,MAAM,EAClB,YAAY,aAAa,CAAC,MAAM,CAAC,EACjC,UACI;IAAE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,SAAS,CAAA;CAAE,GAC5F,SAAS,KACZ,MAY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