@zenstackhq/sdk 3.9.8 → 3.10.1
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/index.cjs +22 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -1
- package/dist/index.d.mts +6 -1
- package/dist/index.mjs +23 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
package/dist/index.d.cts
CHANGED
|
@@ -164,6 +164,12 @@ declare class TsSchemaGenerator {
|
|
|
164
164
|
private getDataSourceDefaultSchema;
|
|
165
165
|
private getFieldMappedDefault;
|
|
166
166
|
private getMappedValue;
|
|
167
|
+
/**
|
|
168
|
+
* The primitive type used to map a literal default value to its JS representation. For a
|
|
169
|
+
* primitive typedef reference (e.g. `type Age with Int`), this resolves to the base type so
|
|
170
|
+
* that e.g. an `Int`-based typedef default is emitted as a number instead of a string.
|
|
171
|
+
*/
|
|
172
|
+
private getMappedValueBaseType;
|
|
167
173
|
private getMemberAccessChain;
|
|
168
174
|
private isAuthMemberAccess;
|
|
169
175
|
private isAuthInvocation;
|
|
@@ -171,7 +177,6 @@ declare class TsSchemaGenerator {
|
|
|
171
177
|
private getReferenceNames;
|
|
172
178
|
private getForeignKeyFor;
|
|
173
179
|
private getOppositeRelationField;
|
|
174
|
-
private getRelationName;
|
|
175
180
|
private createUniqueFieldsObject;
|
|
176
181
|
private getCompoundUniqueKey;
|
|
177
182
|
private generateFieldTypeLiteral;
|
package/dist/index.d.mts
CHANGED
|
@@ -164,6 +164,12 @@ declare class TsSchemaGenerator {
|
|
|
164
164
|
private getDataSourceDefaultSchema;
|
|
165
165
|
private getFieldMappedDefault;
|
|
166
166
|
private getMappedValue;
|
|
167
|
+
/**
|
|
168
|
+
* The primitive type used to map a literal default value to its JS representation. For a
|
|
169
|
+
* primitive typedef reference (e.g. `type Age with Int`), this resolves to the base type so
|
|
170
|
+
* that e.g. an `Int`-based typedef default is emitted as a number instead of a string.
|
|
171
|
+
*/
|
|
172
|
+
private getMappedValueBaseType;
|
|
167
173
|
private getMemberAccessChain;
|
|
168
174
|
private isAuthMemberAccess;
|
|
169
175
|
private isAuthInvocation;
|
|
@@ -171,7 +177,6 @@ declare class TsSchemaGenerator {
|
|
|
171
177
|
private getReferenceNames;
|
|
172
178
|
private getForeignKeyFor;
|
|
173
179
|
private getOppositeRelationField;
|
|
174
|
-
private getRelationName;
|
|
175
180
|
private createUniqueFieldsObject;
|
|
176
181
|
private getCompoundUniqueKey;
|
|
177
182
|
private generateFieldTypeLiteral;
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { t as __exportAll } from "./chunk-CfYAbeIz.mjs";
|
|
2
2
|
import { BooleanLiteral, DataModel, DataSource, Enum, GeneratorDecl, NumberLiteral, StringLiteral, isArrayExpr, isBinaryExpr, isCollectionPredicateBinding, isDataField, isDataModel, isDataSource, isEnum, isEnumField, isGeneratorDecl, isInvocationExpr, isLiteralExpr, isMemberAccessExpr, isModel, isNullExpr, isProcedure, isReferenceExpr, isStringLiteral, isThisExpr, isTypeDef, isUnaryExpr } from "@zenstackhq/language/ast";
|
|
3
|
-
import { getAllAttributes, getAllFields, getAttributeArg, getModelIdFields, getModelUniqueFields, getStringLiteral, isAuthInvocation, isDataFieldReference, isDelegateModel, isLiteAttribute } from "@zenstackhq/language/utils";
|
|
3
|
+
import { getAllAttributes, getAllFields, getAttributeArg, getModelIdFields, getModelUniqueFields, getRelationName, getStringLiteral, isAuthInvocation, isDataFieldReference, isDelegateModel, isLiteAttribute, isPrimitiveTypeDef } from "@zenstackhq/language/utils";
|
|
4
4
|
import { invariant, lowerCaseFirst } from "@zenstackhq/common-helpers";
|
|
5
5
|
import { ZModelCodeGenerator } from "@zenstackhq/language";
|
|
6
6
|
import { AstUtils } from "langium";
|
|
@@ -50,7 +50,8 @@ function isDelegateModel$1(node) {
|
|
|
50
50
|
*/
|
|
51
51
|
function getOwnedFields(model) {
|
|
52
52
|
const fields = [...model.fields];
|
|
53
|
-
|
|
53
|
+
const mixins = model.mixins.filter((m) => !m.ref?.base);
|
|
54
|
+
for (const mixin of mixins) if (mixin.ref) fields.push(...getOwnedFields(mixin.ref));
|
|
54
55
|
return fields;
|
|
55
56
|
}
|
|
56
57
|
/**
|
|
@@ -506,14 +507,14 @@ var PrismaSchemaGenerator = class {
|
|
|
506
507
|
generateModelField(model, field, contextModel, addToFront = false) {
|
|
507
508
|
let fieldType;
|
|
508
509
|
if (field.type.type) fieldType = field.type.type;
|
|
509
|
-
else if (field.type.reference?.ref) if (isTypeDef(field.type.reference.ref)) fieldType = "Json";
|
|
510
|
+
else if (field.type.reference?.ref) if (isTypeDef(field.type.reference.ref)) fieldType = isPrimitiveTypeDef(field.type.reference.ref) ? field.type.reference.ref.base : "Json";
|
|
510
511
|
else fieldType = field.type.reference.ref.name;
|
|
511
512
|
else {
|
|
512
513
|
const unsupported = this.getUnsupportedFieldType(field.type);
|
|
513
514
|
if (unsupported) fieldType = unsupported;
|
|
514
515
|
}
|
|
515
516
|
if (!fieldType) throw new Error(`Field type is not resolved: ${field.$container.name}.${field.name}`);
|
|
516
|
-
const isArray = isTypeDef(field.type.reference?.ref) ? false : field.type.array;
|
|
517
|
+
const isArray = isTypeDef(field.type.reference?.ref) ? isPrimitiveTypeDef(field.type.reference.ref) ? field.type.array : false : field.type.array;
|
|
517
518
|
const type = new ModelFieldType(fieldType, isArray, field.type.optional);
|
|
518
519
|
const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithAuthInvocation(attr)).filter((attr) => !(isIdField(field, contextModel) && getDelegateOriginModel(field, contextModel) && attr.decl.$refText === "@default")).map((attr) => this.makeFieldAttribute(attr));
|
|
519
520
|
const docs = [...field.comments];
|
|
@@ -750,6 +751,7 @@ var TsSchemaGenerator = class {
|
|
|
750
751
|
const attributes = lite ? getAllAttributes(td).filter((attr) => isLiteAttribute(attr.decl.ref)) : getAllAttributes(td);
|
|
751
752
|
const fields = [
|
|
752
753
|
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(td.name)),
|
|
754
|
+
...td.base ? [ts.factory.createPropertyAssignment("base", ts.factory.createStringLiteral(td.base))] : [],
|
|
753
755
|
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0, lite))), true)),
|
|
754
756
|
...attributes.length > 0 ? [ts.factory.createPropertyAssignment("attributes", this.createAttributesTypeAssertion(ts.factory.createArrayLiteralExpression(attributes.map((attr) => this.createAttributeObject(attr)), true)))] : []
|
|
755
757
|
];
|
|
@@ -835,12 +837,13 @@ var TsSchemaGenerator = class {
|
|
|
835
837
|
getMappedValue(expr, fieldType) {
|
|
836
838
|
if (isLiteralExpr(expr)) {
|
|
837
839
|
const lit = expr.value;
|
|
838
|
-
|
|
840
|
+
const effectiveType = this.getMappedValueBaseType(fieldType);
|
|
841
|
+
return effectiveType === "Boolean" ? lit : [
|
|
839
842
|
"Int",
|
|
840
843
|
"Float",
|
|
841
844
|
"Decimal",
|
|
842
845
|
"BigInt"
|
|
843
|
-
].includes(
|
|
846
|
+
].includes(effectiveType) ? Number(lit) : lit;
|
|
844
847
|
} else if (isArrayExpr(expr)) return expr.items.map((item) => this.getMappedValue(item, fieldType));
|
|
845
848
|
else if (isReferenceExpr(expr) && isEnumField(expr.target.ref)) return expr.target.ref.name;
|
|
846
849
|
else if (isInvocationExpr(expr)) return {
|
|
@@ -851,6 +854,16 @@ var TsSchemaGenerator = class {
|
|
|
851
854
|
else if (isNullExpr(expr)) return null;
|
|
852
855
|
else throw new Error(`Unsupported expression type: ${expr.$type}`);
|
|
853
856
|
}
|
|
857
|
+
/**
|
|
858
|
+
* The primitive type used to map a literal default value to its JS representation. For a
|
|
859
|
+
* primitive typedef reference (e.g. `type Age with Int`), this resolves to the base type so
|
|
860
|
+
* that e.g. an `Int`-based typedef default is emitted as a number instead of a string.
|
|
861
|
+
*/
|
|
862
|
+
getMappedValueBaseType(fieldType) {
|
|
863
|
+
if (fieldType.type) return fieldType.type;
|
|
864
|
+
const ref = fieldType.reference?.ref;
|
|
865
|
+
return ref && isPrimitiveTypeDef(ref) ? ref.base : void 0;
|
|
866
|
+
}
|
|
854
867
|
getMemberAccessChain(expr) {
|
|
855
868
|
if (!isMemberAccessExpr(expr.operand)) return [expr.member.$refText];
|
|
856
869
|
else return [...this.getMemberAccessChain(expr.operand), expr.member.$refText];
|
|
@@ -866,7 +879,7 @@ var TsSchemaGenerator = class {
|
|
|
866
879
|
const relationFields = [];
|
|
867
880
|
const oppositeRelation = this.getOppositeRelationField(field, contextModel);
|
|
868
881
|
if (oppositeRelation) relationFields.push(ts.factory.createPropertyAssignment("opposite", ts.factory.createStringLiteral(oppositeRelation.name)));
|
|
869
|
-
const relationName =
|
|
882
|
+
const relationName = getRelationName(field);
|
|
870
883
|
if (relationName) relationFields.push(ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(relationName)));
|
|
871
884
|
const relation = getAttribute(field, "@relation");
|
|
872
885
|
const fkFields = [];
|
|
@@ -910,21 +923,11 @@ var TsSchemaGenerator = class {
|
|
|
910
923
|
if (!field.type.reference?.ref || !isDataModel(field.type.reference?.ref) || !contextModel) return;
|
|
911
924
|
const sourceModel = isTypeDef(field.$container) ? contextModel : field.$container;
|
|
912
925
|
const targetModel = field.type.reference.ref;
|
|
913
|
-
const relationName =
|
|
926
|
+
const relationName = getRelationName(field);
|
|
914
927
|
for (const otherField of getAllFields(targetModel)) {
|
|
915
928
|
if (otherField === field) continue;
|
|
916
929
|
if (otherField.type.reference?.ref === sourceModel) {
|
|
917
|
-
if (
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
}
|
|
921
|
-
getRelationName(field) {
|
|
922
|
-
const relation = getAttribute(field, "@relation");
|
|
923
|
-
if (relation) {
|
|
924
|
-
const nameArg = relation.args.find((arg) => arg.$resolvedParam?.name === "name");
|
|
925
|
-
if (nameArg) {
|
|
926
|
-
invariant(isLiteralExpr(nameArg.value), "name must be a literal");
|
|
927
|
-
return nameArg.value.value;
|
|
930
|
+
if (getRelationName(otherField) === relationName) return otherField;
|
|
928
931
|
}
|
|
929
932
|
}
|
|
930
933
|
}
|
|
@@ -1061,7 +1064,7 @@ var TsSchemaGenerator = class {
|
|
|
1061
1064
|
}
|
|
1062
1065
|
createRefExpression(expr) {
|
|
1063
1066
|
const target = expr.target.ref;
|
|
1064
|
-
return match(target).when(isDataField, () => this.createExpressionUtilsCall("field", [this.createLiteralNode(expr.target.$refText)])).when(isEnumField, () => this.createLiteralExpression("StringLiteral", expr.target.$refText)).when(isCollectionPredicateBinding, () => this.createExpressionUtilsCall("binding", [this.createLiteralNode(expr.target.$refText)])).otherwise(() => {
|
|
1067
|
+
return match(target).when(isDataField, () => this.createExpressionUtilsCall("field", [this.createLiteralNode(expr.target.$refText)])).when(isEnumField, () => this.createLiteralExpression("StringLiteral", expr.target.$refText)).when(isCollectionPredicateBinding, () => this.createExpressionUtilsCall("binding", [this.createLiteralNode(expr.target.$refText)])).when(isEnum, () => this.createExpressionUtilsCall("array", [this.createLiteralNode(expr.target.$refText), ts.factory.createArrayLiteralExpression(target.fields.map((field) => this.createLiteralExpression("StringLiteral", field.name)))])).otherwise(() => {
|
|
1065
1068
|
throw Error(`Unsupported reference type: ${expr.target.$refText}`);
|
|
1066
1069
|
});
|
|
1067
1070
|
}
|