@prisma-next/sql-contract-ts 0.13.0 → 0.14.0
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 +12 -9
- package/dist/{build-contract-C-x2pfu4.mjs → build-contract-CQ4u83jx.mjs} +24 -54
- package/dist/build-contract-CQ4u83jx.mjs.map +1 -0
- package/dist/config-types.mjs +1 -1
- package/dist/contract-builder.d.mts +129 -63
- package/dist/contract-builder.d.mts.map +1 -1
- package/dist/contract-builder.mjs +48 -11
- package/dist/contract-builder.mjs.map +1 -1
- package/package.json +10 -10
- package/src/build-contract.ts +33 -91
- package/src/contract-builder.ts +47 -31
- package/src/contract-definition.ts +11 -23
- package/src/contract-dsl.ts +41 -17
- package/src/contract-lowering.ts +19 -26
- package/src/contract-types.ts +135 -32
- package/src/contract-warnings.ts +3 -6
- package/src/enum-type.ts +98 -28
- package/src/exports/contract-builder.ts +10 -2
- package/dist/build-contract-C-x2pfu4.mjs.map +0 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { t as buildSqlContractFromDefinition } from "./build-contract-
|
|
1
|
+
import { t as buildSqlContractFromDefinition } from "./build-contract-CQ4u83jx.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";
|
|
5
5
|
import { createEntityHelpersFromNamespace } from "@prisma-next/contract-authoring";
|
|
6
|
-
import {
|
|
6
|
+
import { toStorageTypeInstance } from "@prisma-next/sql-contract/types";
|
|
7
7
|
import { assertNoCrossRegistryCollisions, instantiateAuthoringFieldPreset, instantiateAuthoringTypeConstructor, isAuthoringEntityTypeDescriptor, isAuthoringFieldPresetDescriptor, isAuthoringTypeConstructorDescriptor, mergeAuthoringNamespaces, validateAuthoringHelperArguments } from "@prisma-next/framework-components/authoring";
|
|
8
8
|
//#region src/authoring-helper-runtime.ts
|
|
9
9
|
function isNamedConstraintOptionsLike(value) {
|
|
@@ -98,8 +98,9 @@ function enumType(name, codec, ...members) {
|
|
|
98
98
|
for (const m of members) {
|
|
99
99
|
if (seenNames.has(m.name)) throw new Error(`enumType("${name}"): duplicate member name "${m.name}". Member names must be unique.`);
|
|
100
100
|
seenNames.add(m.name);
|
|
101
|
-
|
|
102
|
-
seenValues.
|
|
101
|
+
const loweredValue = String(m.value);
|
|
102
|
+
if (seenValues.has(loweredValue)) throw new Error(`enumType("${name}"): duplicate member value "${loweredValue}". Member values must be unique.`);
|
|
103
|
+
seenValues.add(loweredValue);
|
|
103
104
|
}
|
|
104
105
|
const values = Object.freeze(members.map((m) => m.value));
|
|
105
106
|
const names = Object.freeze(members.map((m) => m.name));
|
|
@@ -126,6 +127,15 @@ function enumType(name, codec, ...members) {
|
|
|
126
127
|
};
|
|
127
128
|
}
|
|
128
129
|
/**
|
|
130
|
+
* Bind `enumType` to a target's codec typemap. The returned function is the
|
|
131
|
+
* same runtime `enumType`, retyped so member values are constrained to the
|
|
132
|
+
* codec's input type. Target packages call this with their pack's
|
|
133
|
+
* `ExtractCodecTypesFromPack<Pack>` to expose a codec-aware `enumType`.
|
|
134
|
+
*/
|
|
135
|
+
function bindEnumType() {
|
|
136
|
+
return enumType;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
129
139
|
* Returns true when the value is an `EnumTypeHandle` produced by
|
|
130
140
|
* `enumType()`. Used in the lowering pipeline to detect enum handles
|
|
131
141
|
* in field state without importing the BRAND symbol at every call site.
|
|
@@ -211,6 +221,25 @@ var ScalarFieldBuilder = class ScalarFieldBuilder {
|
|
|
211
221
|
return this.state;
|
|
212
222
|
}
|
|
213
223
|
};
|
|
224
|
+
var EnumScalarFieldBuilder = class EnumScalarFieldBuilder extends ScalarFieldBuilder {
|
|
225
|
+
#handle;
|
|
226
|
+
constructor(state, handle) {
|
|
227
|
+
super(state);
|
|
228
|
+
this.#handle = handle;
|
|
229
|
+
}
|
|
230
|
+
default(value) {
|
|
231
|
+
return blindCast(new EnumScalarFieldBuilder({
|
|
232
|
+
...this.build(),
|
|
233
|
+
default: {
|
|
234
|
+
kind: "literal",
|
|
235
|
+
value
|
|
236
|
+
}
|
|
237
|
+
}, this.#handle));
|
|
238
|
+
}
|
|
239
|
+
defaultSql(_expression) {
|
|
240
|
+
throw new Error("defaultSql is not available on an enum field; use .default(members.X) instead");
|
|
241
|
+
}
|
|
242
|
+
};
|
|
214
243
|
function columnField(descriptor) {
|
|
215
244
|
return new ScalarFieldBuilder({
|
|
216
245
|
kind: "scalar",
|
|
@@ -230,11 +259,11 @@ function generatedField(spec) {
|
|
|
230
259
|
});
|
|
231
260
|
}
|
|
232
261
|
function namedTypeField(typeRef) {
|
|
233
|
-
if (isEnumTypeHandle(typeRef)) return new
|
|
262
|
+
if (isEnumTypeHandle(typeRef)) return new EnumScalarFieldBuilder({
|
|
234
263
|
kind: "scalar",
|
|
235
264
|
typeRef,
|
|
236
265
|
nullable: false
|
|
237
|
-
});
|
|
266
|
+
}, typeRef);
|
|
238
267
|
return new ScalarFieldBuilder({
|
|
239
268
|
kind: "scalar",
|
|
240
269
|
typeRef,
|
|
@@ -818,7 +847,7 @@ function resolveFieldDescriptor(modelName, fieldName, fieldState, storageTypes,
|
|
|
818
847
|
const referencedType = storageTypes[typeRef];
|
|
819
848
|
if (!referencedType) throw new Error(`Field "${modelName}.${fieldName}" references unknown storage type "${typeRef}"`);
|
|
820
849
|
return {
|
|
821
|
-
codecId:
|
|
850
|
+
codecId: referencedType.codecId,
|
|
822
851
|
nativeType: referencedType.nativeType,
|
|
823
852
|
typeRef
|
|
824
853
|
};
|
|
@@ -1037,6 +1066,7 @@ function lowerManyToManyRelation(relationName, relation, currentSpec, allSpecs)
|
|
|
1037
1066
|
cardinality: "N:M",
|
|
1038
1067
|
through: {
|
|
1039
1068
|
table: throughSpec.tableName,
|
|
1069
|
+
...ifDefined("namespaceId", throughSpec.namespace),
|
|
1040
1070
|
parentColumns: mapFieldNamesToColumnNames(throughSpec.modelName, throughFromFields, throughSpec.fieldToColumn),
|
|
1041
1071
|
childColumns: mapFieldNamesToColumnNames(throughSpec.modelName, throughToFields, throughSpec.fieldToColumn)
|
|
1042
1072
|
},
|
|
@@ -1175,9 +1205,11 @@ function collectRuntimeModelSpecs(definition) {
|
|
|
1175
1205
|
const attributesSpec = modelDefinition.buildAttributesSpec();
|
|
1176
1206
|
const sqlSpec = modelDefinition.buildSqlSpec();
|
|
1177
1207
|
const tableName = sqlSpec?.table ?? applyNaming(modelName, definition.naming?.tables);
|
|
1178
|
-
const
|
|
1208
|
+
const namespaceId = modelDefinition.stageOne.namespace ?? definition.target.defaultNamespaceId;
|
|
1209
|
+
const tableKey = JSON.stringify([namespaceId, tableName]);
|
|
1210
|
+
const existingModel = tableOwners.get(tableKey);
|
|
1179
1211
|
if (existingModel) throw new Error(`Models "${existingModel}" and "${modelName}" both map to table "${tableName}".`);
|
|
1180
|
-
tableOwners.set(
|
|
1212
|
+
tableOwners.set(tableKey, modelName);
|
|
1181
1213
|
const fieldToColumn = {};
|
|
1182
1214
|
const columnOwners = /* @__PURE__ */ new Map();
|
|
1183
1215
|
for (const [fieldName, fieldBuilder] of Object.entries(modelDefinition.stageOne.fields)) {
|
|
@@ -1329,10 +1361,15 @@ function buildBoundContract(family, target, definition, factory) {
|
|
|
1329
1361
|
target,
|
|
1330
1362
|
extensionPacks: definition.extensionPacks
|
|
1331
1363
|
}));
|
|
1364
|
+
const mergedEnums = {
|
|
1365
|
+
...definition.enums ?? {},
|
|
1366
|
+
...built.enums
|
|
1367
|
+
};
|
|
1332
1368
|
return buildContractFromDsl({
|
|
1333
1369
|
...full,
|
|
1334
1370
|
...ifDefined("types", built.types),
|
|
1335
|
-
...ifDefined("models", built.models)
|
|
1371
|
+
...ifDefined("models", built.models),
|
|
1372
|
+
...ifDefined("enums", Object.keys(mergedEnums).length > 0 ? mergedEnums : void 0)
|
|
1336
1373
|
});
|
|
1337
1374
|
}
|
|
1338
1375
|
return buildContractFromDsl(full);
|
|
@@ -1343,6 +1380,6 @@ function defineContract(definition, factory) {
|
|
|
1343
1380
|
return buildBoundContract(definition.family, definition.target, definition);
|
|
1344
1381
|
}
|
|
1345
1382
|
//#endregion
|
|
1346
|
-
export { buildBoundContract, buildSqlContractFromDefinition, defineContract, enumType, extensionModel, field, member, model, rel };
|
|
1383
|
+
export { bindEnumType, buildBoundContract, buildSqlContractFromDefinition, defineContract, enumType, extensionModel, field, member, model, rel };
|
|
1347
1384
|
|
|
1348
1385
|
//# sourceMappingURL=contract-builder.mjs.map
|