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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,78 @@
1
+ import { b as DocShape, c as UpdaterResult, n as FieldAccessor } from "../field-accessor-BmcQj0lD.mjs";
2
+ import { AggregateCommand, FindOneAndUpdateCommand, InsertOneCommand, MongoFilterExpr, MongoPipelineStage } from "@prisma-next/mongo-query-ast/execution";
3
+ import { MongoValue } from "@prisma-next/mongo-value";
4
+
5
+ //#region src/contract-free/collection.d.ts
6
+ /**
7
+ * Fluent aggregate chain. Accumulates `$match` / `$sort` / `$limit` stages and
8
+ * produces an `AggregateCommand` via `.build()`.
9
+ *
10
+ * Instances are immutable — each stage method returns a new chain.
11
+ */
12
+ declare class AggregateChain<Shape extends DocShape> {
13
+ #private;
14
+ constructor(collection: string, stages: ReadonlyArray<MongoPipelineStage>);
15
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): AggregateChain<Shape>;
16
+ sort(spec: Record<string, 1 | -1>): AggregateChain<Shape>;
17
+ limit(n: number): AggregateChain<Shape>;
18
+ build(): AggregateCommand;
19
+ }
20
+ /**
21
+ * Fluent find-and-modify chain. Holds an accumulated list of filter expressions
22
+ * (AND-folded into the wire command's `filter` slot) and exposes
23
+ * `.findOneAndUpdate(...)` as the only terminal.
24
+ *
25
+ * Multiple `.match()` calls AND-fold internally — `MongoAndExpr.of` never
26
+ * appears at the call site.
27
+ *
28
+ * Instances are immutable — `.match()` returns a new `FilteredBuilder`.
29
+ */
30
+ declare class FilteredBuilder<Shape extends DocShape> {
31
+ #private;
32
+ constructor(collection: string, filters: ReadonlyArray<MongoFilterExpr>);
33
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape>;
34
+ findOneAndUpdate(updaterFn: (fields: FieldAccessor<Shape>) => UpdaterResult, opts?: {
35
+ readonly upsert?: boolean;
36
+ readonly returnDocument?: 'before' | 'after';
37
+ }): FindOneAndUpdateCommand;
38
+ }
39
+ /**
40
+ * Contract-free fluent Mongo collection builder. Produces the canonical
41
+ * `AggregateCommand` / `InsertOneCommand` / `FindOneAndUpdateCommand` command
42
+ * nodes without any contract coupling — parameterised by an explicit `DocShape`
43
+ * instead of a `MongoContract`.
44
+ *
45
+ * Mirrors SQL's `table(source, schema)` → `TableHandle` in spirit: a top-level
46
+ * entry point that exposes fluent query chains from which call sites never write
47
+ * `new MongoMatchStage(...)`, `MongoAndExpr.of([...])`, or `new AggregateCommand(...)`.
48
+ *
49
+ * ```ts
50
+ * const markerLedger = collection<MarkerLedgerDocShape>('_prisma_migrations');
51
+ *
52
+ * // aggregate
53
+ * markerLedger.aggregate().match(f => f._id.eq(space)).limit(1).build();
54
+ *
55
+ * // insertOne
56
+ * markerLedger.insertOne({ _id: space, space, storageHash });
57
+ *
58
+ * // findOneAndUpdate (CAS)
59
+ * markerLedger.match(f => f._id.eq(space)).match(f => f.storageHash.eq(expectedFrom))
60
+ * .findOneAndUpdate(f => [f.stage.set({ storageHash: newHash })], { upsert: false });
61
+ * ```
62
+ */
63
+ interface CollectionBuilder<Shape extends DocShape> {
64
+ aggregate(): AggregateChain<Shape>;
65
+ insertOne(document: Record<string, MongoValue>): InsertOneCommand;
66
+ match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape>;
67
+ }
68
+ /**
69
+ * Declare a contract-free collection builder parameterised by a `DocShape`.
70
+ * The collection name is bound once; field access reuses `createFieldAccessor`
71
+ * so the shape is typed at every call site without a contract.
72
+ *
73
+ * @param name The MongoDB collection name (e.g. `'_prisma_migrations'`)
74
+ */
75
+ declare function collection<Shape extends DocShape>(name: string): CollectionBuilder<Shape>;
76
+ //#endregion
77
+ export { AggregateChain, type CollectionBuilder, FilteredBuilder, collection };
78
+ //# sourceMappingURL=contract-free.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract-free.d.mts","names":[],"sources":["../../src/contract-free/collection.ts"],"mappings":";;;;;AAoCA;;;;;;AAAA,cAAa,cAAA,eAA6B,QAAA;EAAA;cAI5B,UAAA,UAAoB,MAAA,EAAQ,aAAA,CAAc,kBAAA;EAKtD,KAAA,CAAM,QAAA,GAAW,MAAA,EAAQ,aAAA,CAAc,KAAA,MAAW,eAAA,GAAkB,cAAA,CAAe,KAAA;EASnF,IAAA,CAAK,IAAA,EAAM,MAAA,mBAAyB,cAAA,CAAe,KAAA;EAInD,KAAA,CAAM,CAAA,WAAY,cAAA,CAAe,KAAA;EAIjC,KAAA,IAAS,gBAAA;AAAA;;;;;;;;;;;cAeE,eAAA,eAA8B,QAAA;EAAA;cAI7B,UAAA,UAAoB,OAAA,EAAS,aAAA,CAAc,eAAA;EAQvD,KAAA,CAAM,QAAA,GAAW,MAAA,EAAQ,aAAA,CAAc,KAAA,MAAW,eAAA,GAAkB,eAAA,CAAgB,KAAA;EAMpF,gBAAA,CACE,SAAA,GAAY,MAAA,EAAQ,aAAA,CAAc,KAAA,MAAW,aAAA,EAC7C,IAAA;IAAA,SAAiB,MAAA;IAAA,SAA2B,cAAA;EAAA,IAC3C,uBAAA;AAAA;;;;;;;;;;;;;;;AApCsB;AAe3B;;;;;;;;;UA6DiB,iBAAA,eAAgC,QAAA;EAC/C,SAAA,IAAa,cAAA,CAAe,KAAA;EAC5B,SAAA,CAAU,QAAA,EAAU,MAAA,SAAe,UAAA,IAAc,gBAAA;EACjD,KAAA,CAAM,QAAA,GAAW,MAAA,EAAQ,aAAA,CAAc,KAAA,MAAW,eAAA,GAAkB,eAAA,CAAgB,KAAA;AAAA;;;;;;;;iBAgCtE,UAAA,eAAyB,QAAA,EAAU,IAAA,WAAe,iBAAA,CAAkB,KAAA"}
@@ -0,0 +1,98 @@
1
+ import { r as resolveUpdaterResult, t as createFieldAccessor } from "../field-accessor-Dh8GUEe7.mjs";
2
+ import { AggregateCommand, FindOneAndUpdateCommand, InsertOneCommand, MongoAndExpr, MongoLimitStage, MongoMatchStage, MongoSortStage } from "@prisma-next/mongo-query-ast/execution";
3
+ //#region src/contract-free/collection.ts
4
+ /**
5
+ * Fold an array of filter expressions into a single `MongoFilterExpr`. Length-1
6
+ * short-circuits to avoid a redundant `$and` wrapper; the call site never writes
7
+ * `MongoAndExpr.of([...])` directly.
8
+ */
9
+ function foldFilters(filters) {
10
+ const first = filters[0];
11
+ if (first === void 0) throw new Error("foldFilters: invariant violated — empty filter list");
12
+ return filters.length === 1 ? first : MongoAndExpr.of(filters);
13
+ }
14
+ /**
15
+ * Fluent aggregate chain. Accumulates `$match` / `$sort` / `$limit` stages and
16
+ * produces an `AggregateCommand` via `.build()`.
17
+ *
18
+ * Instances are immutable — each stage method returns a new chain.
19
+ */
20
+ var AggregateChain = class AggregateChain {
21
+ #collection;
22
+ #stages;
23
+ constructor(collection, stages) {
24
+ this.#collection = collection;
25
+ this.#stages = stages;
26
+ }
27
+ match(filterFn) {
28
+ const filter = filterFn(createFieldAccessor());
29
+ return new AggregateChain(this.#collection, [...this.#stages, new MongoMatchStage(filter)]);
30
+ }
31
+ sort(spec) {
32
+ return new AggregateChain(this.#collection, [...this.#stages, new MongoSortStage(spec)]);
33
+ }
34
+ limit(n) {
35
+ return new AggregateChain(this.#collection, [...this.#stages, new MongoLimitStage(n)]);
36
+ }
37
+ build() {
38
+ return new AggregateCommand(this.#collection, this.#stages);
39
+ }
40
+ };
41
+ /**
42
+ * Fluent find-and-modify chain. Holds an accumulated list of filter expressions
43
+ * (AND-folded into the wire command's `filter` slot) and exposes
44
+ * `.findOneAndUpdate(...)` as the only terminal.
45
+ *
46
+ * Multiple `.match()` calls AND-fold internally — `MongoAndExpr.of` never
47
+ * appears at the call site.
48
+ *
49
+ * Instances are immutable — `.match()` returns a new `FilteredBuilder`.
50
+ */
51
+ var FilteredBuilder = class FilteredBuilder {
52
+ #collection;
53
+ #filters;
54
+ constructor(collection, filters) {
55
+ if (filters.length === 0) throw new Error("FilteredBuilder requires at least one filter");
56
+ this.#collection = collection;
57
+ this.#filters = filters;
58
+ }
59
+ match(filterFn) {
60
+ const filter = filterFn(createFieldAccessor());
61
+ return new FilteredBuilder(this.#collection, [...this.#filters, filter]);
62
+ }
63
+ findOneAndUpdate(updaterFn, opts = {}) {
64
+ const filter = foldFilters(this.#filters);
65
+ const update = resolveUpdaterResult(updaterFn(createFieldAccessor()));
66
+ return new FindOneAndUpdateCommand(this.#collection, filter, update, opts.upsert ?? false, void 0, opts.returnDocument ?? "after");
67
+ }
68
+ };
69
+ var CollectionBuilderImpl = class {
70
+ #name;
71
+ constructor(name) {
72
+ this.#name = name;
73
+ }
74
+ aggregate() {
75
+ return new AggregateChain(this.#name, []);
76
+ }
77
+ insertOne(document) {
78
+ return new InsertOneCommand(this.#name, document);
79
+ }
80
+ match(filterFn) {
81
+ const filter = filterFn(createFieldAccessor());
82
+ return new FilteredBuilder(this.#name, [filter]);
83
+ }
84
+ };
85
+ /**
86
+ * Declare a contract-free collection builder parameterised by a `DocShape`.
87
+ * The collection name is bound once; field access reuses `createFieldAccessor`
88
+ * so the shape is typed at every call site without a contract.
89
+ *
90
+ * @param name The MongoDB collection name (e.g. `'_prisma_migrations'`)
91
+ */
92
+ function collection(name) {
93
+ return new CollectionBuilderImpl(name);
94
+ }
95
+ //#endregion
96
+ export { AggregateChain, FilteredBuilder, collection };
97
+
98
+ //# sourceMappingURL=contract-free.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"contract-free.mjs","names":["#collection","#stages","#filters","#name"],"sources":["../../src/contract-free/collection.ts"],"sourcesContent":["import {\n AggregateCommand,\n FindOneAndUpdateCommand,\n InsertOneCommand,\n MongoAndExpr,\n type MongoFilterExpr,\n MongoLimitStage,\n MongoMatchStage,\n type MongoPipelineStage,\n MongoSortStage,\n type MongoUpdatePipelineStage,\n} from '@prisma-next/mongo-query-ast/execution';\nimport type { MongoValue } from '@prisma-next/mongo-value';\nimport { createFieldAccessor, type FieldAccessor } from '../field-accessor';\nimport type { DocShape } from '../types';\nimport { resolveUpdaterResult, type UpdaterResult } from '../update-ops';\n\n/**\n * Fold an array of filter expressions into a single `MongoFilterExpr`. Length-1\n * short-circuits to avoid a redundant `$and` wrapper; the call site never writes\n * `MongoAndExpr.of([...])` directly.\n */\nfunction foldFilters(filters: ReadonlyArray<MongoFilterExpr>): MongoFilterExpr {\n const first = filters[0];\n if (first === undefined) {\n throw new Error('foldFilters: invariant violated — empty filter list');\n }\n return filters.length === 1 ? first : MongoAndExpr.of(filters);\n}\n\n/**\n * Fluent aggregate chain. Accumulates `$match` / `$sort` / `$limit` stages and\n * produces an `AggregateCommand` via `.build()`.\n *\n * Instances are immutable — each stage method returns a new chain.\n */\nexport class AggregateChain<Shape extends DocShape> {\n readonly #collection: string;\n readonly #stages: ReadonlyArray<MongoPipelineStage>;\n\n constructor(collection: string, stages: ReadonlyArray<MongoPipelineStage>) {\n this.#collection = collection;\n this.#stages = stages;\n }\n\n match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): AggregateChain<Shape> {\n const f = createFieldAccessor<Shape>();\n const filter = filterFn(f);\n return new AggregateChain<Shape>(this.#collection, [\n ...this.#stages,\n new MongoMatchStage(filter),\n ]);\n }\n\n sort(spec: Record<string, 1 | -1>): AggregateChain<Shape> {\n return new AggregateChain<Shape>(this.#collection, [...this.#stages, new MongoSortStage(spec)]);\n }\n\n limit(n: number): AggregateChain<Shape> {\n return new AggregateChain<Shape>(this.#collection, [...this.#stages, new MongoLimitStage(n)]);\n }\n\n build(): AggregateCommand {\n return new AggregateCommand(this.#collection, this.#stages);\n }\n}\n\n/**\n * Fluent find-and-modify chain. Holds an accumulated list of filter expressions\n * (AND-folded into the wire command's `filter` slot) and exposes\n * `.findOneAndUpdate(...)` as the only terminal.\n *\n * Multiple `.match()` calls AND-fold internally — `MongoAndExpr.of` never\n * appears at the call site.\n *\n * Instances are immutable — `.match()` returns a new `FilteredBuilder`.\n */\nexport class FilteredBuilder<Shape extends DocShape> {\n readonly #collection: string;\n readonly #filters: ReadonlyArray<MongoFilterExpr>;\n\n constructor(collection: string, filters: ReadonlyArray<MongoFilterExpr>) {\n if (filters.length === 0) {\n throw new Error('FilteredBuilder requires at least one filter');\n }\n this.#collection = collection;\n this.#filters = filters;\n }\n\n match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape> {\n const f = createFieldAccessor<Shape>();\n const filter = filterFn(f);\n return new FilteredBuilder<Shape>(this.#collection, [...this.#filters, filter]);\n }\n\n findOneAndUpdate(\n updaterFn: (fields: FieldAccessor<Shape>) => UpdaterResult,\n opts: { readonly upsert?: boolean; readonly returnDocument?: 'before' | 'after' } = {},\n ): FindOneAndUpdateCommand {\n const filter = foldFilters(this.#filters);\n const f = createFieldAccessor<Shape>();\n const items = updaterFn(f);\n const update = resolveUpdaterResult(items);\n return new FindOneAndUpdateCommand(\n this.#collection,\n filter,\n update,\n opts.upsert ?? false,\n undefined,\n opts.returnDocument ?? 'after',\n );\n }\n}\n\n/**\n * Contract-free fluent Mongo collection builder. Produces the canonical\n * `AggregateCommand` / `InsertOneCommand` / `FindOneAndUpdateCommand` command\n * nodes without any contract coupling — parameterised by an explicit `DocShape`\n * instead of a `MongoContract`.\n *\n * Mirrors SQL's `table(source, schema)` → `TableHandle` in spirit: a top-level\n * entry point that exposes fluent query chains from which call sites never write\n * `new MongoMatchStage(...)`, `MongoAndExpr.of([...])`, or `new AggregateCommand(...)`.\n *\n * ```ts\n * const markerLedger = collection<MarkerLedgerDocShape>('_prisma_migrations');\n *\n * // aggregate\n * markerLedger.aggregate().match(f => f._id.eq(space)).limit(1).build();\n *\n * // insertOne\n * markerLedger.insertOne({ _id: space, space, storageHash });\n *\n * // findOneAndUpdate (CAS)\n * markerLedger.match(f => f._id.eq(space)).match(f => f.storageHash.eq(expectedFrom))\n * .findOneAndUpdate(f => [f.stage.set({ storageHash: newHash })], { upsert: false });\n * ```\n */\nexport interface CollectionBuilder<Shape extends DocShape> {\n aggregate(): AggregateChain<Shape>;\n insertOne(document: Record<string, MongoValue>): InsertOneCommand;\n match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape>;\n}\n\nclass CollectionBuilderImpl<Shape extends DocShape> implements CollectionBuilder<Shape> {\n readonly #name: string;\n\n constructor(name: string) {\n this.#name = name;\n }\n\n aggregate(): AggregateChain<Shape> {\n return new AggregateChain<Shape>(this.#name, []);\n }\n\n insertOne(document: Record<string, MongoValue>): InsertOneCommand {\n return new InsertOneCommand(this.#name, document);\n }\n\n match(filterFn: (fields: FieldAccessor<Shape>) => MongoFilterExpr): FilteredBuilder<Shape> {\n const f = createFieldAccessor<Shape>();\n const filter = filterFn(f);\n return new FilteredBuilder<Shape>(this.#name, [filter]);\n }\n}\n\n/**\n * Declare a contract-free collection builder parameterised by a `DocShape`.\n * The collection name is bound once; field access reuses `createFieldAccessor`\n * so the shape is typed at every call site without a contract.\n *\n * @param name The MongoDB collection name (e.g. `'_prisma_migrations'`)\n */\nexport function collection<Shape extends DocShape>(name: string): CollectionBuilder<Shape> {\n return new CollectionBuilderImpl<Shape>(name);\n}\n\nexport type { MongoUpdatePipelineStage };\n"],"mappings":";;;;;;;;AAsBA,SAAS,YAAY,SAA0D;CAC7E,MAAM,QAAQ,QAAQ;CACtB,IAAI,UAAU,KAAA,GACZ,MAAM,IAAI,MAAM,qDAAqD;CAEvE,OAAO,QAAQ,WAAW,IAAI,QAAQ,aAAa,GAAG,OAAO;AAC/D;;;;;;;AAQA,IAAa,iBAAb,MAAa,eAAuC;CAClD;CACA;CAEA,YAAY,YAAoB,QAA2C;EACzE,KAAKA,cAAc;EACnB,KAAKC,UAAU;CACjB;CAEA,MAAM,UAAoF;EAExF,MAAM,SAAS,SADL,oBACc,CAAC;EACzB,OAAO,IAAI,eAAsB,KAAKD,aAAa,CACjD,GAAG,KAAKC,SACR,IAAI,gBAAgB,MAAM,CAC5B,CAAC;CACH;CAEA,KAAK,MAAqD;EACxD,OAAO,IAAI,eA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