@effect-dynamodb/schema 1.42.0 → 1.42.1

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.
@@ -1124,6 +1124,8 @@ const childOptions = (container, deeper, index, opts) => {
1124
1124
  collidingKinds,
1125
1125
  ...(opts?.unionDateOverride ? { dateEncodingOverride: opts.unionDateOverride } : {}),
1126
1126
  ...(opts?.unionDateOverride && opts.readsDeclaredStorage ? { readsDeclaredStorage: true } : {}),
1127
+ // A nullable ref (`NullOr(Team)`): its struct member holds what the ref's entity holds.
1128
+ ...(opts?.configuredStorage ? { configuredStorage: opts.configuredStorage } : {}),
1127
1129
  };
1128
1130
  };
1129
1131
  /** The options an optional wrapper hands its inner schema: it is transparent. */
@@ -1189,13 +1191,17 @@ const decodingDefaultField = (field) => {
1189
1191
  const writesFirstMemberOnly = (opts) => !opts?.decodeOnly && !opts?.absentReads && !opts?.undefinedPositionsOnly;
1190
1192
  const throughOptional = (opts) => {
1191
1193
  const deeper = descend(opts);
1192
- return opts?.unionDateOverride || opts?.collidingKinds || opts?.dateEncodingOverride
1194
+ return opts?.unionDateOverride ||
1195
+ opts?.collidingKinds ||
1196
+ opts?.dateEncodingOverride ||
1197
+ opts?.configuredStorage
1193
1198
  ? {
1194
1199
  ...deeper,
1195
1200
  ...(opts.unionDateOverride ? { unionDateOverride: opts.unionDateOverride } : {}),
1196
1201
  ...(opts.collidingKinds ? { collidingKinds: opts.collidingKinds } : {}),
1197
1202
  ...(opts.dateEncodingOverride ? { dateEncodingOverride: opts.dateEncodingOverride } : {}),
1198
1203
  ...(opts.readsDeclaredStorage ? { readsDeclaredStorage: true } : {}),
1204
+ ...(opts.configuredStorage ? { configuredStorage: opts.configuredStorage } : {}),
1199
1205
  }
1200
1206
  : deeper;
1201
1207
  };
@@ -1881,7 +1887,7 @@ export const substituteSchemaDeep = (schema, opts) => {
1881
1887
  setOwn(subFields, name, fieldNamed(name, fieldOpt ? fieldOpt.rewrap(sub) : sub, field));
1882
1888
  }
1883
1889
  else {
1884
- const sub = substituteSchemaDeep(field, withConfiguredStorage(atField(deeper, opts, name), name, field, ownValue(opts?.configuredStorage, name)));
1890
+ const sub = substituteSchemaDeep(field, withConfiguredStorage(atField(deeper, opts, name), name, field, ownValue(opts?.configuredStorage?.fields, name), ownValue(opts?.configuredStorage?.refs, name)));
1885
1891
  setOwn(subFields, name, name === "__proto__"
1886
1892
  ? fieldNamed(name, sub, field)
1887
1893
  : opts?.absentReads
@@ -2152,8 +2158,8 @@ export const substituteSchemas = (modelFields, fieldEncodings, options) => {
2152
2158
  // 1. Self-date substitution. Pattern A: user declared `Schema.DateTimeUtc`
2153
2159
  // and chose a wire format via annotation. We substitute with the
2154
2160
  // tolerant date transform so the encode pipeline produces the right
2155
- // wire primitive.
2156
- if (isSelfDateSchema(schema)) {
2161
+ // wire primitive. An optional one keeps its wrapper (step 3).
2162
+ if (isSelfDateSchema(schema) && optionalField(schema) === undefined) {
2157
2163
  const enc = ownValue(fieldEncodings, name);
2158
2164
  if (enc) {
2159
2165
  setOwn(out, name, fieldNamed(name, buildDateTransform(enc), schema, storedAsProto));
@@ -2185,13 +2191,9 @@ export const substituteSchemas = (modelFields, fieldEncodings, options) => {
2185
2191
  // — `substituteSchemaDeep` substitutes those while preserving class
2186
2192
  // identity, and returns the schema unchanged when nothing nested needs it.
2187
2193
  // A configured storage override on a UNION field (`NullOr(DateTimeUtc)`)
2188
- // is applied to the union's self-date member (#133).
2189
- const unionOverride = unionDateOverride(name, schema, ownValue(fieldEncodings, name));
2190
- setOwn(out, name, fieldNamed(name, substituteSchemaDeep(schema, {
2191
- ...deep,
2192
- path: [name],
2193
- ...(unionOverride === undefined ? {} : { unionDateOverride: unionOverride }),
2194
- }), schema, storedAsProto));
2194
+ // is applied to the union's self-date member (#133), and on an optional
2195
+ // date to the date beneath the wrapper (`withConfiguredStorage`).
2196
+ setOwn(out, name, fieldNamed(name, substituteSchemaDeep(schema, withConfiguredStorage({ ...deep, path: [name] }, name, schema, ownValue(fieldEncodings, name))), schema, storedAsProto));
2195
2197
  }
2196
2198
  if (options?.absentReads) {
2197
2199
  for (const [name, field] of Object.entries(out)) {
@@ -2230,8 +2232,7 @@ const unionDateOverride = (name, schema, encoding) => {
2230
2232
  };
2231
2233
  /**
2232
2234
  * @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
+ * by name, or `undefined` for a model with none.
2235
2236
  */
2236
2237
  export const configuredStorageOf = (model) => {
2237
2238
  if (!isConfiguredModel(model))
@@ -2243,20 +2244,73 @@ export const configuredStorageOf = (model) => {
2243
2244
  }
2244
2245
  return Object.keys(storage).length === 0 ? undefined : storage;
2245
2246
  };
2247
+ /** Each entity's {@link HeldStorage}, by its derived schemas (shared by its pure and runtime forms). */
2248
+ const heldStorages = new WeakMap();
2246
2249
  /**
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`).
2250
+ * @internal Record what an entity holds (`fields`, the storage its schemas apply to its own fields;
2251
+ * `refs`, by ref field, each entity it refs), when it's made. An entity can only ref one made
2252
+ * before it, so the tree is finite.
2253
+ */
2254
+ export const holdStorage = (schemas, fields, refs) => {
2255
+ const held = {};
2256
+ for (const ref of refs) {
2257
+ const storage = heldStorageOf(ref.refEntity);
2258
+ if (storage !== undefined)
2259
+ setOwn(held, ref.fieldName, storage);
2260
+ }
2261
+ heldStorages.set(schemas, { fields: fields ?? {}, refs: held });
2262
+ };
2263
+ /**
2264
+ * @internal The {@link HeldStorage} of a ref's entity, or `undefined` where it holds every field in
2265
+ * the default storage. An entity this module didn't make is taken by its model's `configure`.
2252
2266
  */
2253
- const withConfiguredStorage = (options, name, field, encoding) => {
2267
+ export const heldStorageOf = (entity) => {
2268
+ const { schemas, model } = (entity ?? {});
2269
+ const held = typeof schemas === "object" && schemas !== null ? heldStorages.get(schemas) : undefined;
2270
+ if (held === undefined) {
2271
+ const fields = configuredStorageOf(model);
2272
+ return fields === undefined ? undefined : { fields, refs: {} };
2273
+ }
2274
+ return Object.keys(held.fields).length === 0 && Object.keys(held.refs).length === 0
2275
+ ? undefined
2276
+ : held;
2277
+ };
2278
+ /**
2279
+ * @internal Whether the storage `DynamoModel.configure` gives this field was left unapplied up to
2280
+ * 1.41.0: a date beneath an optional wrapper that is a union (`Schema.optional(date)`,
2281
+ * `Schema.optionalKey(Schema.NullOr(date))`), which those releases stored as ISO text.
2282
+ */
2283
+ export const configuredStorageWasIgnored = (field) => {
2284
+ const optional = optionalField(field);
2285
+ return optional !== undefined && !isSelfDateSchema(field) && isSelfDateSchema(optional.inner);
2286
+ };
2287
+ /**
2288
+ * A field's substitution options with the storage `encoding` its model's `DynamoModel.configure`
2289
+ * gives it: a date, the date beneath an optional wrapper (`Schema.optionalKey`, `Schema.optional`,
2290
+ * the first member of an `optionalKey` union, as `optionalKey(NullOr(date))`), or beneath a
2291
+ * decoding default, or a union's date member. The wrapper is kept as declared. A field holding a
2292
+ * ref takes what that ref's entity holds (`refs`). Elsewhere, and without either, `options` as
2293
+ * given. A read (`legacyReads`) also takes the form the date's own storage writes
2294
+ * (`readsDeclaredStorage`). Up to 1.41.0 an entity's own `optional` date and the date of an
2295
+ * `optionalKey(NullOr(…))` were stored as ISO text, and an `optionalKey` date became required.
2296
+ */
2297
+ const withConfiguredStorage = (options, name, field, encoding, refs) => {
2298
+ if (refs !== undefined)
2299
+ return { ...options, configuredStorage: refs };
2254
2300
  if (encoding === undefined)
2255
2301
  return options;
2256
2302
  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 };
2303
+ const optional = optionalField(field);
2304
+ const date = optional?.inner ?? decodingDefaultField(field)?.inner ?? field;
2305
+ if (isSelfDateSchema(date)) {
2306
+ // A read takes the whole union beneath `optionalKey`, its date member in this storage too.
2307
+ const union = optional?.union === undefined ? undefined : unionDateOverride(name, optional.union, encoding);
2308
+ return {
2309
+ ...options,
2310
+ dateEncodingOverride: encoding,
2311
+ ...(union === undefined ? {} : { unionDateOverride: union }),
2312
+ ...reads,
2313
+ };
2260
2314
  }
2261
2315
  const union = unionDateOverride(name, field, encoding);
2262
2316
  return union === undefined ? options : { ...options, unionDateOverride: union, ...reads };
@@ -2419,9 +2473,9 @@ protoRenamed = false) => {
2419
2473
  const optional = optionalField(ownValue(modelFields, ref.fieldName));
2420
2474
  const asDeclared = (target) => optional === undefined ? target : optional.rewrap(target);
2421
2475
  // 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);
2476
+ // in (`refDataOf`), and its refs' fields in theirs. Up to 1.41.0 the default: a date stored as
2477
+ // epoch seconds read back as milliseconds, and a condition or `pathSet` on it sent ISO text.
2478
+ const storage = heldStorageOf(ref.refEntity);
2425
2479
  const configured = storage === undefined ? {} : { configuredStorage: storage };
2426
2480
  // What a read of the target returns (`refHeldSchema`): its required hidden fields left out.
2427
2481
  setOwn(fields, ref.fieldName, asDeclared(refHeldSchema(resolved, { enforceChecks: true, ...configured })));
@@ -2997,8 +3051,8 @@ storable,
2997
3051
  /** The entity's READ schemas of the model's fields, which a row is decoded by (`readsAsGiven`). */
2998
3052
  readFields = {},
2999
3053
  /**
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.
3054
+ * What each ref's entity holds its fields in (`heldStorageOf`), its own refs' included, keyed by
3055
+ * ref field: the ref holds them in it, as its schemas do.
3002
3056
  */
3003
3057
  refStorage = {}) => {
3004
3058
  const recordFields = writeFields;
@@ -3165,14 +3219,17 @@ refStorage = {}) => {
3165
3219
  /**
3166
3220
  * `original`, the model's schema at `segments` (below a field), as a write stores it: a field of a
3167
3221
  * 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.
3222
+ * (`refStorage`), as the ref's own schemas do, and a field of a ref that target holds in turn in
3223
+ * its entity's.
3169
3224
  */
3170
3225
  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));
3226
+ const name = segments[segments.length - 1];
3227
+ let held = typeof segments[0] === "string" ? ownValue(refStorage, segments[0]) : undefined;
3228
+ for (const segment of segments.slice(1, -1)) {
3229
+ held = typeof segment === "string" ? ownValue(held?.refs, segment) : undefined;
3230
+ }
3231
+ const named = typeof name === "string";
3232
+ return substituteSchemaDeep(original, withConfiguredStorage({ enforceChecks: true }, name, original, named ? ownValue(held?.fields, name) : undefined, named ? ownValue(held?.refs, name) : undefined));
3176
3233
  };
3177
3234
  const schemaAt = (segments) => {
3178
3235
  const at = originalAt(segments);