@prisma/orm-family-mongo 8.0.0-rc.23-dev.1 → 8.0.0-rc.23-dev.2

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.
@@ -5,14 +5,15 @@ import { t as contractError } from "./contract-errors-BCN6RVI5-kMurtAbI.mjs";
5
5
  import { blindCast } from "@prisma/orm-framework/utils/casts";
6
6
  import { ifDefined } from "@prisma/orm-framework/utils/defined";
7
7
  import { structuredError } from "@prisma/orm-framework/utils/structured-error";
8
+ import { dataTypeId, numeralText } from "@prisma/orm-framework/components/codec";
8
9
  import { UNBOUND_NAMESPACE_ID } from "@prisma/orm-framework/components/ir";
9
10
  import { crossRef } from "@prisma/orm-framework/contract/types";
10
11
  import { buildExecutionSection, computeProfileHash } from "@prisma/orm-framework/contract/hashing";
11
12
  import { InternalError } from "@prisma/orm-framework/utils/internal-error";
12
13
  import { assertDefined } from "@prisma/orm-framework/utils/assertions";
13
14
  import { notOk, ok } from "@prisma/orm-framework/utils/result";
14
- import { answerMissingPropertyReads, bindEnumType, composePackAuthoringNamespace, createEntityHelpersFromNamespace, enumMemberError, enumType as enumType$1, isEnumTypeHandle, member, resolveToOneRelationNullable } from "@prisma/orm-framework/contract-authoring";
15
- import { applyDiscriminatorRule, assembleDataTypeSupport, assertNoCrossRegistryCollisions, assertTypeAliasesValid, encodeEnumMembers, flushAuthoringWarnings } from "@prisma/orm-framework/components/authoring";
15
+ import { answerMissingPropertyReads, assertSafeAuthoringHelperKey, bindEnumType, composePackAuthoringNamespace, createEntityHelpersFromNamespace, enumMemberError, enumType as enumType$1, isEnumTypeHandle, member, resolveToOneRelationNullable } from "@prisma/orm-framework/contract-authoring";
16
+ import { applyDiscriminatorRule, assembleDataTypeSupport, assertNoCrossRegistryCollisions, assertTypeAliasesValid, assertTypeChoicesValid, describeAdmittedForms, encodeEnumMembers, flushAuthoringWarnings, instantiateAuthoringTypeAlias, instantiateAuthoringTypeConstructor, isAuthoringTypeAliasDescriptor, isAuthoringTypeChoiceDescriptor, isAuthoringTypeConstructorDescriptor, readWrittenValueAs, resolveAuthoringTypeChoice, typeChoiceArgumentsFromObject, typeConstructorLookup, validateAuthoringHelperArguments, validateAuthoringTypeChoiceParams, validateAuthoringTypeParams } from "@prisma/orm-framework/components/authoring";
16
17
  import { assembleGeneratorFunctions, createGeneratorHelpers } from "@prisma/orm-framework/components/components";
17
18
  import { canonicalStringify } from "@prisma/orm-framework/utils/canonical-stringify";
18
19
  import { extractCodecLookup } from "@prisma/orm-framework/components/control";
@@ -1064,6 +1065,97 @@ function buildMongoContractFromDefinition(definition, codecLookup, dataTypes) {
1064
1065
  warnings
1065
1066
  });
1066
1067
  }
1068
+ /** The written value a TypeScript argument stands for: a number, string or boolean as PSL writes it. */
1069
+ function writtenValueOf(value) {
1070
+ if (typeof value === "number") return {
1071
+ kind: "number",
1072
+ text: numeralText(value)
1073
+ };
1074
+ if (typeof value === "string") return {
1075
+ kind: "string",
1076
+ text: value
1077
+ };
1078
+ if (typeof value === "boolean") return {
1079
+ kind: "boolean",
1080
+ value
1081
+ };
1082
+ }
1083
+ /** The arguments of a call as the constructor receives them: each `value` argument read as a value of its data type, through the same read as a PSL argument, so both give the same type parameters. */
1084
+ function readValueArguments(helperPath, descriptors, args, dataTypes) {
1085
+ return args.map((value, index) => {
1086
+ const descriptor = descriptors?.[index];
1087
+ if (descriptor?.kind !== "value" || value === void 0) return value;
1088
+ const label = descriptor.name === void 0 ? `Argument #${index + 1} of ${helperPath}` : `Argument "${descriptor.name}" of ${helperPath}`;
1089
+ const dataType = dataTypeId(descriptor.dataType);
1090
+ const refuse = (message) => {
1091
+ throw contractError("CONTRACT.ARGUMENT_INVALID", `${label}: ${message}`, { meta: { dataType } });
1092
+ };
1093
+ const written = writtenValueOf(value);
1094
+ if (written === void 0) return refuse(`Expected ${describeAdmittedForms(dataTypes, dataType)}`);
1095
+ const read = readWrittenValueAs(dataTypes, dataType, written);
1096
+ return read.ok ? read.value.value : refuse(read.failure.message);
1097
+ });
1098
+ }
1099
+ function fieldTypeOf(output) {
1100
+ return {
1101
+ kind: "scalar",
1102
+ codecId: output.codecId,
1103
+ ...ifDefined("typeParams", output.typeParams)
1104
+ };
1105
+ }
1106
+ /** The runtime `type` helpers of `namespace`: each type constructor, type choice and type alias as a function giving the MongoDB field type it stands for. A constructor marked `deprecated` is a PSL name only, and has no helper. */
1107
+ function createTypeHelpersFromNamespace(namespace, lookups, path = []) {
1108
+ const helpers = {};
1109
+ for (const [key, value] of Object.entries(namespace)) {
1110
+ assertSafeAuthoringHelperKey(key, path);
1111
+ const helperPath = [...path, key].join(".");
1112
+ if (isAuthoringTypeConstructorDescriptor(value)) {
1113
+ if (value.deprecated !== void 0) continue;
1114
+ helpers[key] = (...written) => {
1115
+ validateAuthoringHelperArguments(helperPath, value.args, written);
1116
+ const args = readValueArguments(helperPath, value.args, written, lookups.dataTypes);
1117
+ const output = instantiateAuthoringTypeConstructor(value, args);
1118
+ validateAuthoringTypeParams(helperPath, value.output, output.typeParams, lookups.codecLookup.descriptorFor(output.codecId)?.paramsSchema);
1119
+ return fieldTypeOf(output);
1120
+ };
1121
+ continue;
1122
+ }
1123
+ if (isAuthoringTypeChoiceDescriptor(value)) {
1124
+ const constructorNamed = typeConstructorLookup(namespace);
1125
+ helpers[key] = (...written) => {
1126
+ const args = typeChoiceArgumentsFromObject(helperPath, value, written);
1127
+ validateAuthoringHelperArguments(helperPath, value.args, args);
1128
+ const chosen = resolveAuthoringTypeChoice({
1129
+ helperPath,
1130
+ descriptor: value,
1131
+ args: readValueArguments(helperPath, value.args, args, lookups.dataTypes),
1132
+ constructorNamed
1133
+ });
1134
+ const output = instantiateAuthoringTypeConstructor(chosen.descriptor, chosen.args);
1135
+ validateAuthoringTypeChoiceParams(helperPath, value, chosen.descriptor, output.typeParams, lookups.codecLookup.descriptorFor(output.codecId)?.paramsSchema);
1136
+ return fieldTypeOf(output);
1137
+ };
1138
+ continue;
1139
+ }
1140
+ if (isAuthoringTypeAliasDescriptor(value)) {
1141
+ helpers[key] = (...args) => {
1142
+ validateAuthoringHelperArguments(helperPath, [], args);
1143
+ const { type, onCreate, onUpdate } = instantiateAuthoringTypeAlias(value);
1144
+ const generatorCalls = {
1145
+ ...ifDefined("onCreate", onCreate),
1146
+ ...ifDefined("onUpdate", onUpdate)
1147
+ };
1148
+ return {
1149
+ ...fieldTypeOf(type),
1150
+ ...Object.keys(generatorCalls).length === 0 ? {} : { generatorCalls }
1151
+ };
1152
+ };
1153
+ continue;
1154
+ }
1155
+ helpers[key] = createTypeHelpersFromNamespace(value, lookups, [...path, key]);
1156
+ }
1157
+ return helpers;
1158
+ }
1067
1159
  function resolveModelName(value) {
1068
1160
  return typeof value === "string" ? value : value.__name;
1069
1161
  }
@@ -1404,7 +1496,7 @@ function discriminatorFieldError(refusal) {
1404
1496
  } });
1405
1497
  case "not-text": {
1406
1498
  const { dataType } = refusal.refusal;
1407
- return contractError("CONTRACT.ARGUMENT_INVALID", `${field} must be text, such as field.string(), but its data type is ${dataType === void 0 ? "not a scalar" : `"${dataType}"`}.`, { meta: {
1499
+ return contractError("CONTRACT.ARGUMENT_INVALID", `${field} must be text, such as field.scalar(type.String()), but its data type is ${dataType === void 0 ? "not a scalar" : `"${dataType}"`}.`, { meta: {
1408
1500
  ...meta,
1409
1501
  dataType,
1410
1502
  reason: "discriminator-not-text"
@@ -1644,19 +1736,47 @@ function differenceWording(difference) {
1644
1736
  }
1645
1737
  const GUIDE = "The upgrade guide \"Field presets are removed\" lists every rewrite.";
1646
1738
  const TEMPORAL_REWRITES = {
1647
- createdAt: "field.date().onCreate(gen.now())",
1648
- updatedAt: "field.date().onCreate(gen.now()).onUpdate(gen.now())",
1649
- timestamp: "field.date(), with .onCreate(gen.now()) and .onUpdate(gen.now()) for the phases the preset set"
1739
+ createdAt: "field.scalar(type.temporal.CreatedAt())",
1740
+ updatedAt: "field.scalar(type.temporal.UpdatedAt())",
1741
+ timestamp: "field.scalar(type.Date()), with .onCreate(gen.now()) and .onUpdate(gen.now()) for the phases the preset set"
1742
+ };
1743
+ /** Each removed scalar field helper and the type constructor its field is now written with. */
1744
+ const REMOVED_SCALAR_HELPERS = {
1745
+ objectId: "ObjectId",
1746
+ string: "String",
1747
+ double: "Double",
1748
+ int32: "Int32",
1749
+ bool: "Bool",
1750
+ date: "Date",
1751
+ int64: "Int64",
1752
+ int64Number: "Int64Number",
1753
+ decimal128: "Decimal128",
1754
+ binary: "Binary",
1755
+ json: "Json",
1756
+ bson: "Bson",
1757
+ vector: "Vector"
1650
1758
  };
1651
- function refuse(preset) {
1759
+ function refusePreset(preset) {
1652
1760
  const helper = `field.temporal.${preset}`;
1653
1761
  throw contractError("CONTRACT.FIELD_HELPER_UNKNOWN", `${helper} was a field preset, and Prisma 8 removed field presets. Write ${TEMPORAL_REWRITES[preset]}. ${GUIDE}`, { meta: { helper } });
1654
1762
  }
1763
+ function refuseScalarHelper(name) {
1764
+ const helper = `field.${name}`;
1765
+ const typeName = REMOVED_SCALAR_HELPERS[name];
1766
+ throw contractError("CONTRACT.FIELD_HELPER_UNKNOWN", `${helper}() was removed. Write field.scalar(type.${typeName}())${name === "vector" ? ", or field.scalar(type.Vector(n)) for a length n" : ""}.`, { meta: { helper } });
1767
+ }
1768
+ /** Refuses an argument of `field.scalar()` that is not a type from the type helpers, naming the type to write when `value` is the codec id of a removed scalar helper. */
1769
+ function refuseScalarArgument(value) {
1770
+ const helper = Object.keys(REMOVED_SCALAR_HELPERS).find((name) => value === `mongo/${name}@1`);
1771
+ const typeName = helper === void 0 ? void 0 : REMOVED_SCALAR_HELPERS[helper];
1772
+ throw contractError("CONTRACT.ARGUMENT_INVALID", typeName === void 0 ? `field.scalar() takes a type from the type helpers, such as field.scalar(type.String()), and was given ${JSON.stringify(value)}.` : `field.scalar() takes a type, not the codec id "${String(value)}". Write field.scalar(type.${typeName}()).`, { meta: { helper: "field.scalar" } });
1773
+ }
1655
1774
  /**
1656
- * The MongoDB `field` helpers, where `field.temporal`, which held the removed field presets, gives an object whose read of a preset throws `CONTRACT.FIELD_HELPER_UNKNOWN` with its rewrite, so a JavaScript caller, or a contract emitted before it is type-checked, learns what to write instead. Every other read behaves as on `helpers`.
1775
+ * The MongoDB `field` helpers, where a read of a removed scalar helper such as `field.objectId`, or of a removed field preset under `field.temporal`, throws `CONTRACT.FIELD_HELPER_UNKNOWN` with its rewrite, so a JavaScript caller, or a contract emitted before it is type-checked, learns what to write instead. Every other read behaves as on `helpers`.
1657
1776
  */
1658
- function refusingRemovedFieldPresets(helpers) {
1659
- return answerMissingPropertyReads(helpers, /* @__PURE__ */ new Set(["temporal"]), () => answerMissingPropertyReads({}, new Set(Object.keys(TEMPORAL_REWRITES)), refuse));
1777
+ function refusingRemovedFieldHelpers(helpers) {
1778
+ const temporal = () => answerMissingPropertyReads({}, new Set(Object.keys(TEMPORAL_REWRITES)), refusePreset);
1779
+ return answerMissingPropertyReads(helpers, /* @__PURE__ */ new Set(["temporal", ...Object.keys(REMOVED_SCALAR_HELPERS)]), (name) => name === "temporal" ? temporal() : refuseScalarHelper(name));
1660
1780
  }
1661
1781
  const MONGO_RESERVED_HELPER_KEYS = [
1662
1782
  "field",
@@ -1664,6 +1784,7 @@ const MONGO_RESERVED_HELPER_KEYS = [
1664
1784
  "index",
1665
1785
  "model",
1666
1786
  "rel",
1787
+ "type",
1667
1788
  "valueObject"
1668
1789
  ];
1669
1790
  function stackLookupsOf(components) {
@@ -1686,7 +1807,9 @@ function composeMongoAuthoringHelpers(family, target, extensions, lookups) {
1686
1807
  ...Object.values(extensions ?? {})
1687
1808
  ];
1688
1809
  const entityNamespace = composePackAuthoringNamespace(components, "entityTypes");
1689
- assertNoCrossRegistryCollisions(composePackAuthoringNamespace(components, "type"), entityNamespace);
1810
+ const typeNamespace = composePackAuthoringNamespace(components, "type");
1811
+ assertNoCrossRegistryCollisions(typeNamespace, entityNamespace);
1812
+ for (const component of components) assertTypeChoicesValid(component.authoring?.type ?? {}, component.id);
1690
1813
  const collisions = Object.keys(entityNamespace).filter((name) => MONGO_RESERVED_HELPER_KEYS.includes(name));
1691
1814
  if (collisions.length > 0) throw contractError("CONTRACT.PACK_CONTRIBUTION_INVALID", `Pack-contributed entity type(s) ${collisions.map((c) => `"${c}"`).join(", ")} collide with the reserved built-in helper key(s) on the Mongo composed helpers surface. Reserved keys: ${MONGO_RESERVED_HELPER_KEYS.map((k) => `"${k}"`).join(", ")}.`, { meta: {
1692
1815
  reason: "reserved-helper-key-collision",
@@ -1704,9 +1827,21 @@ function composeMongoAuthoringHelpers(family, target, extensions, lookups) {
1704
1827
  index,
1705
1828
  model,
1706
1829
  rel,
1830
+ type: createTypeHelpersFromNamespace(typeNamespace, lookups),
1707
1831
  valueObject
1708
1832
  });
1709
1833
  }
1834
+ /**
1835
+ * The helpers the `defineContract` callback receives for a stack of `family`, `target` and `extensions`: `field`, `model`, `rel`, `index`, `valueObject`, `gen`, `type` and the packs' entity types. A facade builds its standalone `field`, `gen` and `type` from them.
1836
+ */
1837
+ function createContractAuthoringHelpers(options) {
1838
+ const { family, target, extensions } = options;
1839
+ return composeMongoAuthoringHelpers(family, target, extensions, stackLookupsOf([
1840
+ family,
1841
+ target,
1842
+ ...Object.values(extensions ?? {})
1843
+ ]));
1844
+ }
1710
1845
  function createFieldBuilder(spec, enumHandle, executionDefaults, storedName, generatorCalls) {
1711
1846
  function many(options) {
1712
1847
  return createFieldBuilder({
@@ -1745,63 +1880,20 @@ function createFieldBuilder(spec, enumHandle, executionDefaults, storedName, gen
1745
1880
  mongo: ({ field }) => createFieldBuilder(spec, enumHandle, executionDefaults, field, generatorCalls)
1746
1881
  };
1747
1882
  }
1748
- function normalizeOptionalTypeParams(typeParams) {
1749
- if (!typeParams) return {};
1750
- return { typeParams };
1883
+ function isTypeInstance(value) {
1884
+ return typeof value === "object" && value !== null && "kind" in value && value.kind === "scalar" && "codecId" in value && typeof value.codecId === "string";
1751
1885
  }
1752
- function createScalarFieldBuilder(codecId, options) {
1886
+ function scalarField(type) {
1887
+ if (!isTypeInstance(type)) refuseScalarArgument(type);
1888
+ const { generatorCalls, ...fieldType } = type;
1753
1889
  return createFieldBuilder({
1754
- type: blindCast({
1755
- kind: "scalar",
1756
- codecId,
1757
- ...normalizeOptionalTypeParams(options?.typeParams)
1758
- }),
1890
+ type: blindCast(fieldType),
1759
1891
  nullable: false,
1760
1892
  many: false
1761
- });
1762
- }
1763
- function vectorField(options) {
1764
- return createScalarFieldBuilder("mongo/vector@1", options);
1893
+ }, void 0, blindCast(void 0), void 0, generatorCalls);
1765
1894
  }
1766
- const field = refusingRemovedFieldPresets({
1767
- scalar: createScalarFieldBuilder,
1768
- objectId() {
1769
- return createScalarFieldBuilder("mongo/objectId@1");
1770
- },
1771
- string() {
1772
- return createScalarFieldBuilder("mongo/string@1");
1773
- },
1774
- double() {
1775
- return createScalarFieldBuilder("mongo/double@1");
1776
- },
1777
- int32() {
1778
- return createScalarFieldBuilder("mongo/int32@1");
1779
- },
1780
- bool() {
1781
- return createScalarFieldBuilder("mongo/bool@1");
1782
- },
1783
- date() {
1784
- return createScalarFieldBuilder("mongo/date@1");
1785
- },
1786
- int64() {
1787
- return createScalarFieldBuilder("mongo/int64@1");
1788
- },
1789
- int64Number() {
1790
- return createScalarFieldBuilder("mongo/int64Number@1");
1791
- },
1792
- decimal128() {
1793
- return createScalarFieldBuilder("mongo/decimal128@1");
1794
- },
1795
- binary() {
1796
- return createScalarFieldBuilder("mongo/binary@1");
1797
- },
1798
- json() {
1799
- return createScalarFieldBuilder("mongo/json@1");
1800
- },
1801
- bson() {
1802
- return createScalarFieldBuilder("mongo/bson@1");
1803
- },
1804
- vector: vectorField,
1895
+ const field = refusingRemovedFieldHelpers({
1896
+ scalar: scalarField,
1805
1897
  valueObject(valueObjectName) {
1806
1898
  return createFieldBuilder({
1807
1899
  type: blindCast({
@@ -1985,6 +2077,6 @@ function defineContract(definition, factory) {
1985
2077
  return buildBoundContract(definition.family, definition.target, definition);
1986
2078
  }
1987
2079
  //#endregion
1988
- export { enumType$1 as a, isEnumTypeHandle as c, model as d, nameIndexes as f, defineContract as i, member as l, valueObject as m, buildBoundContract as n, field as o, rel as p, buildMongoContractFromDefinition as r, index as s, bindEnumType as t, mergeRedundantIndexes as u };
2080
+ export { defineContract as a, index as c, mergeRedundantIndexes as d, model as f, valueObject as h, createContractAuthoringHelpers as i, isEnumTypeHandle as l, rel as m, buildBoundContract as n, enumType$1 as o, nameIndexes as p, buildMongoContractFromDefinition as r, field as s, bindEnumType as t, member as u };
1989
2081
 
1990
- //# sourceMappingURL=contract-builder-BUa0Oz34.mjs.map
2082
+ //# sourceMappingURL=contract-builder-B0Uzl6w0.mjs.map