@prisma-next/sql-contract-ts 0.16.0-dev.20 → 0.16.0-dev.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{build-contract-DWRZ3jRz.mjs → build-contract-Drz7JW70.mjs} +9 -9
- package/dist/build-contract-Drz7JW70.mjs.map +1 -0
- package/dist/config-types.mjs +4 -4
- package/dist/config-types.mjs.map +1 -1
- package/dist/contract-builder.d.mts +27 -27
- package/dist/contract-builder.d.mts.map +1 -1
- package/dist/contract-builder.mjs +26 -26
- package/dist/contract-builder.mjs.map +1 -1
- package/package.json +10 -10
- package/schemas/data-contract-sql-v1.json +1 -1
- package/src/build-contract.ts +11 -11
- package/src/composed-authoring-helpers.ts +19 -19
- package/src/config-types.ts +3 -3
- package/src/contract-builder.ts +22 -26
- package/src/contract-definition.ts +1 -1
- package/src/contract-dsl.ts +2 -2
- package/src/contract-lowering.ts +18 -18
- package/src/contract-types.ts +7 -7
- package/dist/build-contract-DWRZ3jRz.mjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { n as contractError, t as buildSqlContractFromDefinition } from "./build-contract-
|
|
1
|
+
import { n as contractError, t as buildSqlContractFromDefinition } from "./build-contract-Drz7JW70.mjs";
|
|
2
2
|
import { blindCast } from "@prisma-next/utils/casts";
|
|
3
3
|
import { ifDefined } from "@prisma-next/utils/defined";
|
|
4
4
|
import { isColumnDefault } from "@prisma-next/contract/types";
|
|
@@ -675,7 +675,7 @@ function createComposedFieldHelpers(fieldNamespace) {
|
|
|
675
675
|
};
|
|
676
676
|
}
|
|
677
677
|
function createComposedAuthoringHelpers(options) {
|
|
678
|
-
const extensionValues = Object.values(options.
|
|
678
|
+
const extensionValues = Object.values(options.extensions ?? {});
|
|
679
679
|
const components = [
|
|
680
680
|
options.family,
|
|
681
681
|
options.target,
|
|
@@ -974,7 +974,7 @@ function resolveRelationAnchorFields(spec) {
|
|
|
974
974
|
reason: "missing-anchor-id"
|
|
975
975
|
} });
|
|
976
976
|
}
|
|
977
|
-
function lowerBelongsToRelation(relationName, relation, currentSpec, allSpecs,
|
|
977
|
+
function lowerBelongsToRelation(relationName, relation, currentSpec, allSpecs, extensions) {
|
|
978
978
|
const targetModelName = resolveRelationModelName(relation.toModel);
|
|
979
979
|
const fromFields = normalizeRelationFieldNames(relation.from);
|
|
980
980
|
const toFields = normalizeRelationFieldNames(relation.to);
|
|
@@ -987,7 +987,7 @@ function lowerBelongsToRelation(relationName, relation, currentSpec, allSpecs, e
|
|
|
987
987
|
rightFields: toFields
|
|
988
988
|
});
|
|
989
989
|
if (relation.spaceId !== void 0) {
|
|
990
|
-
assertKnownExtensionPack(
|
|
990
|
+
assertKnownExtensionPack(extensions, relation.spaceId, `Relation "${currentSpec.modelName}.${relationName}"`);
|
|
991
991
|
const targetTable = relation.tableName ?? targetModelName.toLowerCase();
|
|
992
992
|
const parentColumns = mapFieldNamesToColumnNames(currentSpec.modelName, fromFields, currentSpec.fieldToColumn);
|
|
993
993
|
return {
|
|
@@ -1098,8 +1098,8 @@ function lowerManyToManyRelation(relationName, relation, currentSpec, allSpecs)
|
|
|
1098
1098
|
}
|
|
1099
1099
|
};
|
|
1100
1100
|
}
|
|
1101
|
-
function resolveRelationNode(relationName, relation, currentSpec, allSpecs,
|
|
1102
|
-
if (relation.kind === "belongsTo") return lowerBelongsToRelation(relationName, relation, currentSpec, allSpecs,
|
|
1101
|
+
function resolveRelationNode(relationName, relation, currentSpec, allSpecs, extensions) {
|
|
1102
|
+
if (relation.kind === "belongsTo") return lowerBelongsToRelation(relationName, relation, currentSpec, allSpecs, extensions);
|
|
1103
1103
|
if (relation.kind === "hasMany" || relation.kind === "hasOne") return lowerHasOwnershipRelation(relationName, relation, currentSpec, allSpecs);
|
|
1104
1104
|
return lowerManyToManyRelation(relationName, relation, currentSpec, allSpecs);
|
|
1105
1105
|
}
|
|
@@ -1135,17 +1135,17 @@ function lowerCrossSpaceForeignKeyNode(spec, foreignKey) {
|
|
|
1135
1135
|
...foreignKey.index !== void 0 ? { index: foreignKey.index } : {}
|
|
1136
1136
|
};
|
|
1137
1137
|
}
|
|
1138
|
-
function assertKnownExtensionPack(
|
|
1139
|
-
if (
|
|
1140
|
-
throw contractError("CONTRACT.PACK_MISSING", `${context} references contract space "${spaceId}" but "${spaceId}" is not declared in
|
|
1138
|
+
function assertKnownExtensionPack(extensions, spaceId, context) {
|
|
1139
|
+
if (extensions !== void 0 && Object.hasOwn(extensions, spaceId)) return;
|
|
1140
|
+
throw contractError("CONTRACT.PACK_MISSING", `${context} references contract space "${spaceId}" but "${spaceId}" is not declared in extensions. Add the pack to extensions.`, { meta: {
|
|
1141
1141
|
spaceId,
|
|
1142
1142
|
context
|
|
1143
1143
|
} });
|
|
1144
1144
|
}
|
|
1145
|
-
function resolveForeignKeyNodes(spec, allSpecs,
|
|
1145
|
+
function resolveForeignKeyNodes(spec, allSpecs, extensions) {
|
|
1146
1146
|
const relationForeignKeys = resolveRelationForeignKeys(spec, allSpecs).map((foreignKey) => {
|
|
1147
1147
|
if (foreignKey.targetSpaceId !== void 0) {
|
|
1148
|
-
assertKnownExtensionPack(
|
|
1148
|
+
assertKnownExtensionPack(extensions, foreignKey.targetSpaceId, `Relation-derived foreign key on "${spec.modelName}"`);
|
|
1149
1149
|
return lowerCrossSpaceForeignKeyNode(spec, {
|
|
1150
1150
|
...foreignKey,
|
|
1151
1151
|
targetSpaceId: foreignKey.targetSpaceId
|
|
@@ -1160,7 +1160,7 @@ function resolveForeignKeyNodes(spec, allSpecs, extensionPacks) {
|
|
|
1160
1160
|
});
|
|
1161
1161
|
const sqlForeignKeys = (spec.sqlSpec?.foreignKeys ?? []).map((foreignKey) => {
|
|
1162
1162
|
if (foreignKey.targetSpaceId !== void 0) {
|
|
1163
|
-
assertKnownExtensionPack(
|
|
1163
|
+
assertKnownExtensionPack(extensions, foreignKey.targetSpaceId, `Foreign key on "${spec.modelName}"`);
|
|
1164
1164
|
return lowerCrossSpaceForeignKeyNode(spec, {
|
|
1165
1165
|
...foreignKey,
|
|
1166
1166
|
targetSpaceId: foreignKey.targetSpaceId
|
|
@@ -1175,7 +1175,7 @@ function resolveForeignKeyNodes(spec, allSpecs, extensionPacks) {
|
|
|
1175
1175
|
});
|
|
1176
1176
|
return [...relationForeignKeys, ...sqlForeignKeys];
|
|
1177
1177
|
}
|
|
1178
|
-
function resolveModelNode(spec, allSpecs, storageTypes, storageTypeReverseLookup,
|
|
1178
|
+
function resolveModelNode(spec, allSpecs, storageTypes, storageTypeReverseLookup, extensions) {
|
|
1179
1179
|
const fields = [];
|
|
1180
1180
|
for (const [fieldName, fieldBuilder] of Object.entries(spec.fieldBuilders)) {
|
|
1181
1181
|
const fieldState = fieldBuilder.build();
|
|
@@ -1205,8 +1205,8 @@ function resolveModelNode(spec, allSpecs, storageTypes, storageTypeReverseLookup
|
|
|
1205
1205
|
...ifDefined("type", index.type),
|
|
1206
1206
|
...ifDefined("options", index.options)
|
|
1207
1207
|
}));
|
|
1208
|
-
const foreignKeys = resolveForeignKeyNodes(spec, allSpecs,
|
|
1209
|
-
const relations = Object.entries(spec.relations).map(([relationName, relationBuilder]) => resolveRelationNode(relationName, relationBuilder.build(), spec, allSpecs,
|
|
1208
|
+
const foreignKeys = resolveForeignKeyNodes(spec, allSpecs, extensions);
|
|
1209
|
+
const relations = Object.entries(spec.relations).map(([relationName, relationBuilder]) => resolveRelationNode(relationName, relationBuilder.build(), spec, allSpecs, extensions));
|
|
1210
1210
|
return {
|
|
1211
1211
|
modelName: spec.modelName,
|
|
1212
1212
|
tableName: spec.tableName,
|
|
@@ -1286,10 +1286,10 @@ function collectRuntimeModelSpecs(definition) {
|
|
|
1286
1286
|
modelSpecs
|
|
1287
1287
|
};
|
|
1288
1288
|
}
|
|
1289
|
-
function lowerModels(collection,
|
|
1289
|
+
function lowerModels(collection, extensions) {
|
|
1290
1290
|
emitTypedCrossModelFallbackWarnings(collection);
|
|
1291
1291
|
const storageTypeReverseLookup = buildStorageTypeReverseLookup(collection.storageTypes);
|
|
1292
|
-
return Array.from(collection.modelSpecs.values()).map((spec) => resolveModelNode(spec, collection.modelSpecs, collection.storageTypes, storageTypeReverseLookup,
|
|
1292
|
+
return Array.from(collection.modelSpecs.values()).map((spec) => resolveModelNode(spec, collection.modelSpecs, collection.storageTypes, storageTypeReverseLookup, extensions));
|
|
1293
1293
|
}
|
|
1294
1294
|
/**
|
|
1295
1295
|
* Kind-agnostic walk over the author-declared `entities` handle list:
|
|
@@ -1314,7 +1314,7 @@ function lowerModels(collection, extensionPacks) {
|
|
|
1314
1314
|
function lowerPackEntityHandles(definition, modelSpecs) {
|
|
1315
1315
|
const entities = definition.entities;
|
|
1316
1316
|
if (entities === void 0 || entities.length === 0) return void 0;
|
|
1317
|
-
const components = [definition.target, ...Object.values(definition.
|
|
1317
|
+
const components = [definition.target, ...Object.values(definition.extensions ?? {})];
|
|
1318
1318
|
const owningComponent = /* @__PURE__ */ new Map();
|
|
1319
1319
|
const walkEntityTypes = (namespace, component) => {
|
|
1320
1320
|
for (const value of Object.values(namespace)) if (isAuthoringEntityTypeDescriptor(value)) owningComponent.set(value.discriminator, component);
|
|
@@ -1410,12 +1410,12 @@ function lowerPackEntityHandles(definition, modelSpecs) {
|
|
|
1410
1410
|
}
|
|
1411
1411
|
function buildContractDefinition(definition) {
|
|
1412
1412
|
const collection = collectRuntimeModelSpecs(definition);
|
|
1413
|
-
const models = lowerModels(collection, definition.
|
|
1413
|
+
const models = lowerModels(collection, definition.extensions);
|
|
1414
1414
|
const attachedEntities = lowerPackEntityHandles(definition, collection.modelSpecs);
|
|
1415
1415
|
return {
|
|
1416
1416
|
target: definition.target,
|
|
1417
1417
|
...ifDefined("defaultControlPolicy", definition.defaultControlPolicy),
|
|
1418
|
-
...definition.
|
|
1418
|
+
...definition.extensions ? { extensions: definition.extensions } : {},
|
|
1419
1419
|
...definition.storageHash ? { storageHash: definition.storageHash } : {},
|
|
1420
1420
|
...definition.foreignKeyDefaults ? { foreignKeyDefaults: definition.foreignKeyDefaults } : {},
|
|
1421
1421
|
...Object.keys(collection.storageTypes).length > 0 ? { storageTypes: collection.storageTypes } : {},
|
|
@@ -1534,10 +1534,10 @@ function validatePerModelNamespaces(target, namespaces, models) {
|
|
|
1534
1534
|
} });
|
|
1535
1535
|
}
|
|
1536
1536
|
}
|
|
1537
|
-
function validateExtensionPackRefs(target,
|
|
1538
|
-
if (!
|
|
1539
|
-
for (const packRef of Object.values(
|
|
1540
|
-
if (packRef.kind !== "extension") throw contractError("CONTRACT.PACK_REF_INVALID", `defineContract only accepts extension pack refs in
|
|
1537
|
+
function validateExtensionPackRefs(target, extensions) {
|
|
1538
|
+
if (!extensions) return;
|
|
1539
|
+
for (const packRef of Object.values(extensions)) {
|
|
1540
|
+
if (packRef.kind !== "extension") throw contractError("CONTRACT.PACK_REF_INVALID", `defineContract only accepts extension pack refs in extensions. Received kind "${packRef.kind}".`, { meta: {
|
|
1541
1541
|
packId: packRef.id,
|
|
1542
1542
|
kind: packRef.kind
|
|
1543
1543
|
} });
|
|
@@ -1555,7 +1555,7 @@ function validateExtensionPackRefs(target, extensionPacks) {
|
|
|
1555
1555
|
}
|
|
1556
1556
|
function buildContractFromDsl(definition) {
|
|
1557
1557
|
validateTargetPackRef(definition.family, definition.target);
|
|
1558
|
-
validateExtensionPackRefs(definition.target, definition.
|
|
1558
|
+
validateExtensionPackRefs(definition.target, definition.extensions);
|
|
1559
1559
|
validateNamespaceDeclarations(definition.target, definition.namespaces);
|
|
1560
1560
|
validatePerModelNamespaces(definition.target, definition.namespaces, definition.models ?? {});
|
|
1561
1561
|
return blindCast(buildSqlContractFromDefinition(buildContractDefinition(definition), definition.codecLookup));
|
|
@@ -1571,7 +1571,7 @@ function buildBoundContract(family, target, definition, factory) {
|
|
|
1571
1571
|
const built = factory(createComposedAuthoringHelpers({
|
|
1572
1572
|
family,
|
|
1573
1573
|
target,
|
|
1574
|
-
|
|
1574
|
+
extensions: definition.extensions
|
|
1575
1575
|
}));
|
|
1576
1576
|
const mergedEnums = {
|
|
1577
1577
|
...definition.enums ?? {},
|