effect-dynamodb 1.39.0 → 1.41.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 (190) hide show
  1. package/README.md +1 -1
  2. package/dist/Aggregate.d.ts +138 -16
  3. package/dist/Aggregate.d.ts.map +1 -1
  4. package/dist/Aggregate.js +4298 -725
  5. package/dist/Aggregate.js.map +1 -1
  6. package/dist/Batch.d.ts +9 -5
  7. package/dist/Batch.d.ts.map +1 -1
  8. package/dist/Batch.js +28 -44
  9. package/dist/Batch.js.map +1 -1
  10. package/dist/Collection.d.ts.map +1 -1
  11. package/dist/Collection.js +16 -9
  12. package/dist/Collection.js.map +1 -1
  13. package/dist/ConsumedCapacity.js +1 -1
  14. package/dist/ConsumedCapacity.js.map +1 -1
  15. package/dist/DynamoClient.d.ts +44 -21
  16. package/dist/DynamoClient.d.ts.map +1 -1
  17. package/dist/DynamoClient.js +342 -124
  18. package/dist/DynamoClient.js.map +1 -1
  19. package/dist/Entity.d.ts +246 -41
  20. package/dist/Entity.d.ts.map +1 -1
  21. package/dist/Entity.js +6818 -1166
  22. package/dist/Entity.js.map +1 -1
  23. package/dist/EventStore.d.ts +37 -15
  24. package/dist/EventStore.d.ts.map +1 -1
  25. package/dist/EventStore.js +403 -163
  26. package/dist/EventStore.js.map +1 -1
  27. package/dist/Expression.d.ts.map +1 -1
  28. package/dist/Expression.js +40 -24
  29. package/dist/Expression.js.map +1 -1
  30. package/dist/Marshaller.d.ts +72 -11
  31. package/dist/Marshaller.d.ts.map +1 -1
  32. package/dist/Marshaller.js +625 -71
  33. package/dist/Marshaller.js.map +1 -1
  34. package/dist/Query.d.ts +103 -5
  35. package/dist/Query.d.ts.map +1 -1
  36. package/dist/Query.js +262 -15
  37. package/dist/Query.js.map +1 -1
  38. package/dist/StorageEncoding.d.ts +7 -1
  39. package/dist/StorageEncoding.d.ts.map +1 -1
  40. package/dist/StorageEncoding.js +8 -2
  41. package/dist/StorageEncoding.js.map +1 -1
  42. package/dist/Table.d.ts +338 -16
  43. package/dist/Table.d.ts.map +1 -1
  44. package/dist/Table.js +30 -2
  45. package/dist/Table.js.map +1 -1
  46. package/dist/Transaction.d.ts.map +1 -1
  47. package/dist/Transaction.js +70 -7
  48. package/dist/Transaction.js.map +1 -1
  49. package/dist/VectorSearchEmulation.d.ts.map +1 -1
  50. package/dist/VectorSearchEmulation.js +4 -3
  51. package/dist/VectorSearchEmulation.js.map +1 -1
  52. package/dist/change-feed/ChangeFeed.d.ts +8 -3
  53. package/dist/change-feed/ChangeFeed.d.ts.map +1 -1
  54. package/dist/change-feed/ChangeFeed.js +76 -9
  55. package/dist/change-feed/ChangeFeed.js.map +1 -1
  56. package/dist/change-feed/CheckpointStore.js +1 -1
  57. package/dist/change-feed/CheckpointStore.js.map +1 -1
  58. package/dist/change-feed/DynamoStreamsClient.d.ts +19 -10
  59. package/dist/change-feed/DynamoStreamsClient.d.ts.map +1 -1
  60. package/dist/change-feed/DynamoStreamsClient.js +24 -14
  61. package/dist/change-feed/DynamoStreamsClient.js.map +1 -1
  62. package/dist/index.d.ts +2 -2
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +1 -1
  65. package/dist/index.js.map +1 -1
  66. package/dist/internal/BoundCrud.d.ts +78 -32
  67. package/dist/internal/BoundCrud.d.ts.map +1 -1
  68. package/dist/internal/BoundCrud.js +25 -0
  69. package/dist/internal/BoundCrud.js.map +1 -1
  70. package/dist/internal/BoundQuery.d.ts +46 -3
  71. package/dist/internal/BoundQuery.d.ts.map +1 -1
  72. package/dist/internal/BoundQuery.js +402 -37
  73. package/dist/internal/BoundQuery.js.map +1 -1
  74. package/dist/internal/BoundVectorQuery.d.ts +6 -0
  75. package/dist/internal/BoundVectorQuery.d.ts.map +1 -1
  76. package/dist/internal/BoundVectorQuery.js +7 -4
  77. package/dist/internal/BoundVectorQuery.js.map +1 -1
  78. package/dist/internal/Capacity.d.ts +46 -5
  79. package/dist/internal/Capacity.d.ts.map +1 -1
  80. package/dist/internal/Capacity.js +177 -26
  81. package/dist/internal/Capacity.js.map +1 -1
  82. package/dist/internal/ClientOptions.d.ts +144 -0
  83. package/dist/internal/ClientOptions.d.ts.map +1 -0
  84. package/dist/internal/ClientOptions.js +290 -0
  85. package/dist/internal/ClientOptions.js.map +1 -0
  86. package/dist/internal/ConditionHolds.d.ts +29 -0
  87. package/dist/internal/ConditionHolds.d.ts.map +1 -0
  88. package/dist/internal/ConditionHolds.js +453 -0
  89. package/dist/internal/ConditionHolds.js.map +1 -0
  90. package/dist/internal/Cursor.d.ts +19 -10
  91. package/dist/internal/Cursor.d.ts.map +1 -1
  92. package/dist/internal/Cursor.js +25 -10
  93. package/dist/internal/Cursor.js.map +1 -1
  94. package/dist/internal/Digest.d.ts +20 -0
  95. package/dist/internal/Digest.d.ts.map +1 -0
  96. package/dist/internal/Digest.js +106 -0
  97. package/dist/internal/Digest.js.map +1 -0
  98. package/dist/internal/DynamoArithmetic.d.ts +33 -0
  99. package/dist/internal/DynamoArithmetic.d.ts.map +1 -0
  100. package/dist/internal/DynamoArithmetic.js +112 -0
  101. package/dist/internal/DynamoArithmetic.js.map +1 -0
  102. package/dist/internal/EntityCombinators.d.ts +23 -8
  103. package/dist/internal/EntityCombinators.d.ts.map +1 -1
  104. package/dist/internal/EntityCombinators.js +6 -2
  105. package/dist/internal/EntityCombinators.js.map +1 -1
  106. package/dist/internal/EntityOps.d.ts +69 -8
  107. package/dist/internal/EntityOps.d.ts.map +1 -1
  108. package/dist/internal/EntityOps.js.map +1 -1
  109. package/dist/internal/EventProjection.d.ts +18 -4
  110. package/dist/internal/EventProjection.d.ts.map +1 -1
  111. package/dist/internal/EventProjection.js +36 -8
  112. package/dist/internal/EventProjection.js.map +1 -1
  113. package/dist/internal/Expr.d.ts +72 -2
  114. package/dist/internal/Expr.d.ts.map +1 -1
  115. package/dist/internal/Expr.js +264 -5
  116. package/dist/internal/Expr.js.map +1 -1
  117. package/dist/internal/ExpressionLimits.d.ts +57 -1
  118. package/dist/internal/ExpressionLimits.d.ts.map +1 -1
  119. package/dist/internal/ExpressionLimits.js +97 -4
  120. package/dist/internal/ExpressionLimits.js.map +1 -1
  121. package/dist/internal/FormGuard.d.ts +52 -0
  122. package/dist/internal/FormGuard.d.ts.map +1 -0
  123. package/dist/internal/FormGuard.js +83 -0
  124. package/dist/internal/FormGuard.js.map +1 -0
  125. package/dist/internal/Hydrate.d.ts.map +1 -1
  126. package/dist/internal/Hydrate.js +2 -1
  127. package/dist/internal/Hydrate.js.map +1 -1
  128. package/dist/internal/InputForm.d.ts +24 -0
  129. package/dist/internal/InputForm.d.ts.map +1 -0
  130. package/dist/internal/InputForm.js +44 -0
  131. package/dist/internal/InputForm.js.map +1 -0
  132. package/dist/internal/InsertStatement.d.ts +15 -0
  133. package/dist/internal/InsertStatement.d.ts.map +1 -0
  134. package/dist/internal/InsertStatement.js +18 -0
  135. package/dist/internal/InsertStatement.js.map +1 -0
  136. package/dist/internal/ItemSize.d.ts +108 -4
  137. package/dist/internal/ItemSize.d.ts.map +1 -1
  138. package/dist/internal/ItemSize.js +190 -3
  139. package/dist/internal/ItemSize.js.map +1 -1
  140. package/dist/internal/MultiProjection.d.ts +7 -2
  141. package/dist/internal/MultiProjection.d.ts.map +1 -1
  142. package/dist/internal/MultiProjection.js +18 -12
  143. package/dist/internal/MultiProjection.js.map +1 -1
  144. package/dist/internal/OperandReaders.d.ts +36 -0
  145. package/dist/internal/OperandReaders.d.ts.map +1 -0
  146. package/dist/internal/OperandReaders.js +176 -0
  147. package/dist/internal/OperandReaders.js.map +1 -0
  148. package/dist/internal/Packed.d.ts.map +1 -1
  149. package/dist/internal/Packed.js +11 -10
  150. package/dist/internal/Packed.js.map +1 -1
  151. package/dist/internal/PathBuilder.d.ts +57 -8
  152. package/dist/internal/PathBuilder.d.ts.map +1 -1
  153. package/dist/internal/PathBuilder.js +98 -24
  154. package/dist/internal/PathBuilder.js.map +1 -1
  155. package/dist/internal/ProjectionEngine.d.ts +196 -3
  156. package/dist/internal/ProjectionEngine.d.ts.map +1 -1
  157. package/dist/internal/ProjectionEngine.js +1251 -86
  158. package/dist/internal/ProjectionEngine.js.map +1 -1
  159. package/dist/internal/Rekey.d.ts +187 -21
  160. package/dist/internal/Rekey.d.ts.map +1 -1
  161. package/dist/internal/Rekey.js +1349 -63
  162. package/dist/internal/Rekey.js.map +1 -1
  163. package/dist/internal/ReturnedItem.d.ts +23 -0
  164. package/dist/internal/ReturnedItem.d.ts.map +1 -0
  165. package/dist/internal/ReturnedItem.js +39 -0
  166. package/dist/internal/ReturnedItem.js.map +1 -0
  167. package/dist/internal/RoundTrip.d.ts +45 -0
  168. package/dist/internal/RoundTrip.d.ts.map +1 -0
  169. package/dist/internal/RoundTrip.js +106 -0
  170. package/dist/internal/RoundTrip.js.map +1 -0
  171. package/dist/internal/SentRequest.d.ts +14 -0
  172. package/dist/internal/SentRequest.d.ts.map +1 -0
  173. package/dist/internal/SentRequest.js +31 -0
  174. package/dist/internal/SentRequest.js.map +1 -0
  175. package/dist/internal/TransactPlan.d.ts.map +1 -1
  176. package/dist/internal/TransactPlan.js +4 -0
  177. package/dist/internal/TransactPlan.js.map +1 -1
  178. package/dist/internal/TransactWriteOps.d.ts +70 -15
  179. package/dist/internal/TransactWriteOps.d.ts.map +1 -1
  180. package/dist/internal/TransactWriteOps.js +166 -112
  181. package/dist/internal/TransactWriteOps.js.map +1 -1
  182. package/dist/internal/TransactableOps.d.ts +230 -27
  183. package/dist/internal/TransactableOps.d.ts.map +1 -1
  184. package/dist/internal/TransactableOps.js +549 -92
  185. package/dist/internal/TransactableOps.js.map +1 -1
  186. package/dist/internal/WriteDiagnosis.d.ts +66 -3
  187. package/dist/internal/WriteDiagnosis.d.ts.map +1 -1
  188. package/dist/internal/WriteDiagnosis.js +261 -15
  189. package/dist/internal/WriteDiagnosis.js.map +1 -1
  190. package/package.json +5 -5
@@ -13,33 +13,90 @@
13
13
  * - Terminals: `fetch`, `collect`, `paginate`, `count`; `explain` describes
14
14
  * the plan without sending anything.
15
15
  */
16
+ import { ownValue, setOwn } from "@effect-dynamodb/schema/internal/OwnNames.js";
16
17
  import * as KeyComposer from "@effect-dynamodb/schema/KeyComposer.js";
17
18
  import { Effect, Stream } from "effect";
19
+ import { toAttributeValue } from "../Marshaller.js";
18
20
  import * as Query from "../Query.js";
19
21
  import * as Capacity from "./Capacity.js";
20
- import { ExprTag, parseSimpleShorthand } from "./Expr.js";
22
+ import { ExprTag, operandsAsStored, parseSimpleShorthand } from "./Expr.js";
23
+ import { entryOfPath, storedPath, withEntry, } from "./PathBuilder.js";
21
24
  /**
22
25
  * An expression with every attribute path's top-level field renamed to its
23
- * stored attribute (#133). Values are left alone.
26
+ * stored attribute (#133), and the segments below it named as `below` names
27
+ * them (#238). Values are left alone.
24
28
  */
25
- const renamePaths = (node, resolve) => {
29
+ const renamePaths = (node, resolve, below) => {
26
30
  if (Array.isArray(node))
27
- return node.map((item) => renamePaths(item, resolve));
31
+ return node.map((item) => renamePaths(item, resolve, below));
28
32
  if (node === null || typeof node !== "object")
29
33
  return node;
30
34
  const record = node;
31
35
  if (record._tag === "value")
32
36
  return node;
33
37
  if ((record._tag === "path" || record._tag === "size") && Array.isArray(record.segments)) {
34
- const [head, ...rest] = record.segments;
35
- return { ...record, segments: [typeof head === "string" ? resolve(head) : head, ...rest] };
38
+ return {
39
+ ...record,
40
+ segments: storedPath(record.segments, resolve, below),
41
+ };
36
42
  }
37
43
  // Spread keeps the symbol-keyed Expr brand; string keys are walked.
38
44
  const copy = { ...record };
39
45
  for (const [key, value] of Object.entries(record))
40
- copy[key] = renamePaths(value, resolve);
46
+ copy[key] = renamePaths(value, resolve, below);
41
47
  return copy;
42
48
  };
49
+ /**
50
+ * An expression with each attribute path's segments as `rename` gives them: the same expression,
51
+ * by identity, where `rename` gives every path's own segments back.
52
+ */
53
+ const mapPaths = (node, rename) => {
54
+ if (Array.isArray(node)) {
55
+ const mapped = node.map((item) => mapPaths(item, rename));
56
+ return mapped.every((item, i) => item === node[i]) ? node : mapped;
57
+ }
58
+ if (node === null || typeof node !== "object")
59
+ return node;
60
+ const record = node;
61
+ if (record._tag === "value")
62
+ return node;
63
+ if ((record._tag === "path" || record._tag === "size") && Array.isArray(record.segments)) {
64
+ const segments = rename(record.segments);
65
+ return segments === record.segments ? node : { ...record, segments };
66
+ }
67
+ // Spread keeps the symbol-keyed Expr brand; string keys are walked.
68
+ let copy;
69
+ for (const [key, value] of Object.entries(record)) {
70
+ const mapped = mapPaths(value, rename);
71
+ if (mapped === value)
72
+ continue;
73
+ copy ??= { ...record };
74
+ copy[key] = mapped;
75
+ }
76
+ return copy ?? node;
77
+ };
78
+ /** Every path an expression names, in order, as its segments. */
79
+ const pathsIn = (node, into = []) => {
80
+ if (Array.isArray(node)) {
81
+ for (const item of node)
82
+ pathsIn(item, into);
83
+ return into;
84
+ }
85
+ if (node === null || typeof node !== "object")
86
+ return into;
87
+ const record = node;
88
+ if (record._tag === "value")
89
+ return into;
90
+ if ((record._tag === "path" || record._tag === "size") && Array.isArray(record.segments)) {
91
+ into.push(record.segments);
92
+ return into;
93
+ }
94
+ for (const value of Object.values(record))
95
+ pathsIn(value, into);
96
+ return into;
97
+ };
98
+ /** Whether `below` names a segment below a path's first other than as written (#238). */
99
+ const renamedBelow = (path, below) => below?.(path).some((segment, i) => segment !== path[i + 1]) === true;
43
100
  /**
44
101
  * Build the runtime `SkConditionOps` for one `.where()` invocation.
45
102
  *
@@ -122,13 +179,17 @@ const serializeRawCondition = (condition) => {
122
179
  const buildSkAccessor = (fields) => {
123
180
  const acc = {};
124
181
  for (const f of fields)
125
- acc[f] = f;
182
+ setOwn(acc, f, f);
126
183
  return acc;
127
184
  };
128
185
  /** The plain projection: the domain names are the stored names. */
129
186
  const plainSelect = (query, paths, form) => form === "attributes"
130
187
  ? Query.select(query, paths.map((path) => String(path[0])))
131
188
  : Query.selectPaths(query, paths);
189
+ /** `paths` without repeats, in order. */
190
+ const uniquePaths = (paths) => [
191
+ ...new Map(paths.map((path) => [JSON.stringify(path), path])).values(),
192
+ ];
132
193
  /** The top-level field names an expression's paths name. */
133
194
  const pathHeads = (node, into = new Set()) => {
134
195
  if (Array.isArray(node)) {
@@ -151,19 +212,164 @@ const pathHeads = (node, into = new Set()) => {
151
212
  pathHeads(value, into);
152
213
  return into;
153
214
  };
215
+ /** @internal An entity's {@link WholeRows}, or `undefined` when it has no sparse map field. */
216
+ export const wholeRowsOf = (entity) => entity._sparseFieldNames === undefined ||
217
+ entity._sparseFieldNames.length === 0 ||
218
+ entity._sparseMapOf === undefined ||
219
+ entity._toDomainView === undefined ||
220
+ entity._sparseEntryAttribute === undefined ||
221
+ entity._sparseWholeAttribute === undefined
222
+ ? undefined
223
+ : {
224
+ sparseFields: entity._sparseFieldNames,
225
+ mapOf: entity._sparseMapOf,
226
+ view: entity._toDomainView,
227
+ entryForms: entity._entryForms ?? ((expr) => expr),
228
+ entryAttribute: entity._sparseEntryAttribute,
229
+ wholeAttribute: entity._sparseWholeAttribute,
230
+ };
231
+ /** @internal Whether a selected path names one of `sparseFields` (its first segment, as a field). */
232
+ const namesMap = (path, sparseFields) => typeof path[0] === "string" && sparseFields.includes(path[0]);
233
+ /**
234
+ * @internal A selected path as its map's field names it (#238): a path `.entry(key)` built is
235
+ * `[field, key, ...below]`, so a select of an entry returns the map trimmed to that entry, as the
236
+ * read rebuilds it. Any other path as given.
237
+ */
238
+ const asMapPath = (path) => {
239
+ const entry = entryOfPath(path);
240
+ return entry === undefined ? path : [entry.field, entry.key, ...path.slice(1)];
241
+ };
242
+ /** @internal The entry a selected path names through its map field (`[field, key, ...]`), if any. */
243
+ const mapEntryOf = (path, sparseFields) => namesMap(path, sparseFields) && typeof path[1] === "string"
244
+ ? { field: path[0], key: path[1] }
245
+ : undefined;
246
+ /**
247
+ * @internal The stored paths a projection of `path` names, for an entity whose sparse maps
248
+ * `wholeRows` describes (#238): an entry, each place a row may hold it, with the segments below it
249
+ * as stored; any other path as {@link storedPath} names it.
250
+ */
251
+ const projectedPaths = (wholeRows, path, storedOf) => {
252
+ const entry = wholeRows === undefined ? undefined : mapEntryOf(path, wholeRows.sparseFields);
253
+ if (entry === undefined)
254
+ return [storedOf(path)];
255
+ const whole = wholeRows.wholeAttribute(entry.field);
256
+ return [
257
+ [wholeRows.entryAttribute(entry.field, entry.key), ...storedOf(path).slice(2)],
258
+ ...(whole === undefined ? [] : [[whole]]),
259
+ ];
260
+ };
261
+ /**
262
+ * @internal What a whole row gives a select (#238), for one entity: each path naming a sparse map,
263
+ * from the map as a read rebuilds it out of the row's attributes for that map alone, trimmed to the
264
+ * paths ({@link Query.projectRow}); and every other path as its projection gave it, under the name
265
+ * it was selected by, as a projected select hands it back ({@link Query.selectRenamed}): a path
266
+ * naming a stored attribute (`nt`, an entry's `m#k`) gets that attribute.
267
+ *
268
+ * `entriesProjected`: the row is what a projection of the selected entries gave (an entry select),
269
+ * so each entry's attribute holds only what was selected below it, its lists closed up as DynamoDB
270
+ * closes them. A map rebuilt from entries is trimmed only to the selected keys, never again by
271
+ * position below them; one rebuilt from the attribute a `field:` rename stores it under whole, which
272
+ * the projection names whole, is trimmed to the paths.
273
+ */
274
+ const fromWholeRow = (wholeRows, paths, resolve, projected, raw, below, entriesProjected = false) => {
275
+ const storedOf = (path) => storedPath(path, resolve, below);
276
+ const maps = paths.filter((path) => namesMap(path, wholeRows.sparseFields));
277
+ const selected = new Set(maps.map((path) => String(path[0])));
278
+ const held = {};
279
+ for (const attribute of Object.keys(raw)) {
280
+ const map = wholeRows.mapOf(attribute);
281
+ if (map !== undefined && selected.has(map))
282
+ held[attribute] = raw[attribute];
283
+ }
284
+ /** Whether the read takes map `field` from the attribute it's stored under whole. */
285
+ const readWhole = (field) => {
286
+ const whole = wholeRows.wholeAttribute(field);
287
+ return whole !== undefined && Object.hasOwn(held, whole);
288
+ };
289
+ // A map rebuilt holds its entries as stored: below the map, its paths by stored names (#238).
290
+ const rebuilt = maps.length === 0
291
+ ? {}
292
+ : Query.projectRow(wholeRows.view(held), maps.map((path) => {
293
+ const stored = [path[0], ...storedOf(path).slice(1)];
294
+ return entriesProjected && !readWhole(String(path[0])) ? stored.slice(0, 2) : stored;
295
+ }));
296
+ const plans = Query.respellings(paths, storedOf);
297
+ // In the order the paths were selected, as a projected row's names come.
298
+ const item = {};
299
+ for (const path of paths) {
300
+ const head = String(path[0]);
301
+ if (Object.hasOwn(item, head))
302
+ continue;
303
+ const [from, name] = namesMap(path, wholeRows.sparseFields)
304
+ ? [rebuilt, head]
305
+ : [projected, String(storedOf(path)[0])];
306
+ if (!Object.hasOwn(from, name) || from[name] === undefined)
307
+ continue;
308
+ Object.defineProperty(item, head, {
309
+ value: Query.respelled(from[name], plans.get(head)),
310
+ enumerable: true,
311
+ writable: true,
312
+ configurable: true,
313
+ });
314
+ }
315
+ return item;
316
+ };
317
+ /**
318
+ * @internal Whether a select of `paths` reads whole rows (#238): one of them names a sparse map
319
+ * field whole, stored one attribute per entry, which a `ProjectionExpression` can't name by
320
+ * prefix. A select of an entry (`.entry(key)`, or the map's field with a key below it) projects
321
+ * the attributes the entry is stored under instead. A path naming the item itself is left to the
322
+ * projection, which DynamoDB refuses.
323
+ */
324
+ const readsWholeRows = (paths, sparseFields) => paths.every((path) => path.length > 0) &&
325
+ paths.some((path) => namesMap(path, sparseFields) && mapEntryOf(path, sparseFields) === undefined);
154
326
  /**
155
327
  * @internal An entity's naming (#133): filters and selects name the stored
156
328
  * attribute of each renamed field. A select of no renamed field stays the
157
329
  * plain projection, so an entity without renames sends what it always sent.
330
+ * With `storedAt`, a filter's operand that is one of Effect's instances is
331
+ * sent in the form the attribute holds (`operandsAsStored`, #242), judged by
332
+ * the domain path before it's renamed. A select naming a sparse map field
333
+ * reads whole rows and trims each to what it selected, as the read rebuilds it
334
+ * ({@link WholeRows}, #238).
158
335
  */
159
- export const entityNaming = (resolve) => resolve === undefined
160
- ? {}
161
- : {
162
- renameExpr: (expr) => renamePaths(expr, resolve),
163
- selectAs: (query, paths, form) => paths.some((path) => typeof path[0] === "string" && resolve(path[0]) !== path[0])
164
- ? Query.selectRenamed(query, paths, resolve)
165
- : plainSelect(query, paths, form),
336
+ export const entityNaming = (resolve, storedAt, wholeRows, below) => {
337
+ if (resolve === undefined &&
338
+ storedAt === undefined &&
339
+ wholeRows === undefined &&
340
+ below === undefined)
341
+ return {};
342
+ const stored = resolve ?? ((name) => name);
343
+ const asStored = (expr) => storedAt === undefined ? expr : operandsAsStored(expr, storedAt);
344
+ const forms = wholeRows?.entryForms ?? ((expr) => expr);
345
+ return {
346
+ ...((resolve !== undefined || storedAt !== undefined || below !== undefined) && {
347
+ renameExpr: (expr) => resolve === undefined && below === undefined
348
+ ? asStored(expr)
349
+ : renamePaths(forms(asStored(expr)), stored, below),
350
+ }),
351
+ ...((resolve !== undefined || wholeRows !== undefined || below !== undefined) && {
352
+ selectAs: (query, written, form) => {
353
+ const paths = written.map(asMapPath);
354
+ if (wholeRows !== undefined && readsWholeRows(paths, wholeRows.sparseFields)) {
355
+ return Query.selectWhole(query, paths
356
+ .filter((path) => !namesMap(path, wholeRows.sparseFields))
357
+ .map((path) => storedPath(path, stored, below)), (projected, raw) => fromWholeRow(wholeRows, paths, stored, projected, raw, below));
358
+ }
359
+ // An entry selected: its attributes projected, and the map rebuilt from what they hold.
360
+ if (wholeRows !== undefined &&
361
+ paths.some((path) => mapEntryOf(path, wholeRows.sparseFields) !== undefined)) {
362
+ const storedOf = (path) => storedPath(path, stored, below);
363
+ return Query.selectProjected(query, uniquePaths(paths.flatMap((path) => projectedPaths(wholeRows, path, storedOf))), (raw) => fromWholeRow(wholeRows, paths, stored, raw, raw, below, true));
364
+ }
365
+ return paths.some((path) => (typeof path[0] === "string" && stored(path[0]) !== path[0]) ||
366
+ renamedBelow(path, below))
367
+ ? Query.selectRenamed(query, paths, stored, below)
368
+ : plainSelect(query, paths, form);
369
+ },
370
+ }),
166
371
  };
372
+ };
167
373
  /**
168
374
  * @internal A collection's naming (#133). Its members may store one domain
169
375
  * field under different names, so a filter is judged per member — an OR over
@@ -190,21 +396,65 @@ export const collectionNaming = (members) => {
190
396
  }
191
397
  return field;
192
398
  };
399
+ /** A path below its field, as a member stores it (#238). */
400
+ const belowOf = (m, path) => JSON.stringify(m.below === undefined ? path.slice(1) : m.below(path));
401
+ /**
402
+ * An expression with each path `.entry()` built named as member `m` stores the entry (#238): its
403
+ * own entry attribute where it declares the map sparse (members may give one map different
404
+ * prefixes); the map's key below the field where it declares the map otherwise; and where it
405
+ * doesn't declare the map, the attribute as built, as a field a member lacks: an attribute it
406
+ * never holds when it reads that attribute as something of its own (a field, or an entry of
407
+ * one of its maps), so a filter on the entry finds nothing on its rows.
408
+ */
409
+ const entriesFor = (m, expr) => mapPaths(expr, (segments) => {
410
+ const entry = entryOfPath(segments);
411
+ if (entry === undefined)
412
+ return segments;
413
+ const rest = segments.slice(1);
414
+ if (m.wholeRows?.sparseFields.includes(entry.field) === true) {
415
+ const attribute = m.wholeRows.entryAttribute(entry.field, entry.key);
416
+ return attribute === segments[0] ? segments : withEntry([attribute, ...rest], entry);
417
+ }
418
+ if (m.fields === undefined || m.fields.has(entry.field)) {
419
+ return [entry.field, entry.key, ...rest];
420
+ }
421
+ const attribute = String(segments[0]);
422
+ return m.fields.has(attribute) ||
423
+ attributeOf(m, attribute) !== attribute ||
424
+ m.wholeRows?.mapOf(attribute) !== undefined
425
+ ? [ABSENT_ATTRIBUTE]
426
+ : [attribute, ...rest];
427
+ });
428
+ /** Each head's respelling of what a member's projection gives for `paths` (#238). */
429
+ const respellingsOf = (m, paths) => Query.respellings(paths, (path) => storedPath(path, (head) => m.resolve(head), m.below));
193
430
  return {
194
431
  renameExpr: (expr) => {
195
432
  const heads = [...pathHeads(expr)];
196
433
  const first = members[0];
197
434
  if (first === undefined)
198
435
  return expr;
199
- // Every member reads the same attributes: one expression, as before.
200
- if (members.every((m) => heads.every((head) => attributeOf(m, head) === attributeOf(first, head)))) {
201
- return renamePaths(expr, (head) => attributeOf(first, head));
436
+ // Each member's operands in the form its attributes hold (#242), then its entries as it
437
+ // stores them, and its conditions on an entry of a sparse map a `field:` rename stores whole
438
+ // asking its rows where they hold the map (#238).
439
+ const asStoredBy = members.map((m) => m.storedAt === undefined ? expr : operandsAsStored(expr, m.storedAt));
440
+ const stored = members.map((m, at) => {
441
+ const named = entriesFor(m, asStoredBy[at]);
442
+ return m.wholeRows?.entryForms(named) ?? named;
443
+ });
444
+ const paths = pathsIn(expr);
445
+ // Every member reads the same attributes, each operand in the same form: one expression,
446
+ // as before.
447
+ if (members.every((_, at) => stored[at] === asStoredBy[at]) &&
448
+ members.every((m, at) => heads.every((head) => attributeOf(m, head) === attributeOf(first, head)) &&
449
+ sameOperands(stored[at], stored[0])) &&
450
+ members.every((m) => paths.every((path) => belowOf(m, path) === belowOf(first, path)))) {
451
+ return renamePaths(stored[0], (head) => attributeOf(first, head), first.below);
202
452
  }
203
- // They differ: each member's rows judged by its own attributes.
453
+ // They differ: each member's rows judged by its own attributes and forms.
204
454
  return {
205
455
  [ExprTag]: ExprTag,
206
456
  _tag: "or",
207
- exprs: members.map((m) => ({
457
+ exprs: members.map((m, at) => ({
208
458
  [ExprTag]: ExprTag,
209
459
  _tag: "and",
210
460
  exprs: [
@@ -214,43 +464,151 @@ export const collectionNaming = (members) => {
214
464
  left: { _tag: "path", segments: ["__edd_e__"] },
215
465
  right: { _tag: "value", value: m.entityType },
216
466
  },
217
- renamePaths(expr, (head) => attributeOf(m, head)),
467
+ renamePaths(stored[at], (head) => attributeOf(m, head), m.below),
218
468
  ],
219
469
  })),
220
470
  };
221
471
  },
222
- selectAs: (query, paths) => {
472
+ selectAs: (query, written) => {
473
+ // An entry `.entry()` built, as its map's field names it: each member's own, or nothing for
474
+ // a member without the map (#238).
475
+ const paths = written.map(asMapPath);
223
476
  const heads = [...new Set(paths.map((path) => String(path[0])))];
224
477
  // Only a member's own domain fields are read for it.
225
478
  const has = (m, head) => m.fields === undefined || m.fields.has(head);
479
+ /** A member's paths that name an entry of one of its sparse maps (#238). */
480
+ const namesEntry = (m) => m.wholeRows !== undefined &&
481
+ paths.some((path) => has(m, String(path[0])) && mapEntryOf(path, m.wholeRows.sparseFields) !== undefined);
226
482
  const stored = new Map();
227
483
  for (const m of members) {
484
+ const storedOf = (path) => storedPath(path, (head) => m.resolve(head), m.below);
228
485
  for (const path of paths) {
229
486
  if (!has(m, String(path[0])))
230
487
  continue;
231
- const renamed = [m.resolve(String(path[0])), ...path.slice(1)];
232
- stored.set(JSON.stringify(renamed), renamed);
488
+ for (const renamed of projectedPaths(m.wholeRows, path, storedOf)) {
489
+ stored.set(JSON.stringify(renamed), renamed);
490
+ }
233
491
  }
234
492
  }
493
+ const plans = new Map(members.map((m) => [
494
+ m.entityType,
495
+ respellingsOf(m, paths.filter((path) => has(m, String(path[0])))),
496
+ ]));
235
497
  stored.set(JSON.stringify(["__edd_e__"]), ["__edd_e__"]);
236
498
  const byType = new Map(members.map((m) => [m.entityType, m]));
499
+ // A member's sparse map named: every row is read whole, and each is cut to what its member
500
+ // has of the paths, as a projection of them gives it, with the member's maps rebuilt as its
501
+ // read rebuilds them (#238).
502
+ if (members.some((m) => m.wholeRows !== undefined &&
503
+ readsWholeRows(paths.filter((path) => has(m, String(path[0]))), m.wholeRows.sparseFields))) {
504
+ const projected = new Map();
505
+ for (const m of members) {
506
+ for (const path of paths) {
507
+ if (!has(m, String(path[0])))
508
+ continue;
509
+ if (m.wholeRows !== undefined && namesMap(path, m.wholeRows.sparseFields))
510
+ continue;
511
+ const renamed = storedPath(path, (head) => m.resolve(head), m.below);
512
+ projected.set(JSON.stringify(renamed), renamed);
513
+ }
514
+ }
515
+ return Query.selectWhole(query, [...projected.values()], (row, raw) => {
516
+ const m = byType.get(raw.__edd_e__);
517
+ if (m === undefined)
518
+ return { _memberKey: "__unknown__", _decoded: raw };
519
+ const own = paths.filter((path) => has(m, String(path[0])));
520
+ if (m.wholeRows !== undefined) {
521
+ return {
522
+ _memberKey: m.entityKey,
523
+ _decoded: fromWholeRow(m.wholeRows, own, (head) => m.resolve(head), row, raw, m.below),
524
+ };
525
+ }
526
+ // A member without a sparse map: what its projection gave, under its domain names.
527
+ const item = {};
528
+ for (const head of heads) {
529
+ const attribute = m.resolve(head);
530
+ if (!has(m, head) || !Object.hasOwn(row, attribute))
531
+ continue;
532
+ const value = row[attribute];
533
+ if (value !== undefined)
534
+ item[head] = Query.respelled(value, plans.get(m.entityType)?.get(head));
535
+ }
536
+ return { _memberKey: m.entityKey, _decoded: item };
537
+ });
538
+ }
237
539
  return Query.selectProjected(query, [...stored.values()], (raw) => {
238
540
  const m = byType.get(raw.__edd_e__);
239
541
  if (m === undefined)
240
542
  return { _memberKey: "__unknown__", _decoded: raw };
543
+ // An entry of one of its maps selected: the map rebuilt from the attributes projected.
544
+ if (namesEntry(m)) {
545
+ const own = paths.filter((path) => has(m, String(path[0])));
546
+ return {
547
+ _memberKey: m.entityKey,
548
+ _decoded: fromWholeRow(m.wholeRows, own, (head) => m.resolve(head), raw, raw, m.below, true),
549
+ };
550
+ }
241
551
  const item = {};
242
552
  for (const head of heads) {
243
553
  if (!has(m, head))
244
554
  continue;
245
- const value = raw[m.resolve(head)];
246
- if (value !== undefined)
247
- item[head] = value;
555
+ const value = ownValue(raw, m.resolve(head));
556
+ if (value !== undefined) {
557
+ setOwn(item, head, Query.respelled(value, plans.get(m.entityType)?.get(head)));
558
+ }
248
559
  }
249
560
  return { _memberKey: m.entityKey, _decoded: item };
250
561
  });
251
562
  },
252
563
  };
253
564
  };
565
+ /** The value operands of an expression, in order. */
566
+ const operandValues = (node, into = []) => {
567
+ if (Array.isArray(node)) {
568
+ for (const item of node)
569
+ operandValues(item, into);
570
+ return into;
571
+ }
572
+ if (node === null || typeof node !== "object")
573
+ return into;
574
+ const record = node;
575
+ if (record._tag === "value") {
576
+ into.push(record.value);
577
+ return into;
578
+ }
579
+ if (record._tag === "path" || record._tag === "size")
580
+ return into;
581
+ for (const value of Object.values(record))
582
+ operandValues(value, into);
583
+ return into;
584
+ };
585
+ /** The attribute value an operand is sent as, or `undefined` where it can't be sent. */
586
+ const sentAs = (value) => {
587
+ try {
588
+ return JSON.stringify(toAttributeValue(value));
589
+ }
590
+ catch {
591
+ return undefined;
592
+ }
593
+ };
594
+ /**
595
+ * Whether two members send an expression's operands alike (#242): the same operand, or ones sent
596
+ * as the same attribute value. `operandsAsStored` hands an expression back as it is when it puts
597
+ * no operand in another form, so a filter holding none of Effect's instances is never marshalled
598
+ * here.
599
+ */
600
+ const sameOperands = (a, b) => {
601
+ if (a === b)
602
+ return true;
603
+ const [left, right] = [operandValues(a), operandValues(b)];
604
+ return (left.length === right.length &&
605
+ left.every((value, at) => {
606
+ if (Object.is(value, right[at]))
607
+ return true;
608
+ const sent = sentAs(value);
609
+ return sent !== undefined && sent === sentAs(right[at]);
610
+ }));
611
+ };
254
612
  /** An attribute no item ever has (#133): see {@link collectionNaming}. */
255
613
  const ABSENT_ATTRIBUTE = "__edd_absent__";
256
614
  // ---------------------------------------------------------------------------
@@ -309,20 +667,25 @@ export class BoundQueryImpl {
309
667
  const skAccessor = (this._config.skFields ? buildSkAccessor(this._config.skFields) : {});
310
668
  const { ops, targetField } = makeSkConditionOps();
311
669
  const condition = fn(skAccessor, ops);
312
- const finalCondition = this._config.composeSkCondition
313
- ? this._config.composeSkCondition(condition, targetField())
314
- : serializeRawCondition(condition);
315
- if (finalCondition === undefined) {
316
- return new BoundQueryImpl(Query.planEmpty(this._query), this._config, "where");
317
- }
318
- return new BoundQueryImpl(Query.whereUnderPins(this._query, finalCondition), this._config, "where");
670
+ const query = Query.orRefusal(this._config.operation, () => {
671
+ const finalCondition = this._config.composeSkCondition
672
+ ? this._config.composeSkCondition(condition, targetField())
673
+ : serializeRawCondition(condition);
674
+ return finalCondition === undefined
675
+ ? Query.planEmpty(this._query)
676
+ : Query.whereUnderPins(this._query, finalCondition);
677
+ }, (refusal) => Query.refuse(this._query, refusal));
678
+ return new BoundQueryImpl(query, this._config, "where");
319
679
  }
320
680
  // --- within ---
321
681
  within(bounds, options) {
322
- Query.refuseShardedPlan(this._query);
323
682
  if (this._applied !== undefined) {
324
683
  refuseWithin(this._applied === "where" ? "within() after .where()" : "a second .within()");
325
684
  }
685
+ if (Query.isRefused(this._query)) {
686
+ return new BoundQueryImpl(this._query, this._config, "within");
687
+ }
688
+ Query.refuseShardedPlan(this._query);
326
689
  const maxSkips = options?.maxSkips;
327
690
  if (maxSkips !== undefined && !(Number.isInteger(maxSkips) && maxSkips >= 0)) {
328
691
  throw new Error(`[EDD-9095] .within() maxSkips ${JSON.stringify(maxSkips)} is not a probe budget: ` +
@@ -331,10 +694,12 @@ export class BoundQueryImpl {
331
694
  const plan = this._config.planWithin;
332
695
  if (plan === undefined)
333
696
  refuseWithin("within() on a query without sort key composites to bound");
334
- return new BoundQueryImpl(plan(this._query, bounds, options), this._config, "within");
697
+ return new BoundQueryImpl(Query.orRefusal(this._config.operation, () => plan(this._query, bounds, options), (refusal) => Query.refuse(this._query, refusal)), this._config, "within");
335
698
  }
336
699
  // --- zorder ---
337
700
  zorder(options) {
701
+ if (Query.isRefused(this._query))
702
+ return this;
338
703
  Query.refuseShardedPlan(this._query);
339
704
  if (this._config.zorder !== true) {
340
705
  throw new Error(`[EDD-9095] .zorder() applies only to a Z-order index accessor: this query's index ` +
@@ -350,7 +715,7 @@ export class BoundQueryImpl {
350
715
  parseSimpleShorthand(fnOrShorthand);
351
716
  // Paths name the stored attributes of renamed fields.
352
717
  const stored = this._config.renameExpr === undefined ? expr : this._config.renameExpr(expr);
353
- return new BoundQueryImpl(Query.filterExpr(this._query, stored), this._config, this._applied);
718
+ return new BoundQueryImpl(Query.filterExprStored(this._query, stored), this._config, this._applied);
354
719
  }
355
720
  // --- filterBy ---
356
721
  filterBy(predicate) {