@prisma-next/mongo-query-builder 0.12.0 → 0.13.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,349 +1,9 @@
1
+ import { A as ResolveRow, C as GroupedDocShape, D as NullableNumericField, E as NullableDocField, F as UnwoundShape, M as StringField, N as TypedAccumulatorExpr, O as NumericField, P as TypedAggExpr, S as GroupSpec, T as ModelToDocShape, _ as BooleanField, a as createFieldAccessor, b as DocShape, c as UpdaterResult, d as NestedDocShape, f as ObjectField, g as ArrayField, h as ValidPaths, i as ObjectExpression, j as SortSpec, k as ProjectedShape, l as ModelArrayField, m as ResolvePath, n as FieldAccessor, o as expr, p as PathCompletions, r as LeafExpression, s as TypedUpdateOp, t as Expression, u as ModelNestedShape, v as DateField, w as LiteralValue, x as ExtractDocShape, y as DocField } from "./field-accessor-BmcQj0lD.mjs";
1
2
  import { AggregateCommand, AnyMongoCommand, DeleteManyCommand, DeleteOneCommand, DeleteResult, DeleteResult as DeleteResult$1, FindOneAndDeleteCommand, FindOneAndUpdateCommand, InsertManyCommand, InsertManyResult, InsertManyResult as InsertManyResult$1, InsertOneCommand, InsertOneResult, InsertOneResult as InsertOneResult$1, MongoAggAccumulator, MongoAggExpr, MongoDensifyRange, MongoFieldShape, MongoFillOutput, MongoFilterExpr, MongoPipelineStage, MongoQueryPlan, MongoResultShape, MongoUpdatePipelineStage, MongoWindowField, UpdateManyCommand, UpdateOneCommand, UpdateResult, UpdateResult as UpdateResult$1 } from "@prisma-next/mongo-query-ast/execution";
2
- import { ContractField, ContractValueObjectsMap } from "@prisma-next/contract/types";
3
- import { ExtractMongoCodecTypes, ExtractMongoTypeMaps, InferModelRow, MongoContract, MongoContractWithTypeMaps, MongoModelDefinition, MongoModelsMap, MongoTypeMaps, RootModelName } from "@prisma-next/mongo-contract";
3
+ import { ContractField } from "@prisma-next/contract/types";
4
4
  import { MongoValue } from "@prisma-next/mongo-value";
5
+ import { ExtractMongoCodecTypes, MongoContract, MongoContractWithTypeMaps, MongoModelDefinition, MongoModelsMap, MongoTypeMaps, RootModelName } from "@prisma-next/mongo-contract";
5
6
 
6
- //#region src/resolve-path.d.ts
7
- /**
8
- * Marker `DocField` variant representing a non-leaf (value-object) path in
9
- * a [NestedDocShape]. Extends `DocField` with a `fields` property carrying
10
- * the sub-shape so the pipeline builder can recurse into it.
11
- *
12
- * `codecId` is the reserved literal `'prisma/object@1'`; the accessor's
13
- * runtime implementation does not serialize it — the codec id is a purely
14
- * type-level sentinel used by `Expression<F>` to select the reduced
15
- * operator surface for non-leaf paths.
16
- *
17
- * `nullable` tracks whether the value object itself may be absent/null on
18
- * the parent document. The callable form currently does not propagate the
19
- * parent's `nullable` flag onto leaves beneath it (path traversal under a
20
- * nullable parent resolves to the leaf's own `nullable` — matching how
21
- * MongoDB treats missing intermediate documents).
22
- */
23
- interface ObjectField<N extends NestedDocShape> extends DocField {
24
- readonly codecId: 'prisma/object@1';
25
- readonly nullable: boolean;
26
- readonly fields: N;
27
- }
28
- /**
29
- * Marker `DocField` variant representing "an array of foreign-model rows"
30
- * — the result of a `$lookup` whose foreign side was selected via the
31
- * typed `col` accessor. `ModelName` is the literal foreign model name
32
- * (e.g. `'User'`), preserved in the type so `ResolveRow` can resolve the
33
- * field to `ResolveRow<ModelToDocShape<TC, ModelName>, …>[]` rather than
34
- * the opaque `unknown[]` produced by the legacy `mongo/array@1` sentinel.
35
- *
36
- * Like `ObjectField`, the codec id is a purely type-level sentinel —
37
- * there is no runtime codec entry for `'prisma/modelArray@1'`. The
38
- * surrounding pipeline emits the standard `MongoLookupStage` whose
39
- * runtime shape is unchanged; the marker exists solely to thread the
40
- * foreign element type through the result-row resolver.
41
- */
42
- interface ModelArrayField<ModelName extends string> extends DocField {
43
- readonly codecId: 'prisma/modelArray@1';
44
- readonly nullable: false;
45
- readonly model: ModelName;
46
- }
47
- /**
48
- * Phantom-symbol brand placed on `ModelToDocShape`'s output (and inherited
49
- * through shape-extending stages like `addFields`, `match`, `lookup`) so
50
- * `ResolveRow` can route value-object resolution through `InferModelRow`
51
- * — which walks the contract's `valueObjects` registry and resolves
52
- * nested types — instead of falling through the codec-lookup branch to
53
- * `unknown`.
54
- *
55
- * The brand is keyed on a `unique symbol` rather than a string so it is
56
- * invisible to every `keyof Shape & string` walk in the package
57
- * (`SortSpec`, `ProjectedShape`, `GroupedDocShape`, the field accessor's
58
- * `Object.keys` iteration, etc.). Field accessors do not surface a
59
- * `__modelOrigin` autocomplete entry; sorts cannot key on it; projections
60
- * cannot reference it.
61
- *
62
- * Shape-extending stages preserve the brand because intersection types
63
- * (`Shape & NewFields`) carry through symbol-keyed properties. Shape-
64
- * replacing stages (`replaceRoot`, `group`, `project`) construct fresh
65
- * `DocShape`s from scratch, naturally dropping the brand — which is the
66
- * intended behaviour, since those stages legitimately leave model
67
- * territory.
68
- */
69
- declare const ModelOriginBrand: unique symbol;
70
- type ModelOriginBrand = typeof ModelOriginBrand;
71
- /**
72
- * Brand carrier — a Shape whose row type should be resolved via
73
- * `InferModelRow<TContract, ModelName>` rather than the per-field codec
74
- * walk. Use as `ModelToDocShape<TC, ModelName> & ModelOriginBranded<ModelName>`.
75
- */
76
- type ModelOriginBranded<ModelName extends string> = {
77
- readonly [ModelOriginBrand]?: ModelName;
78
- };
79
- /**
80
- * Document shape that carries nested value-object sub-shapes.
81
- *
82
- * Structurally identical to a flat `DocShape` (`Record<string, DocField>`),
83
- * but individual values may be `ObjectField<SubShape>` carrying a nested
84
- * `NestedDocShape` sub-tree. The pipeline builder threads a
85
- * `NestedDocShape` alongside the flat `DocShape` so the callable
86
- * `f('a.b.c')` form can validate dot-paths at the type level.
87
- *
88
- * When a stage transforms the root shape in a way that invalidates nested
89
- * paths (e.g. `$group`, `$project`, `$replaceRoot`), the thread is reset
90
- * to the empty shape `Record<string, never>` — which makes `ValidPaths`
91
- * resolve to `never` and so disables the callable form downstream.
92
- */
93
- type NestedDocShape = Record<string, DocField>;
94
- type FieldToLeaf<F> = F extends {
95
- readonly type: {
96
- readonly kind: 'scalar';
97
- readonly codecId: infer C extends string;
98
- };
99
- readonly nullable: infer N extends boolean;
100
- } ? {
101
- readonly codecId: C;
102
- readonly nullable: N;
103
- } : F extends {
104
- readonly many: true;
105
- readonly nullable: infer N extends boolean;
106
- } ? {
107
- readonly codecId: 'mongo/array@1';
108
- readonly nullable: N;
109
- } : DocField;
110
- /**
111
- * Translate a single contract field to its nested-shape form. Scalars
112
- * become `DocField` leaves; value-object fields become
113
- * `ObjectField<Sub>`; `many: true` stops at a leaf; anything else falls
114
- * through to the opaque `DocField` base.
115
- *
116
- * Kept as a per-field helper (rather than a `Fields → NestedShape` helper
117
- * that maps over keys internally) so the parent mapped type stays
118
- * homomorphic over the model/value-object `fields` record. Homomorphic
119
- * mapped types preserve the literal keys of their source object through
120
- * TypeScript's intersection-collapsing machinery, which keeps
121
- * `ModelNestedShape` hover output and `keyof`/indexed-access resolution
122
- * concrete instead of collapsing to `{ [x: string]: … }`.
123
- */
124
- type TranslateField<TContract extends MongoContract, F> = F extends {
125
- readonly many: true;
126
- } ? FieldToLeaf<F> : F extends {
127
- readonly type: {
128
- readonly kind: 'valueObject';
129
- readonly name: infer VOName extends string;
130
- };
131
- readonly nullable: infer Null extends boolean;
132
- } ? ObjectField<VONestedShape<TContract, VOName>> & {
133
- readonly nullable: Null;
134
- } : F extends {
135
- readonly type: {
136
- readonly kind: 'scalar';
137
- readonly codecId: string;
138
- };
139
- } ? FieldToLeaf<F> : DocField;
140
- /**
141
- * Resolve a named value object from the contract into its own
142
- * `NestedDocShape`. The mapped iteration is inlined here (not delegated
143
- * to a generic helper) so that the homomorphism over
144
- * `VOs[VOName]['fields']` is preserved and the hover / indexed-access
145
- * surface stays concrete at instantiation time.
146
- */
147
- type VONestedShape<TContract extends MongoContract, VOName extends string> = [ContractValueObjectsMap<TContract>] extends [infer VOs extends Record<string, {
148
- readonly fields: Record<string, unknown>;
149
- }>] ? VOName extends keyof VOs & string ? { readonly [K in keyof VOs[VOName]['fields'] & string]: TranslateField<TContract, VOs[VOName]['fields'][K]> } : never : never;
150
- /**
151
- * Build the `NestedDocShape` for a model. Scalar leaves resolve to their
152
- * concrete codec id; value-object fields recurse into the referenced
153
- * `valueObjects[VOName].fields` table, producing a tree that
154
- * `ResolvePath` / `ValidPaths` can walk.
155
- *
156
- * The mapped iteration is inlined (not hidden behind a helper type that
157
- * takes `Fields` as a generic) so TypeScript recognises the mapped type
158
- * as homomorphic over `MongoModelsMap<TContract>[ModelName]['fields']`. That
159
- * preserves the literal field-name keys at instantiation — without this,
160
- * the intersection of `Record<string, ContractField>` and the specific
161
- * literal field record collapses `keyof` to `string` and the result hover
162
- * degrades to `{ readonly [x: string]: any }`.
163
- */
164
- type ModelNestedShape<TContract extends MongoContract, ModelName extends string & keyof MongoModelsMap<TContract>> = { readonly [K in keyof MongoModelsMap<TContract>[ModelName]['fields'] & string]: TranslateField<TContract, MongoModelsMap<TContract>[ModelName]['fields'][K]> };
165
- /**
166
- * Resolve a dot-path against a `NestedDocShape`. Returns:
167
- * - the leaf `DocField` when `Path` terminates on a scalar/array leaf,
168
- * - the `ObjectField<Sub>` when `Path` terminates on a value object (so
169
- * the caller can operate on the whole sub-document),
170
- * - `never` when the path is invalid (unknown segment, or a scalar
171
- * segment followed by further traversal).
172
- *
173
- * Paired with the constrained callable `<P extends ValidPaths<N>>(path: P)
174
- * => Expression<ResolvePath<N, P>>` so the IDE offers completions and
175
- * rejects bad paths with a clear error instead of silently resolving to
176
- * `never`.
177
- */
178
- type ResolvePath<N extends NestedDocShape, Path extends string> = Path extends `${infer Head}.${infer Rest}` ? Head extends keyof N & string ? N[Head] extends ObjectField<infer Sub> ? ResolvePath<Sub, Rest> : never : never : Path extends keyof N & string ? N[Path] : never;
179
- /**
180
- * Union of every valid dot-path within a `NestedDocShape`. Includes
181
- * top-level keys (scalar leaves *and* value-object roots) and every
182
- * recursive descent through `ObjectField` sub-shapes.
183
- *
184
- * Non-leaf paths are intentionally included — `f('address')` yields an
185
- * `Expression<ObjectField<…>>` whose reduced operator surface (`set`,
186
- * `unset`, `exists`, `eq(null)`, `ne(null)`) lets callers operate on the
187
- * whole value object. Leaf paths like `f('address.city')` get the full
188
- * leaf operator surface.
189
- *
190
- * The `string extends keyof N` guard short-circuits to `never` for
191
- * open-ended index-signature shapes (e.g. the default
192
- * `Record<string, never>` used to represent "no nested information" —
193
- * notably downstream of replacement stages in the pipeline builder). An
194
- * open-ended `keyof` cannot resolve a specific literal path, so the
195
- * callable form must be disabled at the type level.
196
- */
197
- type ValidPaths<N extends NestedDocShape> = string extends keyof N ? never : { [K in keyof N & string]: N[K] extends ObjectField<infer Sub> ? K | `${K}.${ValidPaths<Sub>}` : K }[keyof N & string];
198
- /**
199
- * IDE-oriented alias for `ValidPaths`. Kept as a separate export so future
200
- * refinements (e.g. ArkType-style lazy expansion for very deep shapes) can
201
- * diverge from the strict `ValidPaths` constraint without breaking
202
- * downstream consumers. For now the two are intentionally equivalent.
203
- */
204
- type PathCompletions<N extends NestedDocShape> = ValidPaths<N>;
205
- //#endregion
206
- //#region src/types.d.ts
207
- interface DocField {
208
- readonly codecId: string;
209
- readonly nullable: boolean;
210
- }
211
- type NumericField = {
212
- readonly codecId: 'mongo/double@1';
213
- readonly nullable: false;
214
- };
215
- type NullableNumericField = {
216
- readonly codecId: 'mongo/double@1';
217
- readonly nullable: true;
218
- };
219
- type StringField = {
220
- readonly codecId: 'mongo/string@1';
221
- readonly nullable: false;
222
- };
223
- type ArrayField = {
224
- readonly codecId: 'mongo/array@1';
225
- readonly nullable: false;
226
- };
227
- type BooleanField = {
228
- readonly codecId: 'mongo/bool@1';
229
- readonly nullable: false;
230
- };
231
- type DateField = {
232
- readonly codecId: 'mongo/date@1';
233
- readonly nullable: false;
234
- };
235
- type NullableDocField = {
236
- readonly codecId: string;
237
- readonly nullable: true;
238
- };
239
- type LiteralValue<F extends DocField> = F extends StringField ? string : F extends NumericField ? number : F extends BooleanField ? boolean : F extends DateField ? Date : unknown;
240
- type DocShape = Record<string, DocField>;
241
- type ExtractCodecId<F> = F extends {
242
- type: {
243
- kind: 'scalar';
244
- codecId: infer C;
245
- };
246
- } ? C : F extends {
247
- codecId: infer C extends string;
248
- } ? C : string;
249
- type ModelToDocShape<TContract extends MongoContract, ModelName extends string & keyof MongoModelsMap<TContract>> = { [K in keyof MongoModelsMap<TContract>[ModelName]['fields'] & string]: {
250
- readonly codecId: ExtractCodecId<MongoModelsMap<TContract>[ModelName]['fields'][K]>;
251
- readonly nullable: MongoModelsMap<TContract>[ModelName]['fields'][K]['nullable'];
252
- } } & ModelOriginBranded<ModelName>;
253
- /**
254
- * Per-field resolver. Walks `Shape`'s string keys, routing
255
- * `ModelArrayField` (the lookup marker) through `InferModelRow` and
256
- * everything else through the codec-lookup branch.
257
- *
258
- * Internal helper — public callers should use `ResolveRow`, which adds
259
- * the model-origin brand detection on top.
260
- */
261
- type ResolveFields<Shape extends DocShape, CodecTypes extends Record<string, {
262
- readonly output: unknown;
263
- }>, TContract extends MongoContract> = { -readonly [K in keyof Shape & string]: Shape[K] extends ModelArrayField<infer ModelName> ? IsConcreteContract<TContract> extends true ? TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps> ? ModelName extends string & keyof MongoModelsMap<TContract> ? Array<InferModelRow<TContract, ModelName>> : unknown[] : unknown[] : unknown[] : Shape[K]['codecId'] extends keyof CodecTypes ? Shape[K]['nullable'] extends true ? CodecTypes[Shape[K]['codecId']]['output'] | null : CodecTypes[Shape[K]['codecId']]['output'] : unknown };
264
- /**
265
- * Resolve a `DocShape` to a concrete row object type.
266
- *
267
- * The optional `TContract` parameter exists so the resolver can:
268
- *
269
- * 1. Detect the `ModelOriginBrand` on `Shape` — the phantom symbol
270
- * placed by `ModelToDocShape`. When present (and the contract has
271
- * type maps), the row is resolved via `InferModelRow<TC, M>` from
272
- * `@prisma-next/mongo-contract`, which walks scalar / valueObject /
273
- * union field kinds (handling nested value-objects and `many: true`).
274
- * This makes entry-point reads (`q.from('users').build()`) and
275
- * shape-extending stages (`match`, `addFields`) resolve value-object
276
- * fields to their concrete nested types instead of `unknown`.
277
- *
278
- * 2. Detect the per-field `ModelArrayField<ModelName>` marker produced
279
- * by `lookup()` and resolve it to `Array<InferModelRow<TC, M>>` so
280
- * lookup rows carry the same fully-typed foreign rows.
281
- *
282
- * When the contract is not threaded through (or lacks the type-map
283
- * phantom), both branches fall back to `unknown` / `unknown[]` —
284
- * preserving the legacy resolver shape for call sites that do not need
285
- * model-row resolution.
286
- */
287
- /**
288
- * Flatten an intersection `A & B` into a single object literal so callers
289
- * (and `expectTypeOf().toEqualTypeOf<…>()`) see one homogeneous record
290
- * rather than the intersection form. Vitest's strict equality check
291
- * treats `A & B` as distinct from the structurally-equivalent flat
292
- * record, even when assignability is bidirectional, so the
293
- * `ResolveRow` brand-positive branch normalises its result through this.
294
- */
295
- type Flatten<T> = T extends infer U ? { [K in keyof U]: U[K] } : never;
296
- /**
297
- * Decide whether to route a brand-positive `ResolveRow` through
298
- * `InferModelRow`. The default `MongoContract` (no concrete models)
299
- * still satisfies `MongoContractWithTypeMaps<MongoContract, MongoTypeMaps>`
300
- * because the phantom key is optional, but `InferModelRow<MongoContract, …>`
301
- * collapses to an empty/unknown row. Gate on the presence of the
302
- * type-maps phantom: a concrete contract attaches concrete `TestTypeMaps`-
303
- * shaped maps, while the default `MongoContract` has no phantom and
304
- * `ExtractMongoTypeMaps` resolves to `never`.
305
- */
306
- type IsConcreteContract<TContract> = [ExtractMongoTypeMaps<TContract>] extends [never] ? false : true;
307
- type ResolveRow<Shape extends DocShape, CodecTypes extends Record<string, {
308
- readonly output: unknown;
309
- }>, TContract extends MongoContract = MongoContract> = Shape extends {
310
- readonly [ModelOriginBrand]?: infer ModelName extends string;
311
- } ? IsConcreteContract<TContract> extends true ? TContract extends MongoContractWithTypeMaps<MongoContract, MongoTypeMaps> ? ModelName extends string & keyof MongoModelsMap<TContract> ? Flatten<InferModelRow<TContract, ModelName> & Omit<ResolveFields<Shape, CodecTypes, TContract>, keyof InferModelRow<TContract, ModelName>>> : ResolveFields<Shape, CodecTypes, TContract> : ResolveFields<Shape, CodecTypes, TContract> : ResolveFields<Shape, CodecTypes, TContract> : ResolveFields<Shape, CodecTypes, TContract>;
312
- interface TypedAggExpr<F extends DocField> {
313
- readonly _field: F;
314
- readonly node: MongoAggExpr;
315
- }
316
- interface TypedAccumulatorExpr<F extends DocField> {
317
- readonly _field: F;
318
- readonly node: MongoAggAccumulator;
319
- }
320
- type ExtractDocShape<T extends Record<string, TypedAggExpr<DocField>>> = { [K in keyof T & string]: T[K]['_field'] };
321
- type SortSpec<S extends DocShape> = Partial<Record<keyof S & string, 1 | -1>>;
322
- type ProjectedShape<Shape extends DocShape, Spec extends Record<string, 1 | TypedAggExpr<DocField>>> = { [K in keyof Spec & string]: Spec[K] extends 1 ? K extends keyof Shape ? Shape[K] : DocField : Spec[K] extends TypedAggExpr<infer F> ? F : DocField } & ('_id' extends keyof Shape ? '_id' extends keyof Spec ? Record<keyof never, never> : Pick<Shape, '_id'> : Record<keyof never, never>);
323
- type GroupSpec = {
324
- _id: TypedAggExpr<DocField> | null;
325
- [key: string]: TypedAggExpr<DocField> | TypedAccumulatorExpr<DocField> | null;
326
- };
327
- type GroupedDocShape<Spec extends GroupSpec> = { [K in keyof Spec & string]: Spec[K] extends TypedAggExpr<infer F> ? F : Spec[K] extends TypedAccumulatorExpr<infer F> ? F : Spec[K] extends null ? {
328
- readonly codecId: 'mongo/null@1';
329
- readonly nullable: true;
330
- } : DocField };
331
- /**
332
- * Intentionally identity — full array element type extraction is deferred.
333
- * Used by `UnwoundShape` so the unwind result shape can be refined later
334
- * without changing the public API.
335
- */
336
- type UnwrapArrayDocField<F extends DocField> = F;
337
- /**
338
- * `$unwind` reshapes the array slot but leaves the rest of the document
339
- * structurally intact. The mapped iteration is keyed on `keyof S & string`,
340
- * which discards the symbol-keyed `ModelOriginBrand` carried by
341
- * model-rooted shapes. Preserve the brand explicitly so post-unwind
342
- * `ResolveRow` still routes through `InferModelRow` and value-object
343
- * fields keep their concrete nested types.
344
- */
345
- type UnwoundShape<S extends DocShape, K extends keyof S & string> = { [P in keyof S & string]: P extends K ? UnwrapArrayDocField<S[P]> : S[P] } & (S extends ModelOriginBranded<infer ModelName extends string> ? ModelOriginBranded<ModelName> : unknown);
346
- //#endregion
347
7
  //#region src/accumulator-helpers.d.ts
348
8
  declare const acc: {
349
9
  sum<F extends DocField>(expr: TypedAggExpr<F>): TypedAccumulatorExpr<F>;
@@ -405,229 +65,6 @@ declare const acc: {
405
65
  }): TypedAccumulatorExpr<ArrayField>;
406
66
  };
407
67
  //#endregion
408
- //#region src/update-ops.d.ts
409
- /**
410
- * Per-field update operations produced by `Expression`'s update methods
411
- * (`set`, `inc`, `push`, …). A write terminal folds an array of these into a
412
- * `MongoUpdateSpec` record (`{ $set: { … }, $inc: { … }, … }`) before
413
- * constructing the underlying `UpdateManyCommand` / `UpdateOneCommand` AST node.
414
- *
415
- * One `TypedUpdateOp` value corresponds to one Mongo update operator applied
416
- * to one field path. The `op` string is the wire-level operator name (`$set`,
417
- * `$inc`, …); the `path` is the dot-path to the field (or its top-level name).
418
- */
419
- type TypedUpdateOp = {
420
- readonly op: '$set';
421
- readonly path: string;
422
- readonly value: MongoValue;
423
- } | {
424
- readonly op: '$unset';
425
- readonly path: string;
426
- } | {
427
- readonly op: '$rename';
428
- readonly path: string;
429
- readonly newName: string;
430
- } | {
431
- readonly op: '$inc';
432
- readonly path: string;
433
- readonly amount: number;
434
- } | {
435
- readonly op: '$mul';
436
- readonly path: string;
437
- readonly factor: number;
438
- } | {
439
- readonly op: '$min';
440
- readonly path: string;
441
- readonly value: MongoValue;
442
- } | {
443
- readonly op: '$max';
444
- readonly path: string;
445
- readonly value: MongoValue;
446
- } | {
447
- readonly op: '$push';
448
- readonly path: string;
449
- readonly value: MongoValue;
450
- } | {
451
- readonly op: '$addToSet';
452
- readonly path: string;
453
- readonly value: MongoValue;
454
- } | {
455
- readonly op: '$pop';
456
- readonly path: string;
457
- readonly direction: 1 | -1;
458
- } | {
459
- readonly op: '$pull';
460
- readonly path: string;
461
- readonly value: MongoValue;
462
- } | {
463
- readonly op: '$pullAll';
464
- readonly path: string;
465
- readonly values: ReadonlyArray<MongoValue>;
466
- } | {
467
- readonly op: '$currentDate';
468
- readonly path: string;
469
- } | {
470
- readonly op: '$setOnInsert';
471
- readonly path: string;
472
- readonly value: MongoValue;
473
- };
474
- /**
475
- * The return type for updater callbacks. Typed as a union of homogeneous
476
- * arrays so mixed-shape updaters (operator + pipeline stage in the same
477
- * array) are a compile error. The runtime guard in `resolveUpdaterResult`
478
- * remains as defence-in-depth.
479
- */
480
- type UpdaterResult = ReadonlyArray<TypedUpdateOp> | ReadonlyArray<MongoUpdatePipelineStage>;
481
- //#endregion
482
- //#region src/field-accessor.d.ts
483
- /**
484
- * Operator surface for leaf (scalar) paths — today's full set: filter,
485
- * update, and aggregation operators. Returned by `Expression<F>` for any
486
- * `F extends DocField` that is not an `ObjectField<…>` sub-tree.
487
- *
488
- * Operator surfaces are intentionally not trait-gated by codec in this
489
- * revision — tracked on Linear as TML-2259 (scope extended to cover the
490
- * query-builder's `Expression<F>`). Calling, e.g. `.inc(1)` on a
491
- * string-typed expression compiles; the runtime relies on Mongo to
492
- * surface the error. Trait-gating can be tightened in a follow-up
493
- * without changing the accessor's public shape.
494
- */
495
- interface LeafExpression<F extends DocField> extends TypedAggExpr<F> {
496
- readonly _path: string;
497
- eq(value: MongoValue): MongoFilterExpr;
498
- ne(value: MongoValue): MongoFilterExpr;
499
- gt(value: MongoValue): MongoFilterExpr;
500
- gte(value: MongoValue): MongoFilterExpr;
501
- lt(value: MongoValue): MongoFilterExpr;
502
- lte(value: MongoValue): MongoFilterExpr;
503
- in(values: ReadonlyArray<MongoValue>): MongoFilterExpr;
504
- nin(values: ReadonlyArray<MongoValue>): MongoFilterExpr;
505
- exists(flag?: boolean): MongoFilterExpr;
506
- set(value: MongoValue): TypedUpdateOp;
507
- unset(): TypedUpdateOp;
508
- rename(newName: string): TypedUpdateOp;
509
- inc(amount: number): TypedUpdateOp;
510
- mul(factor: number): TypedUpdateOp;
511
- min(value: MongoValue): TypedUpdateOp;
512
- max(value: MongoValue): TypedUpdateOp;
513
- push(value: MongoValue): TypedUpdateOp;
514
- addToSet(value: MongoValue): TypedUpdateOp;
515
- pop(direction?: 1 | -1): TypedUpdateOp;
516
- pull(value: MongoValue): TypedUpdateOp;
517
- pullAll(values: ReadonlyArray<MongoValue>): TypedUpdateOp;
518
- currentDate(): TypedUpdateOp;
519
- setOnInsert(value: MongoValue): TypedUpdateOp;
520
- }
521
- /**
522
- * Operator surface for non-leaf (value-object) paths — `f('address')`
523
- * when `address` is a `ContractValueObject`. Intentionally minimal: the
524
- * whole-value ops that make sense on a structured sub-document
525
- * (`set`/`unset`/`exists`, null presence via `eq(null)`/`ne(null)`). Field-
526
- * level ops belong on the constituent leaves (`f('address.city')`).
527
- *
528
- * The aggregation `node` is still present (`TypedAggExpr<ObjectField<N>>`)
529
- * so the value object can be piped through `$addFields` /
530
- * `$replaceRoot` / etc. as-is.
531
- */
532
- interface ObjectExpression<N extends NestedDocShape> extends TypedAggExpr<ObjectField<N>> {
533
- readonly _path: string;
534
- exists(flag?: boolean): MongoFilterExpr;
535
- eq(value: null): MongoFilterExpr;
536
- ne(value: null): MongoFilterExpr;
537
- set(value: MongoValue): TypedUpdateOp;
538
- unset(): TypedUpdateOp;
539
- }
540
- /**
541
- * The unified field accessor expression returned by `FieldAccessor` (per
542
- * [ADR 180](../../../../docs/architecture%20docs/adrs/ADR%20180%20-%20Dot-path%20field%20accessor.md)).
543
- *
544
- * Resolves to `ObjectExpression<Sub>` when `F` is an `ObjectField<Sub>`
545
- * (non-leaf path), otherwise to `LeafExpression<F>` (the full operator
546
- * surface). The conditional is driven off the `fields` marker that
547
- * `ObjectField` adds to `DocField`, so existing code that uses plain
548
- * `DocField` shapes continues to resolve to `LeafExpression`.
549
- */
550
- type Expression<F extends DocField> = F extends ObjectField<infer N> ? ObjectExpression<N> : LeafExpression<F>;
551
- /**
552
- * Emitters for MongoDB update-pipeline stages (`$addFields`/`$set`,
553
- * `$project`/`$unset`, `$replaceRoot`/`$replaceWith`). These return
554
- * `MongoUpdatePipelineStage` nodes and let an updater callback express
555
- * the pipeline-form update as an alternative to the typed-operator form.
556
- *
557
- * The two forms are mutually exclusive per updater call: `resolveUpdaterResult`
558
- * rejects arrays that mix `TypedUpdateOp` and `MongoUpdatePipelineStage`
559
- * entries with a clear error — an updater callback must return either all
560
- * typed ops or all pipeline stages. Pick the form that matches the update
561
- * you want and commit to it for that call site.
562
- *
563
- * Accessible via `f.stage` on the `FieldAccessor`.
564
- */
565
- interface StageEmitters {
566
- set(fields: Record<string, MongoAggExpr>): MongoUpdatePipelineStage;
567
- unset(...paths: ReadonlyArray<string>): MongoUpdatePipelineStage;
568
- replaceRoot(newRoot: MongoAggExpr): MongoUpdatePipelineStage;
569
- replaceWith(newRoot: MongoAggExpr): MongoUpdatePipelineStage;
570
- }
571
- /**
572
- * The unified `FieldAccessor` per ADR 180.
573
- *
574
- * - Property access (`f.status`) returns an `Expression<F>` whose codec
575
- * comes from the current pipeline shape `S`.
576
- * - Callable form (`f('address.city')`) returns an `Expression<ResolvePath<N, P>>`
577
- * where `N` is the nested shape carrying value-object sub-shapes.
578
- * Paths that don't exist in `N` are rejected with a compile-time error
579
- * (via `P extends ValidPaths<N>`). Non-leaf paths like `f('address')`
580
- * resolve to an `ObjectExpression` whose reduced surface covers the
581
- * whole-value operations (`set`, `unset`, `exists`, `eq(null)`,
582
- * `ne(null)`).
583
- * - `f.rawPath('path')` is a deliberate escape hatch that skips path
584
- * validation and returns a `LeafExpression<F>` for the given string.
585
- * Intended for migration authoring where the target field is not yet
586
- * part of the typed contract (e.g. a backfill writing a newly-added
587
- * column before the contract hash rolls forward). The method name is
588
- * deliberately `rawPath` rather than `raw` so it does not shadow a
589
- * legitimate top-level `raw` field on a user model.
590
- * - `f.stage` exposes pipeline-style update emitters (`$set`, `$unset`,
591
- * `$replaceRoot`, `$replaceWith`).
592
- *
593
- * When `N` is `Record<string, never>` (the default — e.g. after a
594
- * replacement stage like `$group` / `$project` / `$replaceRoot`),
595
- * `ValidPaths<N>` is `never` and the callable form is effectively
596
- * disabled at the type level. This keeps the builder sound downstream of
597
- * stages that invalidate the original document's nested-path tree.
598
- * `f.rawPath(...)` remains available in that state for callers that need
599
- * an explicit unvalidated path.
600
- */
601
- type FieldAccessor<S extends DocShape, N extends NestedDocShape = Record<string, never>> = { readonly [K in keyof S & string]: Expression<S[K]> } & (<P extends ValidPaths<N>>(path: P) => Expression<ResolvePath<N, P>>) & {
602
- readonly stage: StageEmitters;
603
- /**
604
- * Escape hatch: build a `LeafExpression<F>` for an unvalidated string
605
- * path. Use only when the path is intentionally outside the typed
606
- * model surface — data-migration authoring is the canonical case
607
- * (e.g. backfilling a field that is not yet in the contract). Default
608
- * `F` is the opaque `DocField`; callers can narrow via the explicit
609
- * generic: `f.rawPath<StringField>("status").set("active")`.
610
- *
611
- * The method is named `rawPath` (not `raw`) so a user model with a
612
- * top-level `raw` field still resolves `f.raw` to the field-expression
613
- * property, not to this escape hatch. Does not participate in
614
- * `ValidPaths<N>` / `ResolvePath<N, P>` — the path is passed through
615
- * verbatim and no IDE autocomplete is offered.
616
- */
617
- rawPath<F extends DocField = DocField>(path: string): LeafExpression<F>;
618
- };
619
- /**
620
- * Construct a unified `FieldAccessor<S, N>` proxy. Property access creates
621
- * an `Expression` using the property name as the field path; callable
622
- * form accepts a dot-path string validated against `N` at compile time.
623
- *
624
- * The proxy target is a function so the resulting object is both callable
625
- * and indexable. Symbol-keyed accesses (e.g. `Symbol.toPrimitive`) return
626
- * `undefined` to keep accidental coercion behaviour unsurprising —
627
- * matching the previous `FieldProxy` / `FilterProxy` semantics.
628
- */
629
- declare function createFieldAccessor<S extends DocShape, N extends NestedDocShape = Record<string, never>>(): FieldAccessor<S, N>;
630
- //#endregion
631
68
  //#region src/lookup-builder.d.ts
632
69
  /**
633
70
  * Resolved foreign-model name for a contract root. Looks `RootName` up
@@ -1393,5 +830,5 @@ declare function contractModelToMongoResultShape(model: MongoModelDefinition, op
1393
830
  readonly includeRelationNames?: readonly string[];
1394
831
  }): MongoResultShape;
1395
832
  //#endregion
1396
- 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, fn, mongoQuery };
833
+ 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 };
1397
834
  //# sourceMappingURL=index.d.mts.map