@effect-dynamodb/schema 1.41.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.
@@ -1123,6 +1123,7 @@ const childOptions = (container, deeper, index, opts) => {
1123
1123
  strictWireKind: true,
1124
1124
  collidingKinds,
1125
1125
  ...(opts?.unionDateOverride ? { dateEncodingOverride: opts.unionDateOverride } : {}),
1126
+ ...(opts?.unionDateOverride && opts.readsDeclaredStorage ? { readsDeclaredStorage: true } : {}),
1126
1127
  };
1127
1128
  };
1128
1129
  /** The options an optional wrapper hands its inner schema: it is transparent. */
@@ -1194,6 +1195,7 @@ const throughOptional = (opts) => {
1194
1195
  ...(opts.unionDateOverride ? { unionDateOverride: opts.unionDateOverride } : {}),
1195
1196
  ...(opts.collidingKinds ? { collidingKinds: opts.collidingKinds } : {}),
1196
1197
  ...(opts.dateEncodingOverride ? { dateEncodingOverride: opts.dateEncodingOverride } : {}),
1198
+ ...(opts.readsDeclaredStorage ? { readsDeclaredStorage: true } : {}),
1197
1199
  }
1198
1200
  : deeper;
1199
1201
  };
@@ -1231,14 +1233,46 @@ const selfDatePlan = (schema, opts) => {
1231
1233
  }
1232
1234
  return { encoding, canonicalOnly };
1233
1235
  };
1236
+ /**
1237
+ * Under `readsDeclaredStorage`, in a union: the plan of the storage the date declares, where the
1238
+ * override reads another kind of value (see `readsDeclaredStorage`). Out of a union the override's
1239
+ * transform reads a value by its kind already, text as ISO text and a number in its own scale.
1240
+ */
1241
+ const declaredStoragePlan = (schema, opts) => {
1242
+ if (!opts?.readsDeclaredStorage || !opts.strictWireKind || !opts.dateEncodingOverride) {
1243
+ return undefined;
1244
+ }
1245
+ const own = getEncoding(schema) ?? inferDefaultEncoding(schema);
1246
+ const text = (encoding) => encoding.storage === "string";
1247
+ if (own === undefined || text(own) === text(opts.dateEncodingOverride))
1248
+ return undefined;
1249
+ try {
1250
+ return selfDatePlan(schema, { ...opts, dateEncodingOverride: undefined });
1251
+ }
1252
+ catch {
1253
+ // An epoch beside a number member (EDD-9058) is refused where it's the storage, not here.
1254
+ return undefined;
1255
+ }
1256
+ };
1234
1257
  const selfDateSubstitute = (schema, opts) => {
1235
1258
  const plan = selfDatePlan(schema, opts);
1236
1259
  if (plan === undefined)
1237
1260
  return schema;
1238
- return buildDateTransform(plan.encoding, {
1261
+ const substitute = buildDateTransform(plan.encoding, {
1239
1262
  strictWireKind: opts?.strictWireKind,
1240
1263
  canonicalOnly: plan.canonicalOnly,
1241
1264
  });
1265
+ // Encodes as the override, and reads either form, as an aggregate's union member does.
1266
+ const declared = declaredStoragePlan(schema, opts);
1267
+ return declared === undefined
1268
+ ? substitute
1269
+ : Schema.Union([
1270
+ substitute,
1271
+ buildDateTransform(declared.encoding, {
1272
+ strictWireKind: true,
1273
+ canonicalOnly: declared.canonicalOnly,
1274
+ }),
1275
+ ]);
1242
1276
  };
1243
1277
  /**
1244
1278
  * Whether a union member is a self date that claims only its canonical form
@@ -1250,8 +1284,10 @@ const selfDateSubstitute = (schema, opts) => {
1250
1284
  */
1251
1285
  const isCanonicalDateMember = (member, opts) => {
1252
1286
  const inner = optionalField(member)?.inner ?? member;
1253
- if (isSelfDateSchema(inner))
1254
- return selfDatePlan(inner, opts)?.canonicalOnly === true;
1287
+ if (isSelfDateSchema(inner)) {
1288
+ return (selfDatePlan(inner, opts)?.canonicalOnly === true ||
1289
+ declaredStoragePlan(inner, opts)?.canonicalOnly === true);
1290
+ }
1255
1291
  // A nested union holding such a member (`NullOr(DateTimeUtc)` next to a
1256
1292
  // `String`) is decoded first for the same reason.
1257
1293
  const container = walkedContainer(inner);
@@ -1845,7 +1881,7 @@ export const substituteSchemaDeep = (schema, opts) => {
1845
1881
  setOwn(subFields, name, fieldNamed(name, fieldOpt ? fieldOpt.rewrap(sub) : sub, field));
1846
1882
  }
1847
1883
  else {
1848
- const sub = substituteSchemaDeep(field, atField(deeper, opts, name));
1884
+ const sub = substituteSchemaDeep(field, withConfiguredStorage(atField(deeper, opts, name), name, field, ownValue(opts?.configuredStorage, name)));
1849
1885
  setOwn(subFields, name, name === "__proto__"
1850
1886
  ? fieldNamed(name, sub, field)
1851
1887
  : opts?.absentReads
@@ -2192,6 +2228,39 @@ const unionDateOverride = (name, schema, encoding) => {
2192
2228
  }
2193
2229
  return encoding;
2194
2230
  };
2231
+ /**
2232
+ * @internal The storage `DynamoModel.configure` gives a model's fields (`storedAs` a date schema),
2233
+ * by name, or `undefined` for a model with none. What a ref to the model's entity holds such a
2234
+ * field in (`DeepSubstitutionOptions.configuredStorage`).
2235
+ */
2236
+ export const configuredStorageOf = (model) => {
2237
+ if (!isConfiguredModel(model))
2238
+ return undefined;
2239
+ const storage = {};
2240
+ for (const [name, attribute] of Object.entries(model.attributes)) {
2241
+ if (attribute.encoding !== undefined)
2242
+ setOwn(storage, name, attribute.encoding);
2243
+ }
2244
+ return Object.keys(storage).length === 0 ? undefined : storage;
2245
+ };
2246
+ /**
2247
+ * A field's substitution options with the storage `encoding` its model's `DynamoModel.configure`
2248
+ * gives it, where its entity applies that storage ({@link substituteSchemas} decides by the same
2249
+ * tests): a date (a `Schema.optionalKey` one too, or one beneath a decoding default), or a union's
2250
+ * date member. Elsewhere, and without one, `options` as given. A read (`legacyReads`) also takes
2251
+ * the form the date's own storage writes (`readsDeclaredStorage`).
2252
+ */
2253
+ const withConfiguredStorage = (options, name, field, encoding) => {
2254
+ if (encoding === undefined)
2255
+ return options;
2256
+ const reads = options.legacyReads ? { readsDeclaredStorage: true } : {};
2257
+ const defaulted = decodingDefaultField(field);
2258
+ if (isSelfDateSchema(field) || (defaulted !== undefined && isSelfDateSchema(defaulted.inner))) {
2259
+ return { ...options, dateEncodingOverride: encoding, ...reads };
2260
+ }
2261
+ const union = unionDateOverride(name, field, encoding);
2262
+ return union === undefined ? options : { ...options, unionDateOverride: union, ...reads };
2263
+ };
2195
2264
  /**
2196
2265
  * Validate that no model field combines a transform schema with a storage
2197
2266
  * override. The two configuration paths are mutually exclusive: either declare
@@ -2349,11 +2418,17 @@ protoRenamed = false) => {
2349
2418
  // removed from, reads without it. Up to 1.40.0 the target replaced the declaration whole.
2350
2419
  const optional = optionalField(ownValue(modelFields, ref.fieldName));
2351
2420
  const asDeclared = (target) => optional === undefined ? target : optional.rewrap(target);
2421
+ // In the storage the target's `DynamoModel.configure` gives its fields, which the ref holds them
2422
+ // in (`refDataOf`). Up to 1.41.0 the default: a date stored as epoch seconds read back as
2423
+ // milliseconds, and a condition or `pathSet` on it sent ISO text.
2424
+ const storage = configuredStorageOf(targetModel);
2425
+ const configured = storage === undefined ? {} : { configuredStorage: storage };
2352
2426
  // What a read of the target returns (`refHeldSchema`): its required hidden fields left out.
2353
- setOwn(fields, ref.fieldName, asDeclared(refHeldSchema(resolved, { enforceChecks: true })));
2427
+ setOwn(fields, ref.fieldName, asDeclared(refHeldSchema(resolved, { enforceChecks: true, ...configured })));
2354
2428
  setOwn(readFields, ref.fieldName, asDeclared(refHeldSchema(resolved, {
2355
2429
  legacyReads: true,
2356
2430
  absentReads: true,
2431
+ ...configured,
2357
2432
  })));
2358
2433
  }
2359
2434
  // Each of Effect's look-alike declarations (a `BigDecimal`, a `Duration`, an address, a date in
@@ -2768,6 +2843,22 @@ const asStored = (value) => {
2768
2843
  }
2769
2844
  return out;
2770
2845
  };
2846
+ /** Text, a number, a `bigint` or a boolean. */
2847
+ const isPrimitive = (value) => {
2848
+ const type = typeof value;
2849
+ return type === "string" || type === "number" || type === "bigint" || type === "boolean";
2850
+ };
2851
+ /** The type DynamoDB holds a primitive as: text, a number (a `bigint` too), or a boolean. */
2852
+ const dynamoTypeOf = (value) => typeof value === "string" ? "S" : typeof value === "boolean" ? "BOOL" : "N";
2853
+ /**
2854
+ * Whether a condition's or filter's `operand` is sent as given where a write of it stores `stored`
2855
+ * (#242): a primitive is, unless what's stored is a primitive of another type, which no row holds
2856
+ * the operand as (`5` where a `NumberFromString` stores `"5"`). Text where text is stored could be
2857
+ * the stored text itself, a substring for `contains` or a prefix, which the library can't tell
2858
+ * apart, so it is sent as 1.41.0 sent it; so is a number where a number is stored, and a primitive
2859
+ * where what's stored is no primitive.
2860
+ */
2861
+ const keepsAsGiven = (operand, stored) => isPrimitive(operand) && (!isPrimitive(stored) || dynamoTypeOf(stored) === dynamoTypeOf(operand));
2771
2862
  /** A plain object or class instance whose fields a path value can be walked by. */
2772
2863
  const isWalkableObject = (value) => value !== null &&
2773
2864
  typeof value === "object" &&
@@ -2789,8 +2880,10 @@ export const containedKindOf = (field) => {
2789
2880
  /**
2790
2881
  * A `contains` operand in the form the field stores what it compares (#242), from what a write of
2791
2882
  * a whole value there stores (`value`) and of one list element (`element`): the element as a list
2792
- * stores it; the single member of a write of a set holding it; the text a write of it stores, when
2793
- * that's text. `undefined` when the field doesn't take it so, and the operand is sent as given.
2883
+ * stores it; the single member of a write of a set holding it (a primitive member as given, unless
2884
+ * the set stores it as a primitive of another type: {@link keepsAsGiven}); the text a write of it
2885
+ * stores, when that's text. `undefined` when the field doesn't take it so, and the operand is sent
2886
+ * as given.
2794
2887
  *
2795
2888
  * @internal
2796
2889
  */
@@ -2799,7 +2892,10 @@ export const containedOperand = (kind, value, element) => (operand) => {
2799
2892
  return element(operand);
2800
2893
  if (kind === "set") {
2801
2894
  const set = value(new Set([operand]))?.stored;
2802
- return set instanceof Set && set.size === 1 ? { stored: [...set][0] } : undefined;
2895
+ if (!(set instanceof Set) || set.size !== 1)
2896
+ return undefined;
2897
+ const member = [...set][0];
2898
+ return { stored: keepsAsGiven(operand, member) ? operand : member };
2803
2899
  }
2804
2900
  const text = value(operand);
2805
2901
  return typeof text?.stored === "string" ? text : undefined;
@@ -2899,7 +2995,12 @@ refTargets = {},
2899
2995
  */
2900
2996
  storable,
2901
2997
  /** The entity's READ schemas of the model's fields, which a row is decoded by (`readsAsGiven`). */
2902
- readFields = {}) => {
2998
+ readFields = {},
2999
+ /**
3000
+ * The storage the `DynamoModel.configure` of each ref's target gives its fields
3001
+ * (`configuredStorageOf`), keyed by ref field: the ref holds them in it, as its schemas do.
3002
+ */
3003
+ refStorage = {}) => {
2903
3004
  const recordFields = writeFields;
2904
3005
  const cache = new Map();
2905
3006
  const encoderFor = (key, resolve) => {
@@ -3061,6 +3162,18 @@ readFields = {}) => {
3061
3162
  }
3062
3163
  return current === undefined ? undefined : { schema: current, asDeclared };
3063
3164
  };
3165
+ /**
3166
+ * `original`, the model's schema at `segments` (below a field), as a write stores it: a field of a
3167
+ * ref's target in the storage its `DynamoModel.configure` gives it, which the ref holds it in
3168
+ * (`refStorage`), as the ref's own schemas do.
3169
+ */
3170
+ const storedSchema = (segments, original) => {
3171
+ const [head, name] = segments;
3172
+ const encoding = segments.length === 2 && typeof head === "string" && typeof name === "string"
3173
+ ? ownValue(ownValue(refStorage, head), name)
3174
+ : undefined;
3175
+ return substituteSchemaDeep(original, withConfiguredStorage({ enforceChecks: true }, name, original, encoding));
3176
+ };
3064
3177
  const schemaAt = (segments) => {
3065
3178
  const at = originalAt(segments);
3066
3179
  if (at === undefined)
@@ -3071,7 +3184,7 @@ readFields = {}) => {
3071
3184
  ? ownValue(recordFields, head)
3072
3185
  : at.asDeclared
3073
3186
  ? original
3074
- : substituteSchemaDeep(original, { enforceChecks: true });
3187
+ : storedSchema(segments, original);
3075
3188
  return stored === undefined ? undefined : { original, stored };
3076
3189
  };
3077
3190
  const validators = new Map();
@@ -3109,7 +3222,7 @@ readFields = {}) => {
3109
3222
  const [head, ...rest] = segments;
3110
3223
  if (typeof head !== "string")
3111
3224
  return undefined;
3112
- const top = refTargets[head] ?? modelFields[head];
3225
+ const top = ownValue(refTargets, head) ?? ownValue(modelFields, head);
3113
3226
  if (top === undefined)
3114
3227
  return undefined;
3115
3228
  const holdsNoPath = new Set([
@@ -3214,16 +3327,32 @@ readFields = {}) => {
3214
3327
  : declared.flatMap((position) => {
3215
3328
  const original = position.schema;
3216
3329
  const stored = segments.length === 1
3217
- ? recordFields[head]
3330
+ ? ownValue(recordFields, head)
3218
3331
  : position.asDeclared
3219
3332
  ? original
3220
- : substituteSchemaDeep(original, { enforceChecks: true });
3333
+ : storedSchema(segments, original);
3221
3334
  if (stored === undefined)
3222
3335
  return [];
3223
3336
  const formOf = (from, to) => {
3224
3337
  const encode = leafAwareEncoder(from, to);
3225
3338
  const decode = Schema.decodeUnknownExit(to);
3226
3339
  return (value) => {
3340
+ if (isPrimitive(value)) {
3341
+ // A primitive is put in its stored form only where that is a primitive of
3342
+ // another type (`5` where a `NumberFromString` stores `"5"`), and is sent as
3343
+ // given anywhere else, as up to 1.41.0 ({@link keepsAsGiven}).
3344
+ const encoded = encode(value);
3345
+ const issue = issueOf(() => decode(asStored(encoded)));
3346
+ if (issue === CANT_SAY)
3347
+ return { stored: value };
3348
+ if (issue !== undefined)
3349
+ return undefined;
3350
+ if (keepsAsGiven(value, encoded))
3351
+ return { stored: value };
3352
+ return storable === undefined || storable(encoded)
3353
+ ? { stored: encoded }
3354
+ : undefined;
3355
+ }
3227
3356
  const encoded = encode(value);
3228
3357
  if (decode(asStored(encoded))._tag !== "Success")
3229
3358
  return undefined;
@@ -3253,7 +3382,7 @@ readFields = {}) => {
3253
3382
  const [head, ...rest] = segments;
3254
3383
  if (typeof head !== "string")
3255
3384
  return [];
3256
- const start = refTargets[head] ?? modelFields[head];
3385
+ const start = ownValue(refTargets, head) ?? ownValue(modelFields, head);
3257
3386
  if (start === undefined)
3258
3387
  return [];
3259
3388
  const ways = [];
@@ -3453,7 +3582,9 @@ readFields = {}) => {
3453
3582
  };
3454
3583
  const reads = (clauses, strict) => {
3455
3584
  const head = clauses[0]?.segments[0];
3456
- const start = typeof head === "string" ? (refTargets[head] ?? modelFields[head]) : undefined;
3585
+ const start = typeof head === "string"
3586
+ ? (ownValue(refTargets, head) ?? ownValue(modelFields, head))
3587
+ : undefined;
3457
3588
  // A field the walk can't follow is one the schema can't judge.
3458
3589
  if (start === undefined || clauses.some((clause) => clause.segments[0] !== head)) {
3459
3590
  return !strict;