@prisma/orm-family-mongo 0.16.0-dev.36

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 (166) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +27 -0
  3. package/dist/authoring-entity-types-BGtkMyoN-D32kzx28.mjs +176 -0
  4. package/dist/authoring-entity-types-BGtkMyoN-D32kzx28.mjs.map +1 -0
  5. package/dist/canonicalization-hooks-DF3Z6sjO.mjs +22 -0
  6. package/dist/canonicalization-hooks-DF3Z6sjO.mjs.map +1 -0
  7. package/dist/codec.d.mts +2 -0
  8. package/dist/codec.mjs +2 -0
  9. package/dist/config-types-CFDf9ivE.mjs +37 -0
  10. package/dist/config-types-CFDf9ivE.mjs.map +1 -0
  11. package/dist/config-types-CQOf5N7R.d.mts +12 -0
  12. package/dist/config-types-CQOf5N7R.d.mts.map +1 -0
  13. package/dist/contract-builder-BFq0gOMV.mjs +634 -0
  14. package/dist/contract-builder-BFq0gOMV.mjs.map +1 -0
  15. package/dist/contract-builder-DB8nguDE.d.mts +445 -0
  16. package/dist/contract-builder-DB8nguDE.d.mts.map +1 -0
  17. package/dist/contract-errors-BCN6RVI5-kMurtAbI.mjs +9 -0
  18. package/dist/contract-errors-BCN6RVI5-kMurtAbI.mjs.map +1 -0
  19. package/dist/contract-psl.d.mts +45 -0
  20. package/dist/contract-psl.d.mts.map +1 -0
  21. package/dist/contract-psl.mjs +2 -0
  22. package/dist/contract-psl__provider.d.mts +15 -0
  23. package/dist/contract-psl__provider.d.mts.map +1 -0
  24. package/dist/contract-psl__provider.mjs +82 -0
  25. package/dist/contract-psl__provider.mjs.map +1 -0
  26. package/dist/contract-ts.d.mts +3 -0
  27. package/dist/contract-ts.mjs +3 -0
  28. package/dist/contract-ts__config-types.d.mts +2 -0
  29. package/dist/contract-ts__config-types.mjs +2 -0
  30. package/dist/contract-ts__contract-builder.d.mts +2 -0
  31. package/dist/contract-ts__contract-builder.mjs +2 -0
  32. package/dist/contract.d.mts +3 -0
  33. package/dist/contract.mjs +3 -0
  34. package/dist/contract__canonicalization-hooks.d.mts +9 -0
  35. package/dist/contract__canonicalization-hooks.d.mts.map +1 -0
  36. package/dist/contract__canonicalization-hooks.mjs +2 -0
  37. package/dist/contract__entity-kinds.d.mts +17 -0
  38. package/dist/contract__entity-kinds.d.mts.map +1 -0
  39. package/dist/contract__entity-kinds.mjs +2 -0
  40. package/dist/control-C7-kthr1.d.mts +193 -0
  41. package/dist/control-C7-kthr1.d.mts.map +1 -0
  42. package/dist/control-CClR5JMr.mjs +127 -0
  43. package/dist/control-CClR5JMr.mjs.map +1 -0
  44. package/dist/control-CFr3tYsg.mjs +398 -0
  45. package/dist/control-CFr3tYsg.mjs.map +1 -0
  46. package/dist/control-Cj81sPbj.d.mts +101 -0
  47. package/dist/control-Cj81sPbj.d.mts.map +1 -0
  48. package/dist/control-adapter-Db5pT77b.d.mts +83 -0
  49. package/dist/control-adapter-Db5pT77b.d.mts.map +1 -0
  50. package/dist/dist-C7Ykq48Q.mjs +19 -0
  51. package/dist/dist-C7Ykq48Q.mjs.map +1 -0
  52. package/dist/dist-Czwqx_i3.mjs +58 -0
  53. package/dist/dist-Czwqx_i3.mjs.map +1 -0
  54. package/dist/dist-DHrEJnhK.mjs +236 -0
  55. package/dist/dist-DHrEJnhK.mjs.map +1 -0
  56. package/dist/dist-Dyp05u64.mjs +1546 -0
  57. package/dist/dist-Dyp05u64.mjs.map +1 -0
  58. package/dist/emitter.d.mts +22 -0
  59. package/dist/emitter.d.mts.map +1 -0
  60. package/dist/emitter.mjs +2 -0
  61. package/dist/entity-kinds-B4KNTF8z-DPIuUuuB.mjs +603 -0
  62. package/dist/entity-kinds-B4KNTF8z-DPIuUuuB.mjs.map +1 -0
  63. package/dist/execution-CKnUiOfm.d.mts +48 -0
  64. package/dist/execution-CKnUiOfm.d.mts.map +1 -0
  65. package/dist/execution-D5XyZ5XS.mjs +1269 -0
  66. package/dist/execution-D5XyZ5XS.mjs.map +1 -0
  67. package/dist/exports-Bx-hlnZ3.mjs +209 -0
  68. package/dist/exports-Bx-hlnZ3.mjs.map +1 -0
  69. package/dist/exports-CJMh0VTU.mjs +156 -0
  70. package/dist/exports-CJMh0VTU.mjs.map +1 -0
  71. package/dist/family.d.mts +10 -0
  72. package/dist/family.mjs +9 -0
  73. package/dist/family__control-adapter.d.mts +2 -0
  74. package/dist/family__control-adapter.mjs +1 -0
  75. package/dist/family__control.d.mts +2 -0
  76. package/dist/family__control.mjs +3 -0
  77. package/dist/family__ir.d.mts +3 -0
  78. package/dist/family__ir.mjs +4 -0
  79. package/dist/family__migration.d.mts +2 -0
  80. package/dist/family__migration.mjs +2 -0
  81. package/dist/family__pack.d.mts +2 -0
  82. package/dist/family__pack.mjs +16 -0
  83. package/dist/family__pack.mjs.map +1 -0
  84. package/dist/family__runtime.d.mts +3 -0
  85. package/dist/family__runtime.mjs +2 -0
  86. package/dist/family__schema-verify.d.mts +2 -0
  87. package/dist/family__schema-verify.mjs +2 -0
  88. package/dist/field-accessor-0oUt6RZC-B_Z30uAA.mjs +234 -0
  89. package/dist/field-accessor-0oUt6RZC-B_Z30uAA.mjs.map +1 -0
  90. package/dist/field-accessor-DHsAPiW3-BdYCspQC.d.mts +588 -0
  91. package/dist/field-accessor-DHsAPiW3-BdYCspQC.d.mts.map +1 -0
  92. package/dist/filter-expressions-Uo8ZdzwB-53qnqceS.mjs +181 -0
  93. package/dist/filter-expressions-Uo8ZdzwB-53qnqceS.mjs.map +1 -0
  94. package/dist/index-BR0ue13b.d.mts +136 -0
  95. package/dist/index-BR0ue13b.d.mts.map +1 -0
  96. package/dist/index-Cd2zLiU6.d.mts +128 -0
  97. package/dist/index-Cd2zLiU6.d.mts.map +1 -0
  98. package/dist/index-DAOkcTCK.d.mts +151 -0
  99. package/dist/index-DAOkcTCK.d.mts.map +1 -0
  100. package/dist/index-Dv2PNMiG.d.mts +53 -0
  101. package/dist/index-Dv2PNMiG.d.mts.map +1 -0
  102. package/dist/index-DxoO-ywI.d.mts +178 -0
  103. package/dist/index-DxoO-ywI.d.mts.map +1 -0
  104. package/dist/index-wKMm41HC.d.mts +30 -0
  105. package/dist/index-wKMm41HC.d.mts.map +1 -0
  106. package/dist/interpreter-BxbUTQ3k-ClR98Ned.mjs +1091 -0
  107. package/dist/interpreter-BxbUTQ3k-ClR98Ned.mjs.map +1 -0
  108. package/dist/ir-CQrenTYt.d.mts +147 -0
  109. package/dist/ir-CQrenTYt.d.mts.map +1 -0
  110. package/dist/ir-Ch2aPr7a.mjs +62 -0
  111. package/dist/ir-Ch2aPr7a.mjs.map +1 -0
  112. package/dist/lowering.d.mts +2 -0
  113. package/dist/lowering.mjs +1 -0
  114. package/dist/migration-CNDQv5yB.d.mts +50 -0
  115. package/dist/migration-CNDQv5yB.d.mts.map +1 -0
  116. package/dist/migration-dcM7u4HG.mjs +52 -0
  117. package/dist/migration-dcM7u4HG.mjs.map +1 -0
  118. package/dist/mongo-contract-serializer-Cx7Fae8e-C8ZYPXpc.mjs +119 -0
  119. package/dist/mongo-contract-serializer-Cx7Fae8e-C8ZYPXpc.mjs.map +1 -0
  120. package/dist/mongo-contract-view-AnejqfrQ-BaCFT0xm.d.mts +27 -0
  121. package/dist/mongo-contract-view-AnejqfrQ-BaCFT0xm.d.mts.map +1 -0
  122. package/dist/mongo-contract-view-rcWzby6f-wRBCUnBt.mjs +17 -0
  123. package/dist/mongo-contract-view-rcWzby6f-wRBCUnBt.mjs.map +1 -0
  124. package/dist/mongo-value-set-tNn9-pun-dJYx_rVD.d.mts +536 -0
  125. package/dist/mongo-value-set-tNn9-pun-dJYx_rVD.d.mts.map +1 -0
  126. package/dist/mongodb-types-B4HmhF1S.d.mts +2 -0
  127. package/dist/orm.d.mts +311 -0
  128. package/dist/orm.d.mts.map +1 -0
  129. package/dist/orm.mjs +664 -0
  130. package/dist/orm.mjs.map +1 -0
  131. package/dist/pack-D9aLY50F.d.mts +44 -0
  132. package/dist/pack-D9aLY50F.d.mts.map +1 -0
  133. package/dist/query-ast.d.mts +5 -0
  134. package/dist/query-ast.mjs +4 -0
  135. package/dist/query-ast__control.d.mts +4 -0
  136. package/dist/query-ast__control.mjs +3 -0
  137. package/dist/query-ast__execution.d.mts +3 -0
  138. package/dist/query-ast__execution.mjs +3 -0
  139. package/dist/query-builder.d.mts +836 -0
  140. package/dist/query-builder.d.mts.map +1 -0
  141. package/dist/query-builder.mjs +3 -0
  142. package/dist/query-builder__contract-free.d.mts +83 -0
  143. package/dist/query-builder__contract-free.d.mts.map +1 -0
  144. package/dist/query-builder__contract-free.mjs +107 -0
  145. package/dist/query-builder__contract-free.mjs.map +1 -0
  146. package/dist/query-plan-C0-Yu8hf-DdilEdW6.d.mts +822 -0
  147. package/dist/query-plan-C0-Yu8hf-DdilEdW6.d.mts.map +1 -0
  148. package/dist/runtime-DPkRW4iu.d.mts +1 -0
  149. package/dist/runtime.d.mts +286 -0
  150. package/dist/runtime.d.mts.map +1 -0
  151. package/dist/runtime.mjs +532 -0
  152. package/dist/runtime.mjs.map +1 -0
  153. package/dist/schema-ir.d.mts +2 -0
  154. package/dist/schema-ir.mjs +2 -0
  155. package/dist/schema-verify-DJGr28XW.d.mts +23 -0
  156. package/dist/schema-verify-DJGr28XW.d.mts.map +1 -0
  157. package/dist/value.d.mts +2 -0
  158. package/dist/value.mjs +2 -0
  159. package/dist/value__mongodb-types.d.mts +2 -0
  160. package/dist/value__mongodb-types.mjs +1 -0
  161. package/dist/verify-mongo-schema-De0FG2Rm-CCzwnNPY.mjs +447 -0
  162. package/dist/verify-mongo-schema-De0FG2Rm-CCzwnNPY.mjs.map +1 -0
  163. package/dist/wire.d.mts +3 -0
  164. package/dist/wire.mjs +190 -0
  165. package/dist/wire.mjs.map +1 -0
  166. package/package.json +89 -0
@@ -0,0 +1,836 @@
1
+ import { C as MongoContract, V as MongoModelsMap, at as RootModelName, n as ExtractMongoCodecTypes, t as AnyMongoTypeMaps, w as MongoContractWithTypeMaps, z as MongoModelDefinition } from "./mongo-value-set-tNn9-pun-dJYx_rVD.mjs";
2
+ import "./index-Cd2zLiU6.mjs";
3
+ import { c as MongoValue } from "./index-wKMm41HC.mjs";
4
+ import { B as MongoFilterExpr, L as MongoFieldShape, Mt as UpdateManyCommand, Nt as UpdateOneCommand, R as MongoFillOutput, a as DeleteOneCommand, c as InsertManyCommand, d as MongoAggAccumulator, et as MongoPipelineStage, i as DeleteManyCommand, j as MongoDensifyRange, l as InsertOneCommand, m as MongoAggExpr, n as AggregateCommand, o as FindOneAndDeleteCommand, ot as MongoResultShape, r as AnyMongoCommand, rt as MongoQueryPlan, s as FindOneAndUpdateCommand, vt as MongoUpdatePipelineStage, xt as MongoWindowField } from "./query-plan-C0-Yu8hf-DdilEdW6.mjs";
5
+ import { a as InsertOneResult, c as UpdateResult, i as InsertManyResult, r as DeleteResult } from "./execution-CKnUiOfm.mjs";
6
+ import { A as TypedUpdateOp, C as ProjectedShape, D as StringField, E as SortSpec, F as expr, M as UpdaterResult, N as ValidPaths, O as TypedAccumulatorExpr, P as createFieldAccessor, S as PathCompletions, T as ResolveRow, _ as NullableDocField, a as DocShape, b as ObjectExpression, c as FieldAccessor, d as LeafExpression, f as LiteralValue, g as NestedDocShape, h as ModelToDocShape, i as DocField, j as UnwoundShape, k as TypedAggExpr, l as GroupSpec, m as ModelNestedShape, n as BooleanField, o as Expression, p as ModelArrayField, r as DateField, s as ExtractDocShape, t as ArrayField, u as GroupedDocShape, v as NullableNumericField, w as ResolvePath, x as ObjectField, y as NumericField } from "./field-accessor-DHsAPiW3-BdYCspQC.mjs";
7
+ import { ContractField } from "@prisma/orm-framework/contract/types";
8
+ //#region ../../../2-mongo-family/5-query-builders/query-builder/dist/index.d.mts
9
+ //#region src/accumulator-helpers.d.ts
10
+ declare const acc: {
11
+ sum<F extends DocField>(expr: TypedAggExpr<F>): TypedAccumulatorExpr<F>;
12
+ avg(expr: TypedAggExpr<DocField>): TypedAccumulatorExpr<NullableNumericField>;
13
+ min<F extends DocField>(expr: TypedAggExpr<F>): TypedAccumulatorExpr<{
14
+ readonly codecId: F["codecId"];
15
+ readonly nullable: true;
16
+ }>;
17
+ max<F extends DocField>(expr: TypedAggExpr<F>): TypedAccumulatorExpr<{
18
+ readonly codecId: F["codecId"];
19
+ readonly nullable: true;
20
+ }>;
21
+ first<F extends DocField>(expr: TypedAggExpr<F>): TypedAccumulatorExpr<{
22
+ readonly codecId: F["codecId"];
23
+ readonly nullable: true;
24
+ }>;
25
+ last<F extends DocField>(expr: TypedAggExpr<F>): TypedAccumulatorExpr<{
26
+ readonly codecId: F["codecId"];
27
+ readonly nullable: true;
28
+ }>;
29
+ push(expr: TypedAggExpr<DocField>): TypedAccumulatorExpr<ArrayField>;
30
+ addToSet(expr: TypedAggExpr<DocField>): TypedAccumulatorExpr<ArrayField>;
31
+ count(): TypedAccumulatorExpr<NumericField>;
32
+ stdDevPop(expr: TypedAggExpr<DocField>): TypedAccumulatorExpr<NullableNumericField>;
33
+ stdDevSamp(expr: TypedAggExpr<DocField>): TypedAccumulatorExpr<NullableNumericField>;
34
+ firstN(args: {
35
+ input: TypedAggExpr<DocField>;
36
+ n: TypedAggExpr<NumericField>;
37
+ }): TypedAccumulatorExpr<ArrayField>;
38
+ lastN(args: {
39
+ input: TypedAggExpr<DocField>;
40
+ n: TypedAggExpr<NumericField>;
41
+ }): TypedAccumulatorExpr<ArrayField>;
42
+ maxN(args: {
43
+ input: TypedAggExpr<DocField>;
44
+ n: TypedAggExpr<NumericField>;
45
+ }): TypedAccumulatorExpr<ArrayField>;
46
+ minN(args: {
47
+ input: TypedAggExpr<DocField>;
48
+ n: TypedAggExpr<NumericField>;
49
+ }): TypedAccumulatorExpr<ArrayField>;
50
+ top(args: {
51
+ output: TypedAggExpr<DocField>;
52
+ sortBy: Readonly<Record<string, 1 | -1>>;
53
+ }): TypedAccumulatorExpr<DocField>;
54
+ bottom(args: {
55
+ output: TypedAggExpr<DocField>;
56
+ sortBy: Readonly<Record<string, 1 | -1>>;
57
+ }): TypedAccumulatorExpr<DocField>;
58
+ topN(args: {
59
+ output: TypedAggExpr<DocField>;
60
+ sortBy: Readonly<Record<string, 1 | -1>>;
61
+ n: TypedAggExpr<NumericField>;
62
+ }): TypedAccumulatorExpr<ArrayField>;
63
+ bottomN(args: {
64
+ output: TypedAggExpr<DocField>;
65
+ sortBy: Readonly<Record<string, 1 | -1>>;
66
+ n: TypedAggExpr<NumericField>;
67
+ }): TypedAccumulatorExpr<ArrayField>;
68
+ };
69
+ //#endregion
70
+ //#region src/lookup-builder.d.ts
71
+ /**
72
+ * Resolved foreign-model name for a contract root. Looks `RootName` up
73
+ * through `TContract['roots']` and extracts the referenced model name
74
+ * so it can be used as a `ModelName` index into `models`. Resolves to
75
+ * `never` when the root is not present (this surface should never be
76
+ * reachable through normal use because `from()` constrains its `R`
77
+ * parameter to `keyof TContract['roots']`).
78
+ */
79
+ type ModelOf<TContract extends MongoContract, RootName extends keyof TContract['roots'] & string> = RootModelName<TContract, RootName>;
80
+ /**
81
+ * Object returned by the user from the `on(...)` callback. Each side is
82
+ * a `LeafExpression` produced by property access on the corresponding
83
+ * `FieldAccessor` (`local._id`, `foreign.customerId`, etc.). Carrying
84
+ * `LeafExpression` rather than the broader `TypedAggExpr` is what makes
85
+ * non-leaf returns (e.g. `fn.toUpper(local._id)`) a compile-time error
86
+ * without per-field operator gating — `LeafExpression` carries `_path`,
87
+ * `TypedAggExpr` does not (see field-accessor.ts L47–L82).
88
+ */
89
+ interface LookupOnResult {
90
+ readonly local: LeafExpression<DocField>;
91
+ readonly foreign: LeafExpression<DocField>;
92
+ }
93
+ /**
94
+ * Marker brand on the captured spec returned by the `lookup(...)`
95
+ * callback. The phantom `_brand` literal lets `PipelineChain.lookup`
96
+ * accept the result of `from(...).on(...).as(...)` without exposing the
97
+ * internal field shape to user code, and prevents accidental
98
+ * construction of a malformed spec by hand.
99
+ */
100
+ type LookupResultBrand = 'mongo-query-builder/lookup-result@1';
101
+ /**
102
+ * Captured output of the inner `from(name).on(cb).as(name)` chain. The
103
+ * contract is consumed by `PipelineChain.lookup` to construct the
104
+ * `MongoLookupStage` (collection name comes from `models[ModelName]
105
+ * .storage.collection`) and to thread `ModelArrayField<ModelName>` into
106
+ * the resulting `Shape` so the resolver yields `Array<ForeignRow>`.
107
+ *
108
+ * Type parameters carry the foreign-root literal `RootName`, the
109
+ * resolved foreign model name `ModelName`, and the `As` literal so
110
+ * `PipelineChain.lookup`'s return type can encode the result-row
111
+ * promotion precisely.
112
+ */
113
+ interface LookupResult<RootName extends string, ModelName extends string, As extends string> {
114
+ readonly _brand: LookupResultBrand;
115
+ readonly _root: RootName;
116
+ readonly _model: ModelName;
117
+ readonly _localField: string;
118
+ readonly _foreignField: string;
119
+ readonly _as: As;
120
+ }
121
+ /**
122
+ * Builder returned by `from(name).on(cb)`. Carries the foreign root /
123
+ * model literals plus the captured local / foreign paths, and exposes
124
+ * `.as(name)` to finalise the spec with the user-chosen field name.
125
+ */
126
+ interface LookupBuilderWithKey<RootName extends string, ModelName extends string> {
127
+ as<As extends string>(name: As): LookupResult<RootName, ModelName, As>;
128
+ }
129
+ /**
130
+ * Builder returned by `from(name)`. Carries the foreign root / model
131
+ * literals and the local pipeline's `Shape` / nested shape so the
132
+ * `on(...)` callback's `local` and `foreign` accessors are typed
133
+ * narrowly.
134
+ *
135
+ * `on(cb)` runs the user's callback to capture the leaf paths and
136
+ * returns a `LookupBuilderWithKey` that exposes `.as(name)`.
137
+ */
138
+ interface LookupBuilder<TContract extends MongoContract, Shape extends DocShape, Nested extends Record<string, DocField>, RootName extends string, ModelName extends string> {
139
+ on(cb: (local: FieldAccessor<Shape, Nested>, foreign: ModelName extends keyof MongoModelsMap<TContract> & string ? FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>> : never) => LookupOnResult): LookupBuilderWithKey<RootName, ModelName>;
140
+ }
141
+ /**
142
+ * Type of the `from` callable passed to `PipelineChain.lookup`'s outer
143
+ * callback. The generic argument is inferred from a string-literal
144
+ * argument (the same pattern as `mongoQuery<TC>(...).from('orders')`),
145
+ * which grounds `RootName` into the returned `LookupBuilder` *before*
146
+ * the inner `on(...)` callback is type-checked. This sequential
147
+ * inference is what makes `foreign` resolve narrowly to the foreign
148
+ * model's `FieldAccessor` (verified in the R1.5 spike — see spec § Open
149
+ * Questions / Resolved decisions).
150
+ */
151
+ type LookupFrom<TContract extends MongoContract, Shape extends DocShape, Nested extends Record<string, DocField>> = <RootName extends keyof TContract['roots'] & string>(name: RootName) => LookupBuilder<TContract, Shape, Nested, RootName, ModelOf<TContract, RootName>>;
152
+ //#endregion
153
+ //#region src/markers.d.ts
154
+ /**
155
+ * Phantom capability markers for `PipelineChain`.
156
+ *
157
+ * `UpdateEnabled` — gates `.updateMany()` / `.updateOne()` no-arg form
158
+ * (consume accumulated pipeline as an update-with-pipeline spec).
159
+ * `FindAndModifyEnabled` — gates `.findOneAndUpdate(...)` / `.findOneAndDelete(...)`
160
+ * (deconstruct pipeline into the wire command's filter/sort/skip slots).
161
+ * `LeadingMatch` — internal marker tracking whether the chain is still
162
+ * in its leading-`$match` prefix. Flips to `'past-leading'`
163
+ * after the first non-`$match` stage, which lets
164
+ * `match()` clear `UpdateEnabled` on second `$match`
165
+ * stages that sit past the prefix (and would otherwise
166
+ * fail at runtime inside `deconstructUpdateChain`).
167
+ *
168
+ * Each pipeline-stage method either preserves or clears these markers per
169
+ * the marker table (and rationale per row) in
170
+ * `docs/architecture docs/adrs/ADR 201 - State-machine pattern for typed DSL builders.md`.
171
+ *
172
+ * The markers exist only at the type level; nothing reads them at runtime.
173
+ * Value literals are self-identifying so the slots are distinguishable in
174
+ * hover tooltips and error messages (e.g. `'update-ok'` vs `'fam-ok'`).
175
+ */
176
+ type UpdateEnabled = 'update-ok' | 'update-cleared';
177
+ type FindAndModifyEnabled = 'fam-ok' | 'fam-cleared';
178
+ type LeadingMatch = 'leading' | 'past-leading';
179
+ //#endregion
180
+ //#region src/builder.d.ts
181
+ interface PipelineChainState {
182
+ readonly collection: string;
183
+ readonly stages: ReadonlyArray<MongoPipelineStage>;
184
+ readonly storageHash: string;
185
+ readonly modelName?: string;
186
+ }
187
+ /**
188
+ * The pipeline state in the query-builder state machine.
189
+ *
190
+ * Reached from `CollectionHandle` or `FilteredCollection` after the first
191
+ * pipeline-stage method call (or directly via `aggregate()` shortcuts). Holds
192
+ * the accumulated `MongoPipelineStage[]` and exposes pipeline-stage methods,
193
+ * the `merge`/`out` write terminals, and the `build`/`aggregate` read
194
+ * terminals.
195
+ *
196
+ * Two phantom type parameters gate the conditional terminals:
197
+ *
198
+ * - `U extends UpdateEnabled` — when `'update-ok'`, the no-arg `updateMany()` /
199
+ * `updateOne()` form is available (consume the chain as an
200
+ * update-with-pipeline spec). Cleared by stages that produce content the
201
+ * `update` AST cannot represent (e.g. `$group`, `$lookup`, `$limit`).
202
+ * - `F extends FindAndModifyEnabled` — when `'fam-ok'`, the
203
+ * `findOneAndUpdate(...)` / `findOneAndDelete(...)` terminals are
204
+ * available. Cleared by stages incompatible with their wire-command slots
205
+ * (`$limit`, `$group`, mutating stages, …).
206
+ *
207
+ * The marker semantics are encoded in the per-method return types — see the
208
+ * marker table (and rationale per row) in
209
+ * `docs/architecture docs/adrs/ADR 201 - State-machine pattern for typed DSL builders.md`.
210
+ */
211
+ declare class PipelineChain<TContract extends MongoContractWithTypeMaps<MongoContract, AnyMongoTypeMaps>, Shape extends DocShape, U extends UpdateEnabled = 'update-ok', F extends FindAndModifyEnabled = 'fam-ok', L extends LeadingMatch = 'leading', N extends NestedDocShape = Record<string, never>> {
212
+ #private;
213
+ readonly __updateCompat: U;
214
+ readonly __findAndModifyCompat: F;
215
+ readonly __leadingMatch: L;
216
+ constructor(contract: TContract, state: PipelineChainState);
217
+ /**
218
+ * `$match`. `FindAndModifyEnabled` is always preserved. `UpdateEnabled` is
219
+ * preserved only while the chain is still in the leading-`$match` prefix
220
+ * (`L = 'leading'`); a `$match` that follows any non-`$match` stage
221
+ * transitions to `L = 'past-leading'` and clears `UpdateEnabled`, since
222
+ * `deconstructUpdateChain` can only peel leading `$match` stages into the
223
+ * wire-command filter.
224
+ */
225
+ match(filter: MongoFilterExpr): PipelineChain<TContract, Shape, L extends 'leading' ? U : 'update-cleared', F, L, N>;
226
+ match(fn: (fields: FieldAccessor<Shape, N>) => MongoFilterExpr): PipelineChain<TContract, Shape, L extends 'leading' ? U : 'update-cleared', F, L, N>;
227
+ /**
228
+ * `$sort`. Clears `UpdateEnabled` (`update` has no per-document sort) but
229
+ * preserves `FindAndModifyEnabled` (`findAndModify` has a `sort` slot).
230
+ */
231
+ sort(spec: SortSpec<Shape>): PipelineChain<TContract, Shape, 'update-cleared', F, 'past-leading', N>;
232
+ /**
233
+ * `$limit`. Clears both markers — `limit` is incompatible with the `update`
234
+ * wire command, and `findAndModify` already implies single-document
235
+ * semantics (so `.limit(...)` adds no meaning, only ambiguity).
236
+ */
237
+ limit(n: number): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
238
+ /**
239
+ * `$skip`. Clears both markers — MongoDB's `findAndModify` wire command
240
+ * has no `skip` slot, so `deconstructFindAndModifyChain` rejects any
241
+ * `$skip` at runtime; keeping the marker `fam-cleared` makes the type
242
+ * system reflect the same constraint (see ADR 201 marker table).
243
+ */
244
+ skip(n: number): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
245
+ sample(n: number): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
246
+ /**
247
+ * `$addFields`. Preserves `UpdateEnabled` (representable as
248
+ * update-with-pipeline `$set`); clears `FindAndModifyEnabled` (no analogue
249
+ * in the find-and-modify wire commands). The nested-path shape `N` is
250
+ * preserved — newly added flat fields are reachable via property access
251
+ * (`f.newField`) but do not themselves carry nested structure.
252
+ */
253
+ addFields<NewFields extends Record<string, TypedAggExpr<DocField>>>(fn: (fields: FieldAccessor<Shape, N>) => NewFields): PipelineChain<TContract, Shape & ExtractDocShape<NewFields>, U, 'fam-cleared', 'past-leading', N>;
254
+ /**
255
+ * `$lookup`. Clears both markers — joins are not representable in either
256
+ * the `update` or `findAndModify` wire commands. The original document's
257
+ * nested-path shape `N` is preserved (the lookup adds a sidecar array
258
+ * field; existing keys are untouched).
259
+ *
260
+ * The single callback receives a `from` callable that grounds the
261
+ * foreign-root literal sequentially before the inner `on(...)`
262
+ * callback is type-checked — see `lookup-builder.ts`. The resulting
263
+ * `Shape` gains the `As` key as a `ModelArrayField<ModelName>` so
264
+ * `ResolveRow` produces `Array<ForeignRow>` (with concrete leaf
265
+ * types) instead of the legacy `unknown[]`.
266
+ */
267
+ lookup<RootName extends string, ModelName extends string, As extends string>(fn: (from: LookupFrom<TContract, Shape, N>) => LookupResult<RootName, ModelName, As>): PipelineChain<TContract, Shape & Record<As, ModelArrayField<ModelName>>, 'update-cleared', 'fam-cleared', 'past-leading', N>;
268
+ /**
269
+ * `$project`. Preserves `UpdateEnabled` (representable as update-with-pipeline
270
+ * `$project` / `$unset`); clears `FindAndModifyEnabled` (use `.project()` on
271
+ * the result of `.build()` if both projection and find-and-modify are
272
+ * needed — see spec).
273
+ *
274
+ * Resets the nested-path shape to `Record<string, never>` — projection
275
+ * fundamentally rewrites the document, so dot-paths into the *source*
276
+ * document are no longer meaningful downstream.
277
+ */
278
+ project<K extends keyof Shape & string>(...keys: K[]): PipelineChain<TContract, Pick<Shape, K | ('_id' extends keyof Shape ? '_id' : never)>, U, 'fam-cleared', 'past-leading'>;
279
+ project<Spec extends Record<string, 1 | TypedAggExpr<DocField>>>(fn: (fields: FieldAccessor<Shape, N>) => Spec): PipelineChain<TContract, ProjectedShape<Shape, Spec>, U, 'fam-cleared', 'past-leading'>;
280
+ /**
281
+ * `$unwind`. Clears both markers — array unrolling produces multiple output
282
+ * documents per input, incompatible with both single-document update and
283
+ * find-and-modify wire commands. The original `N` is preserved: unwind
284
+ * replaces the unwound array slot with its element but leaves the rest
285
+ * of the document structurally intact.
286
+ */
287
+ unwind<K extends keyof Shape & string>(field: K, options?: {
288
+ preserveNullAndEmptyArrays?: boolean;
289
+ }): PipelineChain<TContract, UnwoundShape<Shape, K>, 'update-cleared', 'fam-cleared', 'past-leading', N>;
290
+ /**
291
+ * `$group`. Clears both markers — group output bears no relation to source
292
+ * documents; neither `update` nor `findAndModify` can consume it. Nested
293
+ * path shape is reset (the source document's path tree is gone).
294
+ */
295
+ group<Spec extends GroupSpec>(fn: (fields: FieldAccessor<Shape, N>) => Spec): PipelineChain<TContract, GroupedDocShape<Spec>, 'update-cleared', 'fam-cleared', 'past-leading'>;
296
+ /**
297
+ * `$replaceRoot`. Preserves `UpdateEnabled` (representable as
298
+ * update-with-pipeline `$replaceRoot`); clears `FindAndModifyEnabled`.
299
+ * Nested path shape is reset — the replaced root has no relation to
300
+ * the original document structure.
301
+ */
302
+ replaceRoot<NewShape extends DocShape>(fn: (fields: FieldAccessor<Shape, N>) => Expression<DocField> | TypedAggExpr<DocField>): PipelineChain<TContract, NewShape, U, 'fam-cleared', 'past-leading'>;
303
+ count<Field extends string>(field: Field): PipelineChain<TContract, Record<Field, {
304
+ readonly codecId: 'mongo/double@1';
305
+ readonly nullable: false;
306
+ }>, 'update-cleared', 'fam-cleared', 'past-leading'>;
307
+ sortByCount<F2 extends DocField>(fn: (fields: FieldAccessor<Shape, N>) => Expression<F2> | TypedAggExpr<F2>): PipelineChain<TContract, {
308
+ _id: F2;
309
+ count: {
310
+ readonly codecId: 'mongo/double@1';
311
+ readonly nullable: false;
312
+ };
313
+ }, 'update-cleared', 'fam-cleared', 'past-leading'>;
314
+ /**
315
+ * `$redact`. Preserves `UpdateEnabled`; clears `FindAndModifyEnabled`.
316
+ * Shape- and nested-path-preserving (the document tree is unchanged).
317
+ */
318
+ redact(fn: (fields: FieldAccessor<Shape, N>) => Expression<DocField> | TypedAggExpr<DocField>): PipelineChain<TContract, Shape, U, 'fam-cleared', 'past-leading', N>;
319
+ /**
320
+ * `$out` write terminal. Materialises the pipeline output into
321
+ * `collection` (optionally in `db`), replacing any prior contents. Unlike
322
+ * the other pipeline-stage methods, this **terminates** the chain — it
323
+ * returns a `MongoQueryPlan` rather than another `PipelineChain`, since
324
+ * `$out` must be the final stage and there is nothing further to chain.
325
+ *
326
+ * Lane is `mongo-query` (matching all other terminals in this package) so
327
+ * middleware can dispatch on intent without inspecting the command.
328
+ *
329
+ * The result row stream is empty (`unknown` row type) — the data lives
330
+ * in the destination collection, not the response.
331
+ */
332
+ out(collection: string, db?: string): MongoQueryPlan<unknown, AggregateCommand>;
333
+ /**
334
+ * `$merge` write terminal. Streams the pipeline output into the target
335
+ * collection per the supplied merge semantics (`whenMatched` /
336
+ * `whenNotMatched`). Like `out()`, terminates the chain — `$merge` must
337
+ * be the final stage.
338
+ */
339
+ merge(options: {
340
+ into: string | {
341
+ db: string;
342
+ coll: string;
343
+ };
344
+ on?: string | ReadonlyArray<string>;
345
+ whenMatched?: string | ReadonlyArray<MongoUpdatePipelineStage>;
346
+ whenNotMatched?: string;
347
+ }): MongoQueryPlan<unknown, AggregateCommand>;
348
+ unionWith(collection: string, pipeline?: ReadonlyArray<MongoPipelineStage>): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
349
+ bucket(options: {
350
+ groupBy: MongoAggExpr;
351
+ boundaries: ReadonlyArray<unknown>;
352
+ default_?: unknown;
353
+ output?: Record<string, MongoAggAccumulator>;
354
+ }): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
355
+ bucketAuto(options: {
356
+ groupBy: MongoAggExpr;
357
+ buckets: number;
358
+ output?: Record<string, MongoAggAccumulator>;
359
+ granularity?: string;
360
+ }): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
361
+ geoNear(options: {
362
+ near: unknown;
363
+ distanceField: string;
364
+ spherical?: boolean;
365
+ maxDistance?: number;
366
+ minDistance?: number;
367
+ query?: MongoFilterExpr;
368
+ key?: string;
369
+ distanceMultiplier?: number;
370
+ includeLocs?: string;
371
+ }): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
372
+ facet(facets: Record<string, ReadonlyArray<MongoPipelineStage>>): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
373
+ graphLookup(options: {
374
+ from: string;
375
+ startWith: MongoAggExpr;
376
+ connectFromField: string;
377
+ connectToField: string;
378
+ as: string;
379
+ maxDepth?: number;
380
+ depthField?: string;
381
+ restrictSearchWithMatch?: MongoFilterExpr;
382
+ }): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
383
+ setWindowFields(options: {
384
+ partitionBy?: MongoAggExpr;
385
+ sortBy?: Record<string, 1 | -1>;
386
+ output: Record<string, MongoWindowField>;
387
+ }): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
388
+ densify(options: {
389
+ field: string;
390
+ partitionByFields?: ReadonlyArray<string>;
391
+ range: MongoDensifyRange;
392
+ }): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
393
+ fill(options: {
394
+ partitionBy?: MongoAggExpr;
395
+ partitionByFields?: ReadonlyArray<string>;
396
+ sortBy?: Record<string, 1 | -1>;
397
+ output: Record<string, MongoFillOutput>;
398
+ }): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
399
+ search(config: Record<string, unknown>, index?: string): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
400
+ searchMeta(config: Record<string, unknown>, index?: string): PipelineChain<TContract, DocShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
401
+ vectorSearch(options: {
402
+ index: string;
403
+ path: string;
404
+ queryVector: ReadonlyArray<number>;
405
+ numCandidates: number;
406
+ limit: number;
407
+ filter?: Record<string, unknown>;
408
+ }): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading', N>;
409
+ pipe(stage: MongoPipelineStage): PipelineChain<TContract, Shape, 'update-cleared', 'fam-cleared', 'past-leading'>;
410
+ pipe<NewShape extends DocShape>(stage: MongoPipelineStage): PipelineChain<TContract, NewShape, 'update-cleared', 'fam-cleared', 'past-leading'>;
411
+ /**
412
+ * No-arg `updateMany()`: deconstruct the chain into leading `$match`
413
+ * stages (folded into the filter) and remaining stages (which must all
414
+ * be valid pipeline-update stages). Available only when `U = 'update-ok'`.
415
+ *
416
+ * The optional callback parameter exists for subclass-override
417
+ * compatibility with `FilteredCollection.updateMany(updaterFn)` — TS's
418
+ * strict override check requires the parent's parameter to accept at
419
+ * least what the child's signature does. A runtime guard throws if a
420
+ * callback is actually passed on a bare `PipelineChain`. Note that
421
+ * because nothing in the public surface transitions `U` from
422
+ * `'update-cleared'` (the initial state on `CollectionHandle` /
423
+ * `FilteredCollection`) back to `'update-ok'`, the no-arg form is
424
+ * reachable only via explicit type casts in internal tests — the
425
+ * callback-form "type hole" is therefore not reachable from user
426
+ * code. See `docs/architecture docs/adrs/ADR 201 - State-machine
427
+ * pattern for typed DSL builders.md` for the marker-transition table.
428
+ */
429
+ updateMany(this: PipelineChain<TContract, Shape, 'update-ok', F, L, N>, updaterFn?: (fields: FieldAccessor<Shape, N>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateManyCommand>;
430
+ /**
431
+ * No-arg `updateOne()`: same as `updateMany()` but maps to a single-doc
432
+ * update. Carries the same optional-callback/subclass-compat caveat
433
+ * documented above — the callback form is reachable only via forced
434
+ * casts in internal tests.
435
+ */
436
+ updateOne(this: PipelineChain<TContract, Shape, 'update-ok', F, L, N>, updaterFn?: (fields: FieldAccessor<Shape, N>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateOneCommand>;
437
+ /**
438
+ * Find a single document matching the accumulated pipeline (which must
439
+ * consist solely of leading `$match` stages followed by at most one
440
+ * `$sort`) and apply `updaterFn`. Available only when
441
+ * `FindAndModifyEnabled` is `'fam-ok'` — stages that clear the marker
442
+ * (including `$skip`, which MongoDB's `findAndModify` has no slot for)
443
+ * make this method invisible at the type level.
444
+ *
445
+ * The pipeline stages are deconstructed into the wire command's `filter`
446
+ * and `sort` slots. If any non-deconstructable stage is present, a
447
+ * runtime error is thrown as a defensive check (the type system should
448
+ * prevent this).
449
+ */
450
+ findOneAndUpdate(this: PipelineChain<TContract, Shape, U, 'fam-ok', L, N>, updaterFn: (fields: FieldAccessor<Shape, N>) => UpdaterResult, opts?: {
451
+ readonly upsert?: boolean;
452
+ readonly returnDocument?: 'before' | 'after';
453
+ }): MongoQueryPlan<ResolveRow<Shape, ExtractMongoCodecTypes<TContract>, TContract> | null, FindOneAndUpdateCommand>;
454
+ /**
455
+ * Find a single document matching the accumulated pipeline and delete it.
456
+ * Same marker gating and deconstruction as `findOneAndUpdate`.
457
+ */
458
+ findOneAndDelete(this: PipelineChain<TContract, Shape, U, 'fam-ok', L, N>): MongoQueryPlan<ResolveRow<Shape, ExtractMongoCodecTypes<TContract>, TContract> | null, FindOneAndDeleteCommand>;
459
+ /**
460
+ * Materialise the chain as a `MongoQueryPlan` wrapping an `AggregateCommand`.
461
+ */
462
+ build(): MongoQueryPlan<ResolveRow<Shape, ExtractMongoCodecTypes<TContract>, TContract>, AggregateCommand>;
463
+ /**
464
+ * Alias for `build()` — surfaces the read intent at the call site.
465
+ */
466
+ aggregate(): MongoQueryPlan<ResolveRow<Shape, ExtractMongoCodecTypes<TContract>, TContract>, AggregateCommand>;
467
+ }
468
+ //#endregion
469
+ //#region src/expression-helpers.d.ts
470
+ declare function literal(value: string): TypedAggExpr<StringField>;
471
+ declare function literal(value: number): TypedAggExpr<NumericField>;
472
+ declare function literal(value: boolean): TypedAggExpr<BooleanField>;
473
+ declare function literal(value: Date): TypedAggExpr<DateField>;
474
+ declare function literal<F extends DocField>(value: LiteralValue<F>): TypedAggExpr<F>;
475
+ declare const fn: {
476
+ add(...args: TypedAggExpr<DocField>[]): TypedAggExpr<NumericField>;
477
+ subtract(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
478
+ multiply(...args: TypedAggExpr<DocField>[]): TypedAggExpr<NumericField>;
479
+ divide(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
480
+ concat(...args: TypedAggExpr<DocField>[]): TypedAggExpr<StringField>;
481
+ toLower(a: TypedAggExpr<DocField>): TypedAggExpr<StringField>;
482
+ toUpper(a: TypedAggExpr<DocField>): TypedAggExpr<StringField>;
483
+ size(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
484
+ cond<F extends DocField>(condition: MongoAggExpr, thenExpr: TypedAggExpr<F>, elseExpr: TypedAggExpr<DocField>): TypedAggExpr<F>;
485
+ literal: typeof literal;
486
+ year(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
487
+ month(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
488
+ dayOfMonth(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
489
+ hour(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
490
+ minute(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
491
+ second(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
492
+ millisecond(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
493
+ dateToString(args: {
494
+ date: TypedAggExpr<DateField>;
495
+ format?: TypedAggExpr<StringField>;
496
+ timezone?: TypedAggExpr<StringField>;
497
+ onNull?: TypedAggExpr<DocField>;
498
+ }): TypedAggExpr<StringField>;
499
+ dateFromString(args: {
500
+ dateString: TypedAggExpr<StringField>;
501
+ format?: TypedAggExpr<StringField>;
502
+ timezone?: TypedAggExpr<StringField>;
503
+ onError?: TypedAggExpr<DocField>;
504
+ onNull?: TypedAggExpr<DocField>;
505
+ }): TypedAggExpr<DateField>;
506
+ dateDiff(args: {
507
+ startDate: TypedAggExpr<DateField>;
508
+ endDate: TypedAggExpr<DateField>;
509
+ unit: TypedAggExpr<StringField>;
510
+ timezone?: TypedAggExpr<StringField>;
511
+ startOfWeek?: TypedAggExpr<StringField>;
512
+ }): TypedAggExpr<NumericField>;
513
+ dateAdd(args: {
514
+ startDate: TypedAggExpr<DateField>;
515
+ unit: TypedAggExpr<StringField>;
516
+ amount: TypedAggExpr<NumericField>;
517
+ timezone?: TypedAggExpr<StringField>;
518
+ }): TypedAggExpr<DateField>;
519
+ dateSubtract(args: {
520
+ startDate: TypedAggExpr<DateField>;
521
+ unit: TypedAggExpr<StringField>;
522
+ amount: TypedAggExpr<NumericField>;
523
+ timezone?: TypedAggExpr<StringField>;
524
+ }): TypedAggExpr<DateField>;
525
+ dateTrunc(args: {
526
+ date: TypedAggExpr<DateField>;
527
+ unit: TypedAggExpr<StringField>;
528
+ binSize?: TypedAggExpr<NumericField>;
529
+ timezone?: TypedAggExpr<StringField>;
530
+ startOfWeek?: TypedAggExpr<StringField>;
531
+ }): TypedAggExpr<DateField>;
532
+ substr(str: TypedAggExpr<DocField>, start: TypedAggExpr<DocField>, length: TypedAggExpr<DocField>): TypedAggExpr<StringField>;
533
+ substrBytes(str: TypedAggExpr<DocField>, start: TypedAggExpr<DocField>, count: TypedAggExpr<DocField>): TypedAggExpr<StringField>;
534
+ trim(args: {
535
+ input: TypedAggExpr<StringField>;
536
+ chars?: TypedAggExpr<StringField>;
537
+ }): TypedAggExpr<StringField>;
538
+ ltrim(args: {
539
+ input: TypedAggExpr<StringField>;
540
+ chars?: TypedAggExpr<StringField>;
541
+ }): TypedAggExpr<StringField>;
542
+ rtrim(args: {
543
+ input: TypedAggExpr<StringField>;
544
+ chars?: TypedAggExpr<StringField>;
545
+ }): TypedAggExpr<StringField>;
546
+ split(str: TypedAggExpr<DocField>, delimiter: TypedAggExpr<DocField>): TypedAggExpr<ArrayField>;
547
+ strLenCP(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
548
+ strLenBytes(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
549
+ regexMatch(args: {
550
+ input: TypedAggExpr<StringField>;
551
+ regex: TypedAggExpr<StringField>;
552
+ options?: TypedAggExpr<StringField>;
553
+ }): TypedAggExpr<BooleanField>;
554
+ regexFind(args: {
555
+ input: TypedAggExpr<StringField>;
556
+ regex: TypedAggExpr<StringField>;
557
+ options?: TypedAggExpr<StringField>;
558
+ }): TypedAggExpr<DocField>;
559
+ regexFindAll(args: {
560
+ input: TypedAggExpr<StringField>;
561
+ regex: TypedAggExpr<StringField>;
562
+ options?: TypedAggExpr<StringField>;
563
+ }): TypedAggExpr<ArrayField>;
564
+ replaceOne(args: {
565
+ input: TypedAggExpr<StringField>;
566
+ find: TypedAggExpr<StringField>;
567
+ replacement: TypedAggExpr<StringField>;
568
+ }): TypedAggExpr<StringField>;
569
+ replaceAll(args: {
570
+ input: TypedAggExpr<StringField>;
571
+ find: TypedAggExpr<StringField>;
572
+ replacement: TypedAggExpr<StringField>;
573
+ }): TypedAggExpr<StringField>;
574
+ cmp(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
575
+ eq(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
576
+ ne(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
577
+ gt(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
578
+ gte(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
579
+ lt(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
580
+ lte(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
581
+ arrayElemAt(arr: TypedAggExpr<DocField>, idx: TypedAggExpr<DocField>): TypedAggExpr<NullableDocField>;
582
+ concatArrays(...args: TypedAggExpr<DocField>[]): TypedAggExpr<ArrayField>;
583
+ firstElem(a: TypedAggExpr<DocField>): TypedAggExpr<NullableDocField>;
584
+ lastElem(a: TypedAggExpr<DocField>): TypedAggExpr<NullableDocField>;
585
+ isIn(elem: TypedAggExpr<DocField>, arr: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
586
+ indexOfArray(arr: TypedAggExpr<DocField>, value: TypedAggExpr<DocField>, ...rest: TypedAggExpr<DocField>[]): TypedAggExpr<NumericField>;
587
+ isArray(a: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
588
+ reverseArray(a: TypedAggExpr<DocField>): TypedAggExpr<ArrayField>;
589
+ slice(arr: TypedAggExpr<DocField>, ...rest: TypedAggExpr<DocField>[]): TypedAggExpr<ArrayField>;
590
+ zip(args: {
591
+ inputs: TypedAggExpr<ArrayField>[];
592
+ useLongestLength?: TypedAggExpr<BooleanField>;
593
+ defaults?: TypedAggExpr<ArrayField>;
594
+ }): TypedAggExpr<ArrayField>;
595
+ range(start: TypedAggExpr<DocField>, end: TypedAggExpr<DocField>, step: TypedAggExpr<DocField>): TypedAggExpr<ArrayField>;
596
+ setUnion(...args: TypedAggExpr<DocField>[]): TypedAggExpr<ArrayField>;
597
+ setIntersection(...args: TypedAggExpr<DocField>[]): TypedAggExpr<ArrayField>;
598
+ setDifference(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<ArrayField>;
599
+ setEquals(...args: TypedAggExpr<DocField>[]): TypedAggExpr<BooleanField>;
600
+ setIsSubset(a: TypedAggExpr<DocField>, b: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
601
+ anyElementTrue(a: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
602
+ allElementsTrue(a: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
603
+ typeOf(a: TypedAggExpr<DocField>): TypedAggExpr<StringField>;
604
+ convert(args: {
605
+ input: TypedAggExpr<DocField>;
606
+ to: TypedAggExpr<StringField | NumericField>;
607
+ onError?: TypedAggExpr<DocField>;
608
+ onNull?: TypedAggExpr<DocField>;
609
+ }): TypedAggExpr<DocField>;
610
+ toInt(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
611
+ toLong(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
612
+ toDouble(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
613
+ toDecimal(a: TypedAggExpr<DocField>): TypedAggExpr<NumericField>;
614
+ toString_(a: TypedAggExpr<DocField>): TypedAggExpr<StringField>;
615
+ toObjectId(a: TypedAggExpr<DocField>): TypedAggExpr<DocField>;
616
+ toBool(a: TypedAggExpr<DocField>): TypedAggExpr<BooleanField>;
617
+ toDate(a: TypedAggExpr<DocField>): TypedAggExpr<DateField>;
618
+ objectToArray(a: TypedAggExpr<DocField>): TypedAggExpr<ArrayField>;
619
+ arrayToObject(a: TypedAggExpr<DocField>): TypedAggExpr<DocField>;
620
+ getField(args: {
621
+ field: TypedAggExpr<StringField>;
622
+ input?: TypedAggExpr<DocField>;
623
+ }): TypedAggExpr<DocField>;
624
+ setField(args: {
625
+ field: TypedAggExpr<StringField>;
626
+ input: TypedAggExpr<DocField>;
627
+ value: TypedAggExpr<DocField>;
628
+ }): TypedAggExpr<DocField>;
629
+ };
630
+ //#endregion
631
+ //#region src/state-classes.d.ts
632
+ /**
633
+ * Root state of the query-builder state machine. Returned from
634
+ * `mongoQuery(...).from(name)` and bound to a single collection.
635
+ *
636
+ * Inherits the entire pipeline-stage surface from `PipelineChain` (since an
637
+ * empty `CollectionHandle` is observably an empty pipeline). Adds:
638
+ *
639
+ * - `match(...)` — overridden to transition to `FilteredCollection`, which
640
+ * accumulates filters for eventual splatting into write/find-and-modify
641
+ * wire commands.
642
+ * - **Insert / unqualified-write methods** (M2): `insertOne`, `insertMany`,
643
+ * `updateAll`, `deleteAll`. These live *only* here — the corresponding
644
+ * methods are absent from `FilteredCollection`, so a caller cannot
645
+ * accidentally produce an unqualified write by forgetting to `.match(...)`
646
+ * later in the chain. Bodies land in M2.
647
+ */
648
+ declare class CollectionHandle<TContract extends MongoContractWithTypeMaps<MongoContract, AnyMongoTypeMaps>, ModelName extends keyof MongoModelsMap<TContract> & string> extends PipelineChain<TContract, ModelToDocShape<TContract, ModelName>, 'update-cleared', 'fam-cleared', 'leading', ModelNestedShape<TContract, ModelName>> {
649
+ #private;
650
+ constructor(ctx: BindingContext<TContract>, modelName: ModelName);
651
+ /**
652
+ * Bound model name. Exposed so type tests can assert the binding without
653
+ * flipping into a pipeline. Not part of the public-API contract.
654
+ */
655
+ get _modelName(): ModelName;
656
+ /**
657
+ * Begin accumulating a filter. Transitions to `FilteredCollection`.
658
+ *
659
+ * Overrides `PipelineChain.match` (which appends another `$match` stage
660
+ * and stays in the chain). The two implementations are semantically
661
+ * equivalent for the read terminal — multiple `$match` stages AND-fold in
662
+ * Mongo — but `FilteredCollection` makes the accumulated filter
663
+ * addressable for the write/find-and-modify terminals landing in M2/M3.
664
+ */
665
+ match(filter: MongoFilterExpr): FilteredCollection<TContract, ModelName>;
666
+ match(fn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => MongoFilterExpr): FilteredCollection<TContract, ModelName>;
667
+ /**
668
+ * Insert a single document. Document fields are passed straight through to
669
+ * the wire `InsertOneCommand` — codec normalisation happens at the
670
+ * adapter/driver boundary, identically to the SQL builder (see Open Item
671
+ * #14 confirmation in the design conversation).
672
+ *
673
+ * Returns a `MongoQueryPlan<InsertOneResult>` whose row stream yields a
674
+ * single result document with the server-assigned `insertedId`.
675
+ */
676
+ insertOne(document: Record<string, MongoValue>): MongoQueryPlan<InsertOneResult, InsertOneCommand>;
677
+ /**
678
+ * Insert a batch of documents. Order is preserved in the returned
679
+ * `insertedIds` array.
680
+ */
681
+ insertMany(documents: ReadonlyArray<Record<string, MongoValue>>): MongoQueryPlan<InsertManyResult, InsertManyCommand>;
682
+ /**
683
+ * Update *every* document in the collection. Lives only on
684
+ * `CollectionHandle` — the corresponding method is intentionally absent
685
+ * from `FilteredCollection` so a caller cannot accidentally produce an
686
+ * unqualified write by forgetting to `.match(...)` first. Pair with
687
+ * `.match(...).updateMany(...)` for the filtered case.
688
+ */
689
+ updateAll(updaterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateManyCommand>;
690
+ /**
691
+ * Delete *every* document in the collection. See `updateAll` for the
692
+ * rationale around the unqualified-write surface being limited to this
693
+ * state class.
694
+ */
695
+ deleteAll(): MongoQueryPlan<DeleteResult, DeleteManyCommand>;
696
+ /**
697
+ * Insert-or-update the document matching `filterFn`. The filter is
698
+ * mandatory (vs. `updateAll`'s tautological match) because an upsert
699
+ * without a discriminating predicate would either match every existing
700
+ * document or insert an indistinguishable new one.
701
+ *
702
+ * Maps to `UpdateOneCommand` with `upsert: true`. The driver inserts a
703
+ * new document derived from the filter equality fields plus the update
704
+ * spec when no match is found; otherwise updates the matched document.
705
+ */
706
+ upsertOne(filterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => MongoFilterExpr, updaterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateOneCommand>;
707
+ }
708
+ /**
709
+ * State reached after one or more `.match(...)` calls on `CollectionHandle`.
710
+ *
711
+ * Inherits the pipeline-stage surface from `PipelineChain`, with the
712
+ * accumulated filters baked in as a leading `$match` stage on the underlying
713
+ * pipeline state. This means read-terminal output (`.aggregate()` /
714
+ * `.build()`) and any subsequent pipeline-stage chain see the filtered
715
+ * collection as input — the read story works through pure inheritance.
716
+ *
717
+ * Adds:
718
+ *
719
+ * - `match(...)` — pushes another `$match` stage *and* records the filter in
720
+ * the accumulator, so the eventual write/find-and-modify terminal can
721
+ * splat the AND-folded filter into the wire command's `filter` slot.
722
+ * - **Filtered writes** (M2): `updateMany`, `updateOne`, `deleteMany`,
723
+ * `deleteOne`, `upsertOne`. Stubbed in M1. (Upsert-many is an open
724
+ * question in the spec — see TML-2267 — and is intentionally absent.)
725
+ * - **Find-and-modify** (M3): `findOneAndUpdate`, `findOneAndDelete`.
726
+ * Stubbed in M1.
727
+ *
728
+ * Notably *does not* expose `insertOne`/`insertMany`/`updateAll`/`deleteAll`
729
+ * — those are insert or unqualified-write operations that are nonsense
730
+ * after a filter has been applied.
731
+ */
732
+ declare class FilteredCollection<TContract extends MongoContractWithTypeMaps<MongoContract, AnyMongoTypeMaps>, ModelName extends keyof MongoModelsMap<TContract> & string> extends PipelineChain<TContract, ModelToDocShape<TContract, ModelName>, 'update-cleared', 'fam-cleared', 'leading', ModelNestedShape<TContract, ModelName>> {
733
+ #private;
734
+ constructor(ctx: BindingContext<TContract>, modelName: ModelName, filters: ReadonlyArray<MongoFilterExpr>);
735
+ get _modelName(): ModelName;
736
+ /**
737
+ * Accumulated filter list. Exposed for the M2/M3 write/find-and-modify
738
+ * terminals to splat into wire-command `filter` slots; not part of the
739
+ * public-API contract.
740
+ */
741
+ get _filters(): ReadonlyArray<MongoFilterExpr>;
742
+ /**
743
+ * Append another filter to the accumulator. Returns a new
744
+ * `FilteredCollection` whose underlying pipeline rebuilds the leading
745
+ * `$match` from the AND-folded accumulator (rather than appending a
746
+ * second `$match` stage), so the write/find-and-modify terminals see a
747
+ * single authoritative filter expression.
748
+ */
749
+ match(filter: MongoFilterExpr): FilteredCollection<TContract, ModelName>;
750
+ match(fn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => MongoFilterExpr): FilteredCollection<TContract, ModelName>;
751
+ /**
752
+ * Update every matching document. `updaterFn` receives a `FieldAccessor`
753
+ * and returns an array of `TypedUpdateOp` (e.g. `[f.amount.inc(1),
754
+ * f.status.set('done')]`). Operators are folded into the wire-format
755
+ * update spec by `foldUpdateOps`, which throws on operator+path
756
+ * collisions.
757
+ */
758
+ updateMany(updaterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateManyCommand>;
759
+ /**
760
+ * Update at most one matching document. The driver picks the document
761
+ * (typically the first one matched by the underlying scan); no ordering
762
+ * guarantee is implied — chain `.sort(...)` and use the M3
763
+ * `.findOneAndUpdate(...)` terminal when ordering matters.
764
+ */
765
+ updateOne(updaterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateOneCommand>;
766
+ /**
767
+ * Delete every matching document.
768
+ */
769
+ deleteMany(): MongoQueryPlan<DeleteResult, DeleteManyCommand>;
770
+ /**
771
+ * Delete at most one matching document. See the `updateOne` note about
772
+ * driver-chosen victim selection.
773
+ */
774
+ deleteOne(): MongoQueryPlan<DeleteResult, DeleteOneCommand>;
775
+ /**
776
+ * Insert-or-update against the accumulated filter. Maps to
777
+ * `UpdateOneCommand` with `upsert: true`. Equivalent to
778
+ * `CollectionHandle.upsertOne(f => filter, updaterFn)` but reuses the
779
+ * already-accumulated `.match(...)` filter chain.
780
+ */
781
+ upsertOne(updaterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => UpdaterResult): MongoQueryPlan<UpdateResult, UpdateOneCommand>;
782
+ /**
783
+ * Find a single matching document and apply `updaterFn` to it.
784
+ *
785
+ * `opts.upsert` (default `false`) toggles insert-on-miss behaviour.
786
+ * `opts.returnDocument` (default `'after'`) controls whether the row
787
+ * stream yields the document as it was before or after the update.
788
+ */
789
+ findOneAndUpdate(updaterFn: (fields: FieldAccessor<ModelToDocShape<TContract, ModelName>, ModelNestedShape<TContract, ModelName>>) => UpdaterResult, opts?: {
790
+ readonly upsert?: boolean;
791
+ readonly returnDocument?: 'before' | 'after';
792
+ }): MongoQueryPlan<ResolveRow<ModelToDocShape<TContract, ModelName>, ExtractMongoCodecTypes<TContract>, TContract> | null, FindOneAndUpdateCommand>;
793
+ /**
794
+ * Find a single matching document and delete it. Returns the deleted
795
+ * document via the row stream.
796
+ */
797
+ findOneAndDelete(): MongoQueryPlan<ResolveRow<ModelToDocShape<TContract, ModelName>, ExtractMongoCodecTypes<TContract>, TContract> | null, FindOneAndDeleteCommand>;
798
+ }
799
+ /**
800
+ * Bound execution context shared across the three state classes.
801
+ */
802
+ interface BindingContext<TContract extends MongoContractWithTypeMaps<MongoContract, AnyMongoTypeMaps>> {
803
+ readonly contract: TContract;
804
+ readonly collection: string;
805
+ readonly storageHash: string;
806
+ }
807
+ //#endregion
808
+ //#region src/query.d.ts
809
+ /**
810
+ * Public entry point of the query builder. `mongoQuery(...).from(rootName)`
811
+ * yields the root state of the three-state machine
812
+ * (`CollectionHandle` → `FilteredCollection` → `PipelineChain`).
813
+ *
814
+ * `rawCommand(cmd)` is the escape hatch for cases the typed surface does
815
+ * not cover (yet) — it accepts any `AnyMongoCommand` (typed CRUD or a
816
+ * `RawMongoCommand` of `Document`s) and packages it into a `MongoQueryPlan`
817
+ * with `lane: 'mongo-query'`. Row type is `unknown` because the runtime
818
+ * cannot know what the caller's command yields.
819
+ */
820
+ interface QueryRoot<TContract extends MongoContractWithTypeMaps<MongoContract, AnyMongoTypeMaps>> {
821
+ from<K extends keyof TContract['roots'] & string>(rootName: K): CollectionHandle<TContract, RootModelName<TContract, K>>;
822
+ rawCommand<C extends AnyMongoCommand>(command: C): MongoQueryPlan<unknown, C>;
823
+ }
824
+ declare function mongoQuery<TContract extends MongoContractWithTypeMaps<MongoContract, AnyMongoTypeMaps>>(options: {
825
+ contractJson: unknown;
826
+ }): QueryRoot<TContract>;
827
+ //#endregion
828
+ //#region src/result-shape.d.ts
829
+ declare function contractFieldToMongoFieldShape(field: ContractField): MongoFieldShape;
830
+ declare function contractModelToMongoResultShape(model: MongoModelDefinition, options?: {
831
+ readonly selection?: readonly string[];
832
+ readonly includeRelationNames?: readonly string[];
833
+ }): MongoResultShape;
834
+ //#endregion
835
+ export { type ArrayField, type BooleanField, CollectionHandle, type DateField, type DeleteResult, type DocField, type DocShape, type Expression, type ExtractDocShape, type FieldAccessor, FilteredCollection, type FindAndModifyEnabled, type GroupSpec, type GroupedDocShape, type InsertManyResult, type InsertOneResult, type LeafExpression, type LiteralValue, type LookupBuilder, type LookupBuilderWithKey, type LookupFrom, type LookupOnResult, type LookupResult, type ModelArrayField, type ModelNestedShape, type ModelOf, type ModelToDocShape, type NestedDocShape, type NullableDocField, type NullableNumericField, type NumericField, type ObjectExpression, type ObjectField, type PathCompletions, PipelineChain, type ProjectedShape, type QueryRoot, type ResolvePath, type ResolveRow, type SortSpec, type StringField, type TypedAccumulatorExpr, type TypedAggExpr, type TypedUpdateOp, type UnwoundShape, type UpdateEnabled, type UpdateResult, type UpdaterResult, type ValidPaths, acc, contractFieldToMongoFieldShape, contractModelToMongoResultShape, createFieldAccessor, expr, fn, mongoQuery };
836
+ //# sourceMappingURL=query-builder.d.mts.map