@prisma-next/family-sql 0.13.0-dev.3 → 0.13.0-dev.31
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/dist/authoring-type-constructors-CjFfO6LM.mjs +342 -0
- package/dist/authoring-type-constructors-CjFfO6LM.mjs.map +1 -0
- package/dist/{control-adapter-CgIL9Vtx.d.mts → control-adapter-Cmw9LvEP.d.mts} +16 -33
- package/dist/control-adapter-Cmw9LvEP.d.mts.map +1 -0
- package/dist/control-adapter.d.mts +2 -2
- package/dist/control.d.mts +36 -34
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +24 -77
- package/dist/control.mjs.map +1 -1
- package/dist/ir.d.mts +3 -2
- package/dist/ir.d.mts.map +1 -1
- package/dist/ir.mjs +1 -1
- package/dist/migration.d.mts +1 -1
- package/dist/migration.d.mts.map +1 -1
- package/dist/migration.mjs +2 -1
- package/dist/migration.mjs.map +1 -1
- package/dist/pack.d.mts +16 -3
- package/dist/pack.d.mts.map +1 -1
- package/dist/pack.mjs +4 -2
- package/dist/pack.mjs.map +1 -1
- package/dist/schema-verify.d.mts +1 -1
- package/dist/schema-verify.mjs +1 -1
- package/dist/{sql-contract-serializer-CY7qnms7.mjs → sql-contract-serializer-D6-28zKd.mjs} +26 -15
- package/dist/sql-contract-serializer-D6-28zKd.mjs.map +1 -0
- package/dist/{types-CbwQCzXY.d.mts → types-kgstZ_Zd.d.mts} +5 -5
- package/dist/types-kgstZ_Zd.d.mts.map +1 -0
- package/dist/{verify-sql-schema-DcMaT5Zj.d.mts → verify-sql-schema-thU-jKpf.d.mts} +2 -14
- package/dist/verify-sql-schema-thU-jKpf.d.mts.map +1 -0
- package/dist/{verify-sql-schema-DlAgBiT_.mjs → verify-sql-schema-xT4udQLQ.mjs} +25 -118
- package/dist/verify-sql-schema-xT4udQLQ.mjs.map +1 -0
- package/package.json +21 -21
- package/src/core/authoring-entity-types.ts +178 -0
- package/src/core/authoring-field-presets.ts +8 -3
- package/src/core/control-adapter.ts +18 -49
- package/src/core/control-descriptor.ts +3 -0
- package/src/core/control-instance.ts +13 -11
- package/src/core/ir/sql-contract-serializer-base.ts +76 -60
- package/src/core/migrations/contract-to-schema-ir.ts +47 -112
- package/src/core/migrations/types.ts +4 -1
- package/src/core/psl-contract-infer/postgres-type-map.ts +5 -13
- package/src/core/psl-contract-infer/sql-schema-ir-to-psl-ast.ts +17 -70
- package/src/core/schema-verify/verify-sql-schema.ts +10 -146
- package/src/core/sql-migration.ts +5 -1
- package/src/exports/control-adapter.ts +1 -0
- package/src/exports/control.ts +1 -1
- package/src/exports/pack.ts +3 -0
- package/dist/authoring-type-constructors-D4lQ-qpj.mjs +0 -192
- package/dist/authoring-type-constructors-D4lQ-qpj.mjs.map +0 -1
- package/dist/control-adapter-CgIL9Vtx.d.mts.map +0 -1
- package/dist/sql-contract-serializer-CY7qnms7.mjs.map +0 -1
- package/dist/types-CbwQCzXY.d.mts.map +0 -1
- package/dist/verify-sql-schema-DcMaT5Zj.d.mts.map +0 -1
- package/dist/verify-sql-schema-DlAgBiT_.mjs.map +0 -1
package/dist/control.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { n as sqlFamilyAuthoringFieldPresets, t as sqlFamilyAuthoringTypes } from "./authoring-type-constructors-
|
|
2
|
-
import { t as SqlContractSerializer } from "./sql-contract-serializer-
|
|
3
|
-
import { a as contractToSchemaIR, c as extractCodecControlHooks, o as detectDestructiveChanges, s as resolveValueSetValues, t as verifySqlSchema } from "./verify-sql-schema-
|
|
1
|
+
import { i as sqlFamilyPslBlockDescriptors, n as sqlFamilyAuthoringFieldPresets, r as sqlFamilyEntityTypes, t as sqlFamilyAuthoringTypes } from "./authoring-type-constructors-CjFfO6LM.mjs";
|
|
2
|
+
import { t as SqlContractSerializer } from "./sql-contract-serializer-D6-28zKd.mjs";
|
|
3
|
+
import { a as contractToSchemaIR, c as extractCodecControlHooks, o as detectDestructiveChanges, s as resolveValueSetValues, t as verifySqlSchema } from "./verify-sql-schema-xT4udQLQ.mjs";
|
|
4
4
|
import { t as collectSupportedCodecTypeIds } from "./verify-C-G0obRm.mjs";
|
|
5
5
|
import { n as temporalAuthoringPresets, r as timestampNowControlDescriptor } from "./timestamp-now-generator-CloimujU.mjs";
|
|
6
6
|
import { sqlEmission } from "@prisma-next/sql-contract-emitter";
|
|
@@ -155,20 +155,6 @@ function toFieldName(columnName) {
|
|
|
155
155
|
};
|
|
156
156
|
return { name };
|
|
157
157
|
}
|
|
158
|
-
function toEnumName(pgTypeName) {
|
|
159
|
-
let name;
|
|
160
|
-
if (hasSeparators(pgTypeName)) name = snakeToPascalCase(pgTypeName);
|
|
161
|
-
else name = pgTypeName.charAt(0).toUpperCase() + pgTypeName.slice(1);
|
|
162
|
-
if (needsEscaping(name)) return {
|
|
163
|
-
name: escapeName(name),
|
|
164
|
-
map: pgTypeName
|
|
165
|
-
};
|
|
166
|
-
if (name !== pgTypeName) return {
|
|
167
|
-
name,
|
|
168
|
-
map: pgTypeName
|
|
169
|
-
};
|
|
170
|
-
return { name };
|
|
171
|
-
}
|
|
172
158
|
function pluralize(word) {
|
|
173
159
|
if (word.endsWith("s") || word.endsWith("x") || word.endsWith("z") || word.endsWith("ch") || word.endsWith("sh")) return `${word}es`;
|
|
174
160
|
if (word.endsWith("y") && !/[aeiou]y$/i.test(word)) return `${word.slice(0, -1)}ies`;
|
|
@@ -333,7 +319,6 @@ const PARAMETERIZED_NATIVE_TYPES = {
|
|
|
333
319
|
}
|
|
334
320
|
};
|
|
335
321
|
const PARAMETERIZED_TYPE_PATTERN = /^(.+?)\((.+)\)$/;
|
|
336
|
-
const ENUM_CODEC_ID = "pg/enum@1";
|
|
337
322
|
function getOwnMappingValue(map, key) {
|
|
338
323
|
return Object.hasOwn(map, key) ? map[key] : void 0;
|
|
339
324
|
}
|
|
@@ -387,15 +372,11 @@ function createPostgresTypeMap(enumTypeNames) {
|
|
|
387
372
|
} };
|
|
388
373
|
}
|
|
389
374
|
function extractEnumInfo(annotations) {
|
|
390
|
-
const
|
|
375
|
+
const nativeEnumTypeNames = (annotations?.["pg"])?.["nativeEnumTypeNames"];
|
|
391
376
|
const typeNames = /* @__PURE__ */ new Set();
|
|
392
377
|
const definitions = /* @__PURE__ */ new Map();
|
|
393
|
-
if (
|
|
394
|
-
for (const
|
|
395
|
-
typeNames.add(key);
|
|
396
|
-
const values = typeInstance.typeParams?.["values"];
|
|
397
|
-
if (Array.isArray(values)) definitions.set(key, values);
|
|
398
|
-
}
|
|
378
|
+
if (Array.isArray(nativeEnumTypeNames)) {
|
|
379
|
+
for (const name of nativeEnumTypeNames) if (typeof name === "string") typeNames.add(name);
|
|
399
380
|
}
|
|
400
381
|
return {
|
|
401
382
|
typeNames,
|
|
@@ -619,37 +600,27 @@ const PSL_SCALAR_TYPE_NAMES = new Set([
|
|
|
619
600
|
*/
|
|
620
601
|
function sqlSchemaIrToPslAst(schemaIR) {
|
|
621
602
|
const enumInfo = extractEnumInfo(schemaIR.annotations);
|
|
603
|
+
if (enumInfo.typeNames.size > 0) {
|
|
604
|
+
const names = [...enumInfo.typeNames].join(", ");
|
|
605
|
+
throw new Error(`contract infer: the database contains native Postgres enum type(s): ${names}. Native Postgres enums (CREATE TYPE … AS ENUM) are not adoptable by contract infer. Drop the native type and replace each column with a text column carrying a CHECK constraint, then re-run contract infer. The domain enum (enum Name { @@type("pg/text@1") … }) authoring surface generates the required check automatically.`);
|
|
606
|
+
}
|
|
622
607
|
return buildPslDocumentAst(schemaIR, {
|
|
623
|
-
typeMap: createPostgresTypeMap(
|
|
608
|
+
typeMap: createPostgresTypeMap(/* @__PURE__ */ new Set()),
|
|
624
609
|
defaultMapping: createPostgresDefaultMapping(),
|
|
625
|
-
enumInfo,
|
|
626
610
|
parseRawDefault
|
|
627
611
|
});
|
|
628
612
|
}
|
|
629
613
|
function buildPslDocumentAst(schemaIR, options) {
|
|
630
|
-
const { typeMap, defaultMapping,
|
|
631
|
-
const { typeNames: enumTypeNames, definitions: enumDefinitions } = enumInfo ?? {
|
|
632
|
-
typeNames: /* @__PURE__ */ new Set(),
|
|
633
|
-
definitions: /* @__PURE__ */ new Map()
|
|
634
|
-
};
|
|
614
|
+
const { typeMap, defaultMapping, parseRawDefault: rawDefaultParser } = options;
|
|
635
615
|
const modelNames = buildTopLevelNameMap(Object.keys(schemaIR.tables), toModelName, "model", "table");
|
|
636
|
-
const enumNames = buildTopLevelNameMap(enumTypeNames, toEnumName, "enum", "enum type");
|
|
637
|
-
assertNoCrossKindNameCollisions(modelNames, enumNames);
|
|
638
616
|
const modelNameMap = new Map([...modelNames].map(([tableName, result]) => [tableName, result.name]));
|
|
639
|
-
const
|
|
640
|
-
const reservedNamedTypeNames = createReservedNamedTypeNames(modelNames, enumNames);
|
|
617
|
+
const reservedNamedTypeNames = createReservedNamedTypeNames(modelNames);
|
|
641
618
|
const fieldNamesByTable = buildFieldNamesByTable(schemaIR.tables);
|
|
642
619
|
const { relationsByTable } = inferRelations(schemaIR.tables, modelNameMap);
|
|
643
|
-
const namedTypes = seedNamedTypeRegistry(schemaIR, typeMap,
|
|
620
|
+
const namedTypes = seedNamedTypeRegistry(schemaIR, typeMap, /* @__PURE__ */ new Map(), reservedNamedTypeNames);
|
|
644
621
|
const models = [];
|
|
645
|
-
for (const table of Object.values(schemaIR.tables)) models.push(buildModel(table, typeMap,
|
|
622
|
+
for (const table of Object.values(schemaIR.tables)) models.push(buildModel(table, typeMap, /* @__PURE__ */ new Map(), fieldNamesByTable, namedTypes, defaultMapping, rawDefaultParser, relationsByTable.get(table.name) ?? []));
|
|
646
623
|
const sortedModels = topologicalSort(models, schemaIR.tables, modelNameMap);
|
|
647
|
-
const enums = [];
|
|
648
|
-
for (const [pgTypeName, values] of enumDefinitions) {
|
|
649
|
-
const enumName = enumNames.get(pgTypeName);
|
|
650
|
-
enums.push(buildEnum(enumName, values));
|
|
651
|
-
}
|
|
652
|
-
enums.sort((a, b) => a.name.localeCompare(b.name));
|
|
653
624
|
const namedTypeEntries = [...namedTypes.entriesByKey.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
654
625
|
const types = namedTypeEntries.length > 0 ? {
|
|
655
626
|
kind: "types",
|
|
@@ -662,7 +633,7 @@ function buildPslDocumentAst(schemaIR, options) {
|
|
|
662
633
|
namespaces: [makePslNamespace({
|
|
663
634
|
kind: "namespace",
|
|
664
635
|
name: UNSPECIFIED_PSL_NAMESPACE_ID,
|
|
665
|
-
entries: makePslNamespaceEntries(sortedModels,
|
|
636
|
+
entries: makePslNamespaceEntries(sortedModels, [], []),
|
|
666
637
|
span: SYNTHETIC_SPAN
|
|
667
638
|
})],
|
|
668
639
|
...types ? { types } : {},
|
|
@@ -818,21 +789,6 @@ function namedArg(name, value) {
|
|
|
818
789
|
span: SYNTHETIC_SPAN
|
|
819
790
|
};
|
|
820
791
|
}
|
|
821
|
-
function buildEnum(name, values) {
|
|
822
|
-
const attrs = [];
|
|
823
|
-
if (name.map) attrs.push(buildMapAttribute("enum", name.map));
|
|
824
|
-
return {
|
|
825
|
-
kind: "enum",
|
|
826
|
-
name: name.name,
|
|
827
|
-
values: values.map((value) => ({
|
|
828
|
-
kind: "enumValue",
|
|
829
|
-
name: value,
|
|
830
|
-
span: SYNTHETIC_SPAN
|
|
831
|
-
})),
|
|
832
|
-
attributes: attrs,
|
|
833
|
-
span: SYNTHETIC_SPAN
|
|
834
|
-
};
|
|
835
|
-
}
|
|
836
792
|
function buildNamedTypeDeclaration(entry) {
|
|
837
793
|
const attribute = buildAttribute("namedType", entry.nativeTypeAttribute.name, (entry.nativeTypeAttribute.args ?? []).map(positionalArg));
|
|
838
794
|
return {
|
|
@@ -912,18 +868,9 @@ function buildTopLevelNameMap(sources, normalize, kind, sourceKind) {
|
|
|
912
868
|
}
|
|
913
869
|
return results;
|
|
914
870
|
}
|
|
915
|
-
function
|
|
916
|
-
const enumSourceByName = new Map([...enumNames].map(([source, result]) => [result.name, source]));
|
|
917
|
-
const collisions = [...modelNames.entries()].map(([tableName, result]) => {
|
|
918
|
-
const enumSource = enumSourceByName.get(result.name);
|
|
919
|
-
return enumSource ? `- identifier "${result.name}" from table "${tableName}" collides with enum type "${enumSource}"` : void 0;
|
|
920
|
-
}).filter((detail) => detail !== void 0);
|
|
921
|
-
if (collisions.length > 0) throw new Error(`PSL top-level name collisions detected:\n${collisions.join("\n")}`);
|
|
922
|
-
}
|
|
923
|
-
function createReservedNamedTypeNames(modelNames, enumNames) {
|
|
871
|
+
function createReservedNamedTypeNames(modelNames) {
|
|
924
872
|
const reservedNames = new Set(PSL_SCALAR_TYPE_NAMES);
|
|
925
873
|
for (const result of modelNames.values()) reservedNames.add(result.name);
|
|
926
|
-
for (const result of enumNames.values()) reservedNames.add(result.name);
|
|
927
874
|
return reservedNames;
|
|
928
875
|
}
|
|
929
876
|
function seedNamedTypeRegistry(schemaIR, typeMap, enumNameMap, reservedNames) {
|
|
@@ -1020,7 +967,7 @@ function extractCodecTypeIdsFromContract(contract) {
|
|
|
1020
967
|
if (typeof contract === "object" && contract !== null && "storage" in contract && typeof contract.storage === "object" && contract.storage !== null && "namespaces" in contract.storage && typeof contract.storage.namespaces === "object" && contract.storage.namespaces !== null) {
|
|
1021
968
|
const namespaces = contract.storage.namespaces;
|
|
1022
969
|
for (const ns of Object.values(namespaces)) {
|
|
1023
|
-
const tbls = ns.entries
|
|
970
|
+
const tbls = ns.entries["table"];
|
|
1024
971
|
if (typeof tbls !== "object" || tbls === null) continue;
|
|
1025
972
|
for (const table of Object.values(tbls)) if (typeof table === "object" && table !== null && "columns" in table && typeof table.columns === "object" && table.columns !== null) {
|
|
1026
973
|
const columns = table.columns;
|
|
@@ -1281,7 +1228,6 @@ function createSqlFamilyInstance(stack) {
|
|
|
1281
1228
|
verifySchema(options) {
|
|
1282
1229
|
const contract = deserializeWithTargetSerializer(options.contract);
|
|
1283
1230
|
const controlAdapter = getControlAdapter();
|
|
1284
|
-
const resolveExistingEnumValues = controlAdapter.resolveExistingEnumValuesForContract?.(contract) ?? controlAdapter.resolveExistingEnumValues;
|
|
1285
1231
|
return verifySqlSchema({
|
|
1286
1232
|
contract,
|
|
1287
1233
|
schema: options.schema,
|
|
@@ -1289,8 +1235,7 @@ function createSqlFamilyInstance(stack) {
|
|
|
1289
1235
|
typeMetadataRegistry,
|
|
1290
1236
|
frameworkComponents: options.frameworkComponents,
|
|
1291
1237
|
...ifDefined("normalizeDefault", controlAdapter.normalizeDefault),
|
|
1292
|
-
...ifDefined("normalizeNativeType", controlAdapter.normalizeNativeType)
|
|
1293
|
-
...ifDefined("resolveExistingEnumValues", resolveExistingEnumValues)
|
|
1238
|
+
...ifDefined("normalizeNativeType", controlAdapter.normalizeNativeType)
|
|
1294
1239
|
});
|
|
1295
1240
|
},
|
|
1296
1241
|
async sign(options) {
|
|
@@ -1303,7 +1248,7 @@ function createSqlFamilyInstance(stack) {
|
|
|
1303
1248
|
const controlAdapter = getControlAdapter();
|
|
1304
1249
|
const lowererContext = { contract };
|
|
1305
1250
|
for (const query of controlAdapter.bootstrapSignMarkerQueries()) {
|
|
1306
|
-
const lowered = controlAdapter.
|
|
1251
|
+
const lowered = await controlAdapter.lowerToExecuteRequest(query, lowererContext);
|
|
1307
1252
|
await driver.query(lowered.sql, lowered.params);
|
|
1308
1253
|
}
|
|
1309
1254
|
const existingMarker = await controlAdapter.readMarker(driver, APP_SPACE_ID);
|
|
@@ -1384,7 +1329,7 @@ function createSqlFamilyInstance(stack) {
|
|
|
1384
1329
|
return sqlSchemaIrToPslAst(schemaIR);
|
|
1385
1330
|
},
|
|
1386
1331
|
lowerAst(ast, context) {
|
|
1387
|
-
return getControlAdapter().
|
|
1332
|
+
return getControlAdapter().lowerToExecuteRequest(ast, context);
|
|
1388
1333
|
},
|
|
1389
1334
|
bootstrapControlTableQueries() {
|
|
1390
1335
|
return getControlAdapter().bootstrapControlTableQueries();
|
|
@@ -1494,7 +1439,9 @@ var SqlFamilyDescriptor = class {
|
|
|
1494
1439
|
emission = sqlEmission;
|
|
1495
1440
|
authoring = {
|
|
1496
1441
|
field: sqlFamilyAuthoringFieldPresets,
|
|
1497
|
-
type: sqlFamilyAuthoringTypes
|
|
1442
|
+
type: sqlFamilyAuthoringTypes,
|
|
1443
|
+
entityTypes: sqlFamilyEntityTypes,
|
|
1444
|
+
pslBlockDescriptors: sqlFamilyPslBlockDescriptors
|
|
1498
1445
|
};
|
|
1499
1446
|
create(stack) {
|
|
1500
1447
|
return createSqlFamilyInstance(stack);
|