@prisma-next/mongo-query-builder 0.12.0-dev.50 → 0.12.0-dev.51

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/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@prisma-next/mongo-query-builder",
3
- "version": "0.12.0-dev.50",
3
+ "version": "0.12.0-dev.51",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "description": "Type-safe MongoDB query builder (reads, writes, find-and-modify, pipeline-terminal writes) with document shape tracking",
8
8
  "dependencies": {
9
- "@prisma-next/contract": "0.12.0-dev.50",
10
- "@prisma-next/mongo-contract": "0.12.0-dev.50",
11
- "@prisma-next/mongo-query-ast": "0.12.0-dev.50",
12
- "@prisma-next/mongo-value": "0.12.0-dev.50",
13
- "@prisma-next/utils": "0.12.0-dev.50"
9
+ "@prisma-next/contract": "0.12.0-dev.51",
10
+ "@prisma-next/mongo-contract": "0.12.0-dev.51",
11
+ "@prisma-next/mongo-query-ast": "0.12.0-dev.51",
12
+ "@prisma-next/mongo-value": "0.12.0-dev.51",
13
+ "@prisma-next/utils": "0.12.0-dev.51"
14
14
  },
15
15
  "devDependencies": {
16
- "@prisma-next/tsconfig": "0.12.0-dev.50",
17
- "@prisma-next/tsdown": "0.12.0-dev.50",
16
+ "@prisma-next/tsconfig": "0.12.0-dev.51",
17
+ "@prisma-next/tsdown": "0.12.0-dev.51",
18
18
  "tsdown": "0.22.0",
19
19
  "typescript": "5.9.3",
20
20
  "vitest": "4.1.6"
@@ -34,6 +34,7 @@
34
34
  "types": "./dist/index.d.mts",
35
35
  "exports": {
36
36
  ".": "./dist/index.mjs",
37
+ "./contract-free": "./dist/exports/contract-free.mjs",
37
38
  "./package.json": "./package.json"
38
39
  },
39
40
  "engines": {
@@ -0,0 +1,178 @@
1
+ import {
2
+ AggregateCommand,
3
+ FindOneAndUpdateCommand,
4
+ InsertOneCommand,
5
+ MongoAndExpr,
6
+ type MongoFilterExpr,
7
+ MongoLimitStage,
8
+ MongoMatchStage,
9
+ type MongoPipelineStage,
10
+ MongoSortStage,
11
+ type MongoUpdatePipelineStage,
12
+ } from '@prisma-next/mongo-query-ast/execution';
13
+ import type { MongoValue } from '@prisma-next/mongo-value';
14
+ import { createFieldAccessor, type FieldAccessor } from '../field-accessor';
15
+ import type { DocShape } from '../types';
16
+ import { resolveUpdaterResult, type UpdaterResult } from '../update-ops';
17
+
18
+ /**
19
+ * Fold an array of filter expressions into a single `MongoFilterExpr`. Length-1
20
+ * short-circuits to avoid a redundant `$and` wrapper; the call site never writes
21
+ * `MongoAndExpr.of([...])` directly.
22
+ */
23
+ function foldFilters(filters: ReadonlyArray<MongoFilterExpr>): MongoFilterExpr {
24
+ const first = filters[0];
25
+ if (first === undefined) {
26
+ throw new Error('foldFilters: invariant violated — empty filter list');
27
+ }
28
+ return filters.length === 1 ? first : MongoAndExpr.of(filters);
29
+ }
30
+
31
+ /**
32
+ * Fluent aggregate chain. Accumulates `$match` / `$sort` / `$limit` stages and
33
+ * produces an `AggregateCommand` via `.build()`.
34
+ *
35
+ * Instances are immutable — each stage method returns a new chain.
36
+ */
37
+ export class AggregateChain<Shape extends DocShape> {
38
+ readonly #collection: string;
39
+ readonly #stages: ReadonlyArray<MongoPipelineStage>;
40
+
41
+ constructor(collection: string, stages: ReadonlyArray<MongoPipelineStage>) {
42
+ this.#collection = collection;
43
+ this.#stages = stages;
44
+ }
45
+
46
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): AggregateChain<Shape> {
47
+ const f = createFieldAccessor<Shape>();
48
+ const filter = filterFn(f);
49
+ return new AggregateChain<Shape>(this.#collection, [
50
+ ...this.#stages,
51
+ new MongoMatchStage(filter),
52
+ ]);
53
+ }
54
+
55
+ sort(spec: Record<string, 1 | -1>): AggregateChain<Shape> {
56
+ return new AggregateChain<Shape>(this.#collection, [...this.#stages, new MongoSortStage(spec)]);
57
+ }
58
+
59
+ limit(n: number): AggregateChain<Shape> {
60
+ return new AggregateChain<Shape>(this.#collection, [...this.#stages, new MongoLimitStage(n)]);
61
+ }
62
+
63
+ build(): AggregateCommand {
64
+ return new AggregateCommand(this.#collection, this.#stages);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * Fluent find-and-modify chain. Holds an accumulated list of filter expressions
70
+ * (AND-folded into the wire command's `filter` slot) and exposes
71
+ * `.findOneAndUpdate(...)` as the only terminal.
72
+ *
73
+ * Multiple `.match()` calls AND-fold internally — `MongoAndExpr.of` never
74
+ * appears at the call site.
75
+ *
76
+ * Instances are immutable — `.match()` returns a new `FilteredBuilder`.
77
+ */
78
+ export class FilteredBuilder<Shape extends DocShape> {
79
+ readonly #collection: string;
80
+ readonly #filters: ReadonlyArray<MongoFilterExpr>;
81
+
82
+ constructor(collection: string, filters: ReadonlyArray<MongoFilterExpr>) {
83
+ if (filters.length === 0) {
84
+ throw new Error('FilteredBuilder requires at least one filter');
85
+ }
86
+ this.#collection = collection;
87
+ this.#filters = filters;
88
+ }
89
+
90
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape> {
91
+ const f = createFieldAccessor<Shape>();
92
+ const filter = filterFn(f);
93
+ return new FilteredBuilder<Shape>(this.#collection, [...this.#filters, filter]);
94
+ }
95
+
96
+ findOneAndUpdate(
97
+ updaterFn: (fields: FieldAccessor<Shape>) => UpdaterResult,
98
+ opts: { readonly upsert?: boolean; readonly returnDocument?: 'before' | 'after' } = {},
99
+ ): FindOneAndUpdateCommand {
100
+ const filter = foldFilters(this.#filters);
101
+ const f = createFieldAccessor<Shape>();
102
+ const items = updaterFn(f);
103
+ const update = resolveUpdaterResult(items);
104
+ return new FindOneAndUpdateCommand(
105
+ this.#collection,
106
+ filter,
107
+ update,
108
+ opts.upsert ?? false,
109
+ undefined,
110
+ opts.returnDocument ?? 'after',
111
+ );
112
+ }
113
+ }
114
+
115
+ /**
116
+ * Contract-free fluent Mongo collection builder. Produces the canonical
117
+ * `AggregateCommand` / `InsertOneCommand` / `FindOneAndUpdateCommand` command
118
+ * nodes without any contract coupling — parameterised by an explicit `DocShape`
119
+ * instead of a `MongoContract`.
120
+ *
121
+ * Mirrors SQL's `table(source, schema)` → `TableHandle` in spirit: a top-level
122
+ * entry point that exposes fluent query chains from which call sites never write
123
+ * `new MongoMatchStage(...)`, `MongoAndExpr.of([...])`, or `new AggregateCommand(...)`.
124
+ *
125
+ * ```ts
126
+ * const markerLedger = collection<MarkerLedgerDocShape>('_prisma_migrations');
127
+ *
128
+ * // aggregate
129
+ * markerLedger.aggregate().match(f => f._id.eq(space)).limit(1).build();
130
+ *
131
+ * // insertOne
132
+ * markerLedger.insertOne({ _id: space, space, storageHash });
133
+ *
134
+ * // findOneAndUpdate (CAS)
135
+ * markerLedger.match(f => f._id.eq(space)).match(f => f.storageHash.eq(expectedFrom))
136
+ * .findOneAndUpdate(f => [f.stage.set({ storageHash: newHash })], { upsert: false });
137
+ * ```
138
+ */
139
+ export interface CollectionBuilder<Shape extends DocShape> {
140
+ aggregate(): AggregateChain<Shape>;
141
+ insertOne(document: Record<string, MongoValue>): InsertOneCommand;
142
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape>;
143
+ }
144
+
145
+ class CollectionBuilderImpl<Shape extends DocShape> implements CollectionBuilder<Shape> {
146
+ readonly #name: string;
147
+
148
+ constructor(name: string) {
149
+ this.#name = name;
150
+ }
151
+
152
+ aggregate(): AggregateChain<Shape> {
153
+ return new AggregateChain<Shape>(this.#name, []);
154
+ }
155
+
156
+ insertOne(document: Record<string, MongoValue>): InsertOneCommand {
157
+ return new InsertOneCommand(this.#name, document);
158
+ }
159
+
160
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape> {
161
+ const f = createFieldAccessor<Shape>();
162
+ const filter = filterFn(f);
163
+ return new FilteredBuilder<Shape>(this.#name, [filter]);
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Declare a contract-free collection builder parameterised by a `DocShape`.
169
+ * The collection name is bound once; field access reuses `createFieldAccessor`
170
+ * so the shape is typed at every call site without a contract.
171
+ *
172
+ * @param name The MongoDB collection name (e.g. `'_prisma_migrations'`)
173
+ */
174
+ export function collection<Shape extends DocShape>(name: string): CollectionBuilder<Shape> {
175
+ return new CollectionBuilderImpl<Shape>(name);
176
+ }
177
+
178
+ export type { MongoUpdatePipelineStage };
@@ -0,0 +1,6 @@
1
+ export {
2
+ AggregateChain,
3
+ type CollectionBuilder,
4
+ collection,
5
+ FilteredBuilder,
6
+ } from '../contract-free/collection';
@@ -13,7 +13,7 @@ export type {
13
13
  LeafExpression,
14
14
  ObjectExpression,
15
15
  } from '../field-accessor';
16
- export { createFieldAccessor } from '../field-accessor';
16
+ export { createFieldAccessor, expr } from '../field-accessor';
17
17
  export type {
18
18
  LookupBuilder,
19
19
  LookupBuilderWithKey,
@@ -7,6 +7,7 @@ import {
7
7
  MongoAddFieldsStage,
8
8
  MongoAggFieldRef,
9
9
  MongoExistsExpr,
10
+ MongoExprFilter,
10
11
  MongoFieldFilter,
11
12
  MongoProjectStage,
12
13
  MongoReplaceRootStage,
@@ -58,6 +59,14 @@ export interface LeafExpression<F extends DocField> extends TypedAggExpr<F> {
58
59
  nin(values: ReadonlyArray<MongoValue>): MongoFilterExpr;
59
60
  exists(flag?: boolean): MongoFilterExpr;
60
61
 
62
+ /**
63
+ * `$type` filter: `{ field: { $type: bsonType } }`. Rides
64
+ * `MongoFieldFilter`'s generic `op` string — no dedicated AST node. The
65
+ * BSON type is expressed as Mongo's alias string (e.g. `'string'`) or
66
+ * numeric type code; an array selects any of several types.
67
+ */
68
+ type(bsonType: MongoValue): MongoFilterExpr;
69
+
61
70
  // Update operators ($set family)
62
71
  set(value: MongoValue): TypedUpdateOp;
63
72
  unset(): TypedUpdateOp;
@@ -203,6 +212,17 @@ export type FieldAccessor<S extends DocShape, N extends NestedDocShape = Record<
203
212
  rawPath<F extends DocField = DocField>(path: string): LeafExpression<F>;
204
213
  };
205
214
 
215
+ /**
216
+ * Wrap a boolean aggregation expression as an `$expr` filter
217
+ * (`MongoExprFilter`). Lets a `$match` express an aggregation-expression
218
+ * predicate — e.g. comparing two field references via `fn.eq(a, b)` — in
219
+ * the typed AST rather than via a raw escape hatch. Pairs with `.type()`
220
+ * to express filters like `{ _id: { $type: 'string' }, $expr: { $eq: ['$_id', '$space'] } }`.
221
+ */
222
+ export function expr(predicate: TypedAggExpr<DocField>): MongoFilterExpr {
223
+ return MongoExprFilter.of(predicate.node);
224
+ }
225
+
206
226
  function buildExpression<F extends DocField>(path: string): Expression<F> {
207
227
  // The runtime object carries the full operator surface unconditionally;
208
228
  // `ObjectExpression` is a strict subset of `LeafExpression`, so a single
@@ -223,6 +243,7 @@ function buildExpression<F extends DocField>(path: string): Expression<F> {
223
243
  nin: (values: ReadonlyArray<MongoValue>) => MongoFieldFilter.nin(path, values),
224
244
  exists: (flag?: boolean) =>
225
245
  flag === false ? MongoExistsExpr.notExists(path) : MongoExistsExpr.exists(path),
246
+ type: (bsonType: MongoValue) => MongoFieldFilter.of(path, '$type', bsonType),
226
247
 
227
248
  set: (value: MongoValue) => setOp(path, value),
228
249
  unset: () => unsetOp(path),