@prisma-next/target-mongo 0.4.0-dev.5 → 0.4.0-dev.7

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.
@@ -1,8 +1,9 @@
1
1
  import { a as DropCollectionCall, c as OpFactoryCallVisitor, i as CreateIndexCall, l as schemaCollectionToCreateCollectionOptions, n as CollModMeta, o as DropIndexCall, r as CreateCollectionCall, s as OpFactoryCall, t as CollModCall, u as schemaIndexToCreateIndexOptions } from "./op-factory-call-CfPGebEH.mjs";
2
2
  import { MongoSchemaIR } from "@prisma-next/mongo-schema-ir";
3
- import { MongoAndExpr, MongoDdlCommandVisitor, MongoExistsExpr, MongoExprFilter, MongoFieldFilter, MongoFilterExpr, MongoFilterVisitor, MongoInspectionCommandVisitor, MongoMigrationPlanOperation, MongoNotExpr, MongoOrExpr } from "@prisma-next/mongo-query-ast/control";
3
+ import { AnyMongoMigrationOperation, MongoAndExpr, MongoDdlCommandVisitor, MongoExistsExpr, MongoExprFilter, MongoFieldFilter, MongoFilterExpr, MongoFilterVisitor, MongoInspectionCommandVisitor, MongoMigrationPlanOperation, MongoNotExpr, MongoOrExpr } from "@prisma-next/mongo-query-ast/control";
4
+ import { Migration, MigrationMeta } from "@prisma-next/migration-tools/migration";
4
5
  import { MongoContract } from "@prisma-next/mongo-contract";
5
- import { MigrationOperationPolicy, MigrationPlan, MigrationPlanOperation, MigrationPlanner, MigrationPlannerConflict, MigrationPlannerResult, MigrationRunnerExecutionChecks, MigrationRunnerResult } from "@prisma-next/framework-components/control";
6
+ import { MigrationOperationPolicy, MigrationPlan, MigrationPlanOperation, MigrationPlanWithAuthoringSurface, MigrationPlanner, MigrationPlannerConflict, MigrationPlannerResult, MigrationRunnerExecutionChecks, MigrationRunnerResult, MigrationScaffoldContext } from "@prisma-next/framework-components/control";
6
7
  import { ContractMarkerRecord } from "@prisma-next/contract/types";
7
8
  import { Db } from "mongodb";
8
9
  import { TargetBoundComponentDescriptor } from "@prisma-next/framework-components/components";
@@ -65,8 +66,20 @@ declare class MongoMigrationPlanner implements MigrationPlanner<'mongo', 'mongo'
65
66
  readonly contract: unknown;
66
67
  readonly schema: unknown;
67
68
  readonly policy: MigrationOperationPolicy;
69
+ readonly fromHash: string;
68
70
  readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;
69
71
  }): MigrationPlannerResult;
72
+ /**
73
+ * Produce an empty `migration.ts` authoring surface for `migration new`.
74
+ *
75
+ * Mongo is a class-flow target, so the "empty migration" is a
76
+ * `PlannerProducedMongoMigration` with no operations; `renderTypeScript()`
77
+ * emits a stub class with the correct `from`/`to` metadata that the user
78
+ * then fills in with operations. The contract path on the context is
79
+ * unused — Mongo's emitted source does not import from the generated
80
+ * contract `.d.ts`.
81
+ */
82
+ emptyMigration(context: MigrationScaffoldContext): MigrationPlanWithAuthoringSurface;
70
83
  }
71
84
  //#endregion
72
85
  //#region src/core/mongo-runner.d.ts
@@ -111,6 +124,33 @@ declare class MongoMigrationRunner {
111
124
  private ensureMarkerCompatibility;
112
125
  }
113
126
  //#endregion
127
+ //#region src/core/planner-produced-migration.d.ts
128
+ /**
129
+ * Planner-produced Mongo migration, returned by `MongoMigrationPlanner.plan(...)`
130
+ * and `MongoMigrationPlanner.emptyMigration(...)`.
131
+ *
132
+ * Unlike user-authored migrations (which extend `MongoMigration` from
133
+ * `@prisma-next/family-mongo/migration`), this class lives inside the target
134
+ * and holds the richer authoring IR (`OpFactoryCall[]`) needed to render
135
+ * itself back to TypeScript source. It implements
136
+ * `MigrationPlanWithAuthoringSurface` so that the CLI can uniformly ask any
137
+ * planner result to serialize itself to a `migration.ts`.
138
+ *
139
+ * Extends the framework `Migration` base class directly (not
140
+ * `MongoMigration`) because `MongoMigration` lives in `@prisma-next/family-mongo`,
141
+ * which depends on this package — extending it here would create a dependency
142
+ * cycle.
143
+ */
144
+ declare class PlannerProducedMongoMigration extends Migration<AnyMongoMigrationOperation> implements MigrationPlanWithAuthoringSurface {
145
+ private readonly calls;
146
+ private readonly meta;
147
+ readonly targetId: "mongo";
148
+ constructor(calls: readonly OpFactoryCall[], meta: MigrationMeta);
149
+ get operations(): readonly AnyMongoMigrationOperation[];
150
+ describe(): MigrationMeta;
151
+ renderTypeScript(): string;
152
+ }
153
+ //#endregion
114
154
  //#region src/core/render-ops.d.ts
115
155
  declare function renderOps(calls: ReadonlyArray<OpFactoryCall>): MongoMigrationPlanOperation[];
116
156
  //#endregion
@@ -121,7 +161,16 @@ interface RenderMigrationMeta {
121
161
  readonly kind?: string;
122
162
  readonly labels?: readonly string[];
123
163
  }
124
- declare function renderTypeScript(calls: ReadonlyArray<OpFactoryCall>, meta?: RenderMigrationMeta): string;
164
+ /**
165
+ * Render a list of Mongo `OpFactoryCall`s as a class-flow `migration.ts`
166
+ * source string. The result is shebanged, extends the user-facing
167
+ * `Migration` (i.e. `MongoMigration`) from `@prisma-next/family-mongo`, and
168
+ * implements the abstract `operations` and `describe` members. `meta` is
169
+ * always rendered — `describe()` is part of the `Migration` contract, so
170
+ * even an empty stub must satisfy it; callers pass empty strings for a
171
+ * migration-new scaffold.
172
+ */
173
+ declare function renderCallsToTypeScript(calls: ReadonlyArray<OpFactoryCall>, meta: RenderMigrationMeta): string;
125
174
  //#endregion
126
- export { CollModCall, type CollModMeta, CreateCollectionCall, CreateIndexCall, DropCollectionCall, DropIndexCall, FilterEvaluator, type MarkerOperations, MongoMigrationPlanner, MongoMigrationRunner, type MongoRunnerDependencies, type OpFactoryCall, type OpFactoryCallVisitor, type PlanCallsResult, type RenderMigrationMeta, contractToMongoSchemaIR, deserializeMongoOp, deserializeMongoOps, formatMongoOperations, initMarker, readMarker, renderOps, renderTypeScript, schemaCollectionToCreateCollectionOptions, schemaIndexToCreateIndexOptions, serializeMongoOps, updateMarker, writeLedgerEntry };
175
+ export { CollModCall, type CollModMeta, CreateCollectionCall, CreateIndexCall, DropCollectionCall, DropIndexCall, FilterEvaluator, type MarkerOperations, MongoMigrationPlanner, MongoMigrationRunner, type MongoRunnerDependencies, type OpFactoryCall, type OpFactoryCallVisitor, type PlanCallsResult, PlannerProducedMongoMigration, type RenderMigrationMeta, contractToMongoSchemaIR, deserializeMongoOp, deserializeMongoOps, formatMongoOperations, initMarker, readMarker, renderCallsToTypeScript, renderOps, schemaCollectionToCreateCollectionOptions, schemaIndexToCreateIndexOptions, serializeMongoOps, updateMarker, writeLedgerEntry };
127
176
  //# sourceMappingURL=control.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"control.d.mts","names":[],"sources":["../src/core/contract-to-schema.ts","../src/core/ddl-formatter.ts","../src/core/filter-evaluator.ts","../src/core/marker-ledger.ts","../src/core/mongo-ops-serializer.ts","../src/core/mongo-planner.ts","../src/core/mongo-runner.ts","../src/core/render-ops.ts","../src/core/render-typescript.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;iBAoDgB,uBAAA,WAAkC,uBAAuB;;;iBC6CzD,qBAAA,sBAA2C;;;cC/C9C,eAAA,YAA2B;;mBAGrB,sBAAsB;cAK3B;YAKF;WAID;YAIC;eAIG;cAKD;AF5Bd;;;iBGrBsB,UAAA,KAAe,KAAK,QAAQ;iBAgB5B,UAAA,KAChB;;;IAEH;iBAcmB,YAAA,KAChB;;;AHbN,CAAA,CAAA,EGgBG,OHhBa,CAAA,OAAA,CAAA;iBGiCM,gBAAA,KAChB;;;EFWU,SAAA,EAAA,EAAA,MAAA;IETb;;;iBC0Ka,kBAAA,iBAAmC;iBAYnC,mBAAA,4BAA+C;iBAI/C,iBAAA,eAAgC;;;KChLpC,eAAA;;kBACoC;;;sBACI;;ALhDpC,cKkDH,qBAAA,YAAiC,gBLlD2B,CAAa,OAAA,EAAA,OAAA,CAAA,CAAA;;;;IC6CtE,SAAA,MAAA,EISK,wBJTsC;kCIUzB,cAAc;MAC1C;;IH1DO,SAAA,QAAgB,EAAA,OAAA;IAGV,SAAA,MAAA,EAAA,OAAA;IAAsB,SAAA,MAAA,EG2KpB,wBH3KoB;I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ources":["../src/core/contract-to-schema.ts","../src/core/ddl-formatter.ts","../src/core/filter-evaluator.ts","../src/core/marker-ledger.ts","../src/core/mongo-ops-serializer.ts","../src/core/mongo-planner.ts","../src/core/mongo-runner.ts","../src/core/planner-produced-migration.ts","../src/core/render-ops.ts","../src/core/render-typescript.ts"],"sourcesContent":[],"mappings":";;;;;;;;;;;iBAoDgB,uBAAA,WAAkC,uBAAuB;;;iBC6CzD,qBAAA,sBAA2C;;;cC/C9C,eAAA,YAA2B;;mBAGrB,sBAAsB;cAK3B;YAKF;WAID;YAIC;eAIG;cAKD;;;;iBCjDQ,UAAA,KAAe,KAAK,QAAQ;iBAgB5B,UAAA,KAChB;;;IAEH;iBAcmB,YAAA,KAChB;;;IAGH;AHhBa,iBGiCM,gBAAA,CHjCiB,EAAW,EGkC5C,EHlC4C,EAAA,KAAA,EAAA;;;;AC6ClD,CAAA,CAAA,EETG,OFSa,CAAA,IAAA,CAAA;;;iBGiKA,kBAAA,iBAAmC;iBAYnC,mBAAA,4BAA+C;iBAI/C,iBAAA,eAAgC;;;KC9KpC,eAAA;;kBACoC;;;sBACI;;cAEvC,qBAAA,YAAiC;ELpD9B,SAAA,CAAA,OAAA,EAAA;;;qBKwDK;IJXL,SAAA,mBAAqB,EIYH,aJZyB,CIYX,8BJZiC,CAAA,OAAA,EAAA,OAAA,CAAA,CAAA;MIa3E;;;IH5DO,SAAA,MAAgB,EAAA,OAAA;IAGV,SAAA,MAAA,EG6KE,wBH7KF;IAAsB,SAAA,QAAA,EAAA,MAAA;IAK3B,SAAA,mBAAA,EG0KoB,aH1KpB,CG0KkC,8BH1KlC,CAAA,OAAA,EAAA,OAAA,CAAA,CAAA;EAKF,CAAA,CAAA,EGsKN,sBHtKM;EAID;;;;;;;;;ACpCX;EAAqC,cAAA,CAAA,OAAA,EE6NX,wBF7NW,CAAA,EE6NgB,iCF7NhB;;;;UGXpB,gBAAA;gBACD,QAAQ;;;;MAIlB;;IN2BU,SAAA,WAAA,EAAuB,M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A,EAAA,MAAA;;;;iBM5CE,SAAA,QAAiB,cAAc,iBAAiB;;;UCzB/C,mBAAA;;;;;;;;;;ATyCjB;;;;AC6CA;iBQtEgB,uBAAA,QACP,cAAc,sBACf"}
package/dist/control.mjs CHANGED
@@ -3,6 +3,9 @@ import { MongoSchemaCollection, MongoSchemaCollectionOptions, MongoSchemaIR, Mon
3
3
  import { RawAggregateCommand, RawFindOneAndUpdateCommand, RawInsertOneCommand } from "@prisma-next/mongo-query-ast/execution";
4
4
  import { CollModCommand, CreateCollectionCommand, CreateIndexCommand, DropCollectionCommand, DropIndexCommand, ListCollectionsCommand, ListIndexesCommand, MongoAndExpr, MongoExistsExpr, MongoFieldFilter, MongoNotExpr, MongoOrExpr } from "@prisma-next/mongo-query-ast/control";
5
5
  import { type } from "arktype";
6
+ import { Migration } from "@prisma-next/migration-tools/migration";
7
+ import { ifDefined } from "@prisma-next/utils/defined";
8
+ import { detectScaffoldRuntime, shebangLineFor } from "@prisma-next/migration-tools/migration-ts";
6
9
  import { notOk, ok } from "@prisma-next/utils/result";
7
10
 
8
11
  //#region src/core/contract-to-schema.ts
@@ -600,6 +603,150 @@ function renderOps(calls) {
600
603
  return calls.map((call) => call.accept(renderVisitor));
601
604
  }
602
605
 
606
+ //#endregion
607
+ //#region src/core/render-typescript.ts
608
+ /**
609
+ * Render a list of Mongo `OpFactoryCall`s as a class-flow `migration.ts`
610
+ * source string. The result is shebanged, extends the user-facing
611
+ * `Migration` (i.e. `MongoMigration`) from `@prisma-next/family-mongo`, and
612
+ * implements the abstract `operations` and `describe` members. `meta` is
613
+ * always rendered — `describe()` is part of the `Migration` contract, so
614
+ * even an empty stub must satisfy it; callers pass empty strings for a
615
+ * migration-new scaffold.
616
+ */
617
+ function renderCallsToTypeScript(calls, meta) {
618
+ const imports = buildImports(collectFactoryNames(calls));
619
+ const operationsBody = calls.map((c) => c.accept(renderCallVisitor)).join(",\n");
620
+ return [
621
+ shebangLineFor(detectScaffoldRuntime()),
622
+ imports,
623
+ "",
624
+ "class M extends Migration {",
625
+ buildDescribeMethod(meta),
626
+ " override get operations() {",
627
+ " return [",
628
+ indent(operationsBody, 6),
629
+ " ];",
630
+ " }",
631
+ "}",
632
+ "",
633
+ "export default M;",
634
+ "Migration.run(import.meta.url, M);",
635
+ ""
636
+ ].join("\n");
637
+ }
638
+ function collectFactoryNames(calls) {
639
+ const names = /* @__PURE__ */ new Set();
640
+ for (const call of calls) names.add(call.factory);
641
+ return [...names].sort();
642
+ }
643
+ function buildImports(factoryNames) {
644
+ const lines = ["import { Migration } from '@prisma-next/family-mongo/migration';"];
645
+ if (factoryNames.length > 0) lines.push(`import { ${factoryNames.join(", ")} } from '@prisma-next/target-mongo/migration';`);
646
+ return lines.join("\n");
647
+ }
648
+ function buildDescribeMethod(meta) {
649
+ const lines = [];
650
+ lines.push(" override describe() {");
651
+ lines.push(" return {");
652
+ lines.push(` from: ${JSON.stringify(meta.from)},`);
653
+ lines.push(` to: ${JSON.stringify(meta.to)},`);
654
+ if (meta.kind) lines.push(` kind: ${JSON.stringify(meta.kind)},`);
655
+ if (meta.labels && meta.labels.length > 0) lines.push(` labels: ${renderLiteral(meta.labels)},`);
656
+ lines.push(" };");
657
+ lines.push(" }");
658
+ lines.push("");
659
+ return lines.join("\n");
660
+ }
661
+ const renderCallVisitor = {
662
+ createIndex(call) {
663
+ return call.options ? `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)}, ${renderLiteral(call.options)})` : `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;
664
+ },
665
+ dropIndex(call) {
666
+ return `dropIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;
667
+ },
668
+ createCollection(call) {
669
+ return call.options ? `createCollection(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})` : `createCollection(${renderLiteral(call.collection)})`;
670
+ },
671
+ dropCollection(call) {
672
+ return `dropCollection(${renderLiteral(call.collection)})`;
673
+ },
674
+ collMod(call) {
675
+ return call.meta ? `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)}, ${renderLiteral(call.meta)})` : `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})`;
676
+ }
677
+ };
678
+ function renderLiteral(value) {
679
+ if (value === void 0) return "undefined";
680
+ if (value === null) return "null";
681
+ if (typeof value === "string") return JSON.stringify(value);
682
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
683
+ if (Array.isArray(value)) {
684
+ if (value.length === 0) return "[]";
685
+ const items = value.map((v) => renderLiteral(v));
686
+ const singleLine = `[${items.join(", ")}]`;
687
+ if (singleLine.length <= 80) return singleLine;
688
+ return `[\n${items.map((i) => ` ${i}`).join(",\n")},\n]`;
689
+ }
690
+ if (typeof value === "object") {
691
+ const entries = Object.entries(value).filter(([, v]) => v !== void 0);
692
+ if (entries.length === 0) return "{}";
693
+ const items = entries.map(([k, v]) => `${renderKey(k)}: ${renderLiteral(v)}`);
694
+ const singleLine = `{ ${items.join(", ")} }`;
695
+ if (singleLine.length <= 80) return singleLine;
696
+ return `{\n${items.map((i) => ` ${i}`).join(",\n")},\n}`;
697
+ }
698
+ return String(value);
699
+ }
700
+ function renderKey(key) {
701
+ if (key === "__proto__") return JSON.stringify(key);
702
+ return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
703
+ }
704
+ function indent(text, spaces) {
705
+ const pad = " ".repeat(spaces);
706
+ return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : line).join("\n");
707
+ }
708
+
709
+ //#endregion
710
+ //#region src/core/planner-produced-migration.ts
711
+ /**
712
+ * Planner-produced Mongo migration, returned by `MongoMigrationPlanner.plan(...)`
713
+ * and `MongoMigrationPlanner.emptyMigration(...)`.
714
+ *
715
+ * Unlike user-authored migrations (which extend `MongoMigration` from
716
+ * `@prisma-next/family-mongo/migration`), this class lives inside the target
717
+ * and holds the richer authoring IR (`OpFactoryCall[]`) needed to render
718
+ * itself back to TypeScript source. It implements
719
+ * `MigrationPlanWithAuthoringSurface` so that the CLI can uniformly ask any
720
+ * planner result to serialize itself to a `migration.ts`.
721
+ *
722
+ * Extends the framework `Migration` base class directly (not
723
+ * `MongoMigration`) because `MongoMigration` lives in `@prisma-next/family-mongo`,
724
+ * which depends on this package — extending it here would create a dependency
725
+ * cycle.
726
+ */
727
+ var PlannerProducedMongoMigration = class extends Migration {
728
+ targetId = "mongo";
729
+ constructor(calls, meta) {
730
+ super();
731
+ this.calls = calls;
732
+ this.meta = meta;
733
+ }
734
+ get operations() {
735
+ return renderOps(this.calls);
736
+ }
737
+ describe() {
738
+ return this.meta;
739
+ }
740
+ renderTypeScript() {
741
+ return renderCallsToTypeScript(this.calls, {
742
+ from: this.meta.from,
743
+ to: this.meta.to,
744
+ ...ifDefined("kind", this.meta.kind),
745
+ ...ifDefined("labels", this.meta.labels)
746
+ });
747
+ }
748
+ };
749
+
603
750
  //#endregion
604
751
  //#region src/core/mongo-planner.ts
605
752
  function buildIndexLookupKey(index) {
@@ -715,13 +862,28 @@ var MongoMigrationPlanner = class {
715
862
  if (result.kind === "failure") return result;
716
863
  return {
717
864
  kind: "success",
718
- plan: {
719
- targetId: "mongo",
720
- destination: { storageHash: contract.storage.storageHash },
721
- operations: renderOps(result.calls)
722
- }
865
+ plan: new PlannerProducedMongoMigration(result.calls, {
866
+ from: options.fromHash,
867
+ to: contract.storage.storageHash
868
+ })
723
869
  };
724
870
  }
871
+ /**
872
+ * Produce an empty `migration.ts` authoring surface for `migration new`.
873
+ *
874
+ * Mongo is a class-flow target, so the "empty migration" is a
875
+ * `PlannerProducedMongoMigration` with no operations; `renderTypeScript()`
876
+ * emits a stub class with the correct `from`/`to` metadata that the user
877
+ * then fills in with operations. The contract path on the context is
878
+ * unused — Mongo's emitted source does not import from the generated
879
+ * contract `.d.ts`.
880
+ */
881
+ emptyMigration(context) {
882
+ return new PlannerProducedMongoMigration([], {
883
+ from: context.fromHash,
884
+ to: context.toHash
885
+ });
886
+ }
725
887
  };
726
888
  function planValidatorDiffCall(collName, originValidator, destValidator) {
727
889
  if (validatorsEqual(originValidator, destValidator)) return void 0;
@@ -869,98 +1031,5 @@ var MongoMigrationRunner = class {
869
1031
  };
870
1032
 
871
1033
  //#endregion
872
- //#region src/core/render-typescript.ts
873
- function renderTypeScript(calls, meta) {
874
- const imports = buildImports(collectFactoryNames(calls));
875
- const planBody = calls.map((c) => c.accept(renderCallVisitor)).join(",\n");
876
- return [
877
- imports,
878
- "",
879
- "class M extends Migration {",
880
- meta ? buildDescribeMethod(meta) : "",
881
- " override plan() {",
882
- " return [",
883
- indent(planBody, 6),
884
- " ];",
885
- " }",
886
- "}",
887
- "",
888
- "export default M;",
889
- "Migration.run(import.meta.url, M);",
890
- ""
891
- ].join("\n");
892
- }
893
- function collectFactoryNames(calls) {
894
- const names = /* @__PURE__ */ new Set();
895
- for (const call of calls) names.add(call.factory);
896
- return [...names].sort();
897
- }
898
- function buildImports(factoryNames) {
899
- const lines = ["import { Migration } from '@prisma-next/family-mongo/migration';"];
900
- if (factoryNames.length > 0) lines.push(`import { ${factoryNames.join(", ")} } from '@prisma-next/target-mongo/migration';`);
901
- return lines.join("\n");
902
- }
903
- function buildDescribeMethod(meta) {
904
- const lines = [];
905
- lines.push(" override describe() {");
906
- lines.push(" return {");
907
- lines.push(` from: ${JSON.stringify(meta.from)},`);
908
- lines.push(` to: ${JSON.stringify(meta.to)},`);
909
- if (meta.kind) lines.push(` kind: ${JSON.stringify(meta.kind)},`);
910
- if (meta.labels && meta.labels.length > 0) lines.push(` labels: ${renderLiteral(meta.labels)},`);
911
- lines.push(" };");
912
- lines.push(" }");
913
- lines.push("");
914
- return lines.join("\n");
915
- }
916
- const renderCallVisitor = {
917
- createIndex(call) {
918
- return call.options ? `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)}, ${renderLiteral(call.options)})` : `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;
919
- },
920
- dropIndex(call) {
921
- return `dropIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;
922
- },
923
- createCollection(call) {
924
- return call.options ? `createCollection(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})` : `createCollection(${renderLiteral(call.collection)})`;
925
- },
926
- dropCollection(call) {
927
- return `dropCollection(${renderLiteral(call.collection)})`;
928
- },
929
- collMod(call) {
930
- return call.meta ? `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)}, ${renderLiteral(call.meta)})` : `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})`;
931
- }
932
- };
933
- function renderLiteral(value) {
934
- if (value === void 0) return "undefined";
935
- if (value === null) return "null";
936
- if (typeof value === "string") return JSON.stringify(value);
937
- if (typeof value === "number" || typeof value === "boolean") return String(value);
938
- if (Array.isArray(value)) {
939
- if (value.length === 0) return "[]";
940
- const items = value.map((v) => renderLiteral(v));
941
- const singleLine = `[${items.join(", ")}]`;
942
- if (singleLine.length <= 80) return singleLine;
943
- return `[\n${items.map((i) => ` ${i}`).join(",\n")},\n]`;
944
- }
945
- if (typeof value === "object") {
946
- const entries = Object.entries(value).filter(([, v]) => v !== void 0);
947
- if (entries.length === 0) return "{}";
948
- const items = entries.map(([k, v]) => `${renderKey(k)}: ${renderLiteral(v)}`);
949
- const singleLine = `{ ${items.join(", ")} }`;
950
- if (singleLine.length <= 80) return singleLine;
951
- return `{\n${items.map((i) => ` ${i}`).join(",\n")},\n}`;
952
- }
953
- return String(value);
954
- }
955
- function renderKey(key) {
956
- if (key === "__proto__") return JSON.stringify(key);
957
- return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);
958
- }
959
- function indent(text, spaces) {
960
- const pad = " ".repeat(spaces);
961
- return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : line).join("\n");
962
- }
963
-
964
- //#endregion
965
- export { CollModCall, CreateCollectionCall, CreateIndexCall, DropCollectionCall, DropIndexCall, FilterEvaluator, MongoMigrationPlanner, MongoMigrationRunner, contractToMongoSchemaIR, deserializeMongoOp, deserializeMongoOps, formatMongoOperations, initMarker, readMarker, renderOps, renderTypeScript, schemaCollectionToCreateCollectionOptions, schemaIndexToCreateIndexOptions, serializeMongoOps, updateMarker, writeLedgerEntry };
1034
+ export { CollModCall, CreateCollectionCall, CreateIndexCall, DropCollectionCall, DropIndexCall, FilterEvaluator, MongoMigrationPlanner, MongoMigrationRunner, PlannerProducedMongoMigration, contractToMongoSchemaIR, deserializeMongoOp, deserializeMongoOps, formatMongoOperations, initMarker, readMarker, renderCallsToTypeScript, renderOps, schemaCollectionToCreateCollectionOptions, schemaIndexToCreateIndexOptions, serializeMongoOps, updateMarker, writeLedgerEntry };
966
1035
  //# sourceMappingURL=control.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"control.mjs","names":["parts: string[]","statements: string[]","current: unknown","opts: MongoSchemaCollectionOptions | undefined","validator: MongoSchemaValidator | undefined","renderVisitor: OpFactoryCallVisitor<MongoMigrationPlanOperation>","collCreates: OpFactoryCall[]","drops: OpFactoryCall[]","creates: OpFactoryCall[]","validatorOps: OpFactoryCall[]","mutableOptionOps: OpFactoryCall[]","collDrops: OpFactoryCall[]","conflicts: MigrationPlannerConflict[]","operationClass: MigrationOperationClass","deps: MongoRunnerDependencies","lines: string[]","renderCallVisitor: OpFactoryCallVisitor<string>"],"sources":["../src/core/contract-to-schema.ts","../src/core/ddl-formatter.ts","../src/core/filter-evaluator.ts","../src/core/marker-ledger.ts","../src/core/mongo-ops-serializer.ts","../src/core/op-factory-call.ts","../src/core/render-ops.ts","../src/core/mongo-planner.ts","../src/core/mongo-runner.ts","../src/core/render-typescript.ts"],"sourcesContent":["import type {\n MongoContract,\n MongoStorageCollection,\n MongoStorageCollectionOptions,\n MongoStorageIndex,\n MongoStorageValidator,\n} from '@prisma-next/mongo-contract';\nimport {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaIR,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\n\nfunction convertIndex(index: MongoStorageIndex): MongoSchemaIndex {\n return new MongoSchemaIndex({\n keys: index.keys,\n unique: index.unique,\n sparse: index.sparse,\n expireAfterSeconds: index.expireAfterSeconds,\n partialFilterExpression: index.partialFilterExpression,\n wildcardProjection: index.wildcardProjection,\n collation: index.collation,\n weights: index.weights,\n default_language: index.default_language,\n language_override: index.language_override,\n });\n}\n\nfunction convertValidator(v: MongoStorageValidator): MongoSchemaValidator {\n return new MongoSchemaValidator({\n jsonSchema: v.jsonSchema,\n validationLevel: v.validationLevel,\n validationAction: v.validationAction,\n });\n}\n\nfunction convertOptions(o: MongoStorageCollectionOptions): MongoSchemaCollectionOptions {\n return new MongoSchemaCollectionOptions(o);\n}\n\nfunction convertCollection(name: string, def: MongoStorageCollection): MongoSchemaCollection {\n const indexes = (def.indexes ?? []).map(convertIndex);\n return new MongoSchemaCollection({\n name,\n indexes,\n ...(def.validator != null && { validator: convertValidator(def.validator) }),\n ...(def.options != null && { options: convertOptions(def.options) }),\n });\n}\n\nexport function contractToMongoSchemaIR(contract: MongoContract | null): MongoSchemaIR {\n if (!contract) {\n return new MongoSchemaIR([]);\n }\n\n const collections = Object.entries(contract.storage.collections).map(([name, def]) =>\n convertCollection(name, def),\n );\n\n return new MongoSchemaIR(collections);\n}\n","import type { MigrationPlanOperation } from '@prisma-next/framework-components/control';\nimport type {\n CollModCommand,\n CreateCollectionCommand,\n CreateIndexCommand,\n DropCollectionCommand,\n DropIndexCommand,\n MongoDdlCommandVisitor,\n MongoIndexKey,\n} from '@prisma-next/mongo-query-ast/control';\n\nfunction formatKeySpec(keys: ReadonlyArray<MongoIndexKey>): string {\n const entries = keys.map((k) => `${JSON.stringify(k.field)}: ${JSON.stringify(k.direction)}`);\n return `{ ${entries.join(', ')} }`;\n}\n\nfunction formatOptions(cmd: CreateIndexCommand): string | undefined {\n const parts: string[] = [];\n if (cmd.unique) parts.push('unique: true');\n if (cmd.sparse) parts.push('sparse: true');\n if (cmd.expireAfterSeconds !== undefined)\n parts.push(`expireAfterSeconds: ${cmd.expireAfterSeconds}`);\n if (cmd.name) parts.push(`name: ${JSON.stringify(cmd.name)}`);\n if (cmd.collation) parts.push(`collation: ${JSON.stringify(cmd.collation)}`);\n if (cmd.weights) parts.push(`weights: ${JSON.stringify(cmd.weights)}`);\n if (cmd.default_language) parts.push(`default_language: ${JSON.stringify(cmd.default_language)}`);\n if (cmd.language_override)\n parts.push(`language_override: ${JSON.stringify(cmd.language_override)}`);\n if (cmd.wildcardProjection)\n parts.push(`wildcardProjection: ${JSON.stringify(cmd.wildcardProjection)}`);\n if (cmd.partialFilterExpression)\n parts.push(`partialFilterExpression: ${JSON.stringify(cmd.partialFilterExpression)}`);\n if (parts.length === 0) return undefined;\n return `{ ${parts.join(', ')} }`;\n}\n\nfunction formatCreateCollectionOptions(cmd: CreateCollectionCommand): string | undefined {\n const parts: string[] = [];\n if (cmd.capped) parts.push('capped: true');\n if (cmd.size !== undefined) parts.push(`size: ${cmd.size}`);\n if (cmd.max !== undefined) parts.push(`max: ${cmd.max}`);\n if (cmd.timeseries) parts.push(`timeseries: ${JSON.stringify(cmd.timeseries)}`);\n if (cmd.collation) parts.push(`collation: ${JSON.stringify(cmd.collation)}`);\n if (cmd.clusteredIndex) parts.push(`clusteredIndex: ${JSON.stringify(cmd.clusteredIndex)}`);\n if (cmd.validator) parts.push(`validator: ${JSON.stringify(cmd.validator)}`);\n if (cmd.validationLevel) parts.push(`validationLevel: ${JSON.stringify(cmd.validationLevel)}`);\n if (cmd.validationAction) parts.push(`validationAction: ${JSON.stringify(cmd.validationAction)}`);\n if (cmd.changeStreamPreAndPostImages)\n parts.push(`changeStreamPreAndPostImages: ${JSON.stringify(cmd.changeStreamPreAndPostImages)}`);\n if (parts.length === 0) return undefined;\n return `{ ${parts.join(', ')} }`;\n}\n\nclass MongoDdlCommandFormatter implements MongoDdlCommandVisitor<string> {\n createIndex(cmd: CreateIndexCommand): string {\n const keySpec = formatKeySpec(cmd.keys);\n const opts = formatOptions(cmd);\n return opts\n ? `db.${cmd.collection}.createIndex(${keySpec}, ${opts})`\n : `db.${cmd.collection}.createIndex(${keySpec})`;\n }\n\n dropIndex(cmd: DropIndexCommand): string {\n return `db.${cmd.collection}.dropIndex(${JSON.stringify(cmd.name)})`;\n }\n\n createCollection(cmd: CreateCollectionCommand): string {\n const opts = formatCreateCollectionOptions(cmd);\n return opts\n ? `db.createCollection(${JSON.stringify(cmd.collection)}, ${opts})`\n : `db.createCollection(${JSON.stringify(cmd.collection)})`;\n }\n\n dropCollection(cmd: DropCollectionCommand): string {\n return `db.${cmd.collection}.drop()`;\n }\n\n collMod(cmd: CollModCommand): string {\n const parts: string[] = [`collMod: ${JSON.stringify(cmd.collection)}`];\n if (cmd.validator) parts.push(`validator: ${JSON.stringify(cmd.validator)}`);\n if (cmd.validationLevel) parts.push(`validationLevel: ${JSON.stringify(cmd.validationLevel)}`);\n if (cmd.validationAction)\n parts.push(`validationAction: ${JSON.stringify(cmd.validationAction)}`);\n if (cmd.changeStreamPreAndPostImages)\n parts.push(\n `changeStreamPreAndPostImages: ${JSON.stringify(cmd.changeStreamPreAndPostImages)}`,\n );\n return `db.runCommand({ ${parts.join(', ')} })`;\n }\n}\n\nconst formatter = new MongoDdlCommandFormatter();\n\ninterface MongoExecuteStep {\n readonly command: { readonly accept: <R>(visitor: MongoDdlCommandVisitor<R>) => R };\n}\n\nexport function formatMongoOperations(operations: readonly MigrationPlanOperation[]): string[] {\n const statements: string[] = [];\n for (const operation of operations) {\n const candidate = operation as unknown as Record<string, unknown>;\n if (!('execute' in candidate) || !Array.isArray(candidate['execute'])) {\n continue;\n }\n for (const step of candidate['execute'] as MongoExecuteStep[]) {\n if (step.command && typeof step.command.accept === 'function') {\n statements.push(step.command.accept(formatter));\n }\n }\n }\n return statements;\n}\n","import type {\n MongoAndExpr,\n MongoExistsExpr,\n MongoExprFilter,\n MongoFieldFilter,\n MongoFilterExpr,\n MongoFilterVisitor,\n MongoNotExpr,\n MongoOrExpr,\n} from '@prisma-next/mongo-query-ast/control';\nimport { deepEqual } from '@prisma-next/mongo-schema-ir';\nimport type { MongoValue } from '@prisma-next/mongo-value';\n\nfunction getNestedField(doc: Record<string, unknown>, path: string): unknown {\n const parts = path.split('.');\n let current: unknown = doc;\n for (const part of parts) {\n if (current === null || current === undefined || typeof current !== 'object') {\n return undefined;\n }\n const record = current as Record<string, unknown>;\n if (!Object.hasOwn(record, part)) {\n return undefined;\n }\n current = record[part];\n }\n return current;\n}\n\nfunction evaluateFieldOp(op: string, actual: unknown, expected: MongoValue): boolean {\n switch (op) {\n case '$eq':\n return deepEqual(actual, expected);\n case '$ne':\n return !deepEqual(actual, expected);\n case '$gt':\n return typeof actual === typeof expected && (actual as number) > (expected as number);\n case '$gte':\n return typeof actual === typeof expected && (actual as number) >= (expected as number);\n case '$lt':\n return typeof actual === typeof expected && (actual as number) < (expected as number);\n case '$lte':\n return typeof actual === typeof expected && (actual as number) <= (expected as number);\n case '$in':\n return Array.isArray(expected) && expected.some((v) => deepEqual(actual, v));\n default:\n throw new Error(`Unsupported filter operator in migration check: ${op}`);\n }\n}\n\nexport class FilterEvaluator implements MongoFilterVisitor<boolean> {\n private doc: Record<string, unknown> = {};\n\n evaluate(filter: MongoFilterExpr, doc: Record<string, unknown>): boolean {\n this.doc = doc;\n return filter.accept(this);\n }\n\n field(expr: MongoFieldFilter): boolean {\n const value = getNestedField(this.doc, expr.field);\n return evaluateFieldOp(expr.op, value, expr.value);\n }\n\n and(expr: MongoAndExpr): boolean {\n return expr.exprs.every((child) => child.accept(this));\n }\n\n or(expr: MongoOrExpr): boolean {\n return expr.exprs.some((child) => child.accept(this));\n }\n\n not(expr: MongoNotExpr): boolean {\n return !expr.expr.accept(this);\n }\n\n exists(expr: MongoExistsExpr): boolean {\n const has = getNestedField(this.doc, expr.field) !== undefined;\n return expr.exists ? has : !has;\n }\n\n expr(_expr: MongoExprFilter): boolean {\n throw new Error('Aggregation expression filters are not supported in migration checks');\n }\n}\n","import type { ContractMarkerRecord } from '@prisma-next/contract/types';\nimport {\n RawAggregateCommand,\n RawFindOneAndUpdateCommand,\n RawInsertOneCommand,\n} from '@prisma-next/mongo-query-ast/execution';\nimport type { Db, Document } from 'mongodb';\n\nconst COLLECTION = '_prisma_migrations';\nconst MARKER_ID = 'marker';\n\nasync function executeAggregate(db: Db, cmd: RawAggregateCommand): Promise<Document[]> {\n return db\n .collection(cmd.collection)\n .aggregate(cmd.pipeline as Record<string, unknown>[])\n .toArray();\n}\n\nasync function executeInsertOne(db: Db, cmd: RawInsertOneCommand): Promise<void> {\n await db.collection(cmd.collection).insertOne(cmd.document);\n}\n\nasync function executeFindOneAndUpdate(\n db: Db,\n cmd: RawFindOneAndUpdateCommand,\n): Promise<Document | null> {\n return db\n .collection(cmd.collection)\n .findOneAndUpdate(cmd.filter, cmd.update as Record<string, unknown>, { upsert: cmd.upsert });\n}\n\nexport async function readMarker(db: Db): Promise<ContractMarkerRecord | null> {\n const cmd = new RawAggregateCommand(COLLECTION, [{ $match: { _id: MARKER_ID } }, { $limit: 1 }]);\n const docs = await executeAggregate(db, cmd);\n const doc = docs[0];\n if (!doc) return null;\n return {\n storageHash: doc['storageHash'] as string,\n profileHash: doc['profileHash'] as string,\n contractJson: (doc['contractJson'] as unknown) ?? null,\n canonicalVersion: (doc['canonicalVersion'] as number) ?? null,\n updatedAt: doc['updatedAt'] as Date,\n appTag: (doc['appTag'] as string) ?? null,\n meta: (doc['meta'] as Record<string, unknown>) ?? {},\n };\n}\n\nexport async function initMarker(\n db: Db,\n destination: { readonly storageHash: string; readonly profileHash: string },\n): Promise<void> {\n const cmd = new RawInsertOneCommand(COLLECTION, {\n _id: MARKER_ID,\n storageHash: destination.storageHash,\n profileHash: destination.profileHash,\n contractJson: null,\n canonicalVersion: null,\n updatedAt: new Date(),\n appTag: null,\n meta: {},\n });\n await executeInsertOne(db, cmd);\n}\n\nexport async function updateMarker(\n db: Db,\n expectedFrom: string,\n destination: { readonly storageHash: string; readonly profileHash: string },\n): Promise<boolean> {\n const cmd = new RawFindOneAndUpdateCommand(\n COLLECTION,\n { _id: MARKER_ID, storageHash: expectedFrom },\n {\n $set: {\n storageHash: destination.storageHash,\n profileHash: destination.profileHash,\n updatedAt: new Date(),\n },\n },\n false,\n );\n const result = await executeFindOneAndUpdate(db, cmd);\n return result !== null;\n}\n\nexport async function writeLedgerEntry(\n db: Db,\n entry: { readonly edgeId: string; readonly from: string; readonly to: string },\n): Promise<void> {\n const cmd = new RawInsertOneCommand(COLLECTION, {\n type: 'ledger',\n edgeId: entry.edgeId,\n from: entry.from,\n to: entry.to,\n appliedAt: new Date(),\n });\n await executeInsertOne(db, cmd);\n}\n","import type { MigrationOperationClass } from '@prisma-next/framework-components/control';\nimport {\n type AnyMongoDdlCommand,\n type AnyMongoInspectionCommand,\n CollModCommand,\n CreateCollectionCommand,\n CreateIndexCommand,\n DropCollectionCommand,\n DropIndexCommand,\n ListCollectionsCommand,\n ListIndexesCommand,\n MongoAndExpr,\n MongoExistsExpr,\n MongoFieldFilter,\n type MongoFilterExpr,\n type MongoMigrationCheck,\n type MongoMigrationPlanOperation,\n type MongoMigrationStep,\n MongoNotExpr,\n MongoOrExpr,\n} from '@prisma-next/mongo-query-ast/control';\nimport { type } from 'arktype';\n\nconst IndexKeyDirection = type('1 | -1 | \"text\" | \"2dsphere\" | \"2d\" | \"hashed\"');\nconst IndexKeyJson = type({ field: 'string', direction: IndexKeyDirection });\n\nconst CreateIndexJson = type({\n kind: '\"createIndex\"',\n collection: 'string',\n keys: IndexKeyJson.array().atLeastLength(1),\n 'unique?': 'boolean',\n 'sparse?': 'boolean',\n 'expireAfterSeconds?': 'number',\n 'partialFilterExpression?': 'Record<string, unknown>',\n 'name?': 'string',\n 'wildcardProjection?': 'Record<string, unknown>',\n 'collation?': 'Record<string, unknown>',\n 'weights?': 'Record<string, unknown>',\n 'default_language?': 'string',\n 'language_override?': 'string',\n});\n\nconst DropIndexJson = type({\n kind: '\"dropIndex\"',\n collection: 'string',\n name: 'string',\n});\n\nconst CreateCollectionJson = type({\n kind: '\"createCollection\"',\n collection: 'string',\n 'validator?': 'Record<string, unknown>',\n 'validationLevel?': '\"strict\" | \"moderate\"',\n 'validationAction?': '\"error\" | \"warn\"',\n 'capped?': 'boolean',\n 'size?': 'number',\n 'max?': 'number',\n 'timeseries?': 'Record<string, unknown>',\n 'collation?': 'Record<string, unknown>',\n 'changeStreamPreAndPostImages?': 'Record<string, unknown>',\n 'clusteredIndex?': 'Record<string, unknown>',\n});\n\nconst DropCollectionJson = type({\n kind: '\"dropCollection\"',\n collection: 'string',\n});\n\nconst CollModJson = type({\n kind: '\"collMod\"',\n collection: 'string',\n 'validator?': 'Record<string, unknown>',\n 'validationLevel?': '\"strict\" | \"moderate\"',\n 'validationAction?': '\"error\" | \"warn\"',\n 'changeStreamPreAndPostImages?': 'Record<string, unknown>',\n});\n\nconst ListIndexesJson = type({\n kind: '\"listIndexes\"',\n collection: 'string',\n});\n\nconst ListCollectionsJson = type({\n kind: '\"listCollections\"',\n});\n\nconst FieldFilterJson = type({\n kind: '\"field\"',\n field: 'string',\n op: 'string',\n value: 'unknown',\n});\n\nconst ExistsFilterJson = type({\n kind: '\"exists\"',\n field: 'string',\n exists: 'boolean',\n});\n\nconst CheckJson = type({\n description: 'string',\n source: 'Record<string, unknown>',\n filter: 'Record<string, unknown>',\n expect: '\"exists\" | \"notExists\"',\n});\n\nconst StepJson = type({\n description: 'string',\n command: 'Record<string, unknown>',\n});\n\nconst OperationJson = type({\n id: 'string',\n label: 'string',\n operationClass: '\"additive\" | \"widening\" | \"destructive\"',\n precheck: 'Record<string, unknown>[]',\n execute: 'Record<string, unknown>[]',\n postcheck: 'Record<string, unknown>[]',\n});\n\nfunction validate<T>(schema: { assert: (data: unknown) => T }, data: unknown, context: string): T {\n try {\n return schema.assert(data);\n } catch (error) {\n /* v8 ignore start -- assertion libraries always throw Error instances */\n const message = error instanceof Error ? error.message : String(error);\n /* v8 ignore stop */\n throw new Error(`Invalid ${context}: ${message}`);\n }\n}\n\nfunction deserializeFilterExpr(json: unknown): MongoFilterExpr {\n const record = json as Record<string, unknown>;\n const kind = record['kind'] as string;\n switch (kind) {\n case 'field': {\n const data = validate(FieldFilterJson, json, 'field filter');\n return MongoFieldFilter.of(data.field, data.op, data.value as never);\n }\n case 'and': {\n const exprs = record['exprs'];\n if (!Array.isArray(exprs)) throw new Error('Invalid and filter: missing exprs array');\n return MongoAndExpr.of(exprs.map(deserializeFilterExpr));\n }\n case 'or': {\n const exprs = record['exprs'];\n if (!Array.isArray(exprs)) throw new Error('Invalid or filter: missing exprs array');\n return MongoOrExpr.of(exprs.map(deserializeFilterExpr));\n }\n case 'not': {\n const expr = record['expr'];\n if (!expr || typeof expr !== 'object') throw new Error('Invalid not filter: missing expr');\n return new MongoNotExpr(deserializeFilterExpr(expr));\n }\n case 'exists': {\n const data = validate(ExistsFilterJson, json, 'exists filter');\n return new MongoExistsExpr(data.field, data.exists);\n }\n default:\n throw new Error(`Unknown filter expression kind: ${kind}`);\n }\n}\n\nfunction deserializeDdlCommand(json: unknown): AnyMongoDdlCommand {\n const record = json as Record<string, unknown>;\n const kind = record['kind'] as string;\n switch (kind) {\n case 'createIndex': {\n const data = validate(CreateIndexJson, json, 'createIndex command');\n return new CreateIndexCommand(data.collection, data.keys, {\n unique: data.unique,\n sparse: data.sparse,\n expireAfterSeconds: data.expireAfterSeconds,\n partialFilterExpression: data.partialFilterExpression,\n name: data.name,\n wildcardProjection: data.wildcardProjection as Record<string, 0 | 1> | undefined,\n collation: data.collation,\n weights: data.weights as Record<string, number> | undefined,\n default_language: data.default_language,\n language_override: data.language_override,\n });\n }\n case 'dropIndex': {\n const data = validate(DropIndexJson, json, 'dropIndex command');\n return new DropIndexCommand(data.collection, data.name);\n }\n case 'createCollection': {\n const data = validate(CreateCollectionJson, json, 'createCollection command');\n return new CreateCollectionCommand(data.collection, {\n validator: data.validator,\n validationLevel: data.validationLevel,\n validationAction: data.validationAction,\n capped: data.capped,\n size: data.size,\n max: data.max,\n timeseries: data.timeseries as CreateCollectionCommand['timeseries'],\n collation: data.collation,\n changeStreamPreAndPostImages: data.changeStreamPreAndPostImages as\n | { enabled: boolean }\n | undefined,\n clusteredIndex: data.clusteredIndex as CreateCollectionCommand['clusteredIndex'],\n });\n }\n case 'dropCollection': {\n const data = validate(DropCollectionJson, json, 'dropCollection command');\n return new DropCollectionCommand(data.collection);\n }\n case 'collMod': {\n const data = validate(CollModJson, json, 'collMod command');\n return new CollModCommand(data.collection, {\n validator: data.validator,\n validationLevel: data.validationLevel,\n validationAction: data.validationAction,\n changeStreamPreAndPostImages: data.changeStreamPreAndPostImages as\n | { enabled: boolean }\n | undefined,\n });\n }\n default:\n throw new Error(`Unknown DDL command kind: ${kind}`);\n }\n}\n\nfunction deserializeInspectionCommand(json: unknown): AnyMongoInspectionCommand {\n const record = json as Record<string, unknown>;\n const kind = record['kind'] as string;\n switch (kind) {\n case 'listIndexes': {\n const data = validate(ListIndexesJson, json, 'listIndexes command');\n return new ListIndexesCommand(data.collection);\n }\n case 'listCollections': {\n validate(ListCollectionsJson, json, 'listCollections command');\n return new ListCollectionsCommand();\n }\n default:\n throw new Error(`Unknown inspection command kind: ${kind}`);\n }\n}\n\nfunction deserializeCheck(json: unknown): MongoMigrationCheck {\n const data = validate(CheckJson, json, 'migration check');\n return {\n description: data.description,\n source: deserializeInspectionCommand(data.source),\n filter: deserializeFilterExpr(data.filter),\n expect: data.expect,\n };\n}\n\nfunction deserializeStep(json: unknown): MongoMigrationStep {\n const data = validate(StepJson, json, 'migration step');\n return {\n description: data.description,\n command: deserializeDdlCommand(data.command),\n };\n}\n\nexport function deserializeMongoOp(json: unknown): MongoMigrationPlanOperation {\n const data = validate(OperationJson, json, 'migration operation');\n return {\n id: data.id,\n label: data.label,\n operationClass: data.operationClass as MigrationOperationClass,\n precheck: data.precheck.map(deserializeCheck),\n execute: data.execute.map(deserializeStep),\n postcheck: data.postcheck.map(deserializeCheck),\n };\n}\n\nexport function deserializeMongoOps(json: readonly unknown[]): MongoMigrationPlanOperation[] {\n return json.map(deserializeMongoOp);\n}\n\nexport function serializeMongoOps(ops: readonly MongoMigrationPlanOperation[]): string {\n return JSON.stringify(ops, null, 2);\n}\n","import type { MigrationOperationClass } from '@prisma-next/framework-components/control';\nimport type {\n CollModOptions,\n CreateCollectionOptions,\n CreateIndexOptions,\n MongoIndexKey,\n} from '@prisma-next/mongo-query-ast/control';\nimport type {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\n\nexport interface CollModMeta {\n readonly id?: string;\n readonly label?: string;\n readonly operationClass?: MigrationOperationClass;\n}\n\nexport interface OpFactoryCallVisitor<R> {\n createIndex(call: CreateIndexCall): R;\n dropIndex(call: DropIndexCall): R;\n createCollection(call: CreateCollectionCall): R;\n dropCollection(call: DropCollectionCall): R;\n collMod(call: CollModCall): R;\n}\n\nabstract class OpFactoryCallNode {\n abstract readonly factory: string;\n abstract readonly operationClass: MigrationOperationClass;\n abstract readonly label: string;\n abstract accept<R>(visitor: OpFactoryCallVisitor<R>): R;\n\n protected freeze(): void {\n Object.freeze(this);\n }\n}\n\nfunction formatKeys(keys: ReadonlyArray<MongoIndexKey>): string {\n return keys.map((k) => `${k.field}:${k.direction}`).join(', ');\n}\n\nexport class CreateIndexCall extends OpFactoryCallNode {\n readonly factory = 'createIndex' as const;\n readonly operationClass = 'additive' as const;\n readonly collection: string;\n readonly keys: ReadonlyArray<MongoIndexKey>;\n readonly options: CreateIndexOptions | undefined;\n readonly label: string;\n\n constructor(\n collection: string,\n keys: ReadonlyArray<MongoIndexKey>,\n options?: CreateIndexOptions,\n ) {\n super();\n this.collection = collection;\n this.keys = keys;\n this.options = options;\n this.label = `Create index on ${collection} (${formatKeys(keys)})`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.createIndex(this);\n }\n}\n\nexport class DropIndexCall extends OpFactoryCallNode {\n readonly factory = 'dropIndex' as const;\n readonly operationClass = 'destructive' as const;\n readonly collection: string;\n readonly keys: ReadonlyArray<MongoIndexKey>;\n readonly label: string;\n\n constructor(collection: string, keys: ReadonlyArray<MongoIndexKey>) {\n super();\n this.collection = collection;\n this.keys = keys;\n this.label = `Drop index on ${collection} (${formatKeys(keys)})`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.dropIndex(this);\n }\n}\n\nexport class CreateCollectionCall extends OpFactoryCallNode {\n readonly factory = 'createCollection' as const;\n readonly operationClass = 'additive' as const;\n readonly collection: string;\n readonly options: CreateCollectionOptions | undefined;\n readonly label: string;\n\n constructor(collection: string, options?: CreateCollectionOptions) {\n super();\n this.collection = collection;\n this.options = options;\n this.label = `Create collection ${collection}`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.createCollection(this);\n }\n}\n\nexport class DropCollectionCall extends OpFactoryCallNode {\n readonly factory = 'dropCollection' as const;\n readonly operationClass = 'destructive' as const;\n readonly collection: string;\n readonly label: string;\n\n constructor(collection: string) {\n super();\n this.collection = collection;\n this.label = `Drop collection ${collection}`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.dropCollection(this);\n }\n}\n\nexport class CollModCall extends OpFactoryCallNode {\n readonly factory = 'collMod' as const;\n readonly collection: string;\n readonly options: CollModOptions;\n readonly meta: CollModMeta | undefined;\n readonly operationClass: MigrationOperationClass;\n readonly label: string;\n\n constructor(collection: string, options: CollModOptions, meta?: CollModMeta) {\n super();\n this.collection = collection;\n this.options = options;\n this.meta = meta;\n this.operationClass = meta?.operationClass ?? 'destructive';\n this.label = meta?.label ?? `Modify collection ${collection}`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.collMod(this);\n }\n}\n\nexport type OpFactoryCall =\n | CreateIndexCall\n | DropIndexCall\n | CreateCollectionCall\n | DropCollectionCall\n | CollModCall;\n\nexport function schemaIndexToCreateIndexOptions(index: MongoSchemaIndex): CreateIndexOptions {\n return {\n unique: index.unique || undefined,\n sparse: index.sparse,\n expireAfterSeconds: index.expireAfterSeconds,\n partialFilterExpression: index.partialFilterExpression,\n wildcardProjection: index.wildcardProjection,\n collation: index.collation,\n weights: index.weights,\n default_language: index.default_language,\n language_override: index.language_override,\n };\n}\n\nexport function schemaCollectionToCreateCollectionOptions(\n coll: MongoSchemaCollection,\n): CreateCollectionOptions | undefined {\n const opts: MongoSchemaCollectionOptions | undefined = coll.options;\n const validator: MongoSchemaValidator | undefined = coll.validator;\n if (!opts && !validator) return undefined;\n return {\n capped: opts?.capped ? true : undefined,\n size: opts?.capped?.size,\n max: opts?.capped?.max,\n timeseries: opts?.timeseries,\n collation: opts?.collation,\n clusteredIndex: opts?.clusteredIndex\n ? {\n key: { _id: 1 } as Record<string, number>,\n unique: true as boolean,\n ...(opts.clusteredIndex.name != null ? { name: opts.clusteredIndex.name } : {}),\n }\n : undefined,\n validator: validator ? { $jsonSchema: validator.jsonSchema } : undefined,\n validationLevel: validator?.validationLevel,\n validationAction: validator?.validationAction,\n changeStreamPreAndPostImages: opts?.changeStreamPreAndPostImages,\n };\n}\n","import type { MongoMigrationPlanOperation } from '@prisma-next/mongo-query-ast/control';\nimport {\n collMod,\n createCollection,\n createIndex,\n dropCollection,\n dropIndex,\n} from './migration-factories';\nimport type {\n CollModCall,\n CreateCollectionCall,\n CreateIndexCall,\n DropCollectionCall,\n DropIndexCall,\n OpFactoryCall,\n OpFactoryCallVisitor,\n} from './op-factory-call';\n\nconst renderVisitor: OpFactoryCallVisitor<MongoMigrationPlanOperation> = {\n createIndex(call: CreateIndexCall) {\n return createIndex(call.collection, call.keys, call.options);\n },\n dropIndex(call: DropIndexCall) {\n return dropIndex(call.collection, call.keys);\n },\n createCollection(call: CreateCollectionCall) {\n return createCollection(call.collection, call.options);\n },\n dropCollection(call: DropCollectionCall) {\n return dropCollection(call.collection);\n },\n collMod(call: CollModCall) {\n return collMod(call.collection, call.options, call.meta);\n },\n};\n\nexport function renderOps(calls: ReadonlyArray<OpFactoryCall>): MongoMigrationPlanOperation[] {\n return calls.map((call) => call.accept(renderVisitor));\n}\n","import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';\nimport type {\n MigrationOperationClass,\n MigrationOperationPolicy,\n MigrationPlanner,\n MigrationPlannerConflict,\n MigrationPlannerResult,\n} from '@prisma-next/framework-components/control';\nimport type { MongoContract } from '@prisma-next/mongo-contract';\nimport type {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaIR,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\nimport { canonicalize, deepEqual } from '@prisma-next/mongo-schema-ir';\nimport { contractToMongoSchemaIR } from './contract-to-schema';\nimport type { OpFactoryCall } from './op-factory-call';\nimport {\n CollModCall,\n CreateCollectionCall,\n CreateIndexCall,\n DropCollectionCall,\n DropIndexCall,\n schemaCollectionToCreateCollectionOptions,\n schemaIndexToCreateIndexOptions,\n} from './op-factory-call';\nimport { renderOps } from './render-ops';\n\nfunction buildIndexLookupKey(index: MongoSchemaIndex): string {\n const keys = index.keys.map((k) => `${k.field}:${k.direction}`).join(',');\n const opts = [\n index.unique ? 'unique' : '',\n index.sparse ? 'sparse' : '',\n index.expireAfterSeconds != null ? `ttl:${index.expireAfterSeconds}` : '',\n index.partialFilterExpression ? `pfe:${canonicalize(index.partialFilterExpression)}` : '',\n index.wildcardProjection ? `wp:${canonicalize(index.wildcardProjection)}` : '',\n index.collation ? `col:${canonicalize(index.collation)}` : '',\n index.weights ? `wt:${canonicalize(index.weights)}` : '',\n index.default_language ? `dl:${index.default_language}` : '',\n index.language_override ? `lo:${index.language_override}` : '',\n ]\n .filter(Boolean)\n .join(';');\n return opts ? `${keys}|${opts}` : keys;\n}\n\nfunction validatorsEqual(\n a: MongoSchemaValidator | undefined,\n b: MongoSchemaValidator | undefined,\n): boolean {\n if (!a && !b) return true;\n if (!a || !b) return false;\n return (\n a.validationLevel === b.validationLevel &&\n a.validationAction === b.validationAction &&\n canonicalize(a.jsonSchema) === canonicalize(b.jsonSchema)\n );\n}\n\nfunction classifyValidatorUpdate(\n origin: MongoSchemaValidator,\n dest: MongoSchemaValidator,\n): 'widening' | 'destructive' {\n let hasDestructive = false;\n\n if (canonicalize(origin.jsonSchema) !== canonicalize(dest.jsonSchema)) {\n hasDestructive = true;\n }\n\n if (origin.validationAction !== dest.validationAction) {\n if (dest.validationAction === 'error') hasDestructive = true;\n }\n\n if (origin.validationLevel !== dest.validationLevel) {\n if (dest.validationLevel === 'strict') hasDestructive = true;\n }\n\n return hasDestructive ? 'destructive' : 'widening';\n}\n\nfunction hasImmutableOptionChange(\n origin: MongoSchemaCollectionOptions | undefined,\n dest: MongoSchemaCollectionOptions | undefined,\n): string | undefined {\n if (canonicalize(origin?.capped) !== canonicalize(dest?.capped)) return 'capped';\n if (canonicalize(origin?.timeseries) !== canonicalize(dest?.timeseries)) return 'timeseries';\n if (canonicalize(origin?.collation) !== canonicalize(dest?.collation)) return 'collation';\n if (canonicalize(origin?.clusteredIndex) !== canonicalize(dest?.clusteredIndex))\n return 'clusteredIndex';\n return undefined;\n}\n\nfunction collectionHasOptions(coll: MongoSchemaCollection): boolean {\n return !!(coll.options || coll.validator);\n}\n\nexport type PlanCallsResult =\n | { readonly kind: 'success'; readonly calls: OpFactoryCall[] }\n | { readonly kind: 'failure'; readonly conflicts: MigrationPlannerConflict[] };\n\nexport class MongoMigrationPlanner implements MigrationPlanner<'mongo', 'mongo'> {\n planCalls(options: {\n readonly contract: unknown;\n readonly schema: unknown;\n readonly policy: MigrationOperationPolicy;\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;\n }): PlanCallsResult {\n const contract = options.contract as MongoContract;\n const originIR = options.schema as MongoSchemaIR;\n const destinationIR = contractToMongoSchemaIR(contract);\n\n const collCreates: OpFactoryCall[] = [];\n const drops: OpFactoryCall[] = [];\n const creates: OpFactoryCall[] = [];\n const validatorOps: OpFactoryCall[] = [];\n const mutableOptionOps: OpFactoryCall[] = [];\n const collDrops: OpFactoryCall[] = [];\n const conflicts: MigrationPlannerConflict[] = [];\n\n const allCollectionNames = new Set([\n ...originIR.collectionNames,\n ...destinationIR.collectionNames,\n ]);\n\n for (const collName of [...allCollectionNames].sort()) {\n const originColl = originIR.collection(collName);\n const destColl = destinationIR.collection(collName);\n\n if (!originColl) {\n if (destColl && collectionHasOptions(destColl)) {\n const opts = schemaCollectionToCreateCollectionOptions(destColl);\n collCreates.push(new CreateCollectionCall(collName, opts));\n }\n } else if (!destColl) {\n collDrops.push(new DropCollectionCall(collName));\n } else {\n const immutableChange = hasImmutableOptionChange(originColl.options, destColl.options);\n if (immutableChange) {\n conflicts.push({\n kind: 'policy-violation',\n summary: `Cannot change immutable collection option '${immutableChange}' on ${collName}`,\n why: `MongoDB does not support modifying the '${immutableChange}' option after collection creation`,\n });\n }\n\n const mutableCall = planMutableOptionsDiffCall(\n collName,\n originColl.options,\n destColl.options,\n );\n if (mutableCall) mutableOptionOps.push(mutableCall);\n\n const validatorCall = planValidatorDiffCall(\n collName,\n originColl.validator,\n destColl.validator,\n );\n if (validatorCall) validatorOps.push(validatorCall);\n }\n\n const originLookup = new Map<string, MongoSchemaIndex>();\n if (originColl) {\n for (const idx of originColl.indexes) {\n originLookup.set(buildIndexLookupKey(idx), idx);\n }\n }\n\n const destLookup = new Map<string, MongoSchemaIndex>();\n if (destColl) {\n for (const idx of destColl.indexes) {\n destLookup.set(buildIndexLookupKey(idx), idx);\n }\n }\n\n for (const [lookupKey, idx] of originLookup) {\n if (!destLookup.has(lookupKey)) {\n drops.push(new DropIndexCall(collName, idx.keys));\n }\n }\n\n for (const [lookupKey, idx] of destLookup) {\n if (!originLookup.has(lookupKey)) {\n creates.push(\n new CreateIndexCall(collName, idx.keys, schemaIndexToCreateIndexOptions(idx)),\n );\n }\n }\n }\n\n if (conflicts.length > 0) {\n return { kind: 'failure', conflicts };\n }\n\n const allCalls = [\n ...collCreates,\n ...drops,\n ...creates,\n ...validatorOps,\n ...mutableOptionOps,\n ...collDrops,\n ];\n\n for (const call of allCalls) {\n if (!options.policy.allowedOperationClasses.includes(call.operationClass)) {\n conflicts.push({\n kind: 'policy-violation',\n summary: `${call.operationClass} operation disallowed: ${call.label}`,\n why: `Policy does not allow '${call.operationClass}' operations`,\n });\n }\n }\n\n if (conflicts.length > 0) {\n return { kind: 'failure', conflicts };\n }\n\n return { kind: 'success', calls: allCalls };\n }\n\n plan(options: {\n readonly contract: unknown;\n readonly schema: unknown;\n readonly policy: MigrationOperationPolicy;\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;\n }): MigrationPlannerResult {\n const contract = options.contract as MongoContract;\n const result = this.planCalls(options);\n if (result.kind === 'failure') return result;\n return {\n kind: 'success',\n plan: {\n targetId: 'mongo',\n destination: {\n storageHash: contract.storage.storageHash,\n },\n operations: renderOps(result.calls),\n },\n };\n }\n}\n\nfunction planValidatorDiffCall(\n collName: string,\n originValidator: MongoSchemaValidator | undefined,\n destValidator: MongoSchemaValidator | undefined,\n): OpFactoryCall | undefined {\n if (validatorsEqual(originValidator, destValidator)) return undefined;\n\n if (destValidator) {\n const operationClass: MigrationOperationClass = originValidator\n ? classifyValidatorUpdate(originValidator, destValidator)\n : 'destructive';\n return new CollModCall(\n collName,\n {\n validator: { $jsonSchema: destValidator.jsonSchema },\n validationLevel: destValidator.validationLevel,\n validationAction: destValidator.validationAction,\n },\n {\n id: `validator.${collName}.${originValidator ? 'update' : 'add'}`,\n label: `${originValidator ? 'Update' : 'Add'} validator on ${collName}`,\n operationClass,\n },\n );\n }\n\n return new CollModCall(\n collName,\n {\n validator: {},\n validationLevel: 'strict',\n validationAction: 'error',\n },\n {\n id: `validator.${collName}.remove`,\n label: `Remove validator on ${collName}`,\n operationClass: 'widening',\n },\n );\n}\n\nfunction planMutableOptionsDiffCall(\n collName: string,\n origin: MongoSchemaCollectionOptions | undefined,\n dest: MongoSchemaCollectionOptions | undefined,\n): OpFactoryCall | undefined {\n const originCSPPI = origin?.changeStreamPreAndPostImages;\n const destCSPPI = dest?.changeStreamPreAndPostImages;\n if (deepEqual(originCSPPI, destCSPPI)) return undefined;\n\n const desiredCSPPI = destCSPPI ?? { enabled: false };\n return new CollModCall(\n collName,\n {\n changeStreamPreAndPostImages: desiredCSPPI,\n },\n {\n id: `options.${collName}.update`,\n label: `Update mutable options on ${collName}`,\n operationClass: desiredCSPPI.enabled ? 'widening' : 'destructive',\n },\n );\n}\n","import type { ContractMarkerRecord } from '@prisma-next/contract/types';\nimport type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';\nimport type {\n MigrationOperationPolicy,\n MigrationPlan,\n MigrationPlanOperation,\n MigrationRunnerExecutionChecks,\n MigrationRunnerFailure,\n MigrationRunnerResult,\n} from '@prisma-next/framework-components/control';\nimport type {\n MongoDdlCommandVisitor,\n MongoInspectionCommandVisitor,\n MongoMigrationCheck,\n MongoMigrationPlanOperation,\n} from '@prisma-next/mongo-query-ast/control';\nimport { notOk, ok } from '@prisma-next/utils/result';\nimport { FilterEvaluator } from './filter-evaluator';\nimport { deserializeMongoOps } from './mongo-ops-serializer';\n\nexport interface MarkerOperations {\n readMarker(): Promise<ContractMarkerRecord | null>;\n initMarker(destination: {\n readonly storageHash: string;\n readonly profileHash: string;\n }): Promise<void>;\n updateMarker(\n expectedFrom: string,\n destination: { readonly storageHash: string; readonly profileHash: string },\n ): Promise<boolean>;\n writeLedgerEntry(entry: {\n readonly edgeId: string;\n readonly from: string;\n readonly to: string;\n }): Promise<void>;\n}\n\nexport interface MongoRunnerDependencies {\n readonly commandExecutor: MongoDdlCommandVisitor<Promise<void>>;\n readonly inspectionExecutor: MongoInspectionCommandVisitor<Promise<Record<string, unknown>[]>>;\n readonly markerOps: MarkerOperations;\n}\n\nfunction runnerFailure(\n code: string,\n summary: string,\n opts?: { why?: string; meta?: Record<string, unknown> },\n): MigrationRunnerResult {\n return notOk<MigrationRunnerFailure>({\n code,\n summary,\n ...opts,\n });\n}\n\nexport class MongoMigrationRunner {\n constructor(private readonly deps: MongoRunnerDependencies) {}\n\n async execute(options: {\n readonly plan: MigrationPlan;\n readonly destinationContract: unknown;\n readonly policy: MigrationOperationPolicy;\n readonly callbacks?: {\n onOperationStart?(op: MigrationPlanOperation): void;\n onOperationComplete?(op: MigrationPlanOperation): void;\n };\n readonly executionChecks?: MigrationRunnerExecutionChecks;\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;\n }): Promise<MigrationRunnerResult> {\n const { commandExecutor, inspectionExecutor, markerOps } = this.deps;\n const operations = deserializeMongoOps(options.plan.operations as readonly unknown[]);\n\n const policyCheck = this.enforcePolicyCompatibility(options.policy, operations);\n if (policyCheck) return policyCheck;\n\n const existingMarker = await markerOps.readMarker();\n\n const markerCheck = this.ensureMarkerCompatibility(existingMarker, options.plan);\n if (markerCheck) return markerCheck;\n\n const checks = options.executionChecks;\n const runPrechecks = checks?.prechecks !== false;\n const runPostchecks = checks?.postchecks !== false;\n const runIdempotency = checks?.idempotencyChecks !== false;\n\n const filterEvaluator = new FilterEvaluator();\n\n let operationsExecuted = 0;\n\n for (const operation of operations) {\n options.callbacks?.onOperationStart?.(operation);\n try {\n if (runPostchecks && runIdempotency) {\n const allSatisfied = await this.allChecksSatisfied(\n operation.postcheck,\n inspectionExecutor,\n filterEvaluator,\n );\n if (allSatisfied) continue;\n }\n\n if (runPrechecks) {\n const precheckResult = await this.evaluateChecks(\n operation.precheck,\n inspectionExecutor,\n filterEvaluator,\n );\n if (!precheckResult) {\n return runnerFailure(\n 'PRECHECK_FAILED',\n `Operation ${operation.id} failed during precheck`,\n { meta: { operationId: operation.id } },\n );\n }\n }\n\n for (const step of operation.execute) {\n await step.command.accept(commandExecutor);\n }\n\n if (runPostchecks) {\n const postcheckResult = await this.evaluateChecks(\n operation.postcheck,\n inspectionExecutor,\n filterEvaluator,\n );\n if (!postcheckResult) {\n return runnerFailure(\n 'POSTCHECK_FAILED',\n `Operation ${operation.id} failed during postcheck`,\n { meta: { operationId: operation.id } },\n );\n }\n }\n\n operationsExecuted += 1;\n } finally {\n options.callbacks?.onOperationComplete?.(operation);\n }\n }\n\n const destination = options.plan.destination;\n const contract = options.destinationContract as { profileHash?: string };\n const profileHash = contract.profileHash ?? destination.storageHash;\n\n if (\n operationsExecuted === 0 &&\n existingMarker?.storageHash === destination.storageHash &&\n existingMarker.profileHash === profileHash\n ) {\n return ok({ operationsPlanned: operations.length, operationsExecuted });\n }\n\n if (existingMarker) {\n const updated = await markerOps.updateMarker(existingMarker.storageHash, {\n storageHash: destination.storageHash,\n profileHash,\n });\n if (!updated) {\n return runnerFailure(\n 'MARKER_CAS_FAILURE',\n 'Marker was modified by another process during migration execution.',\n {\n meta: {\n expectedStorageHash: existingMarker.storageHash,\n destinationStorageHash: destination.storageHash,\n },\n },\n );\n }\n } else {\n await markerOps.initMarker({\n storageHash: destination.storageHash,\n profileHash,\n });\n }\n\n const originHash = existingMarker?.storageHash ?? '';\n await markerOps.writeLedgerEntry({\n edgeId: `${originHash}->${destination.storageHash}`,\n from: originHash,\n to: destination.storageHash,\n });\n\n return ok({ operationsPlanned: operations.length, operationsExecuted });\n }\n\n private async evaluateChecks(\n checks: readonly MongoMigrationCheck[],\n inspectionExecutor: MongoInspectionCommandVisitor<Promise<Record<string, unknown>[]>>,\n filterEvaluator: FilterEvaluator,\n ): Promise<boolean> {\n for (const check of checks) {\n const documents = await check.source.accept(inspectionExecutor);\n const matchFound = documents.some((doc) =>\n filterEvaluator.evaluate(check.filter, doc as Record<string, unknown>),\n );\n const passed = check.expect === 'exists' ? matchFound : !matchFound;\n if (!passed) return false;\n }\n return true;\n }\n\n private async allChecksSatisfied(\n checks: readonly MongoMigrationCheck[],\n inspectionExecutor: MongoInspectionCommandVisitor<Promise<Record<string, unknown>[]>>,\n filterEvaluator: FilterEvaluator,\n ): Promise<boolean> {\n if (checks.length === 0) return false;\n return this.evaluateChecks(checks, inspectionExecutor, filterEvaluator);\n }\n\n private enforcePolicyCompatibility(\n policy: MigrationOperationPolicy,\n operations: readonly MongoMigrationPlanOperation[],\n ): MigrationRunnerResult | undefined {\n const allowedClasses = new Set(policy.allowedOperationClasses);\n for (const operation of operations) {\n if (!allowedClasses.has(operation.operationClass)) {\n return runnerFailure(\n 'POLICY_VIOLATION',\n `Operation ${operation.id} has class \"${operation.operationClass}\" which is not allowed by policy.`,\n {\n why: `Policy only allows: ${[...allowedClasses].join(', ')}.`,\n meta: {\n operationId: operation.id,\n operationClass: operation.operationClass,\n },\n },\n );\n }\n }\n return undefined;\n }\n\n private ensureMarkerCompatibility(\n marker: ContractMarkerRecord | null,\n plan: MigrationPlan,\n ): MigrationRunnerResult | undefined {\n const origin = plan.origin ?? null;\n if (!origin) {\n if (marker) {\n return runnerFailure(\n 'MARKER_ORIGIN_MISMATCH',\n 'Database already has a contract marker but the plan has no origin. This would silently overwrite the existing marker.',\n { meta: { markerStorageHash: marker.storageHash } },\n );\n }\n return undefined;\n }\n\n if (!marker) {\n return runnerFailure(\n 'MARKER_ORIGIN_MISMATCH',\n `Missing contract marker: expected origin storage hash ${origin.storageHash}.`,\n { meta: { expectedOriginStorageHash: origin.storageHash } },\n );\n }\n\n if (marker.storageHash !== origin.storageHash) {\n return runnerFailure(\n 'MARKER_ORIGIN_MISMATCH',\n `Existing contract marker (${marker.storageHash}) does not match plan origin (${origin.storageHash}).`,\n {\n meta: {\n markerStorageHash: marker.storageHash,\n expectedOriginStorageHash: origin.storageHash,\n },\n },\n );\n }\n\n return undefined;\n }\n}\n","import type {\n CollModCall,\n CreateCollectionCall,\n CreateIndexCall,\n DropCollectionCall,\n DropIndexCall,\n OpFactoryCall,\n OpFactoryCallVisitor,\n} from './op-factory-call';\n\nexport interface RenderMigrationMeta {\n readonly from: string;\n readonly to: string;\n readonly kind?: string;\n readonly labels?: readonly string[];\n}\n\nexport function renderTypeScript(\n calls: ReadonlyArray<OpFactoryCall>,\n meta?: RenderMigrationMeta,\n): string {\n const factoryNames = collectFactoryNames(calls);\n const imports = buildImports(factoryNames);\n const planBody = calls.map((c) => c.accept(renderCallVisitor)).join(',\\n');\n const describeMethod = meta ? buildDescribeMethod(meta) : '';\n\n return [\n imports,\n '',\n 'class M extends Migration {',\n describeMethod,\n ' override plan() {',\n ' return [',\n indent(planBody, 6),\n ' ];',\n ' }',\n '}',\n '',\n 'export default M;',\n 'Migration.run(import.meta.url, M);',\n '',\n ].join('\\n');\n}\n\nfunction collectFactoryNames(calls: ReadonlyArray<OpFactoryCall>): string[] {\n const names = new Set<string>();\n for (const call of calls) {\n names.add(call.factory);\n }\n return [...names].sort();\n}\n\nfunction buildImports(factoryNames: string[]): string {\n const lines = [\"import { Migration } from '@prisma-next/family-mongo/migration';\"];\n if (factoryNames.length > 0) {\n lines.push(`import { ${factoryNames.join(', ')} } from '@prisma-next/target-mongo/migration';`);\n }\n return lines.join('\\n');\n}\n\nfunction buildDescribeMethod(meta: RenderMigrationMeta): string {\n const lines: string[] = [];\n lines.push(' override describe() {');\n lines.push(' return {');\n lines.push(` from: ${JSON.stringify(meta.from)},`);\n lines.push(` to: ${JSON.stringify(meta.to)},`);\n if (meta.kind) {\n lines.push(` kind: ${JSON.stringify(meta.kind)},`);\n }\n if (meta.labels && meta.labels.length > 0) {\n lines.push(` labels: ${renderLiteral(meta.labels)},`);\n }\n lines.push(' };');\n lines.push(' }');\n lines.push('');\n return lines.join('\\n');\n}\n\nconst renderCallVisitor: OpFactoryCallVisitor<string> = {\n createIndex(call: CreateIndexCall) {\n return call.options\n ? `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)}, ${renderLiteral(call.options)})`\n : `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;\n },\n dropIndex(call: DropIndexCall) {\n return `dropIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;\n },\n createCollection(call: CreateCollectionCall) {\n return call.options\n ? `createCollection(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})`\n : `createCollection(${renderLiteral(call.collection)})`;\n },\n dropCollection(call: DropCollectionCall) {\n return `dropCollection(${renderLiteral(call.collection)})`;\n },\n collMod(call: CollModCall) {\n return call.meta\n ? `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)}, ${renderLiteral(call.meta)})`\n : `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})`;\n },\n};\n\nfunction renderLiteral(value: unknown): string {\n if (value === undefined) return 'undefined';\n if (value === null) return 'null';\n if (typeof value === 'string') return JSON.stringify(value);\n if (typeof value === 'number' || typeof value === 'boolean') return String(value);\n if (Array.isArray(value)) {\n if (value.length === 0) return '[]';\n const items = value.map((v) => renderLiteral(v));\n const singleLine = `[${items.join(', ')}]`;\n if (singleLine.length <= 80) return singleLine;\n return `[\\n${items.map((i) => ` ${i}`).join(',\\n')},\\n]`;\n }\n if (typeof value === 'object') {\n const entries = Object.entries(value).filter(([, v]) => v !== undefined);\n if (entries.length === 0) return '{}';\n const items = entries.map(([k, v]) => `${renderKey(k)}: ${renderLiteral(v)}`);\n const singleLine = `{ ${items.join(', ')} }`;\n if (singleLine.length <= 80) return singleLine;\n return `{\\n${items.map((i) => ` ${i}`).join(',\\n')},\\n}`;\n }\n return String(value);\n}\n\nfunction renderKey(key: string): string {\n if (key === '__proto__') return JSON.stringify(key);\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);\n}\n\nfunction indent(text: string, spaces: number): string {\n const pad = ' '.repeat(spaces);\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${pad}${line}` : line))\n .join('\\n');\n}\n"],"mappings":";;;;;;;;AAeA,SAAS,aAAa,OAA4C;AAChE,QAAO,IAAI,iBAAiB;EAC1B,MAAM,MAAM;EACZ,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,oB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AG,QAAO;EACjC,MAAM,QAAQ,QAAQ,KAAK,CAAC,GAAG,OAAO,GAAG,UAAU,EAAE,CAAC,IAAI,cAAc,EAAE,GAAG;EAC7E,MAAM,aAAa,KAAK,MAAM,KAAK,KAAK,CAAC;AACzC,MAAI,WAAW,UAAU,GAAI,QAAO;AACpC,SAAO,MAAM,MAAM,KAAK,MAAM,KAAK,IAAI,CAAC,KAAK,MAAM,CAAC;;AAEtD,QAAO,OAAO,MAAM;;AAGtB,SAAS,UAAU,KAAqB;AACtC,KAAI,QAAQ,YAAa,QAAO,KAAK,UAAU,IAAI;AACnD,QAAO,6BAA6B,KAAK,IAAI,GAAG,MAAM,KAAK,UAAU,IAAI;;AAG3E,SAAS,OAAO,MAAc,QAAwB;CACpD,MAAM,MAAM,IAAI,OAAO,OAAO;AAC9B,QAAO,KACJ,MAAM,KAAK,CACX,KAAK,SAAU,KAAK,MAAM,GAAG,GAAG,MAAM,SAAS,KAAM,CACrD,KAAK,KAAK"}
1
+ {"version":3,"file":"control.mjs","names":["parts: string[]","statements: string[]","current: unknown","opts: MongoSchemaCollectionOptions | undefined","validator: MongoSchemaValidator | undefined","renderVisitor: OpFactoryCallVisitor<MongoMigrationPlanOperation>","lines: string[]","renderCallVisitor: OpFactoryCallVisitor<string>","calls: readonly OpFactoryCall[]","meta: MigrationMeta","collCreates: OpFactoryCall[]","drops: OpFactoryCall[]","creates: OpFactoryCall[]","validatorOps: OpFactoryCall[]","mutableOptionOps: OpFactoryCall[]","collDrops: OpFactoryCall[]","conflicts: MigrationPlannerConflict[]","operationClass: MigrationOperationClass","deps: MongoRunnerDependencies"],"sources":["../src/core/contract-to-schema.ts","../src/core/ddl-formatter.ts","../src/core/filter-evaluator.ts","../src/core/marker-ledger.ts","../src/core/mongo-ops-serializer.ts","../src/core/op-factory-call.ts","../src/core/render-ops.ts","../src/core/render-typescript.ts","../src/core/planner-produced-migration.ts","../src/core/mongo-planner.ts","../src/core/mongo-runner.ts"],"sourcesContent":["import type {\n MongoContract,\n MongoStorageCollection,\n MongoStorageCollectionOptions,\n MongoStorageIndex,\n MongoStorageValidator,\n} from '@prisma-next/mongo-contract';\nimport {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaIR,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\n\nfunction convertIndex(index: MongoStorageIndex): MongoSchemaIndex {\n return new MongoSchemaIndex({\n keys: index.keys,\n unique: index.unique,\n sparse: index.sparse,\n expireAfterSeconds: index.expireAfterSeconds,\n partialFilterExpression: index.partialFilterExpression,\n wildcardProjection: index.wildcardProjection,\n collation: index.collation,\n weights: index.weights,\n default_language: index.default_language,\n language_override: index.language_override,\n });\n}\n\nfunction convertValidator(v: MongoStorageValidator): MongoSchemaValidator {\n return new MongoSchemaValidator({\n jsonSchema: v.jsonSchema,\n validationLevel: v.validationLevel,\n validationAction: v.validationAction,\n });\n}\n\nfunction convertOptions(o: MongoStorageCollectionOptions): MongoSchemaCollectionOptions {\n return new MongoSchemaCollectionOptions(o);\n}\n\nfunction convertCollection(name: string, def: MongoStorageCollection): MongoSchemaCollection {\n const indexes = (def.indexes ?? []).map(convertIndex);\n return new MongoSchemaCollection({\n name,\n indexes,\n ...(def.validator != null && { validator: convertValidator(def.validator) }),\n ...(def.options != null && { options: convertOptions(def.options) }),\n });\n}\n\nexport function contractToMongoSchemaIR(contract: MongoContract | null): MongoSchemaIR {\n if (!contract) {\n return new MongoSchemaIR([]);\n }\n\n const collections = Object.entries(contract.storage.collections).map(([name, def]) =>\n convertCollection(name, def),\n );\n\n return new MongoSchemaIR(collections);\n}\n","import type { MigrationPlanOperation } from '@prisma-next/framework-components/control';\nimport type {\n CollModCommand,\n CreateCollectionCommand,\n CreateIndexCommand,\n DropCollectionCommand,\n DropIndexCommand,\n MongoDdlCommandVisitor,\n MongoIndexKey,\n} from '@prisma-next/mongo-query-ast/control';\n\nfunction formatKeySpec(keys: ReadonlyArray<MongoIndexKey>): string {\n const entries = keys.map((k) => `${JSON.stringify(k.field)}: ${JSON.stringify(k.direction)}`);\n return `{ ${entries.join(', ')} }`;\n}\n\nfunction formatOptions(cmd: CreateIndexCommand): string | undefined {\n const parts: string[] = [];\n if (cmd.unique) parts.push('unique: true');\n if (cmd.sparse) parts.push('sparse: true');\n if (cmd.expireAfterSeconds !== undefined)\n parts.push(`expireAfterSeconds: ${cmd.expireAfterSeconds}`);\n if (cmd.name) parts.push(`name: ${JSON.stringify(cmd.name)}`);\n if (cmd.collation) parts.push(`collation: ${JSON.stringify(cmd.collation)}`);\n if (cmd.weights) parts.push(`weights: ${JSON.stringify(cmd.weights)}`);\n if (cmd.default_language) parts.push(`default_language: ${JSON.stringify(cmd.default_language)}`);\n if (cmd.language_override)\n parts.push(`language_override: ${JSON.stringify(cmd.language_override)}`);\n if (cmd.wildcardProjection)\n parts.push(`wildcardProjection: ${JSON.stringify(cmd.wildcardProjection)}`);\n if (cmd.partialFilterExpression)\n parts.push(`partialFilterExpression: ${JSON.stringify(cmd.partialFilterExpression)}`);\n if (parts.length === 0) return undefined;\n return `{ ${parts.join(', ')} }`;\n}\n\nfunction formatCreateCollectionOptions(cmd: CreateCollectionCommand): string | undefined {\n const parts: string[] = [];\n if (cmd.capped) parts.push('capped: true');\n if (cmd.size !== undefined) parts.push(`size: ${cmd.size}`);\n if (cmd.max !== undefined) parts.push(`max: ${cmd.max}`);\n if (cmd.timeseries) parts.push(`timeseries: ${JSON.stringify(cmd.timeseries)}`);\n if (cmd.collation) parts.push(`collation: ${JSON.stringify(cmd.collation)}`);\n if (cmd.clusteredIndex) parts.push(`clusteredIndex: ${JSON.stringify(cmd.clusteredIndex)}`);\n if (cmd.validator) parts.push(`validator: ${JSON.stringify(cmd.validator)}`);\n if (cmd.validationLevel) parts.push(`validationLevel: ${JSON.stringify(cmd.validationLevel)}`);\n if (cmd.validationAction) parts.push(`validationAction: ${JSON.stringify(cmd.validationAction)}`);\n if (cmd.changeStreamPreAndPostImages)\n parts.push(`changeStreamPreAndPostImages: ${JSON.stringify(cmd.changeStreamPreAndPostImages)}`);\n if (parts.length === 0) return undefined;\n return `{ ${parts.join(', ')} }`;\n}\n\nclass MongoDdlCommandFormatter implements MongoDdlCommandVisitor<string> {\n createIndex(cmd: CreateIndexCommand): string {\n const keySpec = formatKeySpec(cmd.keys);\n const opts = formatOptions(cmd);\n return opts\n ? `db.${cmd.collection}.createIndex(${keySpec}, ${opts})`\n : `db.${cmd.collection}.createIndex(${keySpec})`;\n }\n\n dropIndex(cmd: DropIndexCommand): string {\n return `db.${cmd.collection}.dropIndex(${JSON.stringify(cmd.name)})`;\n }\n\n createCollection(cmd: CreateCollectionCommand): string {\n const opts = formatCreateCollectionOptions(cmd);\n return opts\n ? `db.createCollection(${JSON.stringify(cmd.collection)}, ${opts})`\n : `db.createCollection(${JSON.stringify(cmd.collection)})`;\n }\n\n dropCollection(cmd: DropCollectionCommand): string {\n return `db.${cmd.collection}.drop()`;\n }\n\n collMod(cmd: CollModCommand): string {\n const parts: string[] = [`collMod: ${JSON.stringify(cmd.collection)}`];\n if (cmd.validator) parts.push(`validator: ${JSON.stringify(cmd.validator)}`);\n if (cmd.validationLevel) parts.push(`validationLevel: ${JSON.stringify(cmd.validationLevel)}`);\n if (cmd.validationAction)\n parts.push(`validationAction: ${JSON.stringify(cmd.validationAction)}`);\n if (cmd.changeStreamPreAndPostImages)\n parts.push(\n `changeStreamPreAndPostImages: ${JSON.stringify(cmd.changeStreamPreAndPostImages)}`,\n );\n return `db.runCommand({ ${parts.join(', ')} })`;\n }\n}\n\nconst formatter = new MongoDdlCommandFormatter();\n\ninterface MongoExecuteStep {\n readonly command: { readonly accept: <R>(visitor: MongoDdlCommandVisitor<R>) => R };\n}\n\nexport function formatMongoOperations(operations: readonly MigrationPlanOperation[]): string[] {\n const statements: string[] = [];\n for (const operation of operations) {\n const candidate = operation as unknown as Record<string, unknown>;\n if (!('execute' in candidate) || !Array.isArray(candidate['execute'])) {\n continue;\n }\n for (const step of candidate['execute'] as MongoExecuteStep[]) {\n if (step.command && typeof step.command.accept === 'function') {\n statements.push(step.command.accept(formatter));\n }\n }\n }\n return statements;\n}\n","import type {\n MongoAndExpr,\n MongoExistsExpr,\n MongoExprFilter,\n MongoFieldFilter,\n MongoFilterExpr,\n MongoFilterVisitor,\n MongoNotExpr,\n MongoOrExpr,\n} from '@prisma-next/mongo-query-ast/control';\nimport { deepEqual } from '@prisma-next/mongo-schema-ir';\nimport type { MongoValue } from '@prisma-next/mongo-value';\n\nfunction getNestedField(doc: Record<string, unknown>, path: string): unknown {\n const parts = path.split('.');\n let current: unknown = doc;\n for (const part of parts) {\n if (current === null || current === undefined || typeof current !== 'object') {\n return undefined;\n }\n const record = current as Record<string, unknown>;\n if (!Object.hasOwn(record, part)) {\n return undefined;\n }\n current = record[part];\n }\n return current;\n}\n\nfunction evaluateFieldOp(op: string, actual: unknown, expected: MongoValue): boolean {\n switch (op) {\n case '$eq':\n return deepEqual(actual, expected);\n case '$ne':\n return !deepEqual(actual, expected);\n case '$gt':\n return typeof actual === typeof expected && (actual as number) > (expected as number);\n case '$gte':\n return typeof actual === typeof expected && (actual as number) >= (expected as number);\n case '$lt':\n return typeof actual === typeof expected && (actual as number) < (expected as number);\n case '$lte':\n return typeof actual === typeof expected && (actual as number) <= (expected as number);\n case '$in':\n return Array.isArray(expected) && expected.some((v) => deepEqual(actual, v));\n default:\n throw new Error(`Unsupported filter operator in migration check: ${op}`);\n }\n}\n\nexport class FilterEvaluator implements MongoFilterVisitor<boolean> {\n private doc: Record<string, unknown> = {};\n\n evaluate(filter: MongoFilterExpr, doc: Record<string, unknown>): boolean {\n this.doc = doc;\n return filter.accept(this);\n }\n\n field(expr: MongoFieldFilter): boolean {\n const value = getNestedField(this.doc, expr.field);\n return evaluateFieldOp(expr.op, value, expr.value);\n }\n\n and(expr: MongoAndExpr): boolean {\n return expr.exprs.every((child) => child.accept(this));\n }\n\n or(expr: MongoOrExpr): boolean {\n return expr.exprs.some((child) => child.accept(this));\n }\n\n not(expr: MongoNotExpr): boolean {\n return !expr.expr.accept(this);\n }\n\n exists(expr: MongoExistsExpr): boolean {\n const has = getNestedField(this.doc, expr.field) !== undefined;\n return expr.exists ? has : !has;\n }\n\n expr(_expr: MongoExprFilter): boolean {\n throw new Error('Aggregation expression filters are not supported in migration checks');\n }\n}\n","import type { ContractMarkerRecord } from '@prisma-next/contract/types';\nimport {\n RawAggregateCommand,\n RawFindOneAndUpdateCommand,\n RawInsertOneCommand,\n} from '@prisma-next/mongo-query-ast/execution';\nimport type { Db, Document } from 'mongodb';\n\nconst COLLECTION = '_prisma_migrations';\nconst MARKER_ID = 'marker';\n\nasync function executeAggregate(db: Db, cmd: RawAggregateCommand): Promise<Document[]> {\n return db\n .collection(cmd.collection)\n .aggregate(cmd.pipeline as Record<string, unknown>[])\n .toArray();\n}\n\nasync function executeInsertOne(db: Db, cmd: RawInsertOneCommand): Promise<void> {\n await db.collection(cmd.collection).insertOne(cmd.document);\n}\n\nasync function executeFindOneAndUpdate(\n db: Db,\n cmd: RawFindOneAndUpdateCommand,\n): Promise<Document | null> {\n return db\n .collection(cmd.collection)\n .findOneAndUpdate(cmd.filter, cmd.update as Record<string, unknown>, { upsert: cmd.upsert });\n}\n\nexport async function readMarker(db: Db): Promise<ContractMarkerRecord | null> {\n const cmd = new RawAggregateCommand(COLLECTION, [{ $match: { _id: MARKER_ID } }, { $limit: 1 }]);\n const docs = await executeAggregate(db, cmd);\n const doc = docs[0];\n if (!doc) return null;\n return {\n storageHash: doc['storageHash'] as string,\n profileHash: doc['profileHash'] as string,\n contractJson: (doc['contractJson'] as unknown) ?? null,\n canonicalVersion: (doc['canonicalVersion'] as number) ?? null,\n updatedAt: doc['updatedAt'] as Date,\n appTag: (doc['appTag'] as string) ?? null,\n meta: (doc['meta'] as Record<string, unknown>) ?? {},\n };\n}\n\nexport async function initMarker(\n db: Db,\n destination: { readonly storageHash: string; readonly profileHash: string },\n): Promise<void> {\n const cmd = new RawInsertOneCommand(COLLECTION, {\n _id: MARKER_ID,\n storageHash: destination.storageHash,\n profileHash: destination.profileHash,\n contractJson: null,\n canonicalVersion: null,\n updatedAt: new Date(),\n appTag: null,\n meta: {},\n });\n await executeInsertOne(db, cmd);\n}\n\nexport async function updateMarker(\n db: Db,\n expectedFrom: string,\n destination: { readonly storageHash: string; readonly profileHash: string },\n): Promise<boolean> {\n const cmd = new RawFindOneAndUpdateCommand(\n COLLECTION,\n { _id: MARKER_ID, storageHash: expectedFrom },\n {\n $set: {\n storageHash: destination.storageHash,\n profileHash: destination.profileHash,\n updatedAt: new Date(),\n },\n },\n false,\n );\n const result = await executeFindOneAndUpdate(db, cmd);\n return result !== null;\n}\n\nexport async function writeLedgerEntry(\n db: Db,\n entry: { readonly edgeId: string; readonly from: string; readonly to: string },\n): Promise<void> {\n const cmd = new RawInsertOneCommand(COLLECTION, {\n type: 'ledger',\n edgeId: entry.edgeId,\n from: entry.from,\n to: entry.to,\n appliedAt: new Date(),\n });\n await executeInsertOne(db, cmd);\n}\n","import type { MigrationOperationClass } from '@prisma-next/framework-components/control';\nimport {\n type AnyMongoDdlCommand,\n type AnyMongoInspectionCommand,\n CollModCommand,\n CreateCollectionCommand,\n CreateIndexCommand,\n DropCollectionCommand,\n DropIndexCommand,\n ListCollectionsCommand,\n ListIndexesCommand,\n MongoAndExpr,\n MongoExistsExpr,\n MongoFieldFilter,\n type MongoFilterExpr,\n type MongoMigrationCheck,\n type MongoMigrationPlanOperation,\n type MongoMigrationStep,\n MongoNotExpr,\n MongoOrExpr,\n} from '@prisma-next/mongo-query-ast/control';\nimport { type } from 'arktype';\n\nconst IndexKeyDirection = type('1 | -1 | \"text\" | \"2dsphere\" | \"2d\" | \"hashed\"');\nconst IndexKeyJson = type({ field: 'string', direction: IndexKeyDirection });\n\nconst CreateIndexJson = type({\n kind: '\"createIndex\"',\n collection: 'string',\n keys: IndexKeyJson.array().atLeastLength(1),\n 'unique?': 'boolean',\n 'sparse?': 'boolean',\n 'expireAfterSeconds?': 'number',\n 'partialFilterExpression?': 'Record<string, unknown>',\n 'name?': 'string',\n 'wildcardProjection?': 'Record<string, unknown>',\n 'collation?': 'Record<string, unknown>',\n 'weights?': 'Record<string, unknown>',\n 'default_language?': 'string',\n 'language_override?': 'string',\n});\n\nconst DropIndexJson = type({\n kind: '\"dropIndex\"',\n collection: 'string',\n name: 'string',\n});\n\nconst CreateCollectionJson = type({\n kind: '\"createCollection\"',\n collection: 'string',\n 'validator?': 'Record<string, unknown>',\n 'validationLevel?': '\"strict\" | \"moderate\"',\n 'validationAction?': '\"error\" | \"warn\"',\n 'capped?': 'boolean',\n 'size?': 'number',\n 'max?': 'number',\n 'timeseries?': 'Record<string, unknown>',\n 'collation?': 'Record<string, unknown>',\n 'changeStreamPreAndPostImages?': 'Record<string, unknown>',\n 'clusteredIndex?': 'Record<string, unknown>',\n});\n\nconst DropCollectionJson = type({\n kind: '\"dropCollection\"',\n collection: 'string',\n});\n\nconst CollModJson = type({\n kind: '\"collMod\"',\n collection: 'string',\n 'validator?': 'Record<string, unknown>',\n 'validationLevel?': '\"strict\" | \"moderate\"',\n 'validationAction?': '\"error\" | \"warn\"',\n 'changeStreamPreAndPostImages?': 'Record<string, unknown>',\n});\n\nconst ListIndexesJson = type({\n kind: '\"listIndexes\"',\n collection: 'string',\n});\n\nconst ListCollectionsJson = type({\n kind: '\"listCollections\"',\n});\n\nconst FieldFilterJson = type({\n kind: '\"field\"',\n field: 'string',\n op: 'string',\n value: 'unknown',\n});\n\nconst ExistsFilterJson = type({\n kind: '\"exists\"',\n field: 'string',\n exists: 'boolean',\n});\n\nconst CheckJson = type({\n description: 'string',\n source: 'Record<string, unknown>',\n filter: 'Record<string, unknown>',\n expect: '\"exists\" | \"notExists\"',\n});\n\nconst StepJson = type({\n description: 'string',\n command: 'Record<string, unknown>',\n});\n\nconst OperationJson = type({\n id: 'string',\n label: 'string',\n operationClass: '\"additive\" | \"widening\" | \"destructive\"',\n precheck: 'Record<string, unknown>[]',\n execute: 'Record<string, unknown>[]',\n postcheck: 'Record<string, unknown>[]',\n});\n\nfunction validate<T>(schema: { assert: (data: unknown) => T }, data: unknown, context: string): T {\n try {\n return schema.assert(data);\n } catch (error) {\n /* v8 ignore start -- assertion libraries always throw Error instances */\n const message = error instanceof Error ? error.message : String(error);\n /* v8 ignore stop */\n throw new Error(`Invalid ${context}: ${message}`);\n }\n}\n\nfunction deserializeFilterExpr(json: unknown): MongoFilterExpr {\n const record = json as Record<string, unknown>;\n const kind = record['kind'] as string;\n switch (kind) {\n case 'field': {\n const data = validate(FieldFilterJson, json, 'field filter');\n return MongoFieldFilter.of(data.field, data.op, data.value as never);\n }\n case 'and': {\n const exprs = record['exprs'];\n if (!Array.isArray(exprs)) throw new Error('Invalid and filter: missing exprs array');\n return MongoAndExpr.of(exprs.map(deserializeFilterExpr));\n }\n case 'or': {\n const exprs = record['exprs'];\n if (!Array.isArray(exprs)) throw new Error('Invalid or filter: missing exprs array');\n return MongoOrExpr.of(exprs.map(deserializeFilterExpr));\n }\n case 'not': {\n const expr = record['expr'];\n if (!expr || typeof expr !== 'object') throw new Error('Invalid not filter: missing expr');\n return new MongoNotExpr(deserializeFilterExpr(expr));\n }\n case 'exists': {\n const data = validate(ExistsFilterJson, json, 'exists filter');\n return new MongoExistsExpr(data.field, data.exists);\n }\n default:\n throw new Error(`Unknown filter expression kind: ${kind}`);\n }\n}\n\nfunction deserializeDdlCommand(json: unknown): AnyMongoDdlCommand {\n const record = json as Record<string, unknown>;\n const kind = record['kind'] as string;\n switch (kind) {\n case 'createIndex': {\n const data = validate(CreateIndexJson, json, 'createIndex command');\n return new CreateIndexCommand(data.collection, data.keys, {\n unique: data.unique,\n sparse: data.sparse,\n expireAfterSeconds: data.expireAfterSeconds,\n partialFilterExpression: data.partialFilterExpression,\n name: data.name,\n wildcardProjection: data.wildcardProjection as Record<string, 0 | 1> | undefined,\n collation: data.collation,\n weights: data.weights as Record<string, number> | undefined,\n default_language: data.default_language,\n language_override: data.language_override,\n });\n }\n case 'dropIndex': {\n const data = validate(DropIndexJson, json, 'dropIndex command');\n return new DropIndexCommand(data.collection, data.name);\n }\n case 'createCollection': {\n const data = validate(CreateCollectionJson, json, 'createCollection command');\n return new CreateCollectionCommand(data.collection, {\n validator: data.validator,\n validationLevel: data.validationLevel,\n validationAction: data.validationAction,\n capped: data.capped,\n size: data.size,\n max: data.max,\n timeseries: data.timeseries as CreateCollectionCommand['timeseries'],\n collation: data.collation,\n changeStreamPreAndPostImages: data.changeStreamPreAndPostImages as\n | { enabled: boolean }\n | undefined,\n clusteredIndex: data.clusteredIndex as CreateCollectionCommand['clusteredIndex'],\n });\n }\n case 'dropCollection': {\n const data = validate(DropCollectionJson, json, 'dropCollection command');\n return new DropCollectionCommand(data.collection);\n }\n case 'collMod': {\n const data = validate(CollModJson, json, 'collMod command');\n return new CollModCommand(data.collection, {\n validator: data.validator,\n validationLevel: data.validationLevel,\n validationAction: data.validationAction,\n changeStreamPreAndPostImages: data.changeStreamPreAndPostImages as\n | { enabled: boolean }\n | undefined,\n });\n }\n default:\n throw new Error(`Unknown DDL command kind: ${kind}`);\n }\n}\n\nfunction deserializeInspectionCommand(json: unknown): AnyMongoInspectionCommand {\n const record = json as Record<string, unknown>;\n const kind = record['kind'] as string;\n switch (kind) {\n case 'listIndexes': {\n const data = validate(ListIndexesJson, json, 'listIndexes command');\n return new ListIndexesCommand(data.collection);\n }\n case 'listCollections': {\n validate(ListCollectionsJson, json, 'listCollections command');\n return new ListCollectionsCommand();\n }\n default:\n throw new Error(`Unknown inspection command kind: ${kind}`);\n }\n}\n\nfunction deserializeCheck(json: unknown): MongoMigrationCheck {\n const data = validate(CheckJson, json, 'migration check');\n return {\n description: data.description,\n source: deserializeInspectionCommand(data.source),\n filter: deserializeFilterExpr(data.filter),\n expect: data.expect,\n };\n}\n\nfunction deserializeStep(json: unknown): MongoMigrationStep {\n const data = validate(StepJson, json, 'migration step');\n return {\n description: data.description,\n command: deserializeDdlCommand(data.command),\n };\n}\n\nexport function deserializeMongoOp(json: unknown): MongoMigrationPlanOperation {\n const data = validate(OperationJson, json, 'migration operation');\n return {\n id: data.id,\n label: data.label,\n operationClass: data.operationClass as MigrationOperationClass,\n precheck: data.precheck.map(deserializeCheck),\n execute: data.execute.map(deserializeStep),\n postcheck: data.postcheck.map(deserializeCheck),\n };\n}\n\nexport function deserializeMongoOps(json: readonly unknown[]): MongoMigrationPlanOperation[] {\n return json.map(deserializeMongoOp);\n}\n\nexport function serializeMongoOps(ops: readonly MongoMigrationPlanOperation[]): string {\n return JSON.stringify(ops, null, 2);\n}\n","import type { MigrationOperationClass } from '@prisma-next/framework-components/control';\nimport type {\n CollModOptions,\n CreateCollectionOptions,\n CreateIndexOptions,\n MongoIndexKey,\n} from '@prisma-next/mongo-query-ast/control';\nimport type {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\n\nexport interface CollModMeta {\n readonly id?: string;\n readonly label?: string;\n readonly operationClass?: MigrationOperationClass;\n}\n\nexport interface OpFactoryCallVisitor<R> {\n createIndex(call: CreateIndexCall): R;\n dropIndex(call: DropIndexCall): R;\n createCollection(call: CreateCollectionCall): R;\n dropCollection(call: DropCollectionCall): R;\n collMod(call: CollModCall): R;\n}\n\nabstract class OpFactoryCallNode {\n abstract readonly factory: string;\n abstract readonly operationClass: MigrationOperationClass;\n abstract readonly label: string;\n abstract accept<R>(visitor: OpFactoryCallVisitor<R>): R;\n\n protected freeze(): void {\n Object.freeze(this);\n }\n}\n\nfunction formatKeys(keys: ReadonlyArray<MongoIndexKey>): string {\n return keys.map((k) => `${k.field}:${k.direction}`).join(', ');\n}\n\nexport class CreateIndexCall extends OpFactoryCallNode {\n readonly factory = 'createIndex' as const;\n readonly operationClass = 'additive' as const;\n readonly collection: string;\n readonly keys: ReadonlyArray<MongoIndexKey>;\n readonly options: CreateIndexOptions | undefined;\n readonly label: string;\n\n constructor(\n collection: string,\n keys: ReadonlyArray<MongoIndexKey>,\n options?: CreateIndexOptions,\n ) {\n super();\n this.collection = collection;\n this.keys = keys;\n this.options = options;\n this.label = `Create index on ${collection} (${formatKeys(keys)})`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.createIndex(this);\n }\n}\n\nexport class DropIndexCall extends OpFactoryCallNode {\n readonly factory = 'dropIndex' as const;\n readonly operationClass = 'destructive' as const;\n readonly collection: string;\n readonly keys: ReadonlyArray<MongoIndexKey>;\n readonly label: string;\n\n constructor(collection: string, keys: ReadonlyArray<MongoIndexKey>) {\n super();\n this.collection = collection;\n this.keys = keys;\n this.label = `Drop index on ${collection} (${formatKeys(keys)})`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.dropIndex(this);\n }\n}\n\nexport class CreateCollectionCall extends OpFactoryCallNode {\n readonly factory = 'createCollection' as const;\n readonly operationClass = 'additive' as const;\n readonly collection: string;\n readonly options: CreateCollectionOptions | undefined;\n readonly label: string;\n\n constructor(collection: string, options?: CreateCollectionOptions) {\n super();\n this.collection = collection;\n this.options = options;\n this.label = `Create collection ${collection}`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.createCollection(this);\n }\n}\n\nexport class DropCollectionCall extends OpFactoryCallNode {\n readonly factory = 'dropCollection' as const;\n readonly operationClass = 'destructive' as const;\n readonly collection: string;\n readonly label: string;\n\n constructor(collection: string) {\n super();\n this.collection = collection;\n this.label = `Drop collection ${collection}`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.dropCollection(this);\n }\n}\n\nexport class CollModCall extends OpFactoryCallNode {\n readonly factory = 'collMod' as const;\n readonly collection: string;\n readonly options: CollModOptions;\n readonly meta: CollModMeta | undefined;\n readonly operationClass: MigrationOperationClass;\n readonly label: string;\n\n constructor(collection: string, options: CollModOptions, meta?: CollModMeta) {\n super();\n this.collection = collection;\n this.options = options;\n this.meta = meta;\n this.operationClass = meta?.operationClass ?? 'destructive';\n this.label = meta?.label ?? `Modify collection ${collection}`;\n this.freeze();\n }\n\n accept<R>(visitor: OpFactoryCallVisitor<R>): R {\n return visitor.collMod(this);\n }\n}\n\nexport type OpFactoryCall =\n | CreateIndexCall\n | DropIndexCall\n | CreateCollectionCall\n | DropCollectionCall\n | CollModCall;\n\nexport function schemaIndexToCreateIndexOptions(index: MongoSchemaIndex): CreateIndexOptions {\n return {\n unique: index.unique || undefined,\n sparse: index.sparse,\n expireAfterSeconds: index.expireAfterSeconds,\n partialFilterExpression: index.partialFilterExpression,\n wildcardProjection: index.wildcardProjection,\n collation: index.collation,\n weights: index.weights,\n default_language: index.default_language,\n language_override: index.language_override,\n };\n}\n\nexport function schemaCollectionToCreateCollectionOptions(\n coll: MongoSchemaCollection,\n): CreateCollectionOptions | undefined {\n const opts: MongoSchemaCollectionOptions | undefined = coll.options;\n const validator: MongoSchemaValidator | undefined = coll.validator;\n if (!opts && !validator) return undefined;\n return {\n capped: opts?.capped ? true : undefined,\n size: opts?.capped?.size,\n max: opts?.capped?.max,\n timeseries: opts?.timeseries,\n collation: opts?.collation,\n clusteredIndex: opts?.clusteredIndex\n ? {\n key: { _id: 1 } as Record<string, number>,\n unique: true as boolean,\n ...(opts.clusteredIndex.name != null ? { name: opts.clusteredIndex.name } : {}),\n }\n : undefined,\n validator: validator ? { $jsonSchema: validator.jsonSchema } : undefined,\n validationLevel: validator?.validationLevel,\n validationAction: validator?.validationAction,\n changeStreamPreAndPostImages: opts?.changeStreamPreAndPostImages,\n };\n}\n","import type { MongoMigrationPlanOperation } from '@prisma-next/mongo-query-ast/control';\nimport {\n collMod,\n createCollection,\n createIndex,\n dropCollection,\n dropIndex,\n} from './migration-factories';\nimport type {\n CollModCall,\n CreateCollectionCall,\n CreateIndexCall,\n DropCollectionCall,\n DropIndexCall,\n OpFactoryCall,\n OpFactoryCallVisitor,\n} from './op-factory-call';\n\nconst renderVisitor: OpFactoryCallVisitor<MongoMigrationPlanOperation> = {\n createIndex(call: CreateIndexCall) {\n return createIndex(call.collection, call.keys, call.options);\n },\n dropIndex(call: DropIndexCall) {\n return dropIndex(call.collection, call.keys);\n },\n createCollection(call: CreateCollectionCall) {\n return createCollection(call.collection, call.options);\n },\n dropCollection(call: DropCollectionCall) {\n return dropCollection(call.collection);\n },\n collMod(call: CollModCall) {\n return collMod(call.collection, call.options, call.meta);\n },\n};\n\nexport function renderOps(calls: ReadonlyArray<OpFactoryCall>): MongoMigrationPlanOperation[] {\n return calls.map((call) => call.accept(renderVisitor));\n}\n","import { detectScaffoldRuntime, shebangLineFor } from '@prisma-next/migration-tools/migration-ts';\nimport type {\n CollModCall,\n CreateCollectionCall,\n CreateIndexCall,\n DropCollectionCall,\n DropIndexCall,\n OpFactoryCall,\n OpFactoryCallVisitor,\n} from './op-factory-call';\n\nexport interface RenderMigrationMeta {\n readonly from: string;\n readonly to: string;\n readonly kind?: string;\n readonly labels?: readonly string[];\n}\n\n/**\n * Render a list of Mongo `OpFactoryCall`s as a class-flow `migration.ts`\n * source string. The result is shebanged, extends the user-facing\n * `Migration` (i.e. `MongoMigration`) from `@prisma-next/family-mongo`, and\n * implements the abstract `operations` and `describe` members. `meta` is\n * always rendered — `describe()` is part of the `Migration` contract, so\n * even an empty stub must satisfy it; callers pass empty strings for a\n * migration-new scaffold.\n */\nexport function renderCallsToTypeScript(\n calls: ReadonlyArray<OpFactoryCall>,\n meta: RenderMigrationMeta,\n): string {\n const factoryNames = collectFactoryNames(calls);\n const imports = buildImports(factoryNames);\n const operationsBody = calls.map((c) => c.accept(renderCallVisitor)).join(',\\n');\n\n return [\n shebangLineFor(detectScaffoldRuntime()),\n imports,\n '',\n 'class M extends Migration {',\n buildDescribeMethod(meta),\n ' override get operations() {',\n ' return [',\n indent(operationsBody, 6),\n ' ];',\n ' }',\n '}',\n '',\n 'export default M;',\n 'Migration.run(import.meta.url, M);',\n '',\n ].join('\\n');\n}\n\nfunction collectFactoryNames(calls: ReadonlyArray<OpFactoryCall>): string[] {\n const names = new Set<string>();\n for (const call of calls) {\n names.add(call.factory);\n }\n return [...names].sort();\n}\n\nfunction buildImports(factoryNames: string[]): string {\n const lines = [\"import { Migration } from '@prisma-next/family-mongo/migration';\"];\n if (factoryNames.length > 0) {\n lines.push(`import { ${factoryNames.join(', ')} } from '@prisma-next/target-mongo/migration';`);\n }\n return lines.join('\\n');\n}\n\nfunction buildDescribeMethod(meta: RenderMigrationMeta): string {\n const lines: string[] = [];\n lines.push(' override describe() {');\n lines.push(' return {');\n lines.push(` from: ${JSON.stringify(meta.from)},`);\n lines.push(` to: ${JSON.stringify(meta.to)},`);\n if (meta.kind) {\n lines.push(` kind: ${JSON.stringify(meta.kind)},`);\n }\n if (meta.labels && meta.labels.length > 0) {\n lines.push(` labels: ${renderLiteral(meta.labels)},`);\n }\n lines.push(' };');\n lines.push(' }');\n lines.push('');\n return lines.join('\\n');\n}\n\nconst renderCallVisitor: OpFactoryCallVisitor<string> = {\n createIndex(call: CreateIndexCall) {\n return call.options\n ? `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)}, ${renderLiteral(call.options)})`\n : `createIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;\n },\n dropIndex(call: DropIndexCall) {\n return `dropIndex(${renderLiteral(call.collection)}, ${renderLiteral(call.keys)})`;\n },\n createCollection(call: CreateCollectionCall) {\n return call.options\n ? `createCollection(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})`\n : `createCollection(${renderLiteral(call.collection)})`;\n },\n dropCollection(call: DropCollectionCall) {\n return `dropCollection(${renderLiteral(call.collection)})`;\n },\n collMod(call: CollModCall) {\n return call.meta\n ? `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)}, ${renderLiteral(call.meta)})`\n : `collMod(${renderLiteral(call.collection)}, ${renderLiteral(call.options)})`;\n },\n};\n\nfunction renderLiteral(value: unknown): string {\n if (value === undefined) return 'undefined';\n if (value === null) return 'null';\n if (typeof value === 'string') return JSON.stringify(value);\n if (typeof value === 'number' || typeof value === 'boolean') return String(value);\n if (Array.isArray(value)) {\n if (value.length === 0) return '[]';\n const items = value.map((v) => renderLiteral(v));\n const singleLine = `[${items.join(', ')}]`;\n if (singleLine.length <= 80) return singleLine;\n return `[\\n${items.map((i) => ` ${i}`).join(',\\n')},\\n]`;\n }\n if (typeof value === 'object') {\n const entries = Object.entries(value).filter(([, v]) => v !== undefined);\n if (entries.length === 0) return '{}';\n const items = entries.map(([k, v]) => `${renderKey(k)}: ${renderLiteral(v)}`);\n const singleLine = `{ ${items.join(', ')} }`;\n if (singleLine.length <= 80) return singleLine;\n return `{\\n${items.map((i) => ` ${i}`).join(',\\n')},\\n}`;\n }\n return String(value);\n}\n\nfunction renderKey(key: string): string {\n if (key === '__proto__') return JSON.stringify(key);\n return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key) ? key : JSON.stringify(key);\n}\n\nfunction indent(text: string, spaces: number): string {\n const pad = ' '.repeat(spaces);\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${pad}${line}` : line))\n .join('\\n');\n}\n","import type { MigrationPlanWithAuthoringSurface } from '@prisma-next/framework-components/control';\nimport { Migration, type MigrationMeta } from '@prisma-next/migration-tools/migration';\nimport type { AnyMongoMigrationOperation } from '@prisma-next/mongo-query-ast/control';\nimport { ifDefined } from '@prisma-next/utils/defined';\nimport type { OpFactoryCall } from './op-factory-call';\nimport { renderOps } from './render-ops';\nimport { renderCallsToTypeScript } from './render-typescript';\n\n/**\n * Planner-produced Mongo migration, returned by `MongoMigrationPlanner.plan(...)`\n * and `MongoMigrationPlanner.emptyMigration(...)`.\n *\n * Unlike user-authored migrations (which extend `MongoMigration` from\n * `@prisma-next/family-mongo/migration`), this class lives inside the target\n * and holds the richer authoring IR (`OpFactoryCall[]`) needed to render\n * itself back to TypeScript source. It implements\n * `MigrationPlanWithAuthoringSurface` so that the CLI can uniformly ask any\n * planner result to serialize itself to a `migration.ts`.\n *\n * Extends the framework `Migration` base class directly (not\n * `MongoMigration`) because `MongoMigration` lives in `@prisma-next/family-mongo`,\n * which depends on this package — extending it here would create a dependency\n * cycle.\n */\nexport class PlannerProducedMongoMigration\n extends Migration<AnyMongoMigrationOperation>\n implements MigrationPlanWithAuthoringSurface\n{\n readonly targetId = 'mongo' as const;\n\n constructor(\n private readonly calls: readonly OpFactoryCall[],\n private readonly meta: MigrationMeta,\n ) {\n super();\n }\n\n override get operations(): readonly AnyMongoMigrationOperation[] {\n return renderOps(this.calls);\n }\n\n override describe(): MigrationMeta {\n return this.meta;\n }\n\n renderTypeScript(): string {\n return renderCallsToTypeScript(this.calls, {\n from: this.meta.from,\n to: this.meta.to,\n ...ifDefined('kind', this.meta.kind),\n ...ifDefined('labels', this.meta.labels),\n });\n }\n}\n","import type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';\nimport type {\n MigrationOperationClass,\n MigrationOperationPolicy,\n MigrationPlanner,\n MigrationPlannerConflict,\n MigrationPlannerResult,\n MigrationPlanWithAuthoringSurface,\n MigrationScaffoldContext,\n} from '@prisma-next/framework-components/control';\nimport type { MongoContract } from '@prisma-next/mongo-contract';\nimport type {\n MongoSchemaCollection,\n MongoSchemaCollectionOptions,\n MongoSchemaIndex,\n MongoSchemaIR,\n MongoSchemaValidator,\n} from '@prisma-next/mongo-schema-ir';\nimport { canonicalize, deepEqual } from '@prisma-next/mongo-schema-ir';\nimport { contractToMongoSchemaIR } from './contract-to-schema';\nimport type { OpFactoryCall } from './op-factory-call';\nimport {\n CollModCall,\n CreateCollectionCall,\n CreateIndexCall,\n DropCollectionCall,\n DropIndexCall,\n schemaCollectionToCreateCollectionOptions,\n schemaIndexToCreateIndexOptions,\n} from './op-factory-call';\nimport { PlannerProducedMongoMigration } from './planner-produced-migration';\n\nfunction buildIndexLookupKey(index: MongoSchemaIndex): string {\n const keys = index.keys.map((k) => `${k.field}:${k.direction}`).join(',');\n const opts = [\n index.unique ? 'unique' : '',\n index.sparse ? 'sparse' : '',\n index.expireAfterSeconds != null ? `ttl:${index.expireAfterSeconds}` : '',\n index.partialFilterExpression ? `pfe:${canonicalize(index.partialFilterExpression)}` : '',\n index.wildcardProjection ? `wp:${canonicalize(index.wildcardProjection)}` : '',\n index.collation ? `col:${canonicalize(index.collation)}` : '',\n index.weights ? `wt:${canonicalize(index.weights)}` : '',\n index.default_language ? `dl:${index.default_language}` : '',\n index.language_override ? `lo:${index.language_override}` : '',\n ]\n .filter(Boolean)\n .join(';');\n return opts ? `${keys}|${opts}` : keys;\n}\n\nfunction validatorsEqual(\n a: MongoSchemaValidator | undefined,\n b: MongoSchemaValidator | undefined,\n): boolean {\n if (!a && !b) return true;\n if (!a || !b) return false;\n return (\n a.validationLevel === b.validationLevel &&\n a.validationAction === b.validationAction &&\n canonicalize(a.jsonSchema) === canonicalize(b.jsonSchema)\n );\n}\n\nfunction classifyValidatorUpdate(\n origin: MongoSchemaValidator,\n dest: MongoSchemaValidator,\n): 'widening' | 'destructive' {\n let hasDestructive = false;\n\n if (canonicalize(origin.jsonSchema) !== canonicalize(dest.jsonSchema)) {\n hasDestructive = true;\n }\n\n if (origin.validationAction !== dest.validationAction) {\n if (dest.validationAction === 'error') hasDestructive = true;\n }\n\n if (origin.validationLevel !== dest.validationLevel) {\n if (dest.validationLevel === 'strict') hasDestructive = true;\n }\n\n return hasDestructive ? 'destructive' : 'widening';\n}\n\nfunction hasImmutableOptionChange(\n origin: MongoSchemaCollectionOptions | undefined,\n dest: MongoSchemaCollectionOptions | undefined,\n): string | undefined {\n if (canonicalize(origin?.capped) !== canonicalize(dest?.capped)) return 'capped';\n if (canonicalize(origin?.timeseries) !== canonicalize(dest?.timeseries)) return 'timeseries';\n if (canonicalize(origin?.collation) !== canonicalize(dest?.collation)) return 'collation';\n if (canonicalize(origin?.clusteredIndex) !== canonicalize(dest?.clusteredIndex))\n return 'clusteredIndex';\n return undefined;\n}\n\nfunction collectionHasOptions(coll: MongoSchemaCollection): boolean {\n return !!(coll.options || coll.validator);\n}\n\nexport type PlanCallsResult =\n | { readonly kind: 'success'; readonly calls: OpFactoryCall[] }\n | { readonly kind: 'failure'; readonly conflicts: MigrationPlannerConflict[] };\n\nexport class MongoMigrationPlanner implements MigrationPlanner<'mongo', 'mongo'> {\n planCalls(options: {\n readonly contract: unknown;\n readonly schema: unknown;\n readonly policy: MigrationOperationPolicy;\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;\n }): PlanCallsResult {\n const contract = options.contract as MongoContract;\n const originIR = options.schema as MongoSchemaIR;\n const destinationIR = contractToMongoSchemaIR(contract);\n\n const collCreates: OpFactoryCall[] = [];\n const drops: OpFactoryCall[] = [];\n const creates: OpFactoryCall[] = [];\n const validatorOps: OpFactoryCall[] = [];\n const mutableOptionOps: OpFactoryCall[] = [];\n const collDrops: OpFactoryCall[] = [];\n const conflicts: MigrationPlannerConflict[] = [];\n\n const allCollectionNames = new Set([\n ...originIR.collectionNames,\n ...destinationIR.collectionNames,\n ]);\n\n for (const collName of [...allCollectionNames].sort()) {\n const originColl = originIR.collection(collName);\n const destColl = destinationIR.collection(collName);\n\n if (!originColl) {\n if (destColl && collectionHasOptions(destColl)) {\n const opts = schemaCollectionToCreateCollectionOptions(destColl);\n collCreates.push(new CreateCollectionCall(collName, opts));\n }\n } else if (!destColl) {\n collDrops.push(new DropCollectionCall(collName));\n } else {\n const immutableChange = hasImmutableOptionChange(originColl.options, destColl.options);\n if (immutableChange) {\n conflicts.push({\n kind: 'policy-violation',\n summary: `Cannot change immutable collection option '${immutableChange}' on ${collName}`,\n why: `MongoDB does not support modifying the '${immutableChange}' option after collection creation`,\n });\n }\n\n const mutableCall = planMutableOptionsDiffCall(\n collName,\n originColl.options,\n destColl.options,\n );\n if (mutableCall) mutableOptionOps.push(mutableCall);\n\n const validatorCall = planValidatorDiffCall(\n collName,\n originColl.validator,\n destColl.validator,\n );\n if (validatorCall) validatorOps.push(validatorCall);\n }\n\n const originLookup = new Map<string, MongoSchemaIndex>();\n if (originColl) {\n for (const idx of originColl.indexes) {\n originLookup.set(buildIndexLookupKey(idx), idx);\n }\n }\n\n const destLookup = new Map<string, MongoSchemaIndex>();\n if (destColl) {\n for (const idx of destColl.indexes) {\n destLookup.set(buildIndexLookupKey(idx), idx);\n }\n }\n\n for (const [lookupKey, idx] of originLookup) {\n if (!destLookup.has(lookupKey)) {\n drops.push(new DropIndexCall(collName, idx.keys));\n }\n }\n\n for (const [lookupKey, idx] of destLookup) {\n if (!originLookup.has(lookupKey)) {\n creates.push(\n new CreateIndexCall(collName, idx.keys, schemaIndexToCreateIndexOptions(idx)),\n );\n }\n }\n }\n\n if (conflicts.length > 0) {\n return { kind: 'failure', conflicts };\n }\n\n const allCalls = [\n ...collCreates,\n ...drops,\n ...creates,\n ...validatorOps,\n ...mutableOptionOps,\n ...collDrops,\n ];\n\n for (const call of allCalls) {\n if (!options.policy.allowedOperationClasses.includes(call.operationClass)) {\n conflicts.push({\n kind: 'policy-violation',\n summary: `${call.operationClass} operation disallowed: ${call.label}`,\n why: `Policy does not allow '${call.operationClass}' operations`,\n });\n }\n }\n\n if (conflicts.length > 0) {\n return { kind: 'failure', conflicts };\n }\n\n return { kind: 'success', calls: allCalls };\n }\n\n plan(options: {\n readonly contract: unknown;\n readonly schema: unknown;\n readonly policy: MigrationOperationPolicy;\n readonly fromHash: string;\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;\n }): MigrationPlannerResult {\n const contract = options.contract as MongoContract;\n const result = this.planCalls(options);\n if (result.kind === 'failure') return result;\n return {\n kind: 'success',\n plan: new PlannerProducedMongoMigration(result.calls, {\n from: options.fromHash,\n to: contract.storage.storageHash,\n }),\n };\n }\n\n /**\n * Produce an empty `migration.ts` authoring surface for `migration new`.\n *\n * Mongo is a class-flow target, so the \"empty migration\" is a\n * `PlannerProducedMongoMigration` with no operations; `renderTypeScript()`\n * emits a stub class with the correct `from`/`to` metadata that the user\n * then fills in with operations. The contract path on the context is\n * unused — Mongo's emitted source does not import from the generated\n * contract `.d.ts`.\n */\n emptyMigration(context: MigrationScaffoldContext): MigrationPlanWithAuthoringSurface {\n return new PlannerProducedMongoMigration([], {\n from: context.fromHash,\n to: context.toHash,\n });\n }\n}\n\nfunction planValidatorDiffCall(\n collName: string,\n originValidator: MongoSchemaValidator | undefined,\n destValidator: MongoSchemaValidator | undefined,\n): OpFactoryCall | undefined {\n if (validatorsEqual(originValidator, destValidator)) return undefined;\n\n if (destValidator) {\n const operationClass: MigrationOperationClass = originValidator\n ? classifyValidatorUpdate(originValidator, destValidator)\n : 'destructive';\n return new CollModCall(\n collName,\n {\n validator: { $jsonSchema: destValidator.jsonSchema },\n validationLevel: destValidator.validationLevel,\n validationAction: destValidator.validationAction,\n },\n {\n id: `validator.${collName}.${originValidator ? 'update' : 'add'}`,\n label: `${originValidator ? 'Update' : 'Add'} validator on ${collName}`,\n operationClass,\n },\n );\n }\n\n return new CollModCall(\n collName,\n {\n validator: {},\n validationLevel: 'strict',\n validationAction: 'error',\n },\n {\n id: `validator.${collName}.remove`,\n label: `Remove validator on ${collName}`,\n operationClass: 'widening',\n },\n );\n}\n\nfunction planMutableOptionsDiffCall(\n collName: string,\n origin: MongoSchemaCollectionOptions | undefined,\n dest: MongoSchemaCollectionOptions | undefined,\n): OpFactoryCall | undefined {\n const originCSPPI = origin?.changeStreamPreAndPostImages;\n const destCSPPI = dest?.changeStreamPreAndPostImages;\n if (deepEqual(originCSPPI, destCSPPI)) return undefined;\n\n const desiredCSPPI = destCSPPI ?? { enabled: false };\n return new CollModCall(\n collName,\n {\n changeStreamPreAndPostImages: desiredCSPPI,\n },\n {\n id: `options.${collName}.update`,\n label: `Update mutable options on ${collName}`,\n operationClass: desiredCSPPI.enabled ? 'widening' : 'destructive',\n },\n );\n}\n","import type { ContractMarkerRecord } from '@prisma-next/contract/types';\nimport type { TargetBoundComponentDescriptor } from '@prisma-next/framework-components/components';\nimport type {\n MigrationOperationPolicy,\n MigrationPlan,\n MigrationPlanOperation,\n MigrationRunnerExecutionChecks,\n MigrationRunnerFailure,\n MigrationRunnerResult,\n} from '@prisma-next/framework-components/control';\nimport type {\n MongoDdlCommandVisitor,\n MongoInspectionCommandVisitor,\n MongoMigrationCheck,\n MongoMigrationPlanOperation,\n} from '@prisma-next/mongo-query-ast/control';\nimport { notOk, ok } from '@prisma-next/utils/result';\nimport { FilterEvaluator } from './filter-evaluator';\nimport { deserializeMongoOps } from './mongo-ops-serializer';\n\nexport interface MarkerOperations {\n readMarker(): Promise<ContractMarkerRecord | null>;\n initMarker(destination: {\n readonly storageHash: string;\n readonly profileHash: string;\n }): Promise<void>;\n updateMarker(\n expectedFrom: string,\n destination: { readonly storageHash: string; readonly profileHash: string },\n ): Promise<boolean>;\n writeLedgerEntry(entry: {\n readonly edgeId: string;\n readonly from: string;\n readonly to: string;\n }): Promise<void>;\n}\n\nexport interface MongoRunnerDependencies {\n readonly commandExecutor: MongoDdlCommandVisitor<Promise<void>>;\n readonly inspectionExecutor: MongoInspectionCommandVisitor<Promise<Record<string, unknown>[]>>;\n readonly markerOps: MarkerOperations;\n}\n\nfunction runnerFailure(\n code: string,\n summary: string,\n opts?: { why?: string; meta?: Record<string, unknown> },\n): MigrationRunnerResult {\n return notOk<MigrationRunnerFailure>({\n code,\n summary,\n ...opts,\n });\n}\n\nexport class MongoMigrationRunner {\n constructor(private readonly deps: MongoRunnerDependencies) {}\n\n async execute(options: {\n readonly plan: MigrationPlan;\n readonly destinationContract: unknown;\n readonly policy: MigrationOperationPolicy;\n readonly callbacks?: {\n onOperationStart?(op: MigrationPlanOperation): void;\n onOperationComplete?(op: MigrationPlanOperation): void;\n };\n readonly executionChecks?: MigrationRunnerExecutionChecks;\n readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;\n }): Promise<MigrationRunnerResult> {\n const { commandExecutor, inspectionExecutor, markerOps } = this.deps;\n const operations = deserializeMongoOps(options.plan.operations as readonly unknown[]);\n\n const policyCheck = this.enforcePolicyCompatibility(options.policy, operations);\n if (policyCheck) return policyCheck;\n\n const existingMarker = await markerOps.readMarker();\n\n const markerCheck = this.ensureMarkerCompatibility(existingMarker, options.plan);\n if (markerCheck) return markerCheck;\n\n const checks = options.executionChecks;\n const runPrechecks = checks?.prechecks !== false;\n const runPostchecks = checks?.postchecks !== false;\n const runIdempotency = checks?.idempotencyChecks !== false;\n\n const filterEvaluator = new FilterEvaluator();\n\n let operationsExecuted = 0;\n\n for (const operation of operations) {\n options.callbacks?.onOperationStart?.(operation);\n try {\n if (runPostchecks && runIdempotency) {\n const allSatisfied = await this.allChecksSatisfied(\n operation.postcheck,\n inspectionExecutor,\n filterEvaluator,\n );\n if (allSatisfied) continue;\n }\n\n if (runPrechecks) {\n const precheckResult = await this.evaluateChecks(\n operation.precheck,\n inspectionExecutor,\n filterEvaluator,\n );\n if (!precheckResult) {\n return runnerFailure(\n 'PRECHECK_FAILED',\n `Operation ${operation.id} failed during precheck`,\n { meta: { operationId: operation.id } },\n );\n }\n }\n\n for (const step of operation.execute) {\n await step.command.accept(commandExecutor);\n }\n\n if (runPostchecks) {\n const postcheckResult = await this.evaluateChecks(\n operation.postcheck,\n inspectionExecutor,\n filterEvaluator,\n );\n if (!postcheckResult) {\n return runnerFailure(\n 'POSTCHECK_FAILED',\n `Operation ${operation.id} failed during postcheck`,\n { meta: { operationId: operation.id } },\n );\n }\n }\n\n operationsExecuted += 1;\n } finally {\n options.callbacks?.onOperationComplete?.(operation);\n }\n }\n\n const destination = options.plan.destination;\n const contract = options.destinationContract as { profileHash?: string };\n const profileHash = contract.profileHash ?? destination.storageHash;\n\n if (\n operationsExecuted === 0 &&\n existingMarker?.storageHash === destination.storageHash &&\n existingMarker.profileHash === profileHash\n ) {\n return ok({ operationsPlanned: operations.length, operationsExecuted });\n }\n\n if (existingMarker) {\n const updated = await markerOps.updateMarker(existingMarker.storageHash, {\n storageHash: destination.storageHash,\n profileHash,\n });\n if (!updated) {\n return runnerFailure(\n 'MARKER_CAS_FAILURE',\n 'Marker was modified by another process during migration execution.',\n {\n meta: {\n expectedStorageHash: existingMarker.storageHash,\n destinationStorageHash: destination.storageHash,\n },\n },\n );\n }\n } else {\n await markerOps.initMarker({\n storageHash: destination.storageHash,\n profileHash,\n });\n }\n\n const originHash = existingMarker?.storageHash ?? '';\n await markerOps.writeLedgerEntry({\n edgeId: `${originHash}->${destination.storageHash}`,\n from: originHash,\n to: destination.storageHash,\n });\n\n return ok({ operationsPlanned: operations.length, operationsExecuted });\n }\n\n private async evaluateChecks(\n checks: readonly MongoMigrationCheck[],\n inspectionExecutor: MongoInspectionCommandVisitor<Promise<Record<string, unknown>[]>>,\n filterEvaluator: FilterEvaluator,\n ): Promise<boolean> {\n for (const check of checks) {\n const documents = await check.source.accept(inspectionExecutor);\n const matchFound = documents.some((doc) =>\n filterEvaluator.evaluate(check.filter, doc as Record<string, unknown>),\n );\n const passed = check.expect === 'exists' ? matchFound : !matchFound;\n if (!passed) return false;\n }\n return true;\n }\n\n private async allChecksSatisfied(\n checks: readonly MongoMigrationCheck[],\n inspectionExecutor: MongoInspectionCommandVisitor<Promise<Record<string, unknown>[]>>,\n filterEvaluator: FilterEvaluator,\n ): Promise<boolean> {\n if (checks.length === 0) return false;\n return this.evaluateChecks(checks, inspectionExecutor, filterEvaluator);\n }\n\n private enforcePolicyCompatibility(\n policy: MigrationOperationPolicy,\n operations: readonly MongoMigrationPlanOperation[],\n ): MigrationRunnerResult | undefined {\n const allowedClasses = new Set(policy.allowedOperationClasses);\n for (const operation of operations) {\n if (!allowedClasses.has(operation.operationClass)) {\n return runnerFailure(\n 'POLICY_VIOLATION',\n `Operation ${operation.id} has class \"${operation.operationClass}\" which is not allowed by policy.`,\n {\n why: `Policy only allows: ${[...allowedClasses].join(', ')}.`,\n meta: {\n operationId: operation.id,\n operationClass: operation.operationClass,\n },\n },\n );\n }\n }\n return undefined;\n }\n\n private ensureMarkerCompatibility(\n marker: ContractMarkerRecord | null,\n plan: MigrationPlan,\n ): MigrationRunnerResult | undefined {\n const origin = plan.origin ?? null;\n if (!origin) {\n if (marker) {\n return runnerFailure(\n 'MARKER_ORIGIN_MISMATCH',\n 'Database already has a contract marker but the plan has no origin. This would silently overwrite the existing marker.',\n { meta: { markerStorageHash: marker.storageHash } },\n );\n }\n return undefined;\n }\n\n if (!marker) {\n return runnerFailure(\n 'MARKER_ORIGIN_MISMATCH',\n `Missing contract marker: expected origin storage hash ${origin.storageHash}.`,\n { meta: { expectedOriginStorageHash: origin.storageHash } },\n );\n }\n\n if (marker.storageHash !== origin.storageHash) {\n return runnerFailure(\n 'MARKER_ORIGIN_MISMATCH',\n `Existing contract marker (${marker.storageHash}) does not match plan origin (${origin.storageHash}).`,\n {\n meta: {\n markerStorageHash: marker.storageHash,\n expectedOriginStorageHash: origin.storageHash,\n },\n },\n );\n }\n\n return undefined;\n }\n}\n"],"mappings":";;;;;;;;;;;AAeA,SAAS,aAAa,OAA4C;AAChE,QAAO,IAAI,iBAAiB;EAC1B,MAAM,MAAM;EACZ,QAAQ,MAAM;EACd,QAAQ,MAAM;EACd,oBAAoB,MAAM;EAC1B,yBAAyB,MAAM;EAC/B,oBAAoB,MAAM;EAC1B,WAAW,MAAM;EACjB,SAAS,MAAM;EACf,kBAAkB,MAAM;EACxB,mBAAmB,MAAM;EAC1B,CAAC;;AAGJ,SAAS,iBAAiB,GAAgD;AACxE,QAAO,IAAI,qBAAqB;EAC9B,YAAY,EAAE;EACd,iBAAiB,EAAE;EACnB,kBAAkB,EAAE;EACrB,CAAC;;AAGJ,SAAS,eAAe,GAAgE;AACtF,QAAO,IAAI,6BAA6B,EAAE;;AAG5C,SAAS,kBAAkB,MAAc,KAAoD;AAE3F,QAAO,IAAI,sBAAsB;EAC/B;EACA,UAHe,IAAI,WAAW,EAAE,EAAE,IAAI,aAAa;EAInD,GAAI,IAAI,aAAa,QAAQ,EAAE,WAAW,iBAAiB,IAAI,UAAU,EAAE;EAC3E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a,UAAU,GAAG,cAAc,UAAU,eAAe,oCACjE;GACE,KAAK,uBAAuB,CAAC,GAAG,eAAe,CAAC,KAAK,KAAK,CAAC;GAC3D,MAAM;IACJ,aAAa,UAAU;IACvB,gBAAgB,UAAU;IAC3B;GACF,CACF;;CAMP,AAAQ,0BACN,QACA,MACmC;EACnC,MAAM,SAAS,KAAK,UAAU;AAC9B,MAAI,CAAC,QAAQ;AACX,OAAI,OACF,QAAO,cACL,0BACA,yHACA,EAAE,MAAM,EAAE,mBAAmB,OAAO,aAAa,EAAE,CACpD;AAEH;;AAGF,MAAI,CAAC,OACH,QAAO,cACL,0BACA,yDAAyD,OAAO,YAAY,IAC5E,EAAE,MAAM,EAAE,2BAA2B,OAAO,aAAa,EAAE,CAC5D;AAGH,MAAI,OAAO,gBAAgB,OAAO,YAChC,QAAO,cACL,0BACA,6BAA6B,OAAO,YAAY,gCAAgC,OAAO,YAAY,KACnG,EACE,MAAM;GACJ,mBAAmB,OAAO;GAC1B,2BAA2B,OAAO;GACnC,EACF,CACF"}
@@ -1,5 +1,6 @@
1
1
  import { n as CollModMeta } from "./op-factory-call-CfPGebEH.mjs";
2
2
  import { CollModOptions, CreateCollectionOptions, CreateIndexOptions, MongoIndexKey, MongoMigrationPlanOperation } from "@prisma-next/mongo-query-ast/control";
3
+ import { placeholder } from "@prisma-next/errors/migration";
3
4
 
4
5
  //#region src/core/migration-factories.d.ts
5
6
  declare function createIndex(collection: string, keys: ReadonlyArray<MongoIndexKey>, options?: CreateIndexOptions): MongoMigrationPlanOperation;
@@ -16,5 +17,5 @@ declare function validatedCollection(name: string, schema: Record<string, unknow
16
17
  unique?: boolean;
17
18
  }>): MongoMigrationPlanOperation[];
18
19
  //#endregion
19
- export { collMod, createCollection, createIndex, dropCollection, dropIndex, setValidation, validatedCollection };
20
+ export { collMod, createCollection, createIndex, dropCollection, dropIndex, placeholder, setValidation, validatedCollection };
20
21
  //# sourceMappingURL=migration.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"migration.d.mts","names":[],"sources":["../src/core/migration-factories.ts"],"sourcesContent":[],"mappings":";;;;iBAmCgB,WAAA,2BAER,cAAc,0BACV,qBACT;AAJa,iBA4CA,SAAA,CA5CW,UAAA,EAAA,MAAA,EAAA,IAAA,EA8CnB,aA9CmB,CA8CL,aA9CK,CAAA,CAAA,EA+CxB,2BA/CwB;AAEL,iBA8EN,gBAAA,CA9EM,UAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EAgFV,uBAhFU,CAAA,EAiFnB,2BAjFmB;AAAd,iBAwGQ,cAAA,CAxGR,UAAA,EAAA,MAAA,CAAA,EAwG4C,2BAxG5C;AACI,iBAuHI,aAAA,CAvHJ,UAAA,EAAA,MAAA,EAAA,MAAA,EAyHF,MAzHE,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,OAyCZ,CAzCY,EAAA;EACT,eAAA,CAAA,EAAA,QAAA,GAAA,UAAA;EAA2B,gBAAA,CAAA,EAAA,OAAA,GAAA,MAAA;AAwC9B,CAAA,CAAA,EAkFG,2BAlFsB;AAEH,iBAoGN,OAAA,CApGM,UAAA,EAAA,MAAA,EAAA,OAAA,EAsGX,cAtGW,EAAA,IAAA,CAAA,EAuGb,WAvGa,CAAA,EAwGnB,2BAxGmB;AAAd,iBAqJQ,mBAAA,CArJR,IAAA,EAAA,MAAA,EAAA,MAAA,EAuJE,MAvJF,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,OAAA,EAwJG,aAxJH,CAAA;EACL,IAAA,EAuJ8B,aAvJ9B,EAAA;EAA2B,MAAA,CAAA,EAAA,OAAA;AAiC9B,CAAA,CAAA,CAAA,EAuHG,2BAvH6B,EAEpB"}
1
+ {"version":3,"file":"migration.d.mts","names":[],"sources":["../src/core/migration-factories.ts"],"sourcesContent":[],"mappings":";;;;;iBAmCgB,WAAA,2BAER,cAAc,0BACV,qBACT;iBAwCa,SAAA,2BAER,cAAc,iBACnB;AA/Ca,iBAgFA,gBAAA,CAhFW,UAAA,EAAA,MAAA,EAAA,OAAA,CAAA,EAkFf,uBAlFe,CAAA,EAmFxB,2BAnFwB;AAEL,iBAwGN,cAAA,CAxGM,UAAA,EAAA,MAAA,CAAA,EAwG8B,2BAxG9B;AAAd,iBAwHQ,aAAA,CAxHR,UAAA,EAAA,MAAA,EAAA,MAAA,EA0HE,MA1HF,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,OAEsB,CAFtB,EAAA;EACI,eAAA,CAAA,EAAA,QAAA,GAAA,UAAA;EACT,gBAAA,CAAA,EAAA,OAAA,GAAA,MAAA;CAA2B,CAAA,EA0H3B,2BA1H2B;AAwCd,iBAsGA,OAAA,CAtGS,UAAA,EAAA,MAAA,EAAA,OAAA,EAwGd,cAxGc,EAAA,IAAA,CAAA,EAyGhB,WAzGgB,CAAA,EA0GtB,2BA1GsB;AAEH,iBAqJN,mBAAA,CArJM,IAAA,EAAA,MAAA,EAAA,MAAA,EAuJZ,MAvJY,CAAA,MAAA,EAAA,OAAA,CAAA,EAAA,OAAA,EAwJX,aAxJW,CAAA;EAAd,IAAA,EAwJyB,aAxJzB,EAAA;EACL,MAAA,CAAA,EAAA,OAAA;CAA2B,CAAA,CAAA,EAwJ3B,2BAxJ2B,EAAA"}
@@ -1,3 +1,4 @@
1
1
  import { a as dropIndex, i as dropCollection, n as createCollection, o as setValidation, r as createIndex, s as validatedCollection, t as collMod } from "./migration-factories-Brzz-QGG.mjs";
2
+ import { placeholder } from "@prisma-next/errors/migration";
2
3
 
3
- export { collMod, createCollection, createIndex, dropCollection, dropIndex, setValidation, validatedCollection };
4
+ export { collMod, createCollection, createIndex, dropCollection, dropIndex, placeholder, setValidation, validatedCollection };
package/package.json CHANGED
@@ -1,19 +1,21 @@
1
1
  {
2
2
  "name": "@prisma-next/target-mongo",
3
- "version": "0.4.0-dev.5",
3
+ "version": "0.4.0-dev.7",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "MongoDB target pack for Prisma Next",
7
7
  "dependencies": {
8
8
  "arktype": "^2.1.29",
9
9
  "mongodb": "^6.16.0",
10
- "@prisma-next/contract": "0.4.0-dev.5",
11
- "@prisma-next/mongo-contract": "0.4.0-dev.5",
12
- "@prisma-next/mongo-query-ast": "0.4.0-dev.5",
13
- "@prisma-next/framework-components": "0.4.0-dev.5",
14
- "@prisma-next/mongo-schema-ir": "0.4.0-dev.5",
15
- "@prisma-next/utils": "0.4.0-dev.5",
16
- "@prisma-next/mongo-value": "0.4.0-dev.5"
10
+ "@prisma-next/errors": "0.4.0-dev.7",
11
+ "@prisma-next/contract": "0.4.0-dev.7",
12
+ "@prisma-next/framework-components": "0.4.0-dev.7",
13
+ "@prisma-next/migration-tools": "0.4.0-dev.7",
14
+ "@prisma-next/mongo-contract": "0.4.0-dev.7",
15
+ "@prisma-next/mongo-value": "0.4.0-dev.7",
16
+ "@prisma-next/mongo-schema-ir": "0.4.0-dev.7",
17
+ "@prisma-next/utils": "0.4.0-dev.7",
18
+ "@prisma-next/mongo-query-ast": "0.4.0-dev.7"
17
19
  },
18
20
  "devDependencies": {
19
21
  "mongodb-memory-server": "10.4.3",
@@ -22,8 +24,8 @@
22
24
  "typescript": "5.9.3",
23
25
  "vitest": "4.0.17",
24
26
  "@prisma-next/test-utils": "0.0.1",
25
- "@prisma-next/tsdown": "0.0.0",
26
- "@prisma-next/tsconfig": "0.0.0"
27
+ "@prisma-next/tsconfig": "0.0.0",
28
+ "@prisma-next/tsdown": "0.0.0"
27
29
  },
28
30
  "files": [
29
31
  "dist",
@@ -5,6 +5,8 @@ import type {
5
5
  MigrationPlanner,
6
6
  MigrationPlannerConflict,
7
7
  MigrationPlannerResult,
8
+ MigrationPlanWithAuthoringSurface,
9
+ MigrationScaffoldContext,
8
10
  } from '@prisma-next/framework-components/control';
9
11
  import type { MongoContract } from '@prisma-next/mongo-contract';
10
12
  import type {
@@ -26,7 +28,7 @@ import {
26
28
  schemaCollectionToCreateCollectionOptions,
27
29
  schemaIndexToCreateIndexOptions,
28
30
  } from './op-factory-call';
29
- import { renderOps } from './render-ops';
31
+ import { PlannerProducedMongoMigration } from './planner-produced-migration';
30
32
 
31
33
  function buildIndexLookupKey(index: MongoSchemaIndex): string {
32
34
  const keys = index.keys.map((k) => `${k.field}:${k.direction}`).join(',');
@@ -223,6 +225,7 @@ export class MongoMigrationPlanner implements MigrationPlanner<'mongo', 'mongo'>
223
225
  readonly contract: unknown;
224
226
  readonly schema: unknown;
225
227
  readonly policy: MigrationOperationPolicy;
228
+ readonly fromHash: string;
226
229
  readonly frameworkComponents: ReadonlyArray<TargetBoundComponentDescriptor<'mongo', 'mongo'>>;
227
230
  }): MigrationPlannerResult {
228
231
  const contract = options.contract as MongoContract;
@@ -230,15 +233,29 @@ export class MongoMigrationPlanner implements MigrationPlanner<'mongo', 'mongo'>
230
233
  if (result.kind === 'failure') return result;
231
234
  return {
232
235
  kind: 'success',
233
- plan: {
234
- targetId: 'mongo',
235
- destination: {
236
- storageHash: contract.storage.storageHash,
237
- },
238
- operations: renderOps(result.calls),
239
- },
236
+ plan: new PlannerProducedMongoMigration(result.calls, {
237
+ from: options.fromHash,
238
+ to: contract.storage.storageHash,
239
+ }),
240
240
  };
241
241
  }
242
+
243
+ /**
244
+ * Produce an empty `migration.ts` authoring surface for `migration new`.
245
+ *
246
+ * Mongo is a class-flow target, so the "empty migration" is a
247
+ * `PlannerProducedMongoMigration` with no operations; `renderTypeScript()`
248
+ * emits a stub class with the correct `from`/`to` metadata that the user
249
+ * then fills in with operations. The contract path on the context is
250
+ * unused — Mongo's emitted source does not import from the generated
251
+ * contract `.d.ts`.
252
+ */
253
+ emptyMigration(context: MigrationScaffoldContext): MigrationPlanWithAuthoringSurface {
254
+ return new PlannerProducedMongoMigration([], {
255
+ from: context.fromHash,
256
+ to: context.toHash,
257
+ });
258
+ }
242
259
  }
243
260
 
244
261
  function planValidatorDiffCall(
@@ -0,0 +1,54 @@
1
+ import type { MigrationPlanWithAuthoringSurface } from '@prisma-next/framework-components/control';
2
+ import { Migration, type MigrationMeta } from '@prisma-next/migration-tools/migration';
3
+ import type { AnyMongoMigrationOperation } from '@prisma-next/mongo-query-ast/control';
4
+ import { ifDefined } from '@prisma-next/utils/defined';
5
+ import type { OpFactoryCall } from './op-factory-call';
6
+ import { renderOps } from './render-ops';
7
+ import { renderCallsToTypeScript } from './render-typescript';
8
+
9
+ /**
10
+ * Planner-produced Mongo migration, returned by `MongoMigrationPlanner.plan(...)`
11
+ * and `MongoMigrationPlanner.emptyMigration(...)`.
12
+ *
13
+ * Unlike user-authored migrations (which extend `MongoMigration` from
14
+ * `@prisma-next/family-mongo/migration`), this class lives inside the target
15
+ * and holds the richer authoring IR (`OpFactoryCall[]`) needed to render
16
+ * itself back to TypeScript source. It implements
17
+ * `MigrationPlanWithAuthoringSurface` so that the CLI can uniformly ask any
18
+ * planner result to serialize itself to a `migration.ts`.
19
+ *
20
+ * Extends the framework `Migration` base class directly (not
21
+ * `MongoMigration`) because `MongoMigration` lives in `@prisma-next/family-mongo`,
22
+ * which depends on this package — extending it here would create a dependency
23
+ * cycle.
24
+ */
25
+ export class PlannerProducedMongoMigration
26
+ extends Migration<AnyMongoMigrationOperation>
27
+ implements MigrationPlanWithAuthoringSurface
28
+ {
29
+ readonly targetId = 'mongo' as const;
30
+
31
+ constructor(
32
+ private readonly calls: readonly OpFactoryCall[],
33
+ private readonly meta: MigrationMeta,
34
+ ) {
35
+ super();
36
+ }
37
+
38
+ override get operations(): readonly AnyMongoMigrationOperation[] {
39
+ return renderOps(this.calls);
40
+ }
41
+
42
+ override describe(): MigrationMeta {
43
+ return this.meta;
44
+ }
45
+
46
+ renderTypeScript(): string {
47
+ return renderCallsToTypeScript(this.calls, {
48
+ from: this.meta.from,
49
+ to: this.meta.to,
50
+ ...ifDefined('kind', this.meta.kind),
51
+ ...ifDefined('labels', this.meta.labels),
52
+ });
53
+ }
54
+ }
@@ -1,3 +1,4 @@
1
+ import { detectScaffoldRuntime, shebangLineFor } from '@prisma-next/migration-tools/migration-ts';
1
2
  import type {
2
3
  CollModCall,
3
4
  CreateCollectionCall,
@@ -15,23 +16,32 @@ export interface RenderMigrationMeta {
15
16
  readonly labels?: readonly string[];
16
17
  }
17
18
 
18
- export function renderTypeScript(
19
+ /**
20
+ * Render a list of Mongo `OpFactoryCall`s as a class-flow `migration.ts`
21
+ * source string. The result is shebanged, extends the user-facing
22
+ * `Migration` (i.e. `MongoMigration`) from `@prisma-next/family-mongo`, and
23
+ * implements the abstract `operations` and `describe` members. `meta` is
24
+ * always rendered — `describe()` is part of the `Migration` contract, so
25
+ * even an empty stub must satisfy it; callers pass empty strings for a
26
+ * migration-new scaffold.
27
+ */
28
+ export function renderCallsToTypeScript(
19
29
  calls: ReadonlyArray<OpFactoryCall>,
20
- meta?: RenderMigrationMeta,
30
+ meta: RenderMigrationMeta,
21
31
  ): string {
22
32
  const factoryNames = collectFactoryNames(calls);
23
33
  const imports = buildImports(factoryNames);
24
- const planBody = calls.map((c) => c.accept(renderCallVisitor)).join(',\n');
25
- const describeMethod = meta ? buildDescribeMethod(meta) : '';
34
+ const operationsBody = calls.map((c) => c.accept(renderCallVisitor)).join(',\n');
26
35
 
27
36
  return [
37
+ shebangLineFor(detectScaffoldRuntime()),
28
38
  imports,
29
39
  '',
30
40
  'class M extends Migration {',
31
- describeMethod,
32
- ' override plan() {',
41
+ buildDescribeMethod(meta),
42
+ ' override get operations() {',
33
43
  ' return [',
34
- indent(planBody, 6),
44
+ indent(operationsBody, 6),
35
45
  ' ];',
36
46
  ' }',
37
47
  '}',
@@ -21,6 +21,7 @@ export {
21
21
  schemaCollectionToCreateCollectionOptions,
22
22
  schemaIndexToCreateIndexOptions,
23
23
  } from '../core/op-factory-call';
24
+ export { PlannerProducedMongoMigration } from '../core/planner-produced-migration';
24
25
  export { renderOps } from '../core/render-ops';
25
26
  export type { RenderMigrationMeta } from '../core/render-typescript';
26
- export { renderTypeScript } from '../core/render-typescript';
27
+ export { renderCallsToTypeScript } from '../core/render-typescript';
@@ -1,3 +1,4 @@
1
+ export { placeholder } from '@prisma-next/errors/migration';
1
2
  export {
2
3
  collMod,
3
4
  createCollection,