sluice-orm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +373 -0
- package/README.md +191 -0
- package/dist/index.cjs +977 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +2444 -0
- package/dist/index.d.ts +2444 -0
- package/dist/index.js +911 -0
- package/dist/index.js.map +1 -0
- package/package.json +86 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2444 @@
|
|
|
1
|
+
import { Collection as Collection$1, Document, ChangeStreamDocument, ObjectId, CollationOptions, InsertOneResult, InsertManyResult, UpdateResult, DeleteResult, BulkWriteResult, Db, BSONTypeAlias, Decimal128 } from 'mongodb';
|
|
2
|
+
import { BSONValue } from 'bson';
|
|
3
|
+
import * as tf from 'type-fest';
|
|
4
|
+
import { Simplify, GreaterThan, IsAny, IsUnknown, UnionToTuple, ValueOf } from 'type-fest';
|
|
5
|
+
import { JSONSchema } from 'json-schema-to-typescript';
|
|
6
|
+
|
|
7
|
+
declare const functionToString: (fn: Function) => string;
|
|
8
|
+
type TypedAccumulator<InitFn extends (...args: any[]) => any, AccArgs extends readonly unknown[], Result = ReturnType<InitFn>> = {
|
|
9
|
+
init: InitFn;
|
|
10
|
+
initArgs: Parameters<InitFn>;
|
|
11
|
+
accumulate: (state: ReturnType<InitFn>, ...args: [...AccArgs]) => ReturnType<InitFn>;
|
|
12
|
+
accumulateArgs: Readonly<{
|
|
13
|
+
[K in keyof AccArgs]: string;
|
|
14
|
+
}>;
|
|
15
|
+
merge: (state1: ReturnType<InitFn>, state2: ReturnType<InitFn>) => ReturnType<InitFn>;
|
|
16
|
+
finalize?: (state: ReturnType<InitFn>) => Result;
|
|
17
|
+
lang: "js";
|
|
18
|
+
};
|
|
19
|
+
declare const resolveAccumulator: <InitFn extends (...args: any[]) => any, AccArgs extends readonly unknown[], Result>(config: TypedAccumulator<InitFn, AccArgs, Result>) => {
|
|
20
|
+
init: string;
|
|
21
|
+
initArgs: Parameters<InitFn>;
|
|
22
|
+
accumulate: string;
|
|
23
|
+
accumulateArgs: Readonly<{ [K in keyof AccArgs]: string; }>;
|
|
24
|
+
merge: string;
|
|
25
|
+
lang: "js";
|
|
26
|
+
} | {
|
|
27
|
+
finalize: string;
|
|
28
|
+
init: string;
|
|
29
|
+
initArgs: Parameters<InitFn>;
|
|
30
|
+
accumulate: string;
|
|
31
|
+
accumulateArgs: Readonly<{ [K in keyof AccArgs]: string; }>;
|
|
32
|
+
merge: string;
|
|
33
|
+
lang: "js";
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
declare const opaqueErrorBrand: unique symbol;
|
|
37
|
+
/**
|
|
38
|
+
* Opaque error type that carries a message but hides implementation details
|
|
39
|
+
*/
|
|
40
|
+
type OpaqueError<Msg extends string> = {
|
|
41
|
+
readonly __error: Msg;
|
|
42
|
+
readonly __tag: "OpaqueError";
|
|
43
|
+
readonly [opaqueErrorBrand]: never;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Type error with structured details: what went wrong, expected type, got type
|
|
47
|
+
*/
|
|
48
|
+
type TypeError<Msg extends string, Expected = unknown, Got = unknown> = OpaqueError<Msg> & {
|
|
49
|
+
readonly __expected: Expected;
|
|
50
|
+
readonly __got: Got;
|
|
51
|
+
readonly __tag: "TypeError";
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Type utilities for simplifying and making types writable
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
type AnyDict = Dict<any>;
|
|
59
|
+
type __ = unknown;
|
|
60
|
+
type Dict<T> = Record<string, T>;
|
|
61
|
+
/**
|
|
62
|
+
* SimplifyWritable - Makes a type writable (removes readonly) and simplifies it
|
|
63
|
+
* This combines WritableDeep and Simplify for cleaner type signatures
|
|
64
|
+
*/
|
|
65
|
+
type WritableBsonDeep<T> = T extends BSONValue | Date ? T : T extends readonly [] ? [] : T extends readonly [infer A, ...infer R] ? [WritableBsonDeep<A>, ...WritableBsonDeep<R>] : T extends readonly (infer E)[] ? WritableBsonDeep<E>[] : T extends object ? {
|
|
66
|
+
-readonly [K in keyof T]: WritableBsonDeep<T[K]>;
|
|
67
|
+
} : T;
|
|
68
|
+
type SimplifyWritable<T> = tf.Simplify<WritableBsonDeep<T>>;
|
|
69
|
+
type LiteralKeys<T> = keyof T extends infer K ? true extends tf.IsLiteral<K> ? K & string : never : never;
|
|
70
|
+
type HasStringIndex<T> = string extends keyof T ? true : false;
|
|
71
|
+
type IndexValue<T> = string extends keyof T ? T[string] : never;
|
|
72
|
+
type HasLiteralKey<T, K extends string> = K extends LiteralKeys<T> ? true : false;
|
|
73
|
+
type MergeWithIndexOverride<A, B> = tf.Simplify<{
|
|
74
|
+
[K in LiteralKeys<A> | LiteralKeys<B>]: HasLiteralKey<B, K> extends true ? B[K & keyof B] : HasLiteralKey<A, K> extends true ? A[K & keyof A] : never;
|
|
75
|
+
} & (HasStringIndex<B> extends true ? {
|
|
76
|
+
[k: string]: IndexValue<B>;
|
|
77
|
+
} : HasStringIndex<A> extends true ? {
|
|
78
|
+
[k: string]: IndexValue<A>;
|
|
79
|
+
} : NonNullable<unknown>)>;
|
|
80
|
+
/**
|
|
81
|
+
* ShallowMergeObjectsOverride - Shallow merge array of object types with last-wins semantics
|
|
82
|
+
*/
|
|
83
|
+
type ShallowMergeObjectsOverride<T extends readonly AnyDict[]> = T extends readonly [] ? Dict<never> : T extends readonly [infer First extends AnyDict] ? First : T extends readonly [infer First extends AnyDict, ...infer Rest extends readonly AnyDict[]] ? MergeWithIndexOverride<First, ShallowMergeObjectsOverride<Rest>> : never;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Path type utilities for MongoDB dot-notation access
|
|
87
|
+
*
|
|
88
|
+
* This module provides type-safe path utilities for MongoDB operations:
|
|
89
|
+
* - Path<T>: Generate all valid dot-notation paths (using type-fest)
|
|
90
|
+
* - ResolveValue<T, P>: Resolve the type at a given path
|
|
91
|
+
* - PathValueArrayProjection<T, P>: MongoDB array projection semantics
|
|
92
|
+
* - Type-filtered paths (NumericPath, StringPath, etc.)
|
|
93
|
+
* - Positional update paths ($[identifier])
|
|
94
|
+
*
|
|
95
|
+
* Uses type-fest's Paths with BsonStop to handle BSON types as leaf nodes.
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
type BsonGuard<T> = T extends Date | BSONValue ? T & never : T extends readonly (infer E)[] ? readonly BsonGuard<E>[] : {
|
|
99
|
+
[K in keyof T]: BsonGuard<T[K]>;
|
|
100
|
+
};
|
|
101
|
+
type RawPath<T> = tf.Paths<BsonGuard<T>, {
|
|
102
|
+
maxRecursionDepth: 10;
|
|
103
|
+
}> & string;
|
|
104
|
+
type ReplaceDotNumber<T, With extends string> = T extends `${infer Head}.${infer Rest}` ? Rest extends `${number}${infer Tail}` ? `${Head}${With}${ReplaceDotNumber<Tail, With>}` : `${Head}.${ReplaceDotNumber<Rest, With>}` : T;
|
|
105
|
+
type PathType<T> = ReplaceDotNumber<RawPath<T>, "" | `.${number}`>;
|
|
106
|
+
type UpdatePathType<T> = ReplaceDotNumber<RawPath<T>, `.${number}` | `.$` | `.$[]` | `.$[${string}]`>;
|
|
107
|
+
/**
|
|
108
|
+
* Normalize arrayFilters syntax to positional operator
|
|
109
|
+
* Transforms $[] and $[identifier] to $ for type checking
|
|
110
|
+
*/
|
|
111
|
+
type NormalizeArrayFilterPath<T extends string> = T extends `${infer Before}.${infer Rest}` ? `${NormalizeSegment<Before>}.${NormalizeArrayFilterPath<Rest>}` : NormalizeSegment<T>;
|
|
112
|
+
type NormalizeSegment<T extends string> = T extends `${number}` ? "$" : T extends "$[]" | `$[${string}]` ? "$" : T;
|
|
113
|
+
type ArrayIndex = `${number}` | `$` | `$[]` | `$[${string}]`;
|
|
114
|
+
type _PathValue<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? T extends readonly (infer E)[] ? Key extends ArrayIndex ? _PathValue<E, Rest> : _PathValue<E, P> : Key extends keyof T ? _PathValue<T[Key], Rest> : never : T extends readonly (infer E)[] ? P extends ArrayIndex ? E : _PathValue<E, P> : P extends keyof T ? T[P] : never;
|
|
115
|
+
/**
|
|
116
|
+
* ResolveValue<T, P> - Get the value type at a given path P in type T
|
|
117
|
+
*
|
|
118
|
+
* MongoDB-compatible path resolution supporting:
|
|
119
|
+
* - Direct property access: ResolveValue<{name: string}, "name"> = string
|
|
120
|
+
* - Nested access: ResolveValue<{a: {b: string}}, "a.b"> = string
|
|
121
|
+
* - Array access: ResolveValue<{items: Item[]}, "items"> = Item[]
|
|
122
|
+
* - Array element via dot: ResolveValue<{items: {id: string}[]}, "items.id"> = string
|
|
123
|
+
* - Numeric index: ResolveValue<{items: string[]}, "items.0"> = string
|
|
124
|
+
* - Numeric index + nested: ResolveValue<{items: {id: string}[]}, "items.0.id"> = string
|
|
125
|
+
*
|
|
126
|
+
* Returns `never` for invalid paths.
|
|
127
|
+
*/
|
|
128
|
+
type ResolveValue<T, P extends string> = _PathValue<T, P>;
|
|
129
|
+
type ResolvePath<T, P extends PathType<T>> = P extends PathType<T> ? ResolveValue<T, P> : never;
|
|
130
|
+
type ResolveUpdatePath<T, P extends UpdatePathType<T>> = P extends UpdatePathType<T> ? ResolveValue<T, P> : never;
|
|
131
|
+
type FilteredPath<T, Target> = {
|
|
132
|
+
[K in PathType<T>]: [ResolvePath<T, K>] extends [never] ? never : ResolvePath<T, K> extends Target ? K : never;
|
|
133
|
+
}[PathType<T>];
|
|
134
|
+
type UpdateFilteredPath<T, Target> = {
|
|
135
|
+
[K in UpdatePathType<T>]: [ResolveValue<T, K>] extends [never] ? never : ResolveValue<T, K> extends Target ? K : never;
|
|
136
|
+
}[UpdatePathType<T>];
|
|
137
|
+
type NumericPath<T> = FilteredPath<T, number | null | undefined>;
|
|
138
|
+
type StringPath<T> = FilteredPath<T, string | null | undefined>;
|
|
139
|
+
type BooleanPath<T> = FilteredPath<T, boolean | null | undefined>;
|
|
140
|
+
type DatePath<T> = FilteredPath<T, Date | null | undefined>;
|
|
141
|
+
type ArrayPath<T> = FilteredPath<T, readonly unknown[]>;
|
|
142
|
+
type ComparablePath<T> = FilteredPath<T, number | string | boolean | Date | BSONValue | null | undefined>;
|
|
143
|
+
type UpdateArrayPath<T> = UpdateFilteredPath<T, readonly unknown[]>;
|
|
144
|
+
/**
|
|
145
|
+
* UpdateSpecOfType<T, Target, Value> - A spec type that validates paths resolve to Target
|
|
146
|
+
*
|
|
147
|
+
* Uses the 'as' clause to filter paths during iteration. This works because:
|
|
148
|
+
* - TypeScript's pattern matching allows specific strings like "$[x]" to match "$[${string}]"
|
|
149
|
+
* - The filter happens at assignment time, not enumeration time
|
|
150
|
+
* - Invalid paths (wrong type or doesn't exist) are excluded via `as ... never`
|
|
151
|
+
*/
|
|
152
|
+
type UpdateSpecOfType<T, Target, Value> = {
|
|
153
|
+
[K in UpdatePathType<T> as ResolveValue<T, K> extends Target ? ValidPositionalPath<K> : never]?: Value;
|
|
154
|
+
};
|
|
155
|
+
type HasPrefix<Target extends string, Union extends string> = Union extends unknown ? Target extends `${Union}.${string}` ? true : never : never;
|
|
156
|
+
type _ArrayRootPath<T extends string, All extends string = T> = T extends unknown ? true extends HasPrefix<T, Exclude<All, T>> ? never : T : never;
|
|
157
|
+
type IsPrefix<Needle extends string, Union extends string> = Union extends unknown ? Union extends `${Needle}.${string}` ? true : never : never;
|
|
158
|
+
type ArrayRootPath<T> = _ArrayRootPath<ArrayPath<T>>;
|
|
159
|
+
/**
|
|
160
|
+
* ArrayProjectionPath<T> - Paths that resolve to arrays via MongoDB array projection semantics
|
|
161
|
+
* This includes:
|
|
162
|
+
* - Direct array paths (e.g., "tags" where tags is string[])
|
|
163
|
+
* - Projection paths through arrays (e.g., "items.name" where items is {name: string}[] - projects to string[])
|
|
164
|
+
*/
|
|
165
|
+
type ArrayProjectionPath<T> = {
|
|
166
|
+
[K in PathType<T>]: PathValueArrayProjection<T, K> extends readonly unknown[] ? K : never;
|
|
167
|
+
}[PathType<T>];
|
|
168
|
+
type UpdateArrayElementType<T, P extends UpdatePathType<T>> = ResolveUpdatePath<T, P> extends readonly (infer E)[] ? E : never;
|
|
169
|
+
/**
|
|
170
|
+
* PathValueArrayProjection<T, P> - Get the value type at path P with MongoDB array projection semantics
|
|
171
|
+
*
|
|
172
|
+
* In MongoDB, when you reference a path through an array like $items.name where items is an array
|
|
173
|
+
* of documents with a name field, it returns an array of all the name values (not a single value).
|
|
174
|
+
*
|
|
175
|
+
* Examples:
|
|
176
|
+
* - PathValueArrayProjection<{info: {name: string}[]}, "info.name"> = string[]
|
|
177
|
+
* - PathValueArrayProjection<{info: {name: string}[]}, "info"> = {name: string}[]
|
|
178
|
+
* - PathValueArrayProjection<{name: string}, "name"> = string
|
|
179
|
+
* - PathValueArrayProjection<{items: {discounts: {code: string}[]}[]}, "items.discounts.code"> = string[][]
|
|
180
|
+
*/
|
|
181
|
+
type PathValueArrayProjection<T, P extends PathType<T>> = _PathValueArrayProjection<T, P>;
|
|
182
|
+
type _PathValueArrayProjection<T, P extends string> = P extends `${infer Key}.${infer Rest}` ? T extends readonly (infer E)[] ? Key extends `${number}` ? _PathValueArrayProjection<E, Rest> : _PathValueArrayProjection<E, P>[] : Key extends keyof T ? _PathValueArrayProjection<T[Key], Rest> : never : T extends readonly (infer E)[] ? P extends `${number}` ? E : _PathValueArrayProjection<E, P>[] : P extends keyof T ? T[P] : never;
|
|
183
|
+
type NumericFieldRef<T> = `$${NumericPath<T>}`;
|
|
184
|
+
type StringFieldRef<T> = `$${StringPath<T>}`;
|
|
185
|
+
type DateFieldRef<T> = `$${DatePath<T>}`;
|
|
186
|
+
type BooleanFieldRef<T> = `$${BooleanPath<T>}`;
|
|
187
|
+
type ArrayFieldRef<T> = `$${ArrayPath<T>}`;
|
|
188
|
+
/**
|
|
189
|
+
* Validates that positional paths don't contain double $ (.$. pattern) and don't start with $
|
|
190
|
+
* Returns the path if valid, OpaqueError if invalid
|
|
191
|
+
*/
|
|
192
|
+
type ValidPositionalPath<P extends string> = P extends `$${string}` ? OpaqueError<"Paths cannot start with $"> : P extends `${string}.$.${infer Rest1}` ? Rest1 extends `${string}.$.${string}` ? OpaqueError<"Paths cannot contain double $"> : P : P;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Pipeline type utilities for type-safe stage chaining
|
|
196
|
+
*
|
|
197
|
+
* This module provides a unified PipelineBuilder that works for both
|
|
198
|
+
* aggregation pipelines and update pipelines.
|
|
199
|
+
*/
|
|
200
|
+
type CheckedResult<T> = tf.IsAny<T> extends true ? OpaqueError<"Pipeline stage produced or propagated 'any' or 'unknown' type. Please ensure all expressions are fully typed."> & {
|
|
201
|
+
__inferredType: T;
|
|
202
|
+
} : T;
|
|
203
|
+
/**
|
|
204
|
+
* Core Agg type - represents a pipeline at a point with input and current document types
|
|
205
|
+
*/
|
|
206
|
+
type Agg<TIn, TCurrent> = {
|
|
207
|
+
readonly _in: TIn;
|
|
208
|
+
readonly _current: SimplifyWritable<TCurrent>;
|
|
209
|
+
readonly stages: readonly object[];
|
|
210
|
+
pipe<TOut>(stage: (agg: Agg<TIn, TCurrent>) => Agg<TIn, TOut>): Agg<TIn, TOut>;
|
|
211
|
+
toList(collection?: Collection$1): Promise<SimplifyWritable<CheckedResult<TCurrent>>[]>;
|
|
212
|
+
/** Returns the accumulated pipeline stages as formatted JSON */
|
|
213
|
+
toMQL(): string;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Typed pipeline marker - used for sub-pipelines in $facet, $lookup, update pipelines, etc.
|
|
217
|
+
*/
|
|
218
|
+
type TypedPipeline<TIn, TOut> = {
|
|
219
|
+
readonly __pipelineIn: TIn;
|
|
220
|
+
readonly __pipelineOut: TOut;
|
|
221
|
+
readonly stages: readonly unknown[];
|
|
222
|
+
};
|
|
223
|
+
/**
|
|
224
|
+
* A stage function transforms an Agg<TIn, TFrom> to Agg<TIn, TTo>
|
|
225
|
+
* The TIn parameter is the original input type (for expression context)
|
|
226
|
+
*
|
|
227
|
+
* Used by both aggregation stages and update stages.
|
|
228
|
+
*/
|
|
229
|
+
type StageFunction<TIn, TFrom, TTo> = (agg: Agg<TIn, TFrom>) => Agg<TIn, TTo>;
|
|
230
|
+
/**
|
|
231
|
+
* Brand for stages that are allowed in update pipelines.
|
|
232
|
+
* Only $set, $unset, $addFields, $project, $replaceRoot, $replaceWith are branded.
|
|
233
|
+
*
|
|
234
|
+
* IMPORTANT: UpdateStageFunction uses a callable object type with a call signature
|
|
235
|
+
* (`{ (agg): result; [brand]: true }`) rather than a function-object intersection
|
|
236
|
+
* (`((agg) => result) & { [brand]: true }`). TypeScript's inference engine cannot
|
|
237
|
+
* propagate generic parameters through function-object intersections in multi-stage
|
|
238
|
+
* overload resolution, causing C to default to {}.
|
|
239
|
+
*/
|
|
240
|
+
declare const __updateStage: unique symbol;
|
|
241
|
+
type UpdateStageBrand = {
|
|
242
|
+
readonly [__updateStage]: true;
|
|
243
|
+
};
|
|
244
|
+
type UpdateStageFunction<TFrom, TTo> = {
|
|
245
|
+
<TIn>(agg: Agg<TIn, TFrom>): Agg<TIn, TTo>;
|
|
246
|
+
readonly [__updateStage]: true;
|
|
247
|
+
};
|
|
248
|
+
/**
|
|
249
|
+
* Output type resolver for all pipeline builder variants.
|
|
250
|
+
*
|
|
251
|
+
* - "agg": returns Agg (for collection.aggregate)
|
|
252
|
+
* - "pipeline": returns TypedPipeline (for sub-pipelines, pipe())
|
|
253
|
+
* - "update": validates Last extends Target, wraps in CheckedResult
|
|
254
|
+
* - "migration": validates Simplify<Last> extends Target
|
|
255
|
+
*/
|
|
256
|
+
type PipelineOutput<Mode extends "agg" | "pipeline" | "update" | "migration", TIn, Last, Target> = Mode extends "agg" ? Agg<TIn, Last> : Mode extends "pipeline" ? TypedPipeline<TIn, Last> : Mode extends "update" ? Last extends Target ? TypedPipeline<TIn, CheckedResult<Last>> : OpaqueError<"Update pipeline output must be assignable to collection type"> & TypedPipeline<TIn, Last> : tf.Simplify<Last> extends Target ? TypedPipeline<TIn, Last> : OpaqueError<"Migration output does not match target schema"> & TypedPipeline<TIn, Last>;
|
|
257
|
+
/**
|
|
258
|
+
* Branded stage: bare StageFunction for standard builders,
|
|
259
|
+
* callable-object UpdateStageFunction for update/migration builders.
|
|
260
|
+
*/
|
|
261
|
+
type BStage<TIn, Brand, From, To> = [
|
|
262
|
+
{}
|
|
263
|
+
] extends [Brand] ? StageFunction<TIn, From, To> : UpdateStageFunction<From, To>;
|
|
264
|
+
/**
|
|
265
|
+
* GenericPipelineBuilder — single set of 15 overloads parameterized by Brand and Mode.
|
|
266
|
+
* All exported pipeline builder types derive from this.
|
|
267
|
+
*/
|
|
268
|
+
type GenericPipelineBuilder<TIn, Brand, Mode extends "agg" | "pipeline" | "update" | "migration", Target = unknown> = {
|
|
269
|
+
<A>(s1: BStage<TIn, Brand, TIn, A>): PipelineOutput<Mode, TIn, A, Target>;
|
|
270
|
+
<A, B>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>): PipelineOutput<Mode, TIn, B, Target>;
|
|
271
|
+
<A, B, C>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>): PipelineOutput<Mode, TIn, C, Target>;
|
|
272
|
+
<A, B, C, D>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>): PipelineOutput<Mode, TIn, D, Target>;
|
|
273
|
+
<A, B, C, D, E>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>): PipelineOutput<Mode, TIn, E, Target>;
|
|
274
|
+
<A, B, C, D, E, F>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>): PipelineOutput<Mode, TIn, F, Target>;
|
|
275
|
+
<A, B, C, D, E, F, G>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>): PipelineOutput<Mode, TIn, G, Target>;
|
|
276
|
+
<A, B, C, D, E, F, G, H>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>): PipelineOutput<Mode, TIn, H, Target>;
|
|
277
|
+
<A, B, C, D, E, F, G, H, I>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>): PipelineOutput<Mode, TIn, I, Target>;
|
|
278
|
+
<A, B, C, D, E, F, G, H, I, J>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>, s10: BStage<TIn, Brand, I, J>): PipelineOutput<Mode, TIn, J, Target>;
|
|
279
|
+
<A, B, C, D, E, F, G, H, I, J, K>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>, s10: BStage<TIn, Brand, I, J>, s11: BStage<TIn, Brand, J, K>): PipelineOutput<Mode, TIn, K, Target>;
|
|
280
|
+
<A, B, C, D, E, F, G, H, I, J, K, L>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>, s10: BStage<TIn, Brand, I, J>, s11: BStage<TIn, Brand, J, K>, s12: BStage<TIn, Brand, K, L>): PipelineOutput<Mode, TIn, L, Target>;
|
|
281
|
+
<A, B, C, D, E, F, G, H, I, J, K, L, M>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>, s10: BStage<TIn, Brand, I, J>, s11: BStage<TIn, Brand, J, K>, s12: BStage<TIn, Brand, K, L>, s13: BStage<TIn, Brand, L, M>): PipelineOutput<Mode, TIn, M, Target>;
|
|
282
|
+
<A, B, C, D, E, F, G, H, I, J, K, L, M, N>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>, s10: BStage<TIn, Brand, I, J>, s11: BStage<TIn, Brand, J, K>, s12: BStage<TIn, Brand, K, L>, s13: BStage<TIn, Brand, L, M>, s14: BStage<TIn, Brand, M, N>): PipelineOutput<Mode, TIn, N, Target>;
|
|
283
|
+
<A, B, C, D, E, F, G, H, I, J, K, L, M, N, O>(s1: BStage<TIn, Brand, TIn, A>, s2: BStage<TIn, Brand, A, B>, s3: BStage<TIn, Brand, B, C>, s4: BStage<TIn, Brand, C, D>, s5: BStage<TIn, Brand, D, E>, s6: BStage<TIn, Brand, E, F>, s7: BStage<TIn, Brand, F, G>, s8: BStage<TIn, Brand, G, H>, s9: BStage<TIn, Brand, H, I>, s10: BStage<TIn, Brand, I, J>, s11: BStage<TIn, Brand, J, K>, s12: BStage<TIn, Brand, K, L>, s13: BStage<TIn, Brand, L, M>, s14: BStage<TIn, Brand, M, N>, s15: BStage<TIn, Brand, N, O>): PipelineOutput<Mode, TIn, O, Target>;
|
|
284
|
+
};
|
|
285
|
+
/** For sub-pipeline composition (pipe(), $facet, etc.) — no output validation */
|
|
286
|
+
type PipelineBuilder<TIn> = GenericPipelineBuilder<TIn, {}, "pipeline">;
|
|
287
|
+
/** For collection.aggregate() — returns Agg instead of TypedPipeline */
|
|
288
|
+
type AggregateBuilder<T> = GenericPipelineBuilder<T, {}, "agg">;
|
|
289
|
+
/** For update pipelines — only UpdateStageBrand stages, validates output extends C */
|
|
290
|
+
type UpdatePipelineBuilder<C> = GenericPipelineBuilder<C, UpdateStageBrand, "update", C>;
|
|
291
|
+
/** For schema migration — only UpdateStageBrand stages, validates Simplify<output> extends TTo */
|
|
292
|
+
type MigrationPipelineBuilder<TFrom, TTo> = GenericPipelineBuilder<TFrom, UpdateStageBrand, "migration", TTo>;
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Common error types and utilities for consistent error handling
|
|
296
|
+
* across the MongoDB aggregation and CRUD operations.
|
|
297
|
+
*/
|
|
298
|
+
type CallbackOnlyError<Op extends string> = {
|
|
299
|
+
__error: true;
|
|
300
|
+
__tag: "CallbackOnlyError";
|
|
301
|
+
__op: Op;
|
|
302
|
+
message: `Operation '${Op}' requires a callback function, not a raw expression`;
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
type DotPathToObject<Path extends string, Value> = Path extends `${infer Head}.${infer Tail}` ? {
|
|
306
|
+
[K in Head]: DotPathToObject<Tail, Value>;
|
|
307
|
+
} : {
|
|
308
|
+
[K in Path]: Value;
|
|
309
|
+
};
|
|
310
|
+
type DeepMerge<A, B> = A extends object ? B extends object ? {
|
|
311
|
+
[K in keyof A | keyof B]: K extends keyof B ? K extends keyof A ? DeepMerge<A[K], B[K]> : B[K] : K extends keyof A ? A[K] : never;
|
|
312
|
+
} : A | B : A | B;
|
|
313
|
+
type ValidateSpec<C, Spec> = {
|
|
314
|
+
[K in keyof Spec]: Spec[K] extends 0 | 1 ? Spec[K] : Spec[K] extends Ret<any, any> ? Spec[K] : ValidExpressionValue<C, Spec[K]>;
|
|
315
|
+
};
|
|
316
|
+
type HasKey<Spec, K, V> = {
|
|
317
|
+
[P in keyof Spec]: Spec[P] extends V ? true : false;
|
|
318
|
+
}[keyof Spec];
|
|
319
|
+
type OnlyIdMixed<Spec> = HasKey<Spec, any, 1> extends true ? HasKey<Spec, any, 0> extends true ? keyof Spec & string extends "_id" | `_${string}` ? true : false : true : true;
|
|
320
|
+
type ValidateProjectRules<Spec> = OnlyIdMixed<Spec> extends true ? Spec : CallbackOnlyError<"$project cannot mix inclusion (1) and exclusion (0) except for _id">;
|
|
321
|
+
type ProjectIdResult<C, Spec> = Spec extends {
|
|
322
|
+
_id: 0;
|
|
323
|
+
} ? {} : Spec extends {
|
|
324
|
+
_id: 1;
|
|
325
|
+
} ? C extends {
|
|
326
|
+
_id: infer Id;
|
|
327
|
+
} ? {
|
|
328
|
+
_id: Id;
|
|
329
|
+
} : OpaqueError<"$project field does not exist: _id"> : C extends {
|
|
330
|
+
_id: infer Id;
|
|
331
|
+
} ? {
|
|
332
|
+
_id: Id;
|
|
333
|
+
} : {};
|
|
334
|
+
type ProjectResult<C, Spec> = Simplify<ProjectIdResult<C, Spec> & {
|
|
335
|
+
[K in Exclude<keyof Spec, "_id">]: Spec[K] extends 1 ? K extends keyof C ? C[K] : OpaqueError<`$project field does not exist: ${K & string}`> : Spec[K] extends 0 ? never : ResolveType<C, Spec[K]>;
|
|
336
|
+
}>;
|
|
337
|
+
type WindowBoundary$1 = "unbounded" | "current" | number;
|
|
338
|
+
type WindowDef = {
|
|
339
|
+
documents?: [WindowBoundary$1, WindowBoundary$1];
|
|
340
|
+
range?: [WindowBoundary$1, WindowBoundary$1];
|
|
341
|
+
unit?: TimeUnit;
|
|
342
|
+
};
|
|
343
|
+
type AccumulatorOp<C, T = ExpressionIn<C>> = {
|
|
344
|
+
window?: WindowDef;
|
|
345
|
+
} & ({
|
|
346
|
+
$sum: T;
|
|
347
|
+
} | {
|
|
348
|
+
$avg: T;
|
|
349
|
+
} | {
|
|
350
|
+
$min: T;
|
|
351
|
+
} | {
|
|
352
|
+
$max: T;
|
|
353
|
+
} | {
|
|
354
|
+
$first: T;
|
|
355
|
+
} | {
|
|
356
|
+
$last: T;
|
|
357
|
+
} | {
|
|
358
|
+
$count: Dict<never>;
|
|
359
|
+
} | {
|
|
360
|
+
$push: T;
|
|
361
|
+
} | {
|
|
362
|
+
$addToSet: T;
|
|
363
|
+
});
|
|
364
|
+
type WindowOperatorSpec<C> = AccumulatorOp<C> | Ret<C, any> | {
|
|
365
|
+
$rank: Dict<never>;
|
|
366
|
+
} | {
|
|
367
|
+
$denseRank: Dict<never>;
|
|
368
|
+
} | {
|
|
369
|
+
$documentNumber: Dict<never>;
|
|
370
|
+
} | {
|
|
371
|
+
$percentRank: Dict<never>;
|
|
372
|
+
} | {
|
|
373
|
+
$shift: {
|
|
374
|
+
output: ExpressionIn<C>;
|
|
375
|
+
by: number;
|
|
376
|
+
default?: ExpressionIn<C>;
|
|
377
|
+
};
|
|
378
|
+
} | {
|
|
379
|
+
$expMovingAvg: {
|
|
380
|
+
input: ExpressionIn<C>;
|
|
381
|
+
N?: number;
|
|
382
|
+
alpha?: number;
|
|
383
|
+
};
|
|
384
|
+
} | {
|
|
385
|
+
$linearFill: ExpressionIn<C>;
|
|
386
|
+
} | {
|
|
387
|
+
$derivative: {
|
|
388
|
+
input: ExpressionIn<C>;
|
|
389
|
+
unit?: TimeUnit;
|
|
390
|
+
};
|
|
391
|
+
window: WindowDef;
|
|
392
|
+
} | {
|
|
393
|
+
$integral: {
|
|
394
|
+
input: ExpressionIn<C>;
|
|
395
|
+
unit?: TimeUnit;
|
|
396
|
+
};
|
|
397
|
+
};
|
|
398
|
+
type HasNullish$1<T> = [Extract<T, null | undefined>] extends [never] ? false : true;
|
|
399
|
+
type NullishResult$1<C, T, Output> = HasNullish$1<ResolveType<C, T>> extends true ? Output | null : Output;
|
|
400
|
+
type WindowOperatorResult<C, Op> = Op extends Ret<C, infer T> ? T : Op extends {
|
|
401
|
+
$sum: NumericIn<C>;
|
|
402
|
+
} ? number : Op extends {
|
|
403
|
+
$avg: infer A;
|
|
404
|
+
} ? NullishResult$1<C, A, number> : Op extends {
|
|
405
|
+
$min: ExpressionIn<C>;
|
|
406
|
+
} ? ResolveType<C, Op["$min"]> | null : Op extends {
|
|
407
|
+
$max: ExpressionIn<C>;
|
|
408
|
+
} ? ResolveType<C, Op["$max"]> | null : Op extends {
|
|
409
|
+
$first: ExpressionIn<C>;
|
|
410
|
+
} ? ResolveType<C, Op["$first"]> : Op extends {
|
|
411
|
+
$last: ExpressionIn<C>;
|
|
412
|
+
} ? ResolveType<C, Op["$last"]> : Op extends {
|
|
413
|
+
$count: Dict<never>;
|
|
414
|
+
} ? number : Op extends {
|
|
415
|
+
$push: ExpressionIn<C>;
|
|
416
|
+
} ? ResolveType<C, Op["$push"]>[] : Op extends {
|
|
417
|
+
$addToSet: ExpressionIn<C>;
|
|
418
|
+
} ? ResolveType<C, Op["$addToSet"]>[] : Op extends {
|
|
419
|
+
$rank: Dict<never>;
|
|
420
|
+
} ? number : Op extends {
|
|
421
|
+
$denseRank: Dict<never>;
|
|
422
|
+
} ? number : Op extends {
|
|
423
|
+
$documentNumber: Dict<never>;
|
|
424
|
+
} ? number : Op extends {
|
|
425
|
+
$percentRank: Dict<never>;
|
|
426
|
+
} ? number : Op extends {
|
|
427
|
+
$shift: {
|
|
428
|
+
output: infer Output;
|
|
429
|
+
default?: infer Default;
|
|
430
|
+
};
|
|
431
|
+
} ? Default extends ExpressionIn<C> ? ResolveType<C, Output> | ResolveType<C, Default> : ResolveType<C, Output> | null : Op extends {
|
|
432
|
+
$expMovingAvg: any;
|
|
433
|
+
} ? number | null : Op extends {
|
|
434
|
+
$linearFill: ExpressionIn<C>;
|
|
435
|
+
} ? ResolveType<C, Op["$linearFill"]> | null : Op extends {
|
|
436
|
+
$derivative: any;
|
|
437
|
+
} ? number | null : Op extends {
|
|
438
|
+
$integral: any;
|
|
439
|
+
} ? number | null : unknown;
|
|
440
|
+
type SortBySpec$1<C> = Partial<Record<keyof C & string, 1 | -1>>;
|
|
441
|
+
type SortBySingle<C> = {
|
|
442
|
+
[K in keyof C & string]: {
|
|
443
|
+
[P in K]: 1 | -1;
|
|
444
|
+
};
|
|
445
|
+
}[keyof C & string];
|
|
446
|
+
type SortPath$1<C> = ComparablePath<C> | "_id" | `_id.${string}`;
|
|
447
|
+
type SortValue = 1 | -1 | {
|
|
448
|
+
$meta: "textScore" | "searchScore";
|
|
449
|
+
};
|
|
450
|
+
type SortSpec$1<C> = Partial<Record<SortPath$1<C>, SortValue>> | {
|
|
451
|
+
$meta: "textScore" | "searchScore";
|
|
452
|
+
} | (Partial<Record<SortPath$1<C>, SortValue>> & {
|
|
453
|
+
$meta: "textScore" | "searchScore";
|
|
454
|
+
});
|
|
455
|
+
type SortByHasDate<C, S> = [
|
|
456
|
+
S
|
|
457
|
+
] extends [undefined] ? false : true extends ({
|
|
458
|
+
[K in keyof S]: K extends keyof C ? C[K] extends Date ? true : false : false;
|
|
459
|
+
}[keyof S]) ? true : false;
|
|
460
|
+
type HasRangeWindowWithoutUnit<Output> = true extends ({
|
|
461
|
+
[K in keyof Output]: Output[K] extends {
|
|
462
|
+
window: {
|
|
463
|
+
range: any;
|
|
464
|
+
};
|
|
465
|
+
} ? Output[K] extends {
|
|
466
|
+
window: {
|
|
467
|
+
unit: any;
|
|
468
|
+
};
|
|
469
|
+
} ? false : true : false;
|
|
470
|
+
}[keyof Output]) ? true : false;
|
|
471
|
+
type RequiresSortBy<C, Output extends Dict<WindowOperatorSpec<C>>> = true extends ({
|
|
472
|
+
[K in keyof Output]: Output[K] extends ({
|
|
473
|
+
$rank: any;
|
|
474
|
+
} | {
|
|
475
|
+
$denseRank: any;
|
|
476
|
+
} | {
|
|
477
|
+
$percentRank: any;
|
|
478
|
+
}) ? true : false;
|
|
479
|
+
}[keyof Output]) ? true : false;
|
|
480
|
+
type RemoveNever<T> = {
|
|
481
|
+
[K in keyof T as T[K] extends never ? never : K]: T[K];
|
|
482
|
+
};
|
|
483
|
+
type UnwindPath<T extends string> = T extends `$${infer Field}` ? Field : never;
|
|
484
|
+
type UnwindElement<T> = T extends readonly (infer E)[] ? E : T extends (infer E)[] ? E : T;
|
|
485
|
+
type UnwindResult<C, P extends string> = {
|
|
486
|
+
[K in keyof C]: K extends UnwindPath<P> ? UnwindElement<C[K]> : C[K];
|
|
487
|
+
};
|
|
488
|
+
type UnwindResultWithPreserve<C, P extends string, Preserve extends boolean | undefined> = {
|
|
489
|
+
[K in keyof C]: K extends UnwindPath<P> ? Preserve extends true ? UnwindElement<C[K]> | null | undefined : UnwindElement<C[K]> : C[K];
|
|
490
|
+
};
|
|
491
|
+
type IndexStatsResult = {
|
|
492
|
+
name: string;
|
|
493
|
+
key: Dict<number>;
|
|
494
|
+
host: string;
|
|
495
|
+
accesses: {
|
|
496
|
+
ops: number;
|
|
497
|
+
since: Date;
|
|
498
|
+
};
|
|
499
|
+
shard?: string;
|
|
500
|
+
spec?: AnyDict;
|
|
501
|
+
};
|
|
502
|
+
type SortByCountResult<T> = {
|
|
503
|
+
_id: T;
|
|
504
|
+
count: number;
|
|
505
|
+
};
|
|
506
|
+
type UnsetResult<C, Fields extends string[]> = Omit<C, Fields[number]>;
|
|
507
|
+
type CollectionOf<T> = T extends Collection ? T : T extends {
|
|
508
|
+
coll: infer C;
|
|
509
|
+
} ? C extends Collection ? C : never : never;
|
|
510
|
+
type CollectionConstraint<C, TTarget extends Collection> = C extends CollectionType<TTarget> ? {} : CallbackOnlyError<"Collection type mismatch">;
|
|
511
|
+
type MergeIntoConstraint<C, TInto extends Collection> = CollectionConstraint<C, TInto>;
|
|
512
|
+
type OutIntoConstraint<C, T> = CollectionConstraint<C, CollectionOf<T>>;
|
|
513
|
+
type ValidateNoRawOperators<T extends Dict<any>> = {
|
|
514
|
+
[K in keyof T]: T[K] extends {
|
|
515
|
+
readonly __context: any;
|
|
516
|
+
readonly __type: any;
|
|
517
|
+
} ? T[K] : T[K] extends {
|
|
518
|
+
[key: `$${string}`]: any;
|
|
519
|
+
} ? OpaqueError<"Invalid MongoDB operator object - use builder API instead (e.g., $.sum(...))"> : T[K];
|
|
520
|
+
};
|
|
521
|
+
type WindowFieldsOptions<C, T extends {
|
|
522
|
+
partitionBy?: ExpressionIn<C> | Dict<ExpressionIn<C> | Ret<C>> | Ret<C>;
|
|
523
|
+
sortBy?: SortBySpec$1<C>;
|
|
524
|
+
output: Dict<WindowOperatorSpec<C>>;
|
|
525
|
+
}> = SortByHasDate<C, T["sortBy"]> extends true ? HasRangeWindowWithoutUnit<T["output"]> extends true ? OpaqueError<"$setWindowFields range window with date sortBy requires unit"> & T : RequiresSortBy<C, T["output"]> extends true ? T & {
|
|
526
|
+
sortBy: SortBySingle<C>;
|
|
527
|
+
} : T : RequiresSortBy<C, T["output"]> extends true ? T & {
|
|
528
|
+
sortBy: SortBySingle<C>;
|
|
529
|
+
} : T;
|
|
530
|
+
type ReplaceRootScalar<C> = SmartAnyPathFieldRef<C> | Ret<C> | "$$ROOT" | "$$CURRENT";
|
|
531
|
+
type ReplaceRootValue<C> = ReplaceRootScalar<C> | {
|
|
532
|
+
[K in string]: ReplaceRootValue<C>;
|
|
533
|
+
};
|
|
534
|
+
type ReplaceRootResolved<C, R> = R extends "$$ROOT" | "$$CURRENT" ? C : R extends Ret<any, infer T> ? T : R extends object ? ResolveSpec<C, R> : ResolveType<C, R>;
|
|
535
|
+
type IsStrictlyAscending<T extends readonly number[]> = T extends (readonly [infer A extends number, infer B extends number, ...infer Rest extends number[]]) ? number extends A | B ? true : GreaterThan<B, A> extends false ? false : IsStrictlyAscending<[B, ...Rest]> : true;
|
|
536
|
+
type AscendingBoundaries<T extends readonly number[]> = IsStrictlyAscending<T> extends true ? T : OpaqueError<"$bucket boundaries must be strictly increasing">;
|
|
537
|
+
/** Adds new fields to documents. Alias for `$set`.
|
|
538
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/addFields/ */
|
|
539
|
+
declare const $addFields: <C, const T>(fieldsBuilder: ($: ExprBuilder<Simplify<C>>) => T) => UpdateStageFunction<C, Simplify<C & ResolveSpec<C, T>>>;
|
|
540
|
+
/** Groups documents into discrete buckets by a specified expression and boundaries.
|
|
541
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucket/ */
|
|
542
|
+
declare const $bucket: {
|
|
543
|
+
<C, const B extends readonly number[], const D extends string | number = never, const O extends AnyDict = Dict<never>>(options: {
|
|
544
|
+
groupBy: ExpressionIn<C>;
|
|
545
|
+
boundaries: B & AscendingBoundaries<B>;
|
|
546
|
+
default?: D;
|
|
547
|
+
output: ($: AccumulatorBuilder<Simplify<C>>) => O & ValidateNoRawOperators<O>;
|
|
548
|
+
}): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
549
|
+
_id: B[number] | D;
|
|
550
|
+
} & ResolveSpec<C, O>>;
|
|
551
|
+
<C, const B extends readonly number[], const D extends string | number = never>(options: {
|
|
552
|
+
groupBy: ExpressionIn<C>;
|
|
553
|
+
boundaries: B & AscendingBoundaries<B>;
|
|
554
|
+
default?: D;
|
|
555
|
+
}): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
556
|
+
_id: B[number] | D;
|
|
557
|
+
count: number;
|
|
558
|
+
}>;
|
|
559
|
+
};
|
|
560
|
+
/** Automatically groups documents into a specified number of buckets.
|
|
561
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/bucketAuto/ */
|
|
562
|
+
declare const $bucketAuto: <C, const G extends ExpressionIn<C>, const O extends Dict<ExpressionIn<C>> | undefined = undefined>(options: {
|
|
563
|
+
groupBy: G;
|
|
564
|
+
buckets: number;
|
|
565
|
+
output?: O;
|
|
566
|
+
granularity?: "R5" | "R10" | "R20" | "R40" | "R80" | "1-2-5" | "E6" | "E12" | "E24" | "E48" | "E96" | "E192" | "POWERSOF2";
|
|
567
|
+
}) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
568
|
+
_id: {
|
|
569
|
+
min: ResolveType<C, G>;
|
|
570
|
+
max: ResolveType<C, G>;
|
|
571
|
+
};
|
|
572
|
+
count: number;
|
|
573
|
+
} & (O extends AnyDict ? { [K in keyof O]: ResolveType<C, O[K]>; } : {})>;
|
|
574
|
+
/** Returns statistics about the collection (latency, storage, count, query exec stats).
|
|
575
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/collStats/ */
|
|
576
|
+
declare const $collStats: {
|
|
577
|
+
<C, const T extends {
|
|
578
|
+
latencyStats?: {
|
|
579
|
+
histograms?: boolean;
|
|
580
|
+
};
|
|
581
|
+
storageStats?: {
|
|
582
|
+
scale?: number;
|
|
583
|
+
};
|
|
584
|
+
count: Dict<never>;
|
|
585
|
+
queryExecStats?: Dict<never>;
|
|
586
|
+
}>(options: T): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
587
|
+
ns: string;
|
|
588
|
+
localTime: Date;
|
|
589
|
+
} & (T extends {
|
|
590
|
+
latencyStats: any;
|
|
591
|
+
} ? {
|
|
592
|
+
latencyStats: {
|
|
593
|
+
reads: Dict<number>;
|
|
594
|
+
writes: Dict<number>;
|
|
595
|
+
commands: Dict<number>;
|
|
596
|
+
};
|
|
597
|
+
} : {
|
|
598
|
+
latencyStats?: {
|
|
599
|
+
reads: Dict<number>;
|
|
600
|
+
writes: Dict<number>;
|
|
601
|
+
commands: Dict<number>;
|
|
602
|
+
};
|
|
603
|
+
}) & (T extends {
|
|
604
|
+
storageStats: any;
|
|
605
|
+
} ? {
|
|
606
|
+
storageStats: {
|
|
607
|
+
size: number;
|
|
608
|
+
count: number;
|
|
609
|
+
avgObjSize: number;
|
|
610
|
+
storageSize: number;
|
|
611
|
+
};
|
|
612
|
+
} : {
|
|
613
|
+
storageStats?: {
|
|
614
|
+
size: number;
|
|
615
|
+
count: number;
|
|
616
|
+
avgObjSize: number;
|
|
617
|
+
storageSize: number;
|
|
618
|
+
};
|
|
619
|
+
}) & {
|
|
620
|
+
count: number;
|
|
621
|
+
} & (T extends {
|
|
622
|
+
queryExecStats: any;
|
|
623
|
+
} ? {
|
|
624
|
+
queryExecStats: Dict<number>;
|
|
625
|
+
} : {
|
|
626
|
+
queryExecStats?: Dict<number>;
|
|
627
|
+
})>;
|
|
628
|
+
<C, const T extends {
|
|
629
|
+
latencyStats?: {
|
|
630
|
+
histograms?: boolean;
|
|
631
|
+
};
|
|
632
|
+
storageStats?: {
|
|
633
|
+
scale?: number;
|
|
634
|
+
};
|
|
635
|
+
count?: Dict<never>;
|
|
636
|
+
queryExecStats?: Dict<never>;
|
|
637
|
+
}>(options: T): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
638
|
+
ns: string;
|
|
639
|
+
localTime: Date;
|
|
640
|
+
} & (T extends {
|
|
641
|
+
latencyStats: any;
|
|
642
|
+
} ? {
|
|
643
|
+
latencyStats: {
|
|
644
|
+
reads: Dict<number>;
|
|
645
|
+
writes: Dict<number>;
|
|
646
|
+
commands: Dict<number>;
|
|
647
|
+
};
|
|
648
|
+
} : {
|
|
649
|
+
latencyStats?: {
|
|
650
|
+
reads: Dict<number>;
|
|
651
|
+
writes: Dict<number>;
|
|
652
|
+
commands: Dict<number>;
|
|
653
|
+
};
|
|
654
|
+
}) & (T extends {
|
|
655
|
+
storageStats: any;
|
|
656
|
+
} ? {
|
|
657
|
+
storageStats: {
|
|
658
|
+
size: number;
|
|
659
|
+
count: number;
|
|
660
|
+
avgObjSize: number;
|
|
661
|
+
storageSize: number;
|
|
662
|
+
};
|
|
663
|
+
} : {
|
|
664
|
+
storageStats?: {
|
|
665
|
+
size: number;
|
|
666
|
+
count: number;
|
|
667
|
+
avgObjSize: number;
|
|
668
|
+
storageSize: number;
|
|
669
|
+
};
|
|
670
|
+
}) & {
|
|
671
|
+
count?: number;
|
|
672
|
+
} & (T extends {
|
|
673
|
+
queryExecStats: any;
|
|
674
|
+
} ? {
|
|
675
|
+
queryExecStats: Dict<number>;
|
|
676
|
+
} : {
|
|
677
|
+
queryExecStats?: Dict<number>;
|
|
678
|
+
})>;
|
|
679
|
+
};
|
|
680
|
+
/** Replaces the input documents with a count of the documents at this stage.
|
|
681
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/count/ */
|
|
682
|
+
declare const $count: <C, const T extends string>(field: T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, { [K in T]: number; }>;
|
|
683
|
+
/** Creates new documents in a sequence of documents where gaps exist.
|
|
684
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/densify/ */
|
|
685
|
+
declare const $densify: <C, const T extends {
|
|
686
|
+
field: keyof C & string;
|
|
687
|
+
partitionByFields?: (keyof C & string)[];
|
|
688
|
+
range: {
|
|
689
|
+
step: number;
|
|
690
|
+
unit?: TimeUnit;
|
|
691
|
+
bounds: "full" | "partition" | [__, __];
|
|
692
|
+
};
|
|
693
|
+
}>(options: NoInfer<T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
694
|
+
/** Returns literal documents from input values (no collection required).
|
|
695
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/documents/ */
|
|
696
|
+
declare const $documents: <const T extends readonly Dict<unknown>[]>(documents: T) => Agg<T[number], T[number]>;
|
|
697
|
+
/** Processes multiple aggregation pipelines on the same input documents in a single stage.
|
|
698
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/facet/ */
|
|
699
|
+
declare const $facet: <C, const T extends Dict<TypedPipeline<C, __>>>(pipelines: ($: ExprBuilder<Simplify<C>>) => T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, { [K in keyof T]: T[K] extends TypedPipeline<any, infer R> ? R[] : T[K] extends readonly [...any[], (agg: any) => Agg<any, infer R>] ? R[] : __[]; }>;
|
|
700
|
+
/** Populates null and missing field values using linear interpolation or last-observed carry-forward.
|
|
701
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/fill/ */
|
|
702
|
+
declare const $fill: <C, const T extends {
|
|
703
|
+
partitionBy?: ExpressionIn<C>;
|
|
704
|
+
partitionByFields?: (keyof C & string)[];
|
|
705
|
+
sortBy: SortBySpec$1<C>;
|
|
706
|
+
output: { [K in keyof C]?: {
|
|
707
|
+
method: "linear" | "locf";
|
|
708
|
+
} | {
|
|
709
|
+
value: ExpressionIn<C>;
|
|
710
|
+
}; };
|
|
711
|
+
}>(options: T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
712
|
+
/** Returns documents ordered by proximity to a geospatial point.
|
|
713
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/geoNear/ */
|
|
714
|
+
declare const $geoNear: <C, const TDistanceField extends string, const TIncludeLocs extends string | undefined = undefined>(options: {
|
|
715
|
+
near: {
|
|
716
|
+
type: "Point";
|
|
717
|
+
coordinates: [number, number];
|
|
718
|
+
};
|
|
719
|
+
distanceField: TDistanceField;
|
|
720
|
+
spherical?: boolean;
|
|
721
|
+
maxDistance?: number;
|
|
722
|
+
minDistance?: number;
|
|
723
|
+
query?: AnyDict;
|
|
724
|
+
distanceMultiplier?: number;
|
|
725
|
+
includeLocs?: TIncludeLocs;
|
|
726
|
+
uniqueDocs?: boolean;
|
|
727
|
+
key?: string;
|
|
728
|
+
}) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C & DeepMerge<DotPathToObject<TDistanceField, number>, TIncludeLocs extends string ? DotPathToObject<TIncludeLocs, {
|
|
729
|
+
type: "Point";
|
|
730
|
+
coordinates: [number, number];
|
|
731
|
+
}> : {}>>;
|
|
732
|
+
/** Performs a recursive search on a collection following references between documents.
|
|
733
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/graphLookup/ */
|
|
734
|
+
declare const $graphLookup: <C, const TFrom extends Collection, const TAs extends string, const TDF extends string | undefined = undefined>(options: {
|
|
735
|
+
from: TFrom;
|
|
736
|
+
startWith: ExpressionIn<C>;
|
|
737
|
+
connectFromField: keyof ForeignType<TFrom> & string;
|
|
738
|
+
connectToField: keyof ForeignType<TFrom> & string;
|
|
739
|
+
as: TAs;
|
|
740
|
+
maxDepth?: number;
|
|
741
|
+
depthField?: TDF;
|
|
742
|
+
restrictSearchWithMatch?: AnyDict;
|
|
743
|
+
}) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C & Record<TAs, (ForeignType<TFrom> & Record<TDF & string, number>)[]>>;
|
|
744
|
+
type GroupIdSpec<C> = ExpressionIn<C> | Ret<C> | null | Dict<ExpressionIn<C> | Ret<C>>;
|
|
745
|
+
type ValidateGroupOutput<C, T extends {
|
|
746
|
+
_id: GroupIdSpec<C>;
|
|
747
|
+
}> = {
|
|
748
|
+
[K in keyof T]: K extends "_id" ? T[K] : T[K] extends Ret<C, any> ? T[K] : OpaqueError<"Invalid $group output - use builder API (e.g., $.sum(...))">;
|
|
749
|
+
};
|
|
750
|
+
/** Groups documents by a specified expression and applies accumulator expressions.
|
|
751
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/group/ */
|
|
752
|
+
declare const $group: <C, const T extends {
|
|
753
|
+
_id: GroupIdSpec<C>;
|
|
754
|
+
}>(spec: ($: AccumulatorBuilder<Simplify<C>>) => ValidateGroupOutput<C, T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, ResolveSpec<C, T>>;
|
|
755
|
+
/** Returns statistics about index usage for the collection.
|
|
756
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/indexStats/ */
|
|
757
|
+
declare const $indexStats: <C>() => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, IndexStatsResult>;
|
|
758
|
+
/** Limits the number of documents passed to the next stage.
|
|
759
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/limit/ */
|
|
760
|
+
declare const $limit: <C, const T extends number>(n: T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
761
|
+
type LetToContext<C, Let extends Dict<ExpressionIn<C>>> = {
|
|
762
|
+
[K in keyof Let as `$${K & string}`]: ResolveType<C, Let[K]>;
|
|
763
|
+
};
|
|
764
|
+
type StripLookupVars<T> = {
|
|
765
|
+
[K in keyof T as K extends `$${string}` ? never : K]: T[K];
|
|
766
|
+
};
|
|
767
|
+
type LookupContext<TFrom extends Collection, C, TLet extends Dict<ExpressionIn<C>> | undefined> = TLet extends Dict<ExpressionIn<C>> ? ForeignType<TFrom> & LetToContext<C, TLet> : ForeignType<TFrom>;
|
|
768
|
+
/** Performs a left outer join with another collection.
|
|
769
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/ */
|
|
770
|
+
declare const $lookup: {
|
|
771
|
+
<C, TFrom extends Collection, const TAs extends string, const TLet extends Dict<ExpressionIn<C>> | undefined = undefined, TPipelineOut = ForeignType<TFrom>>(options: {
|
|
772
|
+
from: TFrom;
|
|
773
|
+
localField?: PathType<C>;
|
|
774
|
+
foreignField?: PathType<ForeignType<TFrom>>;
|
|
775
|
+
let?: TLet;
|
|
776
|
+
pipeline?: ($: ExprBuilder<LookupContext<TFrom, C, TLet>>) => TypedPipeline<LookupContext<TFrom, C, TLet>, TPipelineOut>;
|
|
777
|
+
as: TAs;
|
|
778
|
+
}): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, Simplify<C & Record<TAs, StripLookupVars<TPipelineOut>[]>>>;
|
|
779
|
+
<C, TFrom extends Collection, const TAs extends string>(options: {
|
|
780
|
+
from: TFrom;
|
|
781
|
+
localField?: PathType<C>;
|
|
782
|
+
foreignField?: PathType<ForeignType<TFrom>>;
|
|
783
|
+
pipeline: readonly object[];
|
|
784
|
+
as: TAs;
|
|
785
|
+
}): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, CallbackOnlyError<"$lookup.pipeline">>;
|
|
786
|
+
};
|
|
787
|
+
/** Filters documents to pass only those that match the specified conditions.
|
|
788
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/match/ */
|
|
789
|
+
declare const $match: <C, const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<Simplify<C>>) => R) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
790
|
+
/** Writes pipeline results to a collection, merging with existing documents. Terminal stage.
|
|
791
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/merge/ */
|
|
792
|
+
declare const $merge: <C, const T extends {
|
|
793
|
+
into: Collection;
|
|
794
|
+
on?: (keyof C & string) | readonly (keyof C & string)[];
|
|
795
|
+
let?: Dict<ExpressionIn<C>>;
|
|
796
|
+
whenMatched?: "replace" | "keepExisting" | "merge" | "fail" | readonly AnyDict[];
|
|
797
|
+
whenNotMatched?: "insert" | "discard" | "fail";
|
|
798
|
+
}>(options: T & MergeIntoConstraint<C, T["into"]>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, never>;
|
|
799
|
+
/** Writes pipeline results to a collection, replacing existing content. Terminal stage.
|
|
800
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/out/ */
|
|
801
|
+
declare const $out: <C, const T extends Collection | {
|
|
802
|
+
db: string;
|
|
803
|
+
coll: Collection;
|
|
804
|
+
}>(options: T & OutIntoConstraint<C, T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, never>;
|
|
805
|
+
/** Reshapes documents by including, excluding, or computing new fields.
|
|
806
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/project/ */
|
|
807
|
+
declare const $project: <C, const T extends Dict<0 | 1 | ExpressionIn<C> | Ret<C>>>(spec: ($: ExprBuilder<Simplify<C>>) => ValidateProjectRules<ValidateSpec<C, T>>) => UpdateStageFunction<C, RemoveNever<ProjectResult<C, T>>>;
|
|
808
|
+
/** Restricts document content based on access-control expressions ($$DESCEND, $$PRUNE, $$KEEP).
|
|
809
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/redact/ */
|
|
810
|
+
declare const $redact: {
|
|
811
|
+
<C>(expression: ($: ExprBuilder<Simplify<C>>) => Ret<C, "$$DESCEND" | "$$PRUNE" | "$$KEEP">): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
812
|
+
<C, const T extends "$$DESCEND" | "$$PRUNE" | "$$KEEP" | Ret<C, "$$DESCEND" | "$$PRUNE" | "$$KEEP">>(expression: T): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
813
|
+
};
|
|
814
|
+
/** Replaces the input document with the specified document (via `newRoot`).
|
|
815
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceRoot/ */
|
|
816
|
+
declare const $replaceRoot: {
|
|
817
|
+
<C, const T extends {
|
|
818
|
+
newRoot: ReplaceRootValue<C>;
|
|
819
|
+
}>(options: T): UpdateStageFunction<C, ReplaceRootResolved<C, T["newRoot"]>>;
|
|
820
|
+
<C, const T extends {
|
|
821
|
+
newRoot: ReplaceRootValue<Simplify<C>>;
|
|
822
|
+
}>(options: ($: ExprBuilder<Simplify<C>>) => T): UpdateStageFunction<C, ReplaceRootResolved<C, T["newRoot"]>>;
|
|
823
|
+
<C, const T extends ReplaceRootValue<C>>(expression: T): UpdateStageFunction<C, ReplaceRootResolved<C, T>>;
|
|
824
|
+
<C, const T extends ReplaceRootValue<Simplify<C>>>(expression: ($: ExprBuilder<Simplify<C>>) => T): UpdateStageFunction<C, ReplaceRootResolved<C, T>>;
|
|
825
|
+
};
|
|
826
|
+
/** Replaces the input document with the specified expression. Shorthand for `$replaceRoot`.
|
|
827
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/replaceWith/ */
|
|
828
|
+
declare const $replaceWith: {
|
|
829
|
+
<C, const T extends ReplaceRootValue<C>>(expression: T): UpdateStageFunction<C, ReplaceRootResolved<C, T>>;
|
|
830
|
+
<C, const T extends ReplaceRootValue<Simplify<C>>>(expression: ($: ExprBuilder<Simplify<C>>) => T): UpdateStageFunction<C, ReplaceRootResolved<C, T>>;
|
|
831
|
+
};
|
|
832
|
+
/** Randomly selects the specified number of documents from input.
|
|
833
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sample/ */
|
|
834
|
+
declare const $sample: <C, const T extends number>(options: {
|
|
835
|
+
size: T;
|
|
836
|
+
}) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
837
|
+
/** Adds new fields to documents. Alias for `$addFields`.
|
|
838
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/set/ */
|
|
839
|
+
declare const $set: {
|
|
840
|
+
<C, const T extends Dict<ExpressionIn<C> | Ret<C>>>(fields: NoInfer<T>): UpdateStageFunction<C, Simplify<C & { [K in keyof T]: ResolveType<C, T[K]>; }>>;
|
|
841
|
+
<C, const T extends Dict<ExpressionIn<Simplify<C>> | Ret<Simplify<C>>>>(fields: ($: ExprBuilder<Simplify<C>>) => T): UpdateStageFunction<C, Simplify<C & { [K in keyof T]: ResolveType<C, T[K]>; }>>;
|
|
842
|
+
};
|
|
843
|
+
/** Performs window function calculations across a specified span of documents.
|
|
844
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/setWindowFields/ */
|
|
845
|
+
declare const $setWindowFields: <C, const T extends {
|
|
846
|
+
partitionBy?: ExpressionIn<C> | Dict<ExpressionIn<C> | Ret<C>> | Ret<C>;
|
|
847
|
+
sortBy?: SortBySpec$1<C>;
|
|
848
|
+
output: Dict<WindowOperatorSpec<C>>;
|
|
849
|
+
}>(options: ($: WindowBuilder<Simplify<C>>) => WindowFieldsOptions<C, T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C & { [K in keyof T["output"]]: WindowOperatorResult<C, T["output"][K]>; }>;
|
|
850
|
+
/** Skips the first N documents.
|
|
851
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/skip/ */
|
|
852
|
+
declare const $skip: <C, const T extends number>(n: T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
853
|
+
/** Sorts all input documents and returns them in the specified order.
|
|
854
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sort/ */
|
|
855
|
+
declare const $sort: <C>(spec: SortSpec$1<C>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C>;
|
|
856
|
+
/** Groups documents by an expression and sorts by count.
|
|
857
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortByCount/ */
|
|
858
|
+
declare const $sortByCount: <C, const T extends ExpressionIn<C>>(expression: T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, SortByCountResult<ResolveType<C, T>>>;
|
|
859
|
+
/** Combines pipeline results from another collection into the current pipeline.
|
|
860
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/unionWith/ */
|
|
861
|
+
declare const $unionWith: <C, const T extends {
|
|
862
|
+
coll: Collection;
|
|
863
|
+
pipeline?: TypedPipeline<ForeignType<T["coll"]>, __> | readonly ((agg: Agg<ForeignType<T["coll"]>, ForeignType<T["coll"]>>) => any)[];
|
|
864
|
+
}>(options: T) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, C | ForeignType<T["coll"]>>;
|
|
865
|
+
/** Removes specified fields from documents.
|
|
866
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/unset/ */
|
|
867
|
+
declare const $unset: <C, const T extends readonly (keyof C & string)[]>(...fields: T) => UpdateStageFunction<C, UnsetResult<C, T extends readonly string[] ? [...T] : never>>;
|
|
868
|
+
/** Deconstructs an array field, outputting one document per array element.
|
|
869
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/unwind/ */
|
|
870
|
+
declare const $unwind: {
|
|
871
|
+
<C, const P extends `$${ArrayRootPath<C>}`>(path: P): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, UnwindResult<C, P>>;
|
|
872
|
+
<C, const P extends `$${ArrayRootPath<C>}`, const TPreserve extends boolean | undefined = undefined, const TIndexField extends string | undefined = undefined>(options: {
|
|
873
|
+
path: P;
|
|
874
|
+
includeArrayIndex?: TIndexField;
|
|
875
|
+
preserveNullAndEmptyArrays?: TPreserve;
|
|
876
|
+
}): <TIn>(agg: Agg<TIn, C>) => Agg<TIn, UnwindResultWithPreserve<C, P, TPreserve> & (TIndexField extends string ? Record<TIndexField, number> : {})>;
|
|
877
|
+
};
|
|
878
|
+
type SanitizeAny<T> = IsAny<T> extends true ? never : IsUnknown<T> extends true ? never : T extends Date ? T : T extends readonly (infer E)[] ? readonly SanitizeAny<E>[] : T extends object ? {
|
|
879
|
+
[K in keyof T]: SanitizeAny<T[K]>;
|
|
880
|
+
} : T;
|
|
881
|
+
type ChangeStreamDocumentSafe<T extends Document> = SanitizeAny<ChangeStreamDocument<T>>;
|
|
882
|
+
/** Opens a change stream cursor on a collection. Must be the first stage.
|
|
883
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/changeStream/ */
|
|
884
|
+
declare const $changeStream: <C extends Document, const T extends {
|
|
885
|
+
allChangesForCluster?: boolean;
|
|
886
|
+
fullDocument?: "default" | "required" | "updateLookup" | "whenAvailable";
|
|
887
|
+
fullDocumentBeforeChange?: "off" | "whenAvailable" | "required";
|
|
888
|
+
resumeAfter?: AnyDict;
|
|
889
|
+
startAfter?: AnyDict;
|
|
890
|
+
startAtOperationTime?: Date;
|
|
891
|
+
showExpandedEvents?: boolean;
|
|
892
|
+
}>(options: NoInfer<T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, Simplify<ChangeStreamDocumentSafe<C>>>;
|
|
893
|
+
/** Returns information on active and queued operations. For `db.aggregate()` only.
|
|
894
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/currentOp/ */
|
|
895
|
+
declare const $currentOp: <C, const T extends {
|
|
896
|
+
allUsers?: boolean;
|
|
897
|
+
idleConnections?: boolean;
|
|
898
|
+
idleCursors?: boolean;
|
|
899
|
+
idleSessions?: boolean;
|
|
900
|
+
localOps?: boolean;
|
|
901
|
+
}>(options: NoInfer<T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
902
|
+
host: string;
|
|
903
|
+
desc: string;
|
|
904
|
+
connectionId: number;
|
|
905
|
+
client: string;
|
|
906
|
+
appName?: string;
|
|
907
|
+
clientMetadata?: AnyDict;
|
|
908
|
+
active: boolean;
|
|
909
|
+
currentOpTime: Date;
|
|
910
|
+
opid: number;
|
|
911
|
+
secs_running: number;
|
|
912
|
+
microsecs_running: number;
|
|
913
|
+
op: string;
|
|
914
|
+
ns: string;
|
|
915
|
+
command: AnyDict;
|
|
916
|
+
planSummary?: string;
|
|
917
|
+
cursor?: {
|
|
918
|
+
cursorId: number;
|
|
919
|
+
createdDate: Date;
|
|
920
|
+
lastAccessDate: Date;
|
|
921
|
+
};
|
|
922
|
+
lsid?: {
|
|
923
|
+
id: ObjectId;
|
|
924
|
+
uid: string;
|
|
925
|
+
};
|
|
926
|
+
transaction?: AnyDict;
|
|
927
|
+
locking?: AnyDict;
|
|
928
|
+
waitingForLock?: boolean;
|
|
929
|
+
msg?: string;
|
|
930
|
+
progress?: {
|
|
931
|
+
done: number;
|
|
932
|
+
total: number;
|
|
933
|
+
};
|
|
934
|
+
killPending?: boolean;
|
|
935
|
+
numYields?: number;
|
|
936
|
+
dataThroughputLastSecond?: number;
|
|
937
|
+
dataThroughputAverage?: number;
|
|
938
|
+
}>;
|
|
939
|
+
/** Lists sessions cached in memory by the `mongod` or `mongos` instance.
|
|
940
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/listLocalSessions/ */
|
|
941
|
+
declare const $listLocalSessions: <C, const T extends {
|
|
942
|
+
allUsers?: boolean;
|
|
943
|
+
users?: {
|
|
944
|
+
user: string;
|
|
945
|
+
db: string;
|
|
946
|
+
}[];
|
|
947
|
+
}>(options: NoInfer<T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
948
|
+
_id: {
|
|
949
|
+
id: ObjectId;
|
|
950
|
+
uid: string;
|
|
951
|
+
};
|
|
952
|
+
lastUse: Date;
|
|
953
|
+
user?: {
|
|
954
|
+
user: string;
|
|
955
|
+
db: string;
|
|
956
|
+
};
|
|
957
|
+
client?: string;
|
|
958
|
+
connections?: number;
|
|
959
|
+
activeTransactions?: AnyDict;
|
|
960
|
+
}>;
|
|
961
|
+
/** Lists all sessions stored in the `system.sessions` collection.
|
|
962
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/listSessions/ */
|
|
963
|
+
declare const $listSessions: <C, const T extends {
|
|
964
|
+
allUsers?: boolean;
|
|
965
|
+
users?: {
|
|
966
|
+
user: string;
|
|
967
|
+
db: string;
|
|
968
|
+
}[];
|
|
969
|
+
}>(options: NoInfer<T>) => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
970
|
+
_id: {
|
|
971
|
+
id: ObjectId;
|
|
972
|
+
uid: string;
|
|
973
|
+
};
|
|
974
|
+
lastUse: Date;
|
|
975
|
+
user?: {
|
|
976
|
+
user: string;
|
|
977
|
+
db: string;
|
|
978
|
+
};
|
|
979
|
+
client?: string;
|
|
980
|
+
connections?: number;
|
|
981
|
+
activeTransactions?: AnyDict;
|
|
982
|
+
}>;
|
|
983
|
+
/** Returns plan cache information for a collection.
|
|
984
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/planCacheStats/ */
|
|
985
|
+
declare const $planCacheStats: <C>() => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
986
|
+
version: string;
|
|
987
|
+
created: Date;
|
|
988
|
+
planCacheKey: string;
|
|
989
|
+
queryHash: string;
|
|
990
|
+
planCacheKeyWithQueryHash: string;
|
|
991
|
+
isActive: boolean;
|
|
992
|
+
works: number;
|
|
993
|
+
timeOfCreation: Date;
|
|
994
|
+
timeOfCreationMicros: number;
|
|
995
|
+
lastSeenDate: Date;
|
|
996
|
+
lastSeenDateMicros: number;
|
|
997
|
+
creationExecStats?: AnyDict;
|
|
998
|
+
cachedPlan?: AnyDict;
|
|
999
|
+
winningPlan?: AnyDict;
|
|
1000
|
+
candidatePlans?: AnyDict[];
|
|
1001
|
+
}>;
|
|
1002
|
+
/** Returns data distribution information for sharded collections.
|
|
1003
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/shardedDataDistribution/ */
|
|
1004
|
+
declare const $shardedDataDistribution: <C>() => <TIn>(agg: Agg<TIn, C>) => Agg<TIn, {
|
|
1005
|
+
ns: string;
|
|
1006
|
+
shards: Record<string, {
|
|
1007
|
+
numOrphanedDocs: number;
|
|
1008
|
+
numOwnedDocuments: number;
|
|
1009
|
+
ownedSizeBytes: number;
|
|
1010
|
+
orphanedSizeBytes: number;
|
|
1011
|
+
}>;
|
|
1012
|
+
numShards: number;
|
|
1013
|
+
collections: Record<string, {
|
|
1014
|
+
numOrphanedDocs: number;
|
|
1015
|
+
numOwnedDocuments: number;
|
|
1016
|
+
ownedSizeBytes: number;
|
|
1017
|
+
orphanedSizeBytes: number;
|
|
1018
|
+
}>;
|
|
1019
|
+
totalSizeBytes: number;
|
|
1020
|
+
totalOrphanedSizeBytes: number;
|
|
1021
|
+
totalOwnedSizeBytes: number;
|
|
1022
|
+
totalNumOrphanedDocs: number;
|
|
1023
|
+
totalNumOwnedDocuments: number;
|
|
1024
|
+
}>;
|
|
1025
|
+
|
|
1026
|
+
/**
|
|
1027
|
+
* Update pipeline stages - unified with aggregation pipeline stages
|
|
1028
|
+
*
|
|
1029
|
+
* All stages are imported from sluice-stages.ts for single source of truth.
|
|
1030
|
+
* Update pipelines use the exact same StageFunction types as aggregation pipelines.
|
|
1031
|
+
*/
|
|
1032
|
+
|
|
1033
|
+
/**
|
|
1034
|
+
* Type for the context-aware update operators passed as `$` to update pipeline callbacks.
|
|
1035
|
+
*
|
|
1036
|
+
* Members are named without the `$` prefix so callback usage reads naturally:
|
|
1037
|
+
* `$ => $.pipe($.set(...), $.unset(...))`
|
|
1038
|
+
*/
|
|
1039
|
+
type UpdateOperators<C> = {
|
|
1040
|
+
set: typeof $set;
|
|
1041
|
+
unset: typeof $unset;
|
|
1042
|
+
replaceRoot: typeof $replaceRoot;
|
|
1043
|
+
replaceWith: typeof $replaceWith;
|
|
1044
|
+
addFields: typeof $addFields;
|
|
1045
|
+
project: typeof $project;
|
|
1046
|
+
pipe: UpdatePipelineBuilder<C>;
|
|
1047
|
+
};
|
|
1048
|
+
/**
|
|
1049
|
+
* Callback type for update pipelines that receives context-aware operators
|
|
1050
|
+
*/
|
|
1051
|
+
type UpdatePipelineCallback<C> = ($: UpdateOperators<C>) => TypedPipeline<C, unknown>;
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* Validation types for MongoDB update operations
|
|
1055
|
+
* Provides three categories of compile-time errors:
|
|
1056
|
+
* 1. Invalid path (including $, $[], $[id] syntax)
|
|
1057
|
+
* 2. Invalid value type for the path
|
|
1058
|
+
* 3. Invalid or missing arrayFilters
|
|
1059
|
+
*/
|
|
1060
|
+
|
|
1061
|
+
/**
|
|
1062
|
+
* Extract all $[identifier] from a path string
|
|
1063
|
+
*/
|
|
1064
|
+
type ExtractIdentifier<P extends string> = P extends `${string}$[${infer Id}]${infer Rest}` ? Id extends "" ? ExtractIdentifier<Rest> : Id | ExtractIdentifier<Rest> : never;
|
|
1065
|
+
/**
|
|
1066
|
+
* Extract all identifiers from all paths in an operator spec
|
|
1067
|
+
*/
|
|
1068
|
+
type ExtractIdentifiersFromSpec<Spec extends object> = {
|
|
1069
|
+
[K in keyof Spec]: K extends string ? ExtractIdentifier<K> : never;
|
|
1070
|
+
}[keyof Spec];
|
|
1071
|
+
/**
|
|
1072
|
+
* Extract all identifiers from entire update spec
|
|
1073
|
+
*/
|
|
1074
|
+
type ExtractRequiredIdentifiers<UpdateSpec> = UpdateSpec extends object ? {
|
|
1075
|
+
[Op in keyof UpdateSpec]: Op extends `$${string}` ? UpdateSpec[Op] extends object ? ExtractIdentifiersFromSpec<UpdateSpec[Op]> : never : never;
|
|
1076
|
+
}[keyof UpdateSpec] : never;
|
|
1077
|
+
type NormalizePaths<Paths extends readonly string[]> = Paths extends readonly [infer Head extends string, ...infer Tail extends readonly string[]] ? readonly [NormalizeArrayFilterPath<Head>, ...NormalizePaths<Tail>] : readonly [];
|
|
1078
|
+
type HasDuplicates<T extends readonly string[]> = T extends readonly [infer Head extends string, ...infer Tail extends readonly string[]] ? Head extends Tail[number] ? true : HasDuplicates<Tail> : false;
|
|
1079
|
+
type HasPathConflict<T extends readonly string[], U extends T[number] = T[number]> = HasDuplicates<T> extends true ? true : (U extends string ? IsPrefix<U, Exclude<T[number], U>> : never) extends infer Result ? [
|
|
1080
|
+
Result
|
|
1081
|
+
] extends [never] ? false : true : never;
|
|
1082
|
+
type ValidatePathConflicts<Paths extends readonly string[]> = [
|
|
1083
|
+
HasPathConflict<NormalizePaths<Paths>>
|
|
1084
|
+
] extends [true] ? OpaqueError<"Conflicting update paths (duplicate or parent/child)"> : Paths;
|
|
1085
|
+
|
|
1086
|
+
/**
|
|
1087
|
+
* Strict type definitions for MongoDB update operators
|
|
1088
|
+
* Each operator has precise path and value constraints
|
|
1089
|
+
*/
|
|
1090
|
+
|
|
1091
|
+
type Nil = null | undefined;
|
|
1092
|
+
type ValueOrExpr<C, T> = T | Ret<C, T>;
|
|
1093
|
+
type EmptyArrayLiteral = readonly [] | never[];
|
|
1094
|
+
type AllowEmptyArray<T> = [T] extends [readonly unknown[]] ? T | EmptyArrayLiteral : T;
|
|
1095
|
+
type AllPaths<T> = UpdatePathType<T> | ArrayRootPath<T>;
|
|
1096
|
+
type ValidPath<T, P extends AllPaths<T>> = P extends string ? ValidPositionalPath<P> : P;
|
|
1097
|
+
type SetSpec<T> = {
|
|
1098
|
+
[P in AllPaths<T> as ValidPath<T, P>]?: ValueOrExpr<T, AllowEmptyArray<ResolveValue<T, P>>>;
|
|
1099
|
+
};
|
|
1100
|
+
type UnsetSpec<T> = {
|
|
1101
|
+
[P in AllPaths<T> as ValidPath<T, P>]?: "" | 1 | true;
|
|
1102
|
+
};
|
|
1103
|
+
type IncSpec<T> = UpdateSpecOfType<T, number | Nil, ValueOrExpr<T, number>>;
|
|
1104
|
+
type MulSpec<T> = UpdateSpecOfType<T, number | Nil, ValueOrExpr<T, number>>;
|
|
1105
|
+
type MinMaxSpec<T> = {
|
|
1106
|
+
[P in UpdatePathType<T> as ResolveValue<T, P> extends string | number | Date ? P : never]?: ValueOrExpr<T, ResolveUpdatePath<T, P>>;
|
|
1107
|
+
};
|
|
1108
|
+
type RenameSpec<T> = {
|
|
1109
|
+
[P in AllPaths<T>]?: string;
|
|
1110
|
+
};
|
|
1111
|
+
type CurrentDateSpec<T> = UpdateSpecOfType<T, Date | Nil, true | {
|
|
1112
|
+
$type: "date" | "timestamp";
|
|
1113
|
+
}>;
|
|
1114
|
+
type PushModifiers<C, Elem> = {
|
|
1115
|
+
$each: readonly ValueOrExpr<C, Elem>[];
|
|
1116
|
+
$position?: ValueOrExpr<C, number>;
|
|
1117
|
+
$slice?: ValueOrExpr<C, number>;
|
|
1118
|
+
$sort?: 1 | -1 | (Elem extends object ? Partial<Record<keyof Elem, 1 | -1>> : never);
|
|
1119
|
+
};
|
|
1120
|
+
type PushSpec<T> = {
|
|
1121
|
+
[P in UpdateArrayPath<T>]?: ValueOrExpr<T, UpdateArrayElementType<T, P>> | (UpdateArrayElementType<T, P> extends infer Elem ? {
|
|
1122
|
+
$each: readonly ValueOrExpr<T, Elem>[];
|
|
1123
|
+
} | Partial<PushModifiers<T, Elem>> : never);
|
|
1124
|
+
};
|
|
1125
|
+
type AddToSetSpec<T> = {
|
|
1126
|
+
[P in UpdateArrayPath<T>]?: ValueOrExpr<T, UpdateArrayElementType<T, P>> | {
|
|
1127
|
+
$each: readonly ValueOrExpr<T, UpdateArrayElementType<T, P>>[];
|
|
1128
|
+
};
|
|
1129
|
+
};
|
|
1130
|
+
type PullMatch<T, V> = {
|
|
1131
|
+
$eq?: ValueOrExpr<T, V>;
|
|
1132
|
+
$ne?: ValueOrExpr<T, V>;
|
|
1133
|
+
$gt?: ValueOrExpr<T, V>;
|
|
1134
|
+
$gte?: ValueOrExpr<T, V>;
|
|
1135
|
+
$lt?: ValueOrExpr<T, V>;
|
|
1136
|
+
$lte?: ValueOrExpr<T, V>;
|
|
1137
|
+
$in?: readonly ValueOrExpr<T, V>[];
|
|
1138
|
+
$nin?: readonly ValueOrExpr<T, V>[];
|
|
1139
|
+
$regex?: string | RegExp;
|
|
1140
|
+
$exists?: boolean;
|
|
1141
|
+
};
|
|
1142
|
+
type PullCondition<T, Elem> = {
|
|
1143
|
+
[K in keyof Elem]?: ValueOrExpr<T, Elem[K]> | PullMatch<T, Elem[K]> | (Elem[K] extends readonly (infer Item)[] ? ValueOrExpr<T, Item> | PullMatch<T, Item> : never);
|
|
1144
|
+
} & {
|
|
1145
|
+
$and?: readonly PullCondition<T, Elem>[];
|
|
1146
|
+
$or?: readonly PullCondition<T, Elem>[];
|
|
1147
|
+
$nor?: readonly PullCondition<T, Elem>[];
|
|
1148
|
+
};
|
|
1149
|
+
type PullSpec<T> = {
|
|
1150
|
+
[P in UpdateArrayPath<T>]?: ValueOrExpr<T, UpdateArrayElementType<T, P>> | PullMatch<T, UpdateArrayElementType<T, P>> | (UpdateArrayElementType<T, P> extends object ? PullCondition<T, UpdateArrayElementType<T, P>> : never);
|
|
1151
|
+
};
|
|
1152
|
+
type PopSpec<T> = {
|
|
1153
|
+
[P in UpdateArrayPath<T>]?: 1 | -1;
|
|
1154
|
+
};
|
|
1155
|
+
type PullAllSpec<T> = {
|
|
1156
|
+
[P in UpdateArrayPath<T>]?: readonly ValueOrExpr<T, UpdateArrayElementType<T, P>>[];
|
|
1157
|
+
};
|
|
1158
|
+
type BitSpec<T> = UpdateSpecOfType<T, number | Nil, {
|
|
1159
|
+
and: ValueOrExpr<T, number>;
|
|
1160
|
+
} | {
|
|
1161
|
+
or: ValueOrExpr<T, number>;
|
|
1162
|
+
} | {
|
|
1163
|
+
xor: ValueOrExpr<T, number>;
|
|
1164
|
+
}>;
|
|
1165
|
+
|
|
1166
|
+
/**
|
|
1167
|
+
* Strict typing for MongoDB update operations
|
|
1168
|
+
* Ensures type safety for update specs, array filters, and positional operators
|
|
1169
|
+
*/
|
|
1170
|
+
|
|
1171
|
+
/**
|
|
1172
|
+
* StrictUpdateSpec - Type-safe MongoDB update document
|
|
1173
|
+
* Each operator validates paths and values against the document schema
|
|
1174
|
+
*/
|
|
1175
|
+
type StrictUpdateSpec<T> = {
|
|
1176
|
+
$set?: SetSpec<T>;
|
|
1177
|
+
$setOnInsert?: SetSpec<T>;
|
|
1178
|
+
$unset?: UnsetSpec<T>;
|
|
1179
|
+
$inc?: IncSpec<T>;
|
|
1180
|
+
$mul?: MulSpec<T>;
|
|
1181
|
+
$min?: MinMaxSpec<T>;
|
|
1182
|
+
$max?: MinMaxSpec<T>;
|
|
1183
|
+
$rename?: RenameSpec<T>;
|
|
1184
|
+
$currentDate?: CurrentDateSpec<T>;
|
|
1185
|
+
$push?: PushSpec<T>;
|
|
1186
|
+
$addToSet?: AddToSetSpec<T>;
|
|
1187
|
+
$pull?: PullSpec<T>;
|
|
1188
|
+
$pop?: PopSpec<T>;
|
|
1189
|
+
$pullAll?: PullAllSpec<T>;
|
|
1190
|
+
$bit?: BitSpec<T>;
|
|
1191
|
+
};
|
|
1192
|
+
type ValidateOperatorSpec<Spec, Expected> = {
|
|
1193
|
+
[P in keyof Spec]: P extends keyof Expected ? Spec[P] : never;
|
|
1194
|
+
};
|
|
1195
|
+
type ValidateUpdateSpec<T, Spec extends StrictUpdateSpec<T>> = {
|
|
1196
|
+
[K in keyof Spec]: K extends "$set" ? ValidateOperatorSpec<Spec[K], SetSpec<T>> : K extends "$setOnInsert" ? ValidateOperatorSpec<Spec[K], SetSpec<T>> : K extends "$unset" ? ValidateOperatorSpec<Spec[K], UnsetSpec<T>> : K extends "$inc" ? ValidateOperatorSpec<Spec[K], IncSpec<T>> : K extends "$mul" ? ValidateOperatorSpec<Spec[K], MulSpec<T>> : K extends "$min" ? ValidateOperatorSpec<Spec[K], MinMaxSpec<T>> : K extends "$max" ? ValidateOperatorSpec<Spec[K], MinMaxSpec<T>> : K extends "$rename" ? ValidateOperatorSpec<Spec[K], RenameSpec<T>> : K extends "$currentDate" ? ValidateOperatorSpec<Spec[K], CurrentDateSpec<T>> : K extends "$push" ? ValidateOperatorSpec<Spec[K], PushSpec<T>> : K extends "$addToSet" ? ValidateOperatorSpec<Spec[K], AddToSetSpec<T>> : K extends "$pull" ? ValidateOperatorSpec<Spec[K], PullSpec<T>> : K extends "$pop" ? ValidateOperatorSpec<Spec[K], PopSpec<T>> : K extends "$pullAll" ? ValidateOperatorSpec<Spec[K], PullAllSpec<T>> : K extends "$bit" ? ValidateOperatorSpec<Spec[K], BitSpec<T>> : never;
|
|
1197
|
+
} & (ValidatePathConflicts<ExtractPaths<Spec>> extends infer Conflict ? Conflict extends OpaqueError<string> ? Conflict : unknown : unknown);
|
|
1198
|
+
type RecordToTupleType<T extends Dict<any>> = UnionToTuple<ValueOf<{
|
|
1199
|
+
[K in keyof T]: [K, T[K]];
|
|
1200
|
+
}>>;
|
|
1201
|
+
type FlattenPaths<T extends readonly any[], Acc extends readonly string[] = readonly []> = T extends readonly [readonly [any, infer Paths extends readonly string[]], ...infer Rest] ? FlattenPaths<Rest, readonly [...Acc, ...Paths]> : Acc;
|
|
1202
|
+
type ExtractPaths<UpdateSpec extends Dict<any>> = FlattenPaths<RecordToTupleType<{
|
|
1203
|
+
[Op in keyof UpdateSpec]: UnionToTuple<keyof UpdateSpec[Op] & string>;
|
|
1204
|
+
}>>;
|
|
1205
|
+
type UnionToIntersection<U> = (U extends unknown ? (value: U) => void : never) extends (value: infer I) => void ? I : never;
|
|
1206
|
+
type ArrayIndexPath<Elem> = Elem extends readonly (infer E)[] ? `${number}` | `${number}.${PathType<E>}` : never;
|
|
1207
|
+
type ArrayFilterKey<Elem, Id extends string> = Elem extends Dict<unknown> ? Id | `${Id}.${PathType<Elem>}` : Elem extends readonly unknown[] ? Id | `${Id}.${ArrayIndexPath<Elem>}` : Id;
|
|
1208
|
+
type ArrayFilterOperators<T> = {
|
|
1209
|
+
$eq?: T;
|
|
1210
|
+
$ne?: T;
|
|
1211
|
+
$gt?: T extends number | Date | string ? T : never;
|
|
1212
|
+
$gte?: T extends number | Date | string ? T : never;
|
|
1213
|
+
$lt?: T extends number | Date | string ? T : never;
|
|
1214
|
+
$lte?: T extends number | Date | string ? T : never;
|
|
1215
|
+
$in?: readonly T[];
|
|
1216
|
+
$nin?: readonly T[];
|
|
1217
|
+
$regex?: T extends string ? string | RegExp : never;
|
|
1218
|
+
$exists?: boolean;
|
|
1219
|
+
$type?: string | number;
|
|
1220
|
+
$size?: T extends readonly unknown[] ? number : never;
|
|
1221
|
+
};
|
|
1222
|
+
type ArrayFilterValue<Elem, Id extends string, K extends string> = K extends `${Id}.${infer P}` ? P extends PathType<Elem> ? ResolvePath<Elem, P> | ArrayFilterOperators<ResolvePath<Elem, P>> : never : Elem | ArrayFilterOperators<Elem>;
|
|
1223
|
+
type ArrayFilterSpecForId<Elem, Id extends string> = {
|
|
1224
|
+
[K in ArrayFilterKey<Elem, Id>]?: ArrayFilterValue<Elem, Id, K>;
|
|
1225
|
+
};
|
|
1226
|
+
/**
|
|
1227
|
+
* Extract array filter ID to path mapping recursively
|
|
1228
|
+
* e.g., "tags.$[i]" => { i: "tags.$[i]" }
|
|
1229
|
+
* "items.$[i].$[j]" => { i: "items.$[i]", j: "items.$[i].$[j]" }
|
|
1230
|
+
*/
|
|
1231
|
+
type ExtractArrayFilterIdPaths<P extends string, Prefix extends string = "", Acc extends Dict<string> = {}> = P extends `${infer Before}.$[${infer Id}]${infer After}` ? Id extends "" ? ExtractArrayFilterIdPaths<After, `${Prefix}${Before}.$[]`, Acc> : ExtractArrayFilterIdPaths<After, `${Prefix}${Before}.$[${Id}]`, Acc & {
|
|
1232
|
+
[K in Id]: `${Prefix}${Before}.$[${Id}]`;
|
|
1233
|
+
}> : Acc;
|
|
1234
|
+
/**
|
|
1235
|
+
* Extract all update paths from a StrictUpdateSpec
|
|
1236
|
+
*/
|
|
1237
|
+
type ExtractPathsFromUpdateSpec<T, Spec extends StrictUpdateSpec<T>> = {
|
|
1238
|
+
[K in keyof Spec]: K extends `$${string}` ? Spec[K] extends object ? keyof Spec[K] & string : never : never;
|
|
1239
|
+
}[keyof Spec] & UpdatePathType<T>;
|
|
1240
|
+
/**
|
|
1241
|
+
* Convert ID->Path map to ID->Type map
|
|
1242
|
+
* Handles union of paths by merging all ID->Type mappings via UnionToIntersection
|
|
1243
|
+
*/
|
|
1244
|
+
type ArrayFilterTypes<C, P extends UpdatePathType<C>> = Simplify<UnionToIntersection<P extends infer Path extends string ? ExtractArrayFilterIdPaths<Path> extends infer M ? {
|
|
1245
|
+
[Id in keyof M]: ResolveUpdatePath<C, M[Id] & UpdatePathType<C>>;
|
|
1246
|
+
} : never : never>>;
|
|
1247
|
+
/**
|
|
1248
|
+
* Infer array filter types from update spec
|
|
1249
|
+
*/
|
|
1250
|
+
type InferArrayFilters<T, Spec extends StrictUpdateSpec<T>> = ArrayFilterTypes<T, ExtractPathsFromUpdateSpec<T, Spec>>;
|
|
1251
|
+
/**
|
|
1252
|
+
* Required array filters when positional operators are used
|
|
1253
|
+
*/
|
|
1254
|
+
type RequiredArrayFilters<T, Spec extends StrictUpdateSpec<T>> = ExtractRequiredIdentifiers<Spec> extends never ? {} : {
|
|
1255
|
+
arrayFilters: InferArrayFilters<T, Spec> extends infer AF extends Dict<unknown> ? keyof AF extends never ? never : readonly Partial<{
|
|
1256
|
+
[Id in keyof AF]: ArrayFilterSpecForId<AF[Id], Id & string>;
|
|
1257
|
+
}[keyof AF]>[] : never;
|
|
1258
|
+
};
|
|
1259
|
+
/**
|
|
1260
|
+
* UpdateOptions - Options for update operations with type-safe arrayFilters
|
|
1261
|
+
*/
|
|
1262
|
+
type UpdateOptions<T, Spec extends StrictUpdateSpec<T>> = {
|
|
1263
|
+
upsert?: boolean;
|
|
1264
|
+
hint?: string | Dict<1 | -1>;
|
|
1265
|
+
} & RequiredArrayFilters<T, Spec>;
|
|
1266
|
+
|
|
1267
|
+
type CrudFilter<C> = ValidMatchFilterWithBuilder<C>;
|
|
1268
|
+
type SortSpec<C> = Partial<Record<string | (keyof C & string), 1 | -1>>;
|
|
1269
|
+
type ProjectionSpec<C> = Partial<Record<keyof C & string, 0 | 1>>;
|
|
1270
|
+
type FindOptions<C> = {
|
|
1271
|
+
projection?: ProjectionSpec<C>;
|
|
1272
|
+
sort?: SortSpec<C>;
|
|
1273
|
+
limit?: number;
|
|
1274
|
+
skip?: number;
|
|
1275
|
+
hint?: string | Document;
|
|
1276
|
+
collation?: CollationOptions;
|
|
1277
|
+
maxTimeMS?: number;
|
|
1278
|
+
comment?: string;
|
|
1279
|
+
};
|
|
1280
|
+
type FindBuilder<C> = {
|
|
1281
|
+
readonly _filter: CrudFilter<C> | undefined;
|
|
1282
|
+
readonly _options: FindOptions<C> | undefined;
|
|
1283
|
+
toList(): Promise<C[]>;
|
|
1284
|
+
toOne(): Promise<C | null>;
|
|
1285
|
+
};
|
|
1286
|
+
type InsertOneBuilder<C> = {
|
|
1287
|
+
readonly _doc: C;
|
|
1288
|
+
execute(): Promise<InsertOneResult>;
|
|
1289
|
+
};
|
|
1290
|
+
type InsertManyBuilder<C> = {
|
|
1291
|
+
readonly _docs: readonly C[];
|
|
1292
|
+
execute(): Promise<InsertManyResult>;
|
|
1293
|
+
};
|
|
1294
|
+
type UpdateOneBuilder<C extends Document, U extends StrictUpdateSpec<C> = StrictUpdateSpec<C>> = {
|
|
1295
|
+
readonly _filter: CrudFilter<C>;
|
|
1296
|
+
readonly _update: U | (($: ExprBuilder<C>) => U);
|
|
1297
|
+
readonly _options: UpdateOptions<C, U> | undefined;
|
|
1298
|
+
execute(): Promise<UpdateResult>;
|
|
1299
|
+
};
|
|
1300
|
+
type UpdateManyBuilder<C extends Document, U extends StrictUpdateSpec<C> = StrictUpdateSpec<C>> = {
|
|
1301
|
+
readonly _filter: CrudFilter<C>;
|
|
1302
|
+
readonly _update: U | (($: ExprBuilder<C>) => U);
|
|
1303
|
+
readonly _options: UpdateOptions<C, U> | undefined;
|
|
1304
|
+
execute(): Promise<UpdateResult>;
|
|
1305
|
+
};
|
|
1306
|
+
type ReplaceBuilder<C> = {
|
|
1307
|
+
readonly _filter: CrudFilter<C>;
|
|
1308
|
+
readonly _replacement: C;
|
|
1309
|
+
readonly _options: unknown;
|
|
1310
|
+
execute(): Promise<UpdateResult>;
|
|
1311
|
+
};
|
|
1312
|
+
type DeleteBuilder<C> = {
|
|
1313
|
+
readonly _filter: CrudFilter<C>;
|
|
1314
|
+
execute(): Promise<DeleteResult>;
|
|
1315
|
+
};
|
|
1316
|
+
type FindOneAndDeleteBuilder<C> = {
|
|
1317
|
+
readonly _filter: CrudFilter<C>;
|
|
1318
|
+
execute(): Promise<C | null>;
|
|
1319
|
+
};
|
|
1320
|
+
type FindOneAndReplaceBuilder<C> = {
|
|
1321
|
+
readonly _filter: CrudFilter<C>;
|
|
1322
|
+
readonly _replacement: C;
|
|
1323
|
+
execute(): Promise<C | null>;
|
|
1324
|
+
};
|
|
1325
|
+
type FindOneAndUpdateBuilder<C extends Document, U extends StrictUpdateSpec<C> = StrictUpdateSpec<C>> = {
|
|
1326
|
+
readonly _filter: CrudFilter<C>;
|
|
1327
|
+
readonly _update: U;
|
|
1328
|
+
execute(): Promise<C | null>;
|
|
1329
|
+
};
|
|
1330
|
+
type FindOneAndOptions<C> = {
|
|
1331
|
+
sort?: SortSpec<C>;
|
|
1332
|
+
projection?: ProjectionSpec<C>;
|
|
1333
|
+
upsert?: boolean;
|
|
1334
|
+
returnDocument?: "before" | "after";
|
|
1335
|
+
hint?: string | Document;
|
|
1336
|
+
collation?: CollationOptions;
|
|
1337
|
+
maxTimeMS?: number;
|
|
1338
|
+
comment?: string;
|
|
1339
|
+
};
|
|
1340
|
+
type CountOptions = {
|
|
1341
|
+
limit?: number;
|
|
1342
|
+
skip?: number;
|
|
1343
|
+
maxTimeMS?: number;
|
|
1344
|
+
hint?: string | Document;
|
|
1345
|
+
collation?: CollationOptions;
|
|
1346
|
+
comment?: string;
|
|
1347
|
+
};
|
|
1348
|
+
type CountBuilder = {
|
|
1349
|
+
execute(): Promise<number>;
|
|
1350
|
+
};
|
|
1351
|
+
type BulkWriteOp<C extends Document> = {
|
|
1352
|
+
insertOne: {
|
|
1353
|
+
document: C;
|
|
1354
|
+
};
|
|
1355
|
+
} | {
|
|
1356
|
+
updateOne: {
|
|
1357
|
+
filter: CrudFilter<C>;
|
|
1358
|
+
update: StrictUpdateSpec<C>;
|
|
1359
|
+
upsert?: boolean;
|
|
1360
|
+
arrayFilters?: Document[];
|
|
1361
|
+
hint?: string | Document;
|
|
1362
|
+
collation?: CollationOptions;
|
|
1363
|
+
};
|
|
1364
|
+
} | {
|
|
1365
|
+
updateMany: {
|
|
1366
|
+
filter: CrudFilter<C>;
|
|
1367
|
+
update: StrictUpdateSpec<C>;
|
|
1368
|
+
upsert?: boolean;
|
|
1369
|
+
arrayFilters?: Document[];
|
|
1370
|
+
hint?: string | Document;
|
|
1371
|
+
collation?: CollationOptions;
|
|
1372
|
+
};
|
|
1373
|
+
} | {
|
|
1374
|
+
deleteOne: {
|
|
1375
|
+
filter: CrudFilter<C>;
|
|
1376
|
+
hint?: string | Document;
|
|
1377
|
+
collation?: CollationOptions;
|
|
1378
|
+
};
|
|
1379
|
+
} | {
|
|
1380
|
+
deleteMany: {
|
|
1381
|
+
filter: CrudFilter<C>;
|
|
1382
|
+
hint?: string | Document;
|
|
1383
|
+
collation?: CollationOptions;
|
|
1384
|
+
};
|
|
1385
|
+
} | {
|
|
1386
|
+
replaceOne: {
|
|
1387
|
+
filter: CrudFilter<C>;
|
|
1388
|
+
replacement: C;
|
|
1389
|
+
upsert?: boolean;
|
|
1390
|
+
hint?: string | Document;
|
|
1391
|
+
collation?: CollationOptions;
|
|
1392
|
+
};
|
|
1393
|
+
};
|
|
1394
|
+
type BulkWriteBuilder<C extends Document> = {
|
|
1395
|
+
readonly _operations: readonly BulkWriteOp<C>[];
|
|
1396
|
+
execute(options?: {
|
|
1397
|
+
ordered?: boolean;
|
|
1398
|
+
}): Promise<BulkWriteResult>;
|
|
1399
|
+
};
|
|
1400
|
+
type DistinctBuilder<T> = {
|
|
1401
|
+
execute(): Promise<T[]>;
|
|
1402
|
+
};
|
|
1403
|
+
interface CrudCollection<C extends Document> {
|
|
1404
|
+
find: {
|
|
1405
|
+
(): FindBuilder<C>;
|
|
1406
|
+
<const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, options?: FindOptions<C>): FindBuilder<C>;
|
|
1407
|
+
};
|
|
1408
|
+
findOne: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter?: ($: ExprBuilder<SimplifyWritable<C>>) => R, options?: FindOptions<C>) => FindBuilder<C>;
|
|
1409
|
+
insertOne: (doc: C) => InsertOneBuilder<C>;
|
|
1410
|
+
insertMany: (docs: readonly C[]) => InsertManyBuilder<C>;
|
|
1411
|
+
updateOne: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>, const Update extends StrictUpdateSpec<C> | UpdatePipelineCallback<C>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, update: Update extends StrictUpdateSpec<C> ? Update & ValidateUpdateSpec<C, Update> : Update extends UpdatePipelineCallback<C> ? Update : never, ...options: NoInfer<Update extends StrictUpdateSpec<C> ? ExtractRequiredIdentifiers<Update> extends never ? [
|
|
1412
|
+
UpdateOptions<C, Update>?
|
|
1413
|
+
] : [UpdateOptions<C, Update>] : []>) => UpdateOneBuilder<C, Update extends StrictUpdateSpec<C> ? Update : StrictUpdateSpec<C>>;
|
|
1414
|
+
updateMany: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>, const Update extends StrictUpdateSpec<C> | UpdatePipelineCallback<C>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, update: Update extends StrictUpdateSpec<C> ? Update & ValidateUpdateSpec<C, Update> : Update extends UpdatePipelineCallback<C> ? Update : never, ...options: NoInfer<Update extends StrictUpdateSpec<C> ? ExtractRequiredIdentifiers<Update> extends never ? [
|
|
1415
|
+
UpdateOptions<C, Update>?
|
|
1416
|
+
] : [UpdateOptions<C, Update>] : []>) => UpdateManyBuilder<C, Update extends StrictUpdateSpec<C> ? Update : StrictUpdateSpec<C>>;
|
|
1417
|
+
replaceOne: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, replacement: C, options?: unknown) => ReplaceBuilder<C>;
|
|
1418
|
+
deleteOne: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R) => DeleteBuilder<C>;
|
|
1419
|
+
deleteMany: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R) => DeleteBuilder<C>;
|
|
1420
|
+
findOneAndDelete: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, options?: FindOneAndOptions<C>) => FindOneAndDeleteBuilder<C>;
|
|
1421
|
+
findOneAndReplace: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, replacement: C, options?: FindOneAndOptions<C>) => FindOneAndReplaceBuilder<C>;
|
|
1422
|
+
findOneAndUpdate: <const R extends NoInfer<ValidMatchFilterWithBuilder<C>>, const Update extends StrictUpdateSpec<C>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, update: Update & ValidateUpdateSpec<C, Update>, ...options: NoInfer<ExtractRequiredIdentifiers<Update> extends never ? [
|
|
1423
|
+
(UpdateOptions<C, Update> & FindOneAndOptions<C>)?
|
|
1424
|
+
] : [UpdateOptions<C, Update> & FindOneAndOptions<C>]>) => FindOneAndUpdateBuilder<C, Update>;
|
|
1425
|
+
countDocuments: {
|
|
1426
|
+
(): CountBuilder;
|
|
1427
|
+
<const R extends NoInfer<ValidMatchFilterWithBuilder<C>>>(filter: ($: ExprBuilder<SimplifyWritable<C>>) => R, options?: CountOptions): CountBuilder;
|
|
1428
|
+
};
|
|
1429
|
+
estimatedDocumentCount: () => CountBuilder;
|
|
1430
|
+
distinct: <K extends keyof C & string>(field: K, filter?: ($: ExprBuilder<SimplifyWritable<C>>) => ValidMatchFilterWithBuilder<C>) => DistinctBuilder<C[K]>;
|
|
1431
|
+
bulkWrite: (operations: readonly BulkWriteOp<C>[], options?: {
|
|
1432
|
+
ordered?: boolean;
|
|
1433
|
+
}) => BulkWriteBuilder<C>;
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
/**
|
|
1437
|
+
* A schema-like object from which a document TypeScript type can be extracted.
|
|
1438
|
+
*
|
|
1439
|
+
* Compatible with:
|
|
1440
|
+
* - **Zod**: `z.object({ name: z.string() })` → inferred via `_output`
|
|
1441
|
+
* - **Effect Schema**: `Schema.Struct({ name: Schema.String })` → inferred via `Type`
|
|
1442
|
+
* - **Plain type marker**: `{ Type: MyType }` for manual schemas
|
|
1443
|
+
*
|
|
1444
|
+
* @example
|
|
1445
|
+
* ```ts
|
|
1446
|
+
* import { z } from "zod";
|
|
1447
|
+
* const UserSchema = z.object({ name: z.string(), age: z.number() });
|
|
1448
|
+
* const reg = registry("8.0", { users: UserSchema });
|
|
1449
|
+
* ```
|
|
1450
|
+
*/
|
|
1451
|
+
type SchemaLike = {
|
|
1452
|
+
readonly Type: Dict<unknown>;
|
|
1453
|
+
} | {
|
|
1454
|
+
readonly _output: Dict<unknown>;
|
|
1455
|
+
} | {
|
|
1456
|
+
readonly [schemaTypeKey]: Dict<unknown>;
|
|
1457
|
+
};
|
|
1458
|
+
declare const schemaTypeKey: unique symbol;
|
|
1459
|
+
/** Extract the inferred document type from a SchemaLike */
|
|
1460
|
+
type InferSchema<S extends SchemaLike> = S extends {
|
|
1461
|
+
readonly Type: infer T extends Dict<unknown>;
|
|
1462
|
+
} ? T : S extends {
|
|
1463
|
+
readonly _output: infer T extends Dict<unknown>;
|
|
1464
|
+
} ? T : S extends {
|
|
1465
|
+
readonly [schemaTypeKey]: infer T extends Dict<unknown>;
|
|
1466
|
+
} ? T : Dict<unknown>;
|
|
1467
|
+
type MongoVersions = ["4.2", "4.4", "5.0", "5.1", "6.0", "6.1", "7.0", "7.1", "8.0"];
|
|
1468
|
+
/**
|
|
1469
|
+
* Collection type that carries both name and schema information
|
|
1470
|
+
* Used for type-safe collection references in $lookup, $unionWith, etc.
|
|
1471
|
+
*/
|
|
1472
|
+
type Collection<TName extends string = string, TSchema extends Dict<unknown> = Dict<unknown>> = {
|
|
1473
|
+
readonly __collectionName: TName;
|
|
1474
|
+
readonly __collectionType: SimplifyWritable<TSchema>;
|
|
1475
|
+
};
|
|
1476
|
+
type CollectionType<T extends Collection> = T["__collectionType"];
|
|
1477
|
+
type BoundCollection<TName extends string, TSchema extends Dict<unknown>> = Collection<TName, TSchema> & {
|
|
1478
|
+
aggregate: AggregateBuilder<SimplifyWritable<TSchema>>;
|
|
1479
|
+
} & CrudCollection<SimplifyWritable<TSchema>>;
|
|
1480
|
+
/**
|
|
1481
|
+
* Creates a bound collection with type-safe aggregate and CRUD methods.
|
|
1482
|
+
*
|
|
1483
|
+
* Wraps a MongoDB native collection with schema-aware operations.
|
|
1484
|
+
* Typically called indirectly via {@link registry} rather than directly.
|
|
1485
|
+
*
|
|
1486
|
+
* @example
|
|
1487
|
+
* ```ts
|
|
1488
|
+
* const users = collection("users", userSchema, db.collection("users"));
|
|
1489
|
+
* const result = await users.aggregate($match($ => ({ age: $.gte(18) }))).toList();
|
|
1490
|
+
* ```
|
|
1491
|
+
*/
|
|
1492
|
+
declare const collection: <TName extends string, TSchema extends SchemaLike>(name: TName, _schema: TSchema, mongoCol: Collection$1<SimplifyWritable<InferSchema<TSchema>>>) => BoundCollection<TName, InferSchema<TSchema>>;
|
|
1493
|
+
/**
|
|
1494
|
+
* Creates a type-safe registry of MongoDB collections from schema definitions.
|
|
1495
|
+
*
|
|
1496
|
+
* Returns a factory that binds schemas to a `Db` instance, producing
|
|
1497
|
+
* collections with fully-typed aggregation pipelines and CRUD operations.
|
|
1498
|
+
*
|
|
1499
|
+
* @example
|
|
1500
|
+
* ```ts
|
|
1501
|
+
* const db = registry("8.0", {
|
|
1502
|
+
* users: userSchema,
|
|
1503
|
+
* orders: orderSchema,
|
|
1504
|
+
* })(client.db("mydb"));
|
|
1505
|
+
*
|
|
1506
|
+
* const result = await db.users.aggregate(
|
|
1507
|
+
* $match($ => ({ active: true })),
|
|
1508
|
+
* $project($ => ({ name: 1 })),
|
|
1509
|
+
* ).toList();
|
|
1510
|
+
* ```
|
|
1511
|
+
*/
|
|
1512
|
+
declare const registry: <const TMap extends Dict<SchemaLike>, const TVersion extends MongoVersions[number]>(version: TVersion, // For later on when we have to deal with version-specific features
|
|
1513
|
+
schemas: { [K in keyof TMap]: TMap[K]; }) => (client: Db) => { [K in keyof TMap]: BoundCollection<K & string, InferSchema<TMap[K]>>; };
|
|
1514
|
+
|
|
1515
|
+
type ResolvePathValue<C, P extends string> = P extends PathType<C> ? PathValueArrayProjection<C, P> extends never ? ResolveValue<C, P> : PathValueArrayProjection<C, P> : OpaqueError<`Invalid path: ${P}`>;
|
|
1516
|
+
type Geometry = {
|
|
1517
|
+
type: "Point";
|
|
1518
|
+
coordinates: [number, number];
|
|
1519
|
+
} | {
|
|
1520
|
+
type: "LineString";
|
|
1521
|
+
coordinates: [number, number][];
|
|
1522
|
+
} | {
|
|
1523
|
+
type: "Polygon";
|
|
1524
|
+
coordinates: [number, number][][];
|
|
1525
|
+
} | {
|
|
1526
|
+
type: "MultiPoint";
|
|
1527
|
+
coordinates: [number, number][];
|
|
1528
|
+
} | {
|
|
1529
|
+
type: "MultiLineString";
|
|
1530
|
+
coordinates: [number, number][][];
|
|
1531
|
+
} | {
|
|
1532
|
+
type: "MultiPolygon";
|
|
1533
|
+
coordinates: [number, number][][][];
|
|
1534
|
+
} | {
|
|
1535
|
+
type: "GeometryCollection";
|
|
1536
|
+
geometries: Geometry[];
|
|
1537
|
+
};
|
|
1538
|
+
|
|
1539
|
+
type ForeignType<T extends Collection> = T["__collectionType"];
|
|
1540
|
+
|
|
1541
|
+
type NumericArrayPath<T> = FilteredPath<T, readonly number[] | number[] | null | undefined>;
|
|
1542
|
+
type NumericArrayFieldRef<T> = `$${NumericArrayPath<T>}`;
|
|
1543
|
+
type AnyPathFieldRef$1<T> = `$${PathType<T>}`;
|
|
1544
|
+
type SmartFieldRef<C, StrictRef> = [
|
|
1545
|
+
StrictRef
|
|
1546
|
+
] extends [never] ? OpaqueError<"Invalid field reference"> : StrictRef;
|
|
1547
|
+
type SmartNumericFieldRef<C> = SmartFieldRef<C, NumericFieldRef<C> | NumericArrayFieldRef<C>>;
|
|
1548
|
+
type SmartAnyPathFieldRef<C> = SmartFieldRef<C, AnyPathFieldRef$1<C>>;
|
|
1549
|
+
type ArrayIn<C> = SmartFieldRef<C, ArrayFieldRef<C>> | readonly unknown[];
|
|
1550
|
+
type NumericIn<C> = SmartFieldRef<C, NumericFieldRef<C>> | (number & {});
|
|
1551
|
+
type BooleanIn<C> = SmartFieldRef<C, BooleanFieldRef<C>> | (boolean & {});
|
|
1552
|
+
type StringIn<C> = SmartFieldRef<C, StringFieldRef<C>> | (string & {});
|
|
1553
|
+
type DateIn<C> = SmartFieldRef<C, DateFieldRef<C>> | (Date & {});
|
|
1554
|
+
type ExpressionIn<C> = ArrayIn<C> | NumericIn<C> | BooleanIn<C> | StringIn<C> | DateIn<C>;
|
|
1555
|
+
type ValidExpressionValue<C, T> = T extends `$${infer _Path}` ? T extends SmartAnyPathFieldRef<C> ? T : OpaqueError<`Invalid field path: ${T & string}`> : T;
|
|
1556
|
+
type ElemType<T> = T extends readonly (infer E)[] ? E : unknown;
|
|
1557
|
+
type ExtractThis<C> = C extends {
|
|
1558
|
+
$this: infer ThisType;
|
|
1559
|
+
} ? ThisType : never;
|
|
1560
|
+
type ExtractValue<C> = C extends {
|
|
1561
|
+
$value: infer ValueType;
|
|
1562
|
+
} ? ValueType : never;
|
|
1563
|
+
type ExtractVars<C> = C extends {
|
|
1564
|
+
$vars: infer Vars;
|
|
1565
|
+
} ? Vars : never;
|
|
1566
|
+
type ResolveType<C, T> = T extends "$$this" ? ExtractThis<C> : T extends `$$this.${infer P}` ? ResolvePathValue<ExtractThis<C>, P> : T extends "$$value" ? ExtractValue<C> : T extends `$$value.${infer P}` ? ResolvePathValue<ExtractValue<C>, P> : T extends "$$ROOT" | "$$CURRENT" ? C : T extends `$$${"DESCEND" | "PRUNE" | "KEEP" | "NOW" | "CLUSTER_TIME"}` ? T : T extends `$$${infer Var}.${infer P}` ? Var extends "ROOT" | "CURRENT" ? ResolvePathValue<C, P> : `$${Var}` extends keyof C ? ResolvePathValue<C[`$${Var}`], P> : `$$${Var}` extends keyof C ? ResolvePathValue<C[`$$${Var}`], P> : ExtractVars<C> extends infer Vars ? Var extends keyof Vars ? ResolvePathValue<Vars[Var & keyof Vars], P> : unknown : unknown : T extends `$$${infer Var}` ? `$${Var}` extends keyof C ? C[`$${Var}`] : T extends keyof C ? C[T] : ExtractVars<C> extends infer Vars ? Var extends keyof Vars ? Vars[Var & keyof Vars] : ExtractThis<C> : ExtractThis<C> : T extends `$${infer P}` ? ResolvePathValue<C, P> : T extends Ret<any, infer R> ? R : T extends object ? ResolveObject<C, T> : T;
|
|
1567
|
+
type ResolveObjectValue<C, V> = V extends Ret<any, infer R> ? R : V extends `$${string}` ? ResolveType<C, V> : V extends object ? ResolveObject<C, V> : V;
|
|
1568
|
+
type ResolveObject<C, Obj> = {
|
|
1569
|
+
[K in keyof Obj]: ResolveObjectValue<C, Obj[K]>;
|
|
1570
|
+
};
|
|
1571
|
+
type ResolveSpecValue<C, V> = V extends Ret<any, infer R> ? R : V extends (c: any) => infer R ? R : V extends `$${string}` ? ResolveType<C, V> : V extends {
|
|
1572
|
+
[key: `$${string}`]: any;
|
|
1573
|
+
} ? OpaqueError<"Invalid MongoDB operator object - use builder API instead (e.g., $.sum(...))"> & never : V extends object ? ResolveObject<C, V> : V;
|
|
1574
|
+
type ResolveSpec<C, Spec> = {
|
|
1575
|
+
[K in keyof Spec]: ResolveSpecValue<C, Spec[K]>;
|
|
1576
|
+
};
|
|
1577
|
+
type DeepSpecInput<C, Spec> = Spec extends Ret<any, any> ? Spec : Spec extends ExprArg<C> ? ExprArgInput<C, Spec> : Spec extends readonly (infer E)[] ? readonly DeepSpecInput<C, E>[] : Spec extends object ? {
|
|
1578
|
+
[K in keyof Spec]: DeepSpecInput<C, Spec[K]>;
|
|
1579
|
+
} : Spec;
|
|
1580
|
+
type DeepSpecResolve<C, Spec> = Spec extends Ret<any, infer R> ? R : Spec extends ExprArg<C> ? ResolveType<C, Spec> : Spec extends readonly (infer E)[] ? DeepSpecResolve<C, E>[] : Spec extends object ? {
|
|
1581
|
+
[K in keyof Spec]: DeepSpecResolve<C, Spec[K]>;
|
|
1582
|
+
} : Spec;
|
|
1583
|
+
type SystemVarName = "ROOT" | "CURRENT" | "DESCEND" | "PRUNE" | "KEEP" | "NOW" | "CLUSTER_TIME";
|
|
1584
|
+
type ThisValueVarName = "this" | "value";
|
|
1585
|
+
type VarsRecord<C> = C extends {
|
|
1586
|
+
$vars: infer Vars extends Dict<unknown>;
|
|
1587
|
+
} ? Vars : Dict<never>;
|
|
1588
|
+
type VarsFromContext<C> = Extract<keyof VarsRecord<C>, string>;
|
|
1589
|
+
type VarName<C> = SystemVarName | ThisValueVarName | VarsFromContext<C>;
|
|
1590
|
+
type VarRef<C> = VarName<C> extends infer V extends string ? `$$${V}` : never;
|
|
1591
|
+
type VarPathRef<C> = (C extends {
|
|
1592
|
+
$this: infer ThisType;
|
|
1593
|
+
} ? `$$this.${PathType<ThisType>}` : never) | (C extends {
|
|
1594
|
+
$value: infer ValueType;
|
|
1595
|
+
} ? `$$value.${PathType<ValueType>}` : never) | {
|
|
1596
|
+
[K in VarsFromContext<C>]: `$$${K}.${PathType<VarsRecord<C>[K]>}`;
|
|
1597
|
+
}[VarsFromContext<C>] | (C extends unknown ? `$$ROOT.${PathType<C>}` | `$$CURRENT.${PathType<C>}` : never);
|
|
1598
|
+
type KnownVar<C, V extends string> = V extends SystemVarName ? true : V extends ThisValueVarName ? true : `$${V}` extends keyof C ? true : `$$${V}` extends keyof C ? true : C extends {
|
|
1599
|
+
$vars: infer Vars;
|
|
1600
|
+
} ? V extends keyof Vars ? true : false : false;
|
|
1601
|
+
type NumericArg<C> = NumericIn<C> | SmartNumericFieldRef<C> | Ret<C, number> | Ret<C, number | null> | Ret<C, number[]> | VarRef<C> | VarPathRef<C>;
|
|
1602
|
+
type BooleanArg<C> = BooleanIn<C> | Ret<C, boolean> | VarRef<C> | VarPathRef<C>;
|
|
1603
|
+
type StringArg<C> = SmartFieldRef<C, StringFieldRef<C>> | Ret<C, string> | VarRef<C> | VarPathRef<C> | (string & {});
|
|
1604
|
+
type DateArg<C> = DateIn<C> | Ret<C, Date> | VarRef<C> | VarPathRef<C>;
|
|
1605
|
+
type ArrayLiteral$1<C> = readonly ExprArg<C>[] | ExprArg<C>[];
|
|
1606
|
+
type ArrayProjectionFieldRef<T> = `$${ArrayProjectionPath<T>}`;
|
|
1607
|
+
type ArrayFieldRefLike<T> = ArrayFieldRef<T> | ArrayProjectionFieldRef<T>;
|
|
1608
|
+
type ArrayArg<C> = SmartFieldRef<C, ArrayFieldRefLike<C>> | Ret<C, unknown[]> | VarRef<C> | VarPathRef<C> | ArrayLiteral$1<C>;
|
|
1609
|
+
type ExprArg<C> = SmartAnyPathFieldRef<C> | Ret<C> | VarRef<C> | VarPathRef<C> | number | boolean | Date | (string & {}) | null;
|
|
1610
|
+
type AccumulatorExprArg<C> = SmartAnyPathFieldRef<C> | Ret<C> | VarRef<C> | VarPathRef<C> | number | boolean | Date | null;
|
|
1611
|
+
type ValidInput<C, T, FieldRefType, TypeName extends string> = T extends VarRef<C> | VarPathRef<C> ? T : T extends Ret<any, any> ? T : T extends `$${string}` ? T extends SmartFieldRef<C, FieldRefType> ? T : TypeError<`"${T}" is not a valid ${TypeName} field`, `${TypeName} | ${TypeName} field ref`, T> : T;
|
|
1612
|
+
type ValidStringInput<C, T> = ValidInput<C, T, StringFieldRef<C>, "string">;
|
|
1613
|
+
type ValidNumericInput<C, T> = T extends number ? T : ValidInput<C, T, SmartNumericFieldRef<C>, "numeric">;
|
|
1614
|
+
type ValidBooleanInput<C, T> = T extends boolean ? T : ValidInput<C, T, BooleanFieldRef<C>, "boolean">;
|
|
1615
|
+
type ValidDateInput<C, T> = T extends Date ? T : ValidInput<C, T, DateFieldRef<C>, "date">;
|
|
1616
|
+
type ValidArrayInput<C, T> = T extends readonly unknown[] ? T : ValidInput<C, T, ArrayFieldRefLike<C>, "array">;
|
|
1617
|
+
type ValidExprArg<C, T> = T extends `$$${infer Var}.${string}` ? KnownVar<C, Var> extends true ? T : OpaqueError<`Invalid variable: $$${Var}`> : T extends VarRef<C> | VarPathRef<C> ? T : T extends `$${string}` ? ValidExpressionValue<C, T> : T;
|
|
1618
|
+
type ValidAccumulatorArg<C, T> = T extends `$$${infer Var}.${string}` ? KnownVar<C, Var> extends true ? T : OpaqueError<`Invalid variable: $$${Var}`> : T extends VarRef<C> | VarPathRef<C> ? T : T extends `$${string}` ? ValidExpressionValue<C, T> : T extends string ? OpaqueError<`Bare string "${T}" is not valid in accumulator position — did you mean "$${T}"?`> : T;
|
|
1619
|
+
type StringArgInput<C, T extends StringArg<C>> = ValidStringInput<C, T>;
|
|
1620
|
+
type NumericArgInput<C, T extends NumericArg<C>> = ValidNumericInput<C, T>;
|
|
1621
|
+
type BooleanArgInput<C, T extends BooleanArg<C>> = ValidBooleanInput<C, T>;
|
|
1622
|
+
type DateArgInput<C, T extends DateArg<C>> = ValidDateInput<C, T>;
|
|
1623
|
+
type ArrayArgInput<C, T extends ArrayArg<C>> = ValidArrayInput<C, T>;
|
|
1624
|
+
type ExprArgInput<C, T extends ExprArg<C>> = ValidExprArg<C, T>;
|
|
1625
|
+
type ExprArgInputs<C, T extends readonly ExprArg<C>[]> = {
|
|
1626
|
+
[K in keyof T]: ExprArgInput<C, T[K]>;
|
|
1627
|
+
};
|
|
1628
|
+
type AccumulatorArgInput<C, T extends AccumulatorExprArg<C>> = ValidAccumulatorArg<C, T>;
|
|
1629
|
+
type AccumulatorArgInputs<C, T extends readonly AccumulatorExprArg<C>[]> = {
|
|
1630
|
+
[K in keyof T]: AccumulatorArgInput<C, T[K]>;
|
|
1631
|
+
};
|
|
1632
|
+
type ComparisonOps<T> = {
|
|
1633
|
+
$eq?: T;
|
|
1634
|
+
$ne?: T;
|
|
1635
|
+
$exists?: boolean;
|
|
1636
|
+
$type?: BSONTypeAlias;
|
|
1637
|
+
};
|
|
1638
|
+
/** Adds $not wrapper to any match operator base type */
|
|
1639
|
+
type WithNot<T> = T & {
|
|
1640
|
+
$not?: T;
|
|
1641
|
+
};
|
|
1642
|
+
type MatchNumericIn = number & {};
|
|
1643
|
+
type MatchStringIn = string & {};
|
|
1644
|
+
type MatchDateIn = Date & {};
|
|
1645
|
+
type MatchBooleanIn = boolean & {};
|
|
1646
|
+
type NumericMatchOpsBase = ComparisonOps<MatchNumericIn> & {
|
|
1647
|
+
$gt?: MatchNumericIn;
|
|
1648
|
+
$gte?: MatchNumericIn;
|
|
1649
|
+
$lt?: MatchNumericIn;
|
|
1650
|
+
$lte?: MatchNumericIn;
|
|
1651
|
+
$in?: MatchNumericIn[];
|
|
1652
|
+
$nin?: MatchNumericIn[];
|
|
1653
|
+
$mod?: [number, number];
|
|
1654
|
+
$bitsAllClear?: number | number[];
|
|
1655
|
+
$bitsAllSet?: number | number[];
|
|
1656
|
+
$bitsAnyClear?: number | number[];
|
|
1657
|
+
$bitsAnySet?: number | number[];
|
|
1658
|
+
};
|
|
1659
|
+
type NumericMatchOperators = WithNot<NumericMatchOpsBase>;
|
|
1660
|
+
type StringRegexOps = {
|
|
1661
|
+
$regex: string | RegExp;
|
|
1662
|
+
$options?: string;
|
|
1663
|
+
} | {
|
|
1664
|
+
$regex?: undefined;
|
|
1665
|
+
$options?: undefined;
|
|
1666
|
+
};
|
|
1667
|
+
type StringMatchOpsBase = ComparisonOps<MatchStringIn> & {
|
|
1668
|
+
$gt?: MatchStringIn;
|
|
1669
|
+
$gte?: MatchStringIn;
|
|
1670
|
+
$lt?: MatchStringIn;
|
|
1671
|
+
$lte?: MatchStringIn;
|
|
1672
|
+
$in?: MatchStringIn[];
|
|
1673
|
+
$nin?: MatchStringIn[];
|
|
1674
|
+
} & StringRegexOps;
|
|
1675
|
+
type StringMatchOperators = WithNot<StringMatchOpsBase>;
|
|
1676
|
+
type BooleanMatchOpsBase = ComparisonOps<MatchBooleanIn>;
|
|
1677
|
+
type BooleanMatchOperators = WithNot<BooleanMatchOpsBase>;
|
|
1678
|
+
type ElemMatchValue<E> = {
|
|
1679
|
+
$elemMatch: ValidMatchFilterWithBuilder<E>;
|
|
1680
|
+
};
|
|
1681
|
+
type ArrayMatchOpsBase<E> = {
|
|
1682
|
+
$size?: number;
|
|
1683
|
+
$all?: readonly E[];
|
|
1684
|
+
$elemMatch?: ElemMatchValue<E>;
|
|
1685
|
+
$in?: readonly E[];
|
|
1686
|
+
$nin?: readonly E[];
|
|
1687
|
+
$exists?: boolean;
|
|
1688
|
+
$type?: BSONTypeAlias;
|
|
1689
|
+
};
|
|
1690
|
+
type ArrayMatchOperators<E> = WithNot<ArrayMatchOpsBase<E>>;
|
|
1691
|
+
type GeoMatchOpsBase<C> = {
|
|
1692
|
+
$geoIntersects?: {
|
|
1693
|
+
$geometry: Geometry;
|
|
1694
|
+
};
|
|
1695
|
+
$geoWithin?: {
|
|
1696
|
+
$geometry?: Geometry;
|
|
1697
|
+
$box?: [[number, number], [number, number]];
|
|
1698
|
+
$polygon?: [number, number][];
|
|
1699
|
+
$center?: [[number, number], number];
|
|
1700
|
+
$centerSphere?: [[number, number], number];
|
|
1701
|
+
};
|
|
1702
|
+
$near?: {
|
|
1703
|
+
$geometry: Geometry;
|
|
1704
|
+
$maxDistance?: number;
|
|
1705
|
+
$minDistance?: number;
|
|
1706
|
+
};
|
|
1707
|
+
$nearSphere?: {
|
|
1708
|
+
$geometry: Geometry;
|
|
1709
|
+
$maxDistance?: number;
|
|
1710
|
+
$minDistance?: number;
|
|
1711
|
+
};
|
|
1712
|
+
};
|
|
1713
|
+
type GeoMatchOperators<C> = WithNot<GeoMatchOpsBase<C>>;
|
|
1714
|
+
type DateMatchOpsBase = ComparisonOps<MatchDateIn> & {
|
|
1715
|
+
$gt?: MatchDateIn;
|
|
1716
|
+
$gte?: MatchDateIn;
|
|
1717
|
+
$lt?: MatchDateIn;
|
|
1718
|
+
$lte?: MatchDateIn;
|
|
1719
|
+
};
|
|
1720
|
+
type DateMatchOperators = WithNot<DateMatchOpsBase>;
|
|
1721
|
+
type ValidMatchFilterWithBuilder<C> = {
|
|
1722
|
+
[K in keyof C]?: C[K] | Ret<C, C[K]> | (C[K] extends number ? NumericMatchOperators : C[K] extends string ? StringMatchOperators : C[K] extends boolean ? BooleanMatchOperators : C[K] extends Date ? DateMatchOperators : C[K] extends readonly (infer E)[] ? E | Ret<C, E> | ArrayMatchOperators<E> : C[K] extends Geometry ? GeoMatchOperators<C> : ComparisonOps<never>);
|
|
1723
|
+
} & {
|
|
1724
|
+
$expr?: BooleanArg<C>;
|
|
1725
|
+
$or?: ValidMatchFilterWithBuilder<C>[];
|
|
1726
|
+
$and?: ValidMatchFilterWithBuilder<C>[];
|
|
1727
|
+
$nor?: ValidMatchFilterWithBuilder<C>[];
|
|
1728
|
+
$text?: {
|
|
1729
|
+
$search: string;
|
|
1730
|
+
$language?: string;
|
|
1731
|
+
$caseSensitive?: boolean;
|
|
1732
|
+
$diacriticSensitive?: boolean;
|
|
1733
|
+
};
|
|
1734
|
+
/** @deprecated $where is a MongoDB escape hatch and bypasses type safety. */
|
|
1735
|
+
$where?: string | ((...args: any[]) => boolean);
|
|
1736
|
+
$jsonSchema?: JSONSchema;
|
|
1737
|
+
$comment?: string;
|
|
1738
|
+
};
|
|
1739
|
+
type TimeUnit = "year" | "quarter" | "month" | "week" | "day" | "hour" | "minute" | "second" | "millisecond";
|
|
1740
|
+
type WindowBoundary = number | "current" | "unbounded";
|
|
1741
|
+
type WindowSpec = {
|
|
1742
|
+
documents: [WindowBoundary, WindowBoundary];
|
|
1743
|
+
} | {
|
|
1744
|
+
range: [WindowBoundary, WindowBoundary];
|
|
1745
|
+
unit: TimeUnit;
|
|
1746
|
+
};
|
|
1747
|
+
|
|
1748
|
+
type BuilderMode = "expression" | "accumulator";
|
|
1749
|
+
type ResolveTypes<C, T extends unknown[]> = {
|
|
1750
|
+
[K in keyof T]: ResolveType<C, T[K]>;
|
|
1751
|
+
};
|
|
1752
|
+
type DeepObjectExpr<C> = ExprArg<C> | {
|
|
1753
|
+
[K in string]: DeepObjectExpr<C>;
|
|
1754
|
+
} | readonly DeepObjectExpr<C>[];
|
|
1755
|
+
type DeepObjectInput<C, T> = T extends ExprArg<C> ? ExprArgInput<C, T> : T extends readonly (infer E)[] ? readonly DeepObjectInput<C, E>[] : T extends object ? {
|
|
1756
|
+
[K in keyof T]: DeepObjectInput<C, T[K]>;
|
|
1757
|
+
} : T;
|
|
1758
|
+
type DeepObjectResolve<C, T> = T extends Ret<any, infer R> ? R : T extends ExprArg<C> ? ResolveType<C, T> : T extends readonly (infer E)[] ? DeepObjectResolve<C, E>[] : T extends object ? {
|
|
1759
|
+
[K in keyof T]: DeepObjectResolve<C, T[K]>;
|
|
1760
|
+
} : T;
|
|
1761
|
+
type ArrayToObjectResult<Elem> = Elem extends {
|
|
1762
|
+
k: infer K;
|
|
1763
|
+
v: infer V;
|
|
1764
|
+
} ? [
|
|
1765
|
+
K
|
|
1766
|
+
] extends [string] ? Record<K, V> : Dict<unknown> : Dict<unknown>;
|
|
1767
|
+
type UnaryNumericOp<C> = <const T extends NumericArg<C>>(arg: T & NumericArgInput<C, T>) => Ret<C, number>;
|
|
1768
|
+
type BinaryNumericOp<C> = <const L extends NumericArg<C>, const R extends NumericArg<C>>(left: L & NumericArgInput<C, L>, right: R & NumericArgInput<C, R>) => Ret<C, number>;
|
|
1769
|
+
type UnaryStringToNumber<C> = <const T extends StringArg<C>>(exp: StringArgInput<C, T>) => Ret<C, number>;
|
|
1770
|
+
type UnaryStringToString<C> = <const T extends StringArg<C>>(exp: StringArgInput<C, T>) => Ret<C, string>;
|
|
1771
|
+
type BinaryStringToNumber<C> = <const L extends StringArg<C>, const R extends StringArg<C>>(left: StringArgInput<C, L>, right: StringArgInput<C, R>) => Ret<C, -1 | 0 | 1>;
|
|
1772
|
+
type UnaryDateToNumber<C> = <const T extends DateArg<C>>(exp: T & DateArgInput<C, T>) => Ret<C, number>;
|
|
1773
|
+
type UnaryArrayToBool<C> = <const T extends ArrayArg<C>>(exp: T & ArrayArgInput<C, T>) => Ret<C, boolean>;
|
|
1774
|
+
type UnaryTypeConversion<C, Output> = <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, NullishResult<C, T, Output>>;
|
|
1775
|
+
type HasNullish<T> = [Extract<T, null | undefined>] extends [never] ? false : true;
|
|
1776
|
+
type NullishResult<C, T, Output> = HasNullish<ResolveType<C, T>> extends true ? Output | null : Output;
|
|
1777
|
+
type NullishResultFromArgs<C, T extends readonly ExprArg<C>[], Output> = HasNullish<ResolveType<C, T[number]>> extends true ? Output | null : Output;
|
|
1778
|
+
type ConvertTo = "double" | "string" | "objectId" | "bool" | "date" | "int" | "long" | "decimal";
|
|
1779
|
+
type ConvertOutput<To extends ConvertTo> = To extends "double" | "int" ? number : To extends "long" ? number : To extends "decimal" ? Decimal128 : To extends "string" ? string : To extends "bool" ? boolean : To extends "objectId" ? ObjectId : Date;
|
|
1780
|
+
type BinaryArrayOp<C> = <const A extends ArrayArg<C>, const B extends ArrayArg<C>>(left: A & ArrayArgInput<C, A>, right: B & ArrayArgInput<C, B>) => Ret<C, boolean>;
|
|
1781
|
+
type UnaryArrayToNumber<C> = <const T extends ArrayArg<C>>(exp: T & ArrayArgInput<C, T>) => Ret<C, number>;
|
|
1782
|
+
type ComparisonOp<C> = <const L extends ExprArg<C>, const R extends CompatibleArg<C, ResolveType<C, L>>>(left: L extends `$${infer Rest}` ? Rest extends `$${string}` ? L : L extends AnyPathFieldRef<C> ? L : never : L, right: R extends `$${infer Rest}` ? Rest extends `$${string}` ? R : ResolveType<C, R> extends ResolveType<C, L> ? R : never : R) => Ret<C, boolean>;
|
|
1783
|
+
type VarargsBooleanOp<C> = <const T extends BooleanArg<C>[]>(...args: {
|
|
1784
|
+
[K in keyof T]: T[K] & BooleanArgInput<C, T[K]>;
|
|
1785
|
+
}) => Ret<C, boolean>;
|
|
1786
|
+
type VarargsNumericOp<C> = <const T extends NumericArg<C>[]>(...args: T extends readonly NumericArg<C>[] ? T : never) => Ret<C, number>;
|
|
1787
|
+
type NumericArgs<C> = readonly [NumericArg<C>, NumericArg<C>, ...NumericArg<C>[]];
|
|
1788
|
+
type NumericArgsInput<C, T extends readonly NumericArg<C>[]> = {
|
|
1789
|
+
[K in keyof T]: NumericArgInput<C, T[K]>;
|
|
1790
|
+
};
|
|
1791
|
+
type AddOp<C> = {
|
|
1792
|
+
<const T extends NumericArgs<C>>(...args: NumericArgsInput<C, T>): Ret<C, number>;
|
|
1793
|
+
<const D extends DateArg<C>, const T extends readonly [NumericArg<C>, ...NumericArg<C>[]]>(date: DateArgInput<C, D>, ...args: NumericArgsInput<C, T>): Ret<C, Date>;
|
|
1794
|
+
<const T extends readonly [NumericArg<C>, ...NumericArg<C>[]], const D extends DateArg<C>>(...args: [...NumericArgsInput<C, T>, DateArgInput<C, D>]): Ret<C, Date>;
|
|
1795
|
+
};
|
|
1796
|
+
type SubtractOp<C> = {
|
|
1797
|
+
<const L extends DateArg<C>, const R extends DateArg<C>>(left: DateArgInput<C, L>, right: DateArgInput<C, R>): Ret<C, number>;
|
|
1798
|
+
<const L extends DateArg<C>, const R extends NumericArg<C>>(left: DateArgInput<C, L>, right: NumericArgInput<C, R>): Ret<C, Date>;
|
|
1799
|
+
<const L extends NumericArg<C>, const R extends NumericArg<C>>(left: NumericArgInput<C, L>, right: NumericArgInput<C, R>): Ret<C, number>;
|
|
1800
|
+
};
|
|
1801
|
+
type SortPath<C> = PathType<C> | `_id.${string}`;
|
|
1802
|
+
type SortBySpec<C> = Partial<Record<SortPath<C>, 1 | -1>> | {
|
|
1803
|
+
$meta: "textScore" | "searchScore";
|
|
1804
|
+
} | (Partial<Record<SortPath<C>, 1 | -1>> & {
|
|
1805
|
+
$meta: "textScore" | "searchScore";
|
|
1806
|
+
});
|
|
1807
|
+
type ArrayLiteral<C> = readonly ExprArg<C>[] | ExprArg<C>[];
|
|
1808
|
+
type FirstNotNil<T extends unknown[]> = T extends [infer First, ...infer Rest] ? First extends null | undefined ? FirstNotNil<Rest> : First : never;
|
|
1809
|
+
type MergeTwo<A, B> = [
|
|
1810
|
+
A
|
|
1811
|
+
] extends [null | undefined] ? B : [B] extends [null | undefined] ? A : MergeWithIndexOverride<A, B>;
|
|
1812
|
+
type MergeObjects<C, Objs extends unknown[], Acc = null> = Objs extends [infer First, ...infer Rest] ? MergeObjects<C, Rest, MergeTwo<ResolveType<C, First>, Acc>> : Acc;
|
|
1813
|
+
type MergeVars<C, Vars extends Dict<unknown>> = C extends {
|
|
1814
|
+
$vars: infer Existing extends Dict<unknown>;
|
|
1815
|
+
} ? SimplifyWritable<Omit<C, "$vars"> & {
|
|
1816
|
+
$vars: SimplifyWritable<Existing & Vars>;
|
|
1817
|
+
}> : SimplifyWritable<C & {
|
|
1818
|
+
$vars: Vars;
|
|
1819
|
+
}>;
|
|
1820
|
+
type ResolveVarSpec<C, V extends Dict<ExprArg<C>>> = {
|
|
1821
|
+
[K in keyof V]: ResolveType<C, V[K]>;
|
|
1822
|
+
};
|
|
1823
|
+
type ContextWithThis<C, ThisType, As extends string | undefined = undefined> = As extends string ? MergeVars<SimplifyWritable<C & {
|
|
1824
|
+
$this: ThisType;
|
|
1825
|
+
}>, Record<As, ThisType>> : SimplifyWritable<C & {
|
|
1826
|
+
$this: ThisType;
|
|
1827
|
+
}>;
|
|
1828
|
+
type ContextWithThisAndValue<C, ThisType, ValueType> = SimplifyWritable<C & {
|
|
1829
|
+
$this: ThisType;
|
|
1830
|
+
$value: ValueType;
|
|
1831
|
+
}>;
|
|
1832
|
+
type ConcatStrings<T extends readonly unknown[]> = T extends readonly [infer First, ...infer Rest] ? First extends string ? Rest extends readonly string[] ? `${First}${ConcatStrings<Rest>}` : string : string : "";
|
|
1833
|
+
type CompatibleArg<C, T> = [
|
|
1834
|
+
T
|
|
1835
|
+
] extends [unknown] ? [
|
|
1836
|
+
unknown
|
|
1837
|
+
] extends [T] ? ExprArg<C> : CompatibleArgKnown<C, T> : CompatibleArgKnown<C, T>;
|
|
1838
|
+
type CompatibleArgKnown<C, T> = Ret<C, T> | `$$${string}` | (T extends boolean ? BooleanArg<C> : T extends number ? NumericArg<C> : T extends string ? `$${string}` | Ret<C, string> | `$$${string}` : T extends Date ? DateArg<C> : never) | (T extends boolean ? boolean : T extends number ? number : T extends string ? string & {} : T extends Date ? Date : T extends null | undefined ? null | undefined : never) | (T extends (infer E)[] ? CompatibleArg<C, E> | ArrayArg<C> : never);
|
|
1839
|
+
type AnyPathFieldRef<C> = `$${string}`;
|
|
1840
|
+
declare class Ret<out Context = unknown, T = unknown> {
|
|
1841
|
+
readonly __fn: unknown;
|
|
1842
|
+
readonly __minVersion: string;
|
|
1843
|
+
readonly __context: Context;
|
|
1844
|
+
readonly __type: SimplifyWritable<T>;
|
|
1845
|
+
readonly __tag: "Ret";
|
|
1846
|
+
constructor(__fn: unknown, __minVersion?: string);
|
|
1847
|
+
}
|
|
1848
|
+
type AnyRet = Ret<never, never>;
|
|
1849
|
+
declare class BaseBuilder<C, Mode extends BuilderMode = "expression"> {
|
|
1850
|
+
_resolve(value: unknown): unknown;
|
|
1851
|
+
protected id: (op: string) => (arg: unknown) => AnyRet;
|
|
1852
|
+
protected varargs: (op: string) => (...args: unknown[]) => AnyRet;
|
|
1853
|
+
protected flexible: (op: string) => (...args: unknown[]) => AnyRet;
|
|
1854
|
+
protected options: (op: string) => (options: unknown) => AnyRet;
|
|
1855
|
+
protected nullary: (op: string) => () => AnyRet;
|
|
1856
|
+
pipe: PipelineBuilder<C>;
|
|
1857
|
+
elemMatch: <const E>(filter: ($: ExprBuilder<E>) => ValidMatchFilterWithBuilder<E>) => {
|
|
1858
|
+
$elemMatch: ValidMatchFilterWithBuilder<E>;
|
|
1859
|
+
};
|
|
1860
|
+
add: AddOp<C>;
|
|
1861
|
+
subtract: SubtractOp<C>;
|
|
1862
|
+
multiply: VarargsNumericOp<C>;
|
|
1863
|
+
divide: BinaryNumericOp<C>;
|
|
1864
|
+
mod: BinaryNumericOp<C>;
|
|
1865
|
+
abs: UnaryNumericOp<C>;
|
|
1866
|
+
ceil: UnaryNumericOp<C>;
|
|
1867
|
+
floor: UnaryNumericOp<C>;
|
|
1868
|
+
ln: UnaryNumericOp<C>;
|
|
1869
|
+
log: BinaryNumericOp<C>;
|
|
1870
|
+
log10: UnaryNumericOp<C>;
|
|
1871
|
+
pow: BinaryNumericOp<C>;
|
|
1872
|
+
sqrt: UnaryNumericOp<C>;
|
|
1873
|
+
trunc: <const T extends NumericArg<C>, const P extends NumericArg<C>>(num: T & NumericArgInput<C, T>, place?: P & NumericArgInput<C, P>) => Ret<C, number>;
|
|
1874
|
+
round: <const T extends NumericArg<C>, const P extends NumericArg<C>>(num: T & NumericArgInput<C, T>, place?: P & NumericArgInput<C, P>) => Ret<C, number>;
|
|
1875
|
+
exp: UnaryNumericOp<C>;
|
|
1876
|
+
rand: () => Ret<C, number>;
|
|
1877
|
+
arrayElemAt: <const A extends ArrayArg<C>, const I extends NumericArg<C>>(array: A & ArrayArgInput<C, A>, index: I & NumericArgInput<C, I>) => Ret<C, ElemType<ResolveType<C, A>>>;
|
|
1878
|
+
arrayToObject: <const T extends ArrayArg<C>>(exp: T & ArrayArgInput<C, T>) => Ret<C, ArrayToObjectResult<ElemType<ResolveType<C, T>>>>;
|
|
1879
|
+
concatArrays: <const T extends ArrayArg<C>[]>(...args: {
|
|
1880
|
+
[K in keyof T]: T[K] & ArrayArgInput<C, T[K]>;
|
|
1881
|
+
}) => Ret<C, ElemType<ResolveType<C, T[number]>>[]>;
|
|
1882
|
+
/** Selects a subset of array elements that match a condition.
|
|
1883
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/filter/ */
|
|
1884
|
+
filter: {
|
|
1885
|
+
<const I extends ArrayArg<C>, const As extends string | undefined = undefined, const L extends NumericArg<C> = NumericArg<C>>(options: {
|
|
1886
|
+
input: I & ArrayArgInput<C, I>;
|
|
1887
|
+
cond: ($: BaseBuilder<ContextWithThis<C, ElemType<ResolveType<C, I>>, As>>) => BooleanArg<ContextWithThis<C, ElemType<ResolveType<C, I>>, As>>;
|
|
1888
|
+
as?: As;
|
|
1889
|
+
limit?: L & NumericArgInput<C, L>;
|
|
1890
|
+
}): Ret<C, ResolveType<C, I>>;
|
|
1891
|
+
<const I extends ArrayArg<C>, const As extends string | undefined = undefined, const B extends BooleanArg<ContextWithThis<C, ElemType<ResolveType<C, I>>, As>> = BooleanArg<ContextWithThis<C, ElemType<ResolveType<C, I>>, As>>, const L extends NumericArg<C> = NumericArg<C>>(options: {
|
|
1892
|
+
input: I & ArrayArgInput<C, I>;
|
|
1893
|
+
cond: B;
|
|
1894
|
+
as?: As;
|
|
1895
|
+
limit?: L & NumericArgInput<C, L>;
|
|
1896
|
+
}): Ret<C, CallbackOnlyError<"filter">>;
|
|
1897
|
+
};
|
|
1898
|
+
first: <const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>) => Ret<C, Mode extends "accumulator" ? ResolveType<C, T> : ResolveType<C, T> extends readonly (infer E)[] ? E | null : ResolveType<C, T> | null>;
|
|
1899
|
+
in: <const V extends ExprArg<C>, const A extends ArrayArg<C>>(value: ExprArgInput<C, V>, array: A & ArrayArgInput<C, A>) => Ret<C, boolean>;
|
|
1900
|
+
indexOfArray: <const A extends ArrayArg<C>, const S extends ExprArg<C>, const N extends NumericArg<C>>(options: {
|
|
1901
|
+
array: A & ArrayArgInput<C, A>;
|
|
1902
|
+
search: ExprArgInput<C, S>;
|
|
1903
|
+
start?: N & NumericArgInput<C, N>;
|
|
1904
|
+
end?: N & NumericArgInput<C, N>;
|
|
1905
|
+
}) => Ret<C, number>;
|
|
1906
|
+
isArray: <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, boolean>;
|
|
1907
|
+
last: <const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>) => Ret<C, Mode extends "accumulator" ? ResolveType<C, T> : ResolveType<C, T> extends readonly (infer E)[] ? E | null : ResolveType<C, T> | null>;
|
|
1908
|
+
/** Applies an expression to each element of an array and returns the results.
|
|
1909
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/ */
|
|
1910
|
+
map: {
|
|
1911
|
+
<const A extends ArrayArg<C>, const As extends string | undefined = undefined, const R extends DeepObjectExpr<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>> = DeepObjectExpr<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>>>(options: {
|
|
1912
|
+
input: A & ArrayArgInput<C, A>;
|
|
1913
|
+
as?: As;
|
|
1914
|
+
in: ($: BaseBuilder<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>>) => R;
|
|
1915
|
+
}): Ret<C, DeepObjectResolve<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>, R>[]>;
|
|
1916
|
+
<const A extends ArrayArg<C>, const As extends string | undefined = undefined, const R extends DeepObjectExpr<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>> = DeepObjectExpr<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>>>(options: {
|
|
1917
|
+
input: A & ArrayArgInput<C, A>;
|
|
1918
|
+
as?: As;
|
|
1919
|
+
in: DeepObjectInput<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>, R>;
|
|
1920
|
+
}): Ret<C, DeepObjectResolve<ContextWithThis<C, ElemType<ResolveType<C, A>>, As>, R>[]>;
|
|
1921
|
+
};
|
|
1922
|
+
maxN: <const T extends ArrayArg<C>, const N extends NumericArg<C>>(options: {
|
|
1923
|
+
input: T & ArrayArgInput<C, T>;
|
|
1924
|
+
n: N & NumericArgInput<C, N>;
|
|
1925
|
+
}) => Ret<C, Mode extends "accumulator" ? ResolveType<C, T>[] : ResolveType<C, T>>;
|
|
1926
|
+
minN: <const T extends ArrayArg<C>, const N extends NumericArg<C>>(options: {
|
|
1927
|
+
input: T & ArrayArgInput<C, T>;
|
|
1928
|
+
n: N & NumericArgInput<C, N>;
|
|
1929
|
+
}) => Ret<C, Mode extends "accumulator" ? ResolveType<C, T>[] : ResolveType<C, T>>;
|
|
1930
|
+
firstN: <const T extends ArrayArg<C>, const N extends NumericArg<C>>(options: {
|
|
1931
|
+
input: T & ArrayArgInput<C, T>;
|
|
1932
|
+
n: N & NumericArgInput<C, N>;
|
|
1933
|
+
}) => Ret<C, Mode extends "accumulator" ? ResolveType<C, T>[] : ResolveType<C, T>>;
|
|
1934
|
+
lastN: <const T extends ArrayArg<C>, const N extends NumericArg<C>>(options: {
|
|
1935
|
+
input: T & ArrayArgInput<C, T>;
|
|
1936
|
+
n: N & NumericArgInput<C, N>;
|
|
1937
|
+
}) => Ret<C, Mode extends "accumulator" ? ResolveType<C, T>[] : ResolveType<C, T>>;
|
|
1938
|
+
objectToArray: <const T extends ExprArg<C>>(expression: ExprArgInput<C, T>) => Ret<C, {
|
|
1939
|
+
k: string;
|
|
1940
|
+
v: ResolveType<C, T> extends Dict<infer V> ? V : unknown;
|
|
1941
|
+
}[]>;
|
|
1942
|
+
range: <const S extends NumericArg<C>, const E extends NumericArg<C>, const St extends NumericArg<C> = never>(start: S & NumericArgInput<C, S>, end: E & NumericArgInput<C, E>, step?: St & NumericArgInput<C, St>) => Ret<C, number[]>;
|
|
1943
|
+
/** Applies an expression to each element of an array, accumulating into a single value.
|
|
1944
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/reduce/ */
|
|
1945
|
+
reduce: {
|
|
1946
|
+
<const A extends ArrayArg<C>, const Init extends ExprArg<C> | ArrayIn<C>, const R extends Exclude<ExprArg<ContextWithThisAndValue<C, ElemType<ResolveType<C, A>>, ResolveType<C, Init>>>, string>>(options: {
|
|
1947
|
+
input: A & ArrayArgInput<C, A>;
|
|
1948
|
+
initialValue: Init;
|
|
1949
|
+
in: ($: BaseBuilder<ContextWithThisAndValue<C, ElemType<ResolveType<C, A>>, ResolveType<C, Init>>>) => R;
|
|
1950
|
+
}): Ret<C, ResolveType<ContextWithThisAndValue<C, ElemType<ResolveType<C, A>>, ResolveType<C, Init>>, R>>;
|
|
1951
|
+
<const A extends ArrayArg<C>, const Init extends ExprArg<C> | ArrayIn<C>, const R>(options: {
|
|
1952
|
+
input: A & ArrayArgInput<C, A>;
|
|
1953
|
+
initialValue: Init;
|
|
1954
|
+
in: R;
|
|
1955
|
+
}): Ret<C, CallbackOnlyError<"reduce">>;
|
|
1956
|
+
};
|
|
1957
|
+
reverseArray: <const T extends ArrayArg<C>>(exp: T & ArrayArgInput<C, T>) => Ret<C, ResolveType<C, T>>;
|
|
1958
|
+
size: UnaryArrayToNumber<C>;
|
|
1959
|
+
slice: <const A extends ArrayArg<C>, const N extends NumericArg<C>>(array: A & ArrayArgInput<C, A>, n: N & NumericArgInput<C, N>, position?: N & NumericArgInput<C, N>) => Ret<C, ResolveType<C, A>>;
|
|
1960
|
+
sortArray: <const T extends {
|
|
1961
|
+
input: ArrayArg<C>;
|
|
1962
|
+
sortBy: Dict<1 | -1> | 1 | -1;
|
|
1963
|
+
}>(options: T & {
|
|
1964
|
+
input: T["input"] & ArrayArgInput<C, T["input"]>;
|
|
1965
|
+
}) => Ret<C, ResolveType<C, T["input"]>>;
|
|
1966
|
+
zip: {
|
|
1967
|
+
<const T extends readonly ArrayArg<C>[]>(options: {
|
|
1968
|
+
inputs: readonly [...T];
|
|
1969
|
+
useLongestLength: true;
|
|
1970
|
+
defaults: Readonly<{
|
|
1971
|
+
[K in keyof T]: ElemType<ResolveType<C, T[K]>>;
|
|
1972
|
+
}>;
|
|
1973
|
+
}): Ret<C, {
|
|
1974
|
+
-readonly [K in keyof T]: ElemType<ResolveType<C, T[K]>>;
|
|
1975
|
+
}[]>;
|
|
1976
|
+
<const T extends readonly ArrayArg<C>[]>(options: {
|
|
1977
|
+
inputs: readonly [...T];
|
|
1978
|
+
useLongestLength: true;
|
|
1979
|
+
defaults?: Readonly<{
|
|
1980
|
+
[K in keyof T]?: ElemType<ResolveType<C, T[K]>>;
|
|
1981
|
+
}>;
|
|
1982
|
+
}): Ret<C, {
|
|
1983
|
+
-readonly [K in keyof T]: ElemType<ResolveType<C, T[K]>> | null;
|
|
1984
|
+
}[]>;
|
|
1985
|
+
<const T extends readonly ArrayArg<C>[]>(options: {
|
|
1986
|
+
inputs: readonly [...T];
|
|
1987
|
+
useLongestLength?: false;
|
|
1988
|
+
defaults?: never;
|
|
1989
|
+
}): Ret<C, {
|
|
1990
|
+
-readonly [K in keyof T]: ElemType<ResolveType<C, T[K]>>;
|
|
1991
|
+
}[]>;
|
|
1992
|
+
};
|
|
1993
|
+
cmp: <const T extends ExprArg<C>>(left: ExprArgInput<C, T>, right: ExprArgInput<C, T>) => Ret<C, -1 | 0 | 1>;
|
|
1994
|
+
eq: ComparisonOp<C>;
|
|
1995
|
+
gt: ComparisonOp<C>;
|
|
1996
|
+
gte: ComparisonOp<C>;
|
|
1997
|
+
lt: ComparisonOp<C>;
|
|
1998
|
+
lte: ComparisonOp<C>;
|
|
1999
|
+
ne: ComparisonOp<C>;
|
|
2000
|
+
and: VarargsBooleanOp<C>;
|
|
2001
|
+
or: VarargsBooleanOp<C>;
|
|
2002
|
+
nor: VarargsBooleanOp<C>;
|
|
2003
|
+
not: <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, boolean>;
|
|
2004
|
+
/** Evaluates a boolean expression and returns one of two values.
|
|
2005
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/cond/ */
|
|
2006
|
+
cond: <const B extends BooleanArg<C>, const T extends ExprArg<C>, const E extends ExprArg<C>>(options: {
|
|
2007
|
+
if: B & BooleanArgInput<C, B>;
|
|
2008
|
+
then: T & ExprArgInput<C, T>;
|
|
2009
|
+
else: E & ExprArgInput<C, E>;
|
|
2010
|
+
}) => Ret<C, ResolveType<C, T> | ResolveType<C, E>>;
|
|
2011
|
+
/** Returns the first non-null/non-missing expression (null coalescing).
|
|
2012
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/ifNull/ */
|
|
2013
|
+
ifNull: <const T extends ExprArg<C>[]>(...args: {
|
|
2014
|
+
[K in keyof T]: T[K] & ExprArgInput<C, T[K]>;
|
|
2015
|
+
}) => Ret<C, FirstNotNil<ResolveTypes<C, T>>>;
|
|
2016
|
+
/** Evaluates a series of case expressions, returning the value of the first match.
|
|
2017
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/switch/ */
|
|
2018
|
+
switch: <const Br extends readonly {
|
|
2019
|
+
case: BooleanArg<C>;
|
|
2020
|
+
then: ExprArg<C>;
|
|
2021
|
+
}[], const D extends ExprArg<C>>(options: {
|
|
2022
|
+
branches: Br;
|
|
2023
|
+
default: D;
|
|
2024
|
+
}) => Ret<C, ResolveType<C, Br[number]["then"]> | ResolveType<C, D>>;
|
|
2025
|
+
concat: <const T extends StringArg<C>[]>(...args: {
|
|
2026
|
+
[K in keyof T]: StringArgInput<C, T[K]>;
|
|
2027
|
+
}) => Ret<C, ConcatStrings<{
|
|
2028
|
+
[K in keyof T]: ResolveType<C, T[K]>;
|
|
2029
|
+
}>>;
|
|
2030
|
+
indexOfBytes: <const S extends StringArg<C>, const Sub extends StringArg<C>, const N extends NumericArg<C>>(string: StringArgInput<C, S>, substring: StringArgInput<C, Sub>, start?: N & NumericArgInput<C, N>, end?: N & NumericArgInput<C, N>) => Ret<C, number>;
|
|
2031
|
+
indexOfCP: <const S extends StringArg<C>, const Sub extends StringArg<C>, const N extends NumericArg<C>>(string: StringArgInput<C, S>, substring: StringArgInput<C, Sub>, start?: N & NumericArgInput<C, N>, end?: N & NumericArgInput<C, N>) => Ret<C, number>;
|
|
2032
|
+
ltrim: <const T extends StringArg<C>>(options: {
|
|
2033
|
+
input: StringArgInput<C, T>;
|
|
2034
|
+
chars?: StringArgInput<C, StringArg<C>>;
|
|
2035
|
+
}) => Ret<C, string>;
|
|
2036
|
+
regexFind: <const T extends StringArg<C>>(options: {
|
|
2037
|
+
input: StringArgInput<C, T>;
|
|
2038
|
+
regex: StringArgInput<C, StringArg<C>> | RegExp;
|
|
2039
|
+
options?: StringArgInput<C, StringArg<C>>;
|
|
2040
|
+
}) => Ret<C, {
|
|
2041
|
+
match: string;
|
|
2042
|
+
idx: number;
|
|
2043
|
+
captures: string[];
|
|
2044
|
+
} | null>;
|
|
2045
|
+
regexFindAll: <const T extends StringArg<C>>(options: {
|
|
2046
|
+
input: StringArgInput<C, T>;
|
|
2047
|
+
regex: StringArgInput<C, StringArg<C>> | RegExp;
|
|
2048
|
+
options?: StringArgInput<C, StringArg<C>>;
|
|
2049
|
+
}) => Ret<C, {
|
|
2050
|
+
match: string;
|
|
2051
|
+
idx: number;
|
|
2052
|
+
captures: string[];
|
|
2053
|
+
}[]>;
|
|
2054
|
+
regexMatch: <const T extends StringArg<C>>(options: {
|
|
2055
|
+
input: StringArgInput<C, T>;
|
|
2056
|
+
regex: StringArgInput<C, StringArg<C>> | RegExp;
|
|
2057
|
+
options?: StringArgInput<C, StringArg<C>>;
|
|
2058
|
+
}) => Ret<C, boolean>;
|
|
2059
|
+
replaceAll: <const T extends StringArg<C>>(options: {
|
|
2060
|
+
input: StringArgInput<C, T>;
|
|
2061
|
+
find: StringArgInput<C, StringArg<C>>;
|
|
2062
|
+
replacement: StringArgInput<C, StringArg<C>>;
|
|
2063
|
+
}) => Ret<C, string>;
|
|
2064
|
+
replaceOne: <const T extends StringArg<C>>(options: {
|
|
2065
|
+
input: StringArgInput<C, T>;
|
|
2066
|
+
find: StringArgInput<C, StringArg<C>>;
|
|
2067
|
+
replacement: StringArgInput<C, StringArg<C>>;
|
|
2068
|
+
}) => Ret<C, string>;
|
|
2069
|
+
rtrim: <const T extends StringArg<C>>(options: {
|
|
2070
|
+
input: StringArgInput<C, T>;
|
|
2071
|
+
chars?: StringArgInput<C, StringArg<C>>;
|
|
2072
|
+
}) => Ret<C, string>;
|
|
2073
|
+
split: <const S extends StringArg<C>, const D extends StringArg<C>>(string: StringArgInput<C, S>, delimiter: StringArgInput<C, D>) => Ret<C, string[]>;
|
|
2074
|
+
strcasecmp: BinaryStringToNumber<C>;
|
|
2075
|
+
strLenBytes: UnaryStringToNumber<C>;
|
|
2076
|
+
strLenCP: UnaryStringToNumber<C>;
|
|
2077
|
+
substr: <const S extends StringArg<C>, const N extends NumericArg<C>>(string: StringArgInput<C, S>, start: (N & NumericArgInput<C, N>) | number, length: (N & NumericArgInput<C, N>) | number) => Ret<C, string>;
|
|
2078
|
+
substrBytes: <const S extends StringArg<C>, const N extends NumericArg<C>>(string: StringArgInput<C, S>, start: N & NumericArgInput<C, N>, length: N & NumericArgInput<C, N>) => Ret<C, string>;
|
|
2079
|
+
substrCP: <const S extends StringArg<C>, const N extends NumericArg<C>>(string: StringArgInput<C, S>, start: N & NumericArgInput<C, N>, length: N & NumericArgInput<C, N>) => Ret<C, string>;
|
|
2080
|
+
toLower: UnaryStringToString<C>;
|
|
2081
|
+
toUpper: UnaryStringToString<C>;
|
|
2082
|
+
trim: <const T extends StringArg<C>>(options: {
|
|
2083
|
+
input: StringArgInput<C, T>;
|
|
2084
|
+
chars?: StringArgInput<C, StringArg<C>>;
|
|
2085
|
+
}) => Ret<C, string>;
|
|
2086
|
+
allElementsTrue: UnaryArrayToBool<C>;
|
|
2087
|
+
anyElementTrue: UnaryArrayToBool<C>;
|
|
2088
|
+
setDifference: <const A extends ArrayArg<C>, const B extends ArrayArg<C>>(left: A & ArrayArgInput<C, A>, right: B & ArrayArgInput<C, B>) => Ret<C, ElemType<ResolveType<C, A>>[]>;
|
|
2089
|
+
setEquals: <const T extends ArrayArg<C>[]>(...args: {
|
|
2090
|
+
[K in keyof T]: T[K] & ArrayArgInput<C, T[K]>;
|
|
2091
|
+
}) => Ret<C, boolean>;
|
|
2092
|
+
setIntersection: <const T extends ArrayArg<C>[]>(...args: {
|
|
2093
|
+
[K in keyof T]: T[K] & ArrayArgInput<C, T[K]>;
|
|
2094
|
+
}) => Ret<C, ElemType<ResolveType<C, T[number]>>[]>;
|
|
2095
|
+
setIsSubset: BinaryArrayOp<C>;
|
|
2096
|
+
setUnion: <const T extends (ArrayArg<C> | ArrayLiteral<C>)[]>(...args: T) => Ret<C, ElemType<ResolveType<C, T[number]>>[]>;
|
|
2097
|
+
dateAdd: <const T extends DateArg<C>, const N extends NumericArg<C>>(options: {
|
|
2098
|
+
startDate: T & DateArgInput<C, T>;
|
|
2099
|
+
unit: string;
|
|
2100
|
+
amount: N & NumericArgInput<C, N>;
|
|
2101
|
+
timezone?: StringArg<C>;
|
|
2102
|
+
}) => Ret<C, Date>;
|
|
2103
|
+
dateDiff: <const S extends DateArg<C>, const E extends DateArg<C>>(options: {
|
|
2104
|
+
startDate: S & DateArgInput<C, S>;
|
|
2105
|
+
endDate: E & DateArgInput<C, E>;
|
|
2106
|
+
unit: string;
|
|
2107
|
+
timezone?: StringArg<C>;
|
|
2108
|
+
startOfWeek?: string;
|
|
2109
|
+
}) => Ret<C, number>;
|
|
2110
|
+
dateFromParts: <const T>(opts: T) => Ret<C, Date>;
|
|
2111
|
+
dateFromString: <const D extends StringArg<C>>(options: {
|
|
2112
|
+
dateString: D;
|
|
2113
|
+
format?: StringArg<C>;
|
|
2114
|
+
timezone?: StringArg<C>;
|
|
2115
|
+
onError?: ExprArgInput<C, ExprArg<C>>;
|
|
2116
|
+
onNull?: ExprArgInput<C, ExprArg<C>>;
|
|
2117
|
+
}) => Ret<C, Date>;
|
|
2118
|
+
dateSubtract: <const T extends DateArg<C>, const N extends NumericArg<C>>(options: {
|
|
2119
|
+
startDate: T & DateArgInput<C, T>;
|
|
2120
|
+
unit: string;
|
|
2121
|
+
amount: N & NumericArgInput<C, N>;
|
|
2122
|
+
timezone?: StringArg<C>;
|
|
2123
|
+
}) => Ret<C, Date>;
|
|
2124
|
+
dateToParts: <const T extends DateArg<C>, const I extends boolean | undefined = undefined>(options: {
|
|
2125
|
+
date: T & DateArgInput<C, T>;
|
|
2126
|
+
timezone?: StringArg<C>;
|
|
2127
|
+
iso8601?: I;
|
|
2128
|
+
}) => Ret<C, I extends true ? {
|
|
2129
|
+
isoWeekYear: number;
|
|
2130
|
+
isoWeek: number;
|
|
2131
|
+
isoDayOfWeek: number;
|
|
2132
|
+
hour: number;
|
|
2133
|
+
minute: number;
|
|
2134
|
+
second: number;
|
|
2135
|
+
millisecond: number;
|
|
2136
|
+
} : {
|
|
2137
|
+
year: number;
|
|
2138
|
+
month: number;
|
|
2139
|
+
day: number;
|
|
2140
|
+
hour: number;
|
|
2141
|
+
minute: number;
|
|
2142
|
+
second: number;
|
|
2143
|
+
millisecond: number;
|
|
2144
|
+
}>;
|
|
2145
|
+
dateToString: <const T extends DateArg<C>>(options: {
|
|
2146
|
+
date: T & DateArgInput<C, T>;
|
|
2147
|
+
format?: StringArg<C>;
|
|
2148
|
+
timezone?: StringArg<C>;
|
|
2149
|
+
onNull?: ExprArgInput<C, ExprArg<C>>;
|
|
2150
|
+
}) => Ret<C, string>;
|
|
2151
|
+
dateTrunc: <const T extends DateArg<C>, const N extends NumericArg<C>>(options: {
|
|
2152
|
+
date: T & DateArgInput<C, T>;
|
|
2153
|
+
unit: string;
|
|
2154
|
+
binSize?: N & NumericArgInput<C, N>;
|
|
2155
|
+
timezone?: StringArg<C>;
|
|
2156
|
+
startOfWeek?: string;
|
|
2157
|
+
}) => Ret<C, Date>;
|
|
2158
|
+
dayOfMonth: UnaryDateToNumber<C>;
|
|
2159
|
+
dayOfWeek: UnaryDateToNumber<C>;
|
|
2160
|
+
dayOfYear: UnaryDateToNumber<C>;
|
|
2161
|
+
hour: UnaryDateToNumber<C>;
|
|
2162
|
+
isoDayOfWeek: UnaryDateToNumber<C>;
|
|
2163
|
+
isoWeek: UnaryDateToNumber<C>;
|
|
2164
|
+
isoWeekYear: UnaryDateToNumber<C>;
|
|
2165
|
+
millisecond: UnaryDateToNumber<C>;
|
|
2166
|
+
minute: UnaryDateToNumber<C>;
|
|
2167
|
+
month: UnaryDateToNumber<C>;
|
|
2168
|
+
second: UnaryDateToNumber<C>;
|
|
2169
|
+
week: UnaryDateToNumber<C>;
|
|
2170
|
+
year: UnaryDateToNumber<C>;
|
|
2171
|
+
convert: {
|
|
2172
|
+
<const I extends ExprArg<C>, const To extends ConvertTo, const ON extends ExprArg<C>>(options: {
|
|
2173
|
+
input: ExprArgInput<C, I>;
|
|
2174
|
+
to: To;
|
|
2175
|
+
onError?: ExprArgInput<C, ExprArg<C>>;
|
|
2176
|
+
onNull: ExprArgInput<C, ON>;
|
|
2177
|
+
}): Ret<C, ConvertOutput<To> | ResolveType<C, ON>>;
|
|
2178
|
+
<const I extends ExprArg<C>, const To extends ConvertTo>(options: {
|
|
2179
|
+
input: ExprArgInput<C, I>;
|
|
2180
|
+
to: To;
|
|
2181
|
+
onError?: ExprArgInput<C, ExprArg<C>>;
|
|
2182
|
+
onNull?: undefined;
|
|
2183
|
+
}): Ret<C, NullishResult<C, I, ConvertOutput<To>>>;
|
|
2184
|
+
};
|
|
2185
|
+
isNumber: UnaryTypeConversion<C, boolean>;
|
|
2186
|
+
toBool: UnaryTypeConversion<C, boolean>;
|
|
2187
|
+
toDate: UnaryTypeConversion<C, Date>;
|
|
2188
|
+
toDecimal: UnaryTypeConversion<C, Decimal128>;
|
|
2189
|
+
toDouble: UnaryTypeConversion<C, number>;
|
|
2190
|
+
toInt: UnaryTypeConversion<C, number>;
|
|
2191
|
+
toLong: UnaryTypeConversion<C, number>;
|
|
2192
|
+
toObjectId: UnaryTypeConversion<C, ObjectId>;
|
|
2193
|
+
toString: UnaryTypeConversion<C, string>;
|
|
2194
|
+
type: <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, "double" | "string" | "object" | "array" | "binData" | "undefined" | "objectId" | "bool" | "date" | "null" | "regex" | "dbPointer" | "javascript" | "javascriptWithScope" | "symbol" | "int" | "timestamp" | "long" | "decimal" | "missing" | "minKey" | "maxKey">;
|
|
2195
|
+
getField: <const F extends string, const I extends ExprArg<C> = never>(options: F | {
|
|
2196
|
+
field: F;
|
|
2197
|
+
input?: ExprArgInput<C, I>;
|
|
2198
|
+
}) => Ret<C, [
|
|
2199
|
+
I
|
|
2200
|
+
] extends [never] ? F extends keyof C ? C[F] : never : F extends keyof ResolveType<C, I> ? ResolveType<C, I>[F] : never>;
|
|
2201
|
+
mergeObjects: {
|
|
2202
|
+
<const T extends [DeepSpecInput<C, any>, ...DeepSpecInput<C, any>[]]>(...args: T): Ret<C, MergeObjects<C, T>>;
|
|
2203
|
+
<const T extends Dict<ExprArgInput<C, any>>[]>(...args: T): Ret<C, ShallowMergeObjectsOverride<{
|
|
2204
|
+
[I in keyof T]: {
|
|
2205
|
+
[K in keyof T[I]]: T[I][K] extends Ret<any, infer U> ? U : T[I][K];
|
|
2206
|
+
};
|
|
2207
|
+
}>>;
|
|
2208
|
+
<const T extends [ExprArg<C>, ...ExprArg<C>[]]>(...args: ExprArgInputs<C, T>): Ret<C, MergeObjects<C, T>>;
|
|
2209
|
+
};
|
|
2210
|
+
setField: <const F extends string, const I extends ExprArg<C>, const V extends ExprArg<C>>(options: {
|
|
2211
|
+
field: F;
|
|
2212
|
+
input: ExprArgInput<C, I>;
|
|
2213
|
+
value: ExprArgInput<C, V>;
|
|
2214
|
+
}) => Ret<C, ResolveType<C, I> & Record<F, ResolveType<C, V>>>;
|
|
2215
|
+
unsetField: <const F extends string, const I extends ExprArg<C>>(options: {
|
|
2216
|
+
field: F;
|
|
2217
|
+
input: ExprArgInput<C, I>;
|
|
2218
|
+
}) => Ret<C, Omit<ResolveType<C, I>, F>>;
|
|
2219
|
+
/** Binds variables for use within a scoped expression.
|
|
2220
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/let/ */
|
|
2221
|
+
let: <const V extends Dict<ExprArg<C>>, const I extends ExprArg<MergeVars<C, ResolveVarSpec<C, V>>>>(options: {
|
|
2222
|
+
vars: {
|
|
2223
|
+
[K in keyof V]: ExprArgInput<C, V[K]>;
|
|
2224
|
+
};
|
|
2225
|
+
in: ($: BaseBuilder<MergeVars<C, ResolveVarSpec<C, V>>>) => I;
|
|
2226
|
+
}) => Ret<C, ResolveType<MergeVars<C, ResolveVarSpec<C, V>>, I>>;
|
|
2227
|
+
/** Returns a value without parsing — prevents field path or operator interpretation.
|
|
2228
|
+
* @see https://www.mongodb.com/docs/manual/reference/operator/aggregation/literal/ */
|
|
2229
|
+
literal: <const T>(value: T) => Ret<C, T>;
|
|
2230
|
+
meta: <const T extends "textScore" | "searchScore" | "searchHighlights" | "indexKey">(metaType: T) => Ret<C, T extends "textScore" | "searchScore" ? number : T extends "searchHighlights" ? {
|
|
2231
|
+
path: string;
|
|
2232
|
+
texts: {
|
|
2233
|
+
value: string;
|
|
2234
|
+
type: "text" | "hit";
|
|
2235
|
+
}[];
|
|
2236
|
+
}[] : T extends "indexKey" ? Dict<string> : never>;
|
|
2237
|
+
sampleRate: <const T extends NumericArg<C>>(rate: T & NumericArgInput<C, T>) => Ret<C, boolean>;
|
|
2238
|
+
binarySize: UnaryTypeConversion<C, number>;
|
|
2239
|
+
bsonSize: UnaryTypeConversion<C, number>;
|
|
2240
|
+
bitAnd: VarargsNumericOp<C>;
|
|
2241
|
+
bitOr: VarargsNumericOp<C>;
|
|
2242
|
+
bitXor: VarargsNumericOp<C>;
|
|
2243
|
+
bitNot: UnaryNumericOp<C>;
|
|
2244
|
+
sin: UnaryNumericOp<C>;
|
|
2245
|
+
cos: UnaryNumericOp<C>;
|
|
2246
|
+
tan: UnaryNumericOp<C>;
|
|
2247
|
+
asin: UnaryNumericOp<C>;
|
|
2248
|
+
acos: UnaryNumericOp<C>;
|
|
2249
|
+
atan: UnaryNumericOp<C>;
|
|
2250
|
+
atan2: BinaryNumericOp<C>;
|
|
2251
|
+
asinh: UnaryNumericOp<C>;
|
|
2252
|
+
acosh: UnaryNumericOp<C>;
|
|
2253
|
+
atanh: UnaryNumericOp<C>;
|
|
2254
|
+
sinh: UnaryNumericOp<C>;
|
|
2255
|
+
cosh: UnaryNumericOp<C>;
|
|
2256
|
+
tanh: UnaryNumericOp<C>;
|
|
2257
|
+
degreesToRadians: UnaryNumericOp<C>;
|
|
2258
|
+
radiansToDegrees: UnaryNumericOp<C>;
|
|
2259
|
+
sum: {
|
|
2260
|
+
<const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>): Ret<C, number>;
|
|
2261
|
+
<const T extends AccumulatorExprArg<C>[]>(...args: AccumulatorArgInputs<C, T>): Ret<C, number>;
|
|
2262
|
+
};
|
|
2263
|
+
avg: {
|
|
2264
|
+
<const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>): Ret<C, NullishResult<C, T, number>>;
|
|
2265
|
+
<const T extends AccumulatorExprArg<C>[]>(...args: AccumulatorArgInputs<C, T>): Ret<C, NullishResultFromArgs<C, T, number>>;
|
|
2266
|
+
};
|
|
2267
|
+
max: {
|
|
2268
|
+
<const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>): Ret<C, ResolveType<C, T>>;
|
|
2269
|
+
<const T extends AccumulatorExprArg<C>[]>(...args: AccumulatorArgInputs<C, T>): Ret<C, ResolveType<C, T[number]>>;
|
|
2270
|
+
};
|
|
2271
|
+
min: {
|
|
2272
|
+
<const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>): Ret<C, ResolveType<C, T>>;
|
|
2273
|
+
<const T extends AccumulatorExprArg<C>[]>(...args: AccumulatorArgInputs<C, T>): Ret<C, ResolveType<C, T[number]>>;
|
|
2274
|
+
};
|
|
2275
|
+
stdDevPop: {
|
|
2276
|
+
<const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>): Ret<C, NullishResult<C, T, number>>;
|
|
2277
|
+
<const T extends AccumulatorExprArg<C>[]>(...args: AccumulatorArgInputs<C, T>): Ret<C, NullishResultFromArgs<C, T, number>>;
|
|
2278
|
+
};
|
|
2279
|
+
stdDevSamp: {
|
|
2280
|
+
<const T extends AccumulatorExprArg<C>>(exp: AccumulatorArgInput<C, T>): Ret<C, NullishResult<C, T, number>>;
|
|
2281
|
+
<const T extends AccumulatorExprArg<C>[]>(...args: AccumulatorArgInputs<C, T>): Ret<C, NullishResultFromArgs<C, T, number>>;
|
|
2282
|
+
};
|
|
2283
|
+
}
|
|
2284
|
+
/**
|
|
2285
|
+
* Expression builder providing type-safe access to all MongoDB aggregation operators.
|
|
2286
|
+
*
|
|
2287
|
+
* Passed as `$` in stage callbacks (e.g., `$project($ => ...)`, `$match($ => ...)`).
|
|
2288
|
+
* Exposes arithmetic, string, array, date, conditional, and comparison operators
|
|
2289
|
+
* that mirror the MongoDB aggregation expression language with full type inference.
|
|
2290
|
+
*/
|
|
2291
|
+
declare class ExprBuilder<C> extends BaseBuilder<C> {
|
|
2292
|
+
/** Include this field in `$project` output (equivalent to `1`). */
|
|
2293
|
+
readonly include: 1;
|
|
2294
|
+
/** Exclude this field from `$project` output (equivalent to `0`). */
|
|
2295
|
+
readonly exclude: 0;
|
|
2296
|
+
}
|
|
2297
|
+
declare class AccumulatorBuilder<C> extends BaseBuilder<C, "accumulator"> {
|
|
2298
|
+
addToSet: <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, ResolveType<C, T>[]>;
|
|
2299
|
+
count: <const T extends Dict<never>>(options: T) => Ret<C, number>;
|
|
2300
|
+
push: <const T>(exp: T & DeepSpecInput<C, T>) => Ret<C, DeepSpecResolve<C, T>[]>;
|
|
2301
|
+
bottom: <const TOutput extends ExprArg<C>, const TSort extends SortBySpec<C>>(options: {
|
|
2302
|
+
output: ExprArgInput<C, TOutput>;
|
|
2303
|
+
sortBy: TSort;
|
|
2304
|
+
}) => Ret<C, ResolveType<C, TOutput>>;
|
|
2305
|
+
bottomN: <const TOutput extends ExprArg<C>, const TSort extends SortBySpec<C>, const N extends NumericArg<C>>(options: {
|
|
2306
|
+
output: ExprArgInput<C, TOutput>;
|
|
2307
|
+
sortBy: TSort;
|
|
2308
|
+
n: N & NumericArgInput<C, N>;
|
|
2309
|
+
}) => Ret<C, ResolveType<C, TOutput>[]>;
|
|
2310
|
+
top: <const T extends SortBySpec<C>, const O extends ExprArg<C>>(options: {
|
|
2311
|
+
output: ExprArgInput<C, O>;
|
|
2312
|
+
sortBy: T;
|
|
2313
|
+
}) => Ret<C, ResolveType<C, O>>;
|
|
2314
|
+
topN: <const T extends SortBySpec<C>, const N extends NumericArg<C>, const O extends ExprArg<C>>(options: {
|
|
2315
|
+
output: ExprArgInput<C, O>;
|
|
2316
|
+
sortBy: T;
|
|
2317
|
+
n: N & NumericArgInput<C, N>;
|
|
2318
|
+
}) => Ret<C, ResolveType<C, O>[]>;
|
|
2319
|
+
accumulator: {
|
|
2320
|
+
<const InitFn extends (...args: any[]) => any, const AccExprs extends readonly ExprArg<C>[], const Result>(options: TypedAccumulator<InitFn, {
|
|
2321
|
+
[K in keyof AccExprs]: ResolveType<C, AccExprs[K]>;
|
|
2322
|
+
}, Result> & Omit<TypedAccumulator<InitFn, {
|
|
2323
|
+
[K in keyof AccExprs]: ResolveType<C, AccExprs[K]>;
|
|
2324
|
+
}, Result>, "accumulate" | "accumulateArgs"> & {
|
|
2325
|
+
accumulate: (state: ReturnType<InitFn>, ...args: {
|
|
2326
|
+
[K in keyof AccExprs]: ResolveType<C, AccExprs[K]>;
|
|
2327
|
+
}) => ReturnType<InitFn>;
|
|
2328
|
+
accumulateArgs: readonly [...AccExprs] & {
|
|
2329
|
+
[K in keyof AccExprs]: ExprArgInput<C, AccExprs[K]>;
|
|
2330
|
+
};
|
|
2331
|
+
finalize: (state: ReturnType<InitFn>) => Result;
|
|
2332
|
+
}): Ret<C, Result>;
|
|
2333
|
+
<InitFn extends (...args: any[]) => any, AccArgs extends readonly unknown[], Result>(options: TypedAccumulator<InitFn, AccArgs, Result> & {
|
|
2334
|
+
finalize: (state: ReturnType<InitFn>) => Result;
|
|
2335
|
+
}): Ret<C, Result>;
|
|
2336
|
+
<InitFn extends (...args: any[]) => any, AccExprs extends readonly ExprArg<C>[]>(options: TypedAccumulator<InitFn, {
|
|
2337
|
+
[K in keyof AccExprs]: ResolveType<C, AccExprs[K]>;
|
|
2338
|
+
}, ReturnType<InitFn>> & Omit<TypedAccumulator<InitFn, {
|
|
2339
|
+
[K in keyof AccExprs]: ResolveType<C, AccExprs[K]>;
|
|
2340
|
+
}, ReturnType<InitFn>>, "accumulate" | "accumulateArgs"> & {
|
|
2341
|
+
accumulate: (state: ReturnType<InitFn>, ...args: {
|
|
2342
|
+
[K in keyof AccExprs]: ResolveType<C, AccExprs[K]>;
|
|
2343
|
+
}) => ReturnType<InitFn>;
|
|
2344
|
+
accumulateArgs: readonly [...AccExprs] & {
|
|
2345
|
+
[K in keyof AccExprs]: ExprArgInput<C, AccExprs[K]>;
|
|
2346
|
+
};
|
|
2347
|
+
finalize?: undefined;
|
|
2348
|
+
}): Ret<C, ReturnType<InitFn>>;
|
|
2349
|
+
<InitFn extends (...args: any[]) => any, AccArgs extends readonly unknown[]>(options: TypedAccumulator<InitFn, AccArgs, ReturnType<InitFn>> & {
|
|
2350
|
+
finalize?: undefined;
|
|
2351
|
+
}): Ret<C, ReturnType<InitFn>>;
|
|
2352
|
+
};
|
|
2353
|
+
median: <const T extends NumericArg<C>>(options: {
|
|
2354
|
+
input: T & NumericArgInput<C, T>;
|
|
2355
|
+
method?: "approximate";
|
|
2356
|
+
}) => Ret<C, number>;
|
|
2357
|
+
percentile: <const T extends NumericArg<C>, const P extends readonly number[]>(options: {
|
|
2358
|
+
input: T & NumericArgInput<C, T>;
|
|
2359
|
+
p: P;
|
|
2360
|
+
method?: "approximate";
|
|
2361
|
+
}) => Ret<C, {
|
|
2362
|
+
[K in keyof P]: number;
|
|
2363
|
+
}>;
|
|
2364
|
+
}
|
|
2365
|
+
declare class WindowBuilder<C> extends AccumulatorBuilder<C> {
|
|
2366
|
+
denseRank: () => Ret<C, number>;
|
|
2367
|
+
covariancePop: <const X extends NumericArg<C>, const Y extends NumericArg<C>>(x: X & NumericArgInput<C, X>, y: Y & NumericArgInput<C, Y>) => Ret<C, number | null>;
|
|
2368
|
+
covarianceSamp: <const X extends NumericArg<C>, const Y extends NumericArg<C>>(x: X & NumericArgInput<C, X>, y: Y & NumericArgInput<C, Y>) => Ret<C, number | null>;
|
|
2369
|
+
derivative: <const T extends NumericArg<C>>(options: {
|
|
2370
|
+
input: T & NumericArgInput<C, T>;
|
|
2371
|
+
unit?: TimeUnit;
|
|
2372
|
+
window?: WindowSpec;
|
|
2373
|
+
}) => Ret<C, number>;
|
|
2374
|
+
documentNumber: () => Ret<C, number>;
|
|
2375
|
+
expMovingAvg: <const T extends NumericArg<C>>(options: {
|
|
2376
|
+
input: T & NumericArgInput<C, T>;
|
|
2377
|
+
N?: NumericArg<C>;
|
|
2378
|
+
alpha?: NumericArg<C>;
|
|
2379
|
+
window?: WindowSpec;
|
|
2380
|
+
}) => Ret<C, number>;
|
|
2381
|
+
integral: <const T extends NumericArg<C>>(options: {
|
|
2382
|
+
input: T & NumericArgInput<C, T>;
|
|
2383
|
+
unit?: TimeUnit;
|
|
2384
|
+
window?: WindowSpec;
|
|
2385
|
+
}) => Ret<C, number>;
|
|
2386
|
+
linearFill: <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, ResolveType<C, T>>;
|
|
2387
|
+
locf: <const T extends ExprArg<C>>(exp: ExprArgInput<C, T>) => Ret<C, ResolveType<C, T>>;
|
|
2388
|
+
percentRank: () => Ret<C, number>;
|
|
2389
|
+
rank: () => Ret<C, number>;
|
|
2390
|
+
shift: <const T extends ExprArg<C>, const B extends NumericArg<C>>(options: {
|
|
2391
|
+
output: ExprArgInput<C, T>;
|
|
2392
|
+
by: B & NumericArgInput<C, B>;
|
|
2393
|
+
default?: ExprArgInput<C, ExprArg<C>>;
|
|
2394
|
+
}) => Ret<C, ResolveType<C, T>>;
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
/**
|
|
2398
|
+
* Migration tool — type-safe collection schema migrations via update pipelines.
|
|
2399
|
+
*
|
|
2400
|
+
* Usage:
|
|
2401
|
+
* const migration = migrate<OldUser, NewUser>();
|
|
2402
|
+
* const pipeline = migration.pipe(
|
|
2403
|
+
* $set({ newField: "default" }),
|
|
2404
|
+
* $unset("deprecatedField"),
|
|
2405
|
+
* );
|
|
2406
|
+
* // pipeline is only valid if the output matches NewUser
|
|
2407
|
+
* await collection.updateMany(() => ({}), $ => pipeline).execute();
|
|
2408
|
+
*/
|
|
2409
|
+
|
|
2410
|
+
/**
|
|
2411
|
+
* MigrationOperators — the migration context with pipe and stage references.
|
|
2412
|
+
*/
|
|
2413
|
+
type MigrationOperators<TFrom, TTo> = {
|
|
2414
|
+
pipe: MigrationPipelineBuilder<TFrom, TTo>;
|
|
2415
|
+
};
|
|
2416
|
+
/**
|
|
2417
|
+
* Creates a type-safe migration pipeline builder.
|
|
2418
|
+
*
|
|
2419
|
+
* The pipeline starts from TFrom (source schema) and validates that the
|
|
2420
|
+
* final output is assignable to TTo (target schema).
|
|
2421
|
+
*
|
|
2422
|
+
* Only update-allowed stages ($set, $unset, $addFields, $project,
|
|
2423
|
+
* $replaceRoot, $replaceWith) are accepted.
|
|
2424
|
+
*
|
|
2425
|
+
* @example
|
|
2426
|
+
* ```ts
|
|
2427
|
+
* type OldUser = { _id: ObjectId; name: string; age: number };
|
|
2428
|
+
* type NewUser = { _id: ObjectId; name: string; age: number; email: string };
|
|
2429
|
+
*
|
|
2430
|
+
* const m = migrate<OldUser, NewUser>();
|
|
2431
|
+
* const pipeline = m.pipe(
|
|
2432
|
+
* $set({ email: "unknown@example.com" }),
|
|
2433
|
+
* );
|
|
2434
|
+
* // ✅ compiles — output matches NewUser
|
|
2435
|
+
*
|
|
2436
|
+
* const bad = m.pipe(
|
|
2437
|
+
* $unset("age"),
|
|
2438
|
+
* );
|
|
2439
|
+
* // ❌ error — output { _id, name } doesn't match NewUser
|
|
2440
|
+
* ```
|
|
2441
|
+
*/
|
|
2442
|
+
declare const migrate: <TFrom, TTo>() => MigrationOperators<SimplifyWritable<TFrom>, SimplifyWritable<TTo>>;
|
|
2443
|
+
|
|
2444
|
+
export { $addFields, $bucket, $bucketAuto, $changeStream, $collStats, $count, $currentOp, $densify, $documents, $facet, $fill, $geoNear, $graphLookup, $group, $indexStats, $limit, $listLocalSessions, $listSessions, $lookup, $match, $merge, $out, $planCacheStats, $project, $redact, $replaceRoot, $replaceWith, $sample, $set, $setWindowFields, $shardedDataDistribution, $skip, $sort, $sortByCount, $unionWith, $unset, $unwind, AccumulatorBuilder, type Agg, type AggregateBuilder, type BoundCollection, type BulkWriteBuilder, type BulkWriteOp, type CallbackOnlyError, type Collection, type CollectionType, type CountBuilder, type CountOptions, type CrudCollection, type CrudFilter, type DeleteBuilder, type DistinctBuilder, ExprBuilder, type FindBuilder, type FindOneAndDeleteBuilder, type FindOneAndOptions, type FindOneAndReplaceBuilder, type FindOneAndUpdateBuilder, type FindOptions, type ForeignType, type Geometry, type InferSchema, type InsertManyBuilder, type InsertOneBuilder, type MigrationOperators, type MigrationPipelineBuilder, type PipelineBuilder, type ProjectionSpec, type ReplaceBuilder, Ret, type SchemaLike, type SortSpec, type StageFunction, type TimeUnit, type TypedAccumulator, type TypedPipeline, type UpdateManyBuilder, type UpdateOneBuilder, type UpdateOperators, type UpdatePipelineBuilder, type UpdatePipelineCallback, WindowBuilder, type WindowSpec, collection, functionToString, migrate, registry, resolveAccumulator };
|