@zenstackhq/sdk 3.0.0-alpha.9 → 3.0.0-beta.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 +354 -166
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +69 -32
- package/dist/index.d.ts +69 -32
- package/dist/index.js +364 -176
- package/dist/index.js.map +1 -1
- package/dist/schema.d.cts +45 -17
- package/dist/schema.d.ts +45 -17
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -41,12 +41,11 @@ module.exports = __toCommonJS(src_exports);
|
|
|
41
41
|
// src/model-utils.ts
|
|
42
42
|
var model_utils_exports = {};
|
|
43
43
|
__export(model_utils_exports, {
|
|
44
|
+
DELEGATE_AUX_RELATION_PREFIX: () => DELEGATE_AUX_RELATION_PREFIX,
|
|
44
45
|
getAttribute: () => getAttribute,
|
|
45
46
|
getAuthDecl: () => getAuthDecl,
|
|
46
47
|
getContainingModel: () => getContainingModel,
|
|
47
|
-
|
|
48
|
-
getModelUniqueFields: () => getModelUniqueFields,
|
|
49
|
-
getRecursiveBases: () => getRecursiveBases,
|
|
48
|
+
getIdFields: () => getIdFields,
|
|
50
49
|
hasAttribute: () => hasAttribute,
|
|
51
50
|
isDelegateModel: () => isDelegateModel,
|
|
52
51
|
isFromStdlib: () => isFromStdlib,
|
|
@@ -55,23 +54,24 @@ __export(model_utils_exports, {
|
|
|
55
54
|
resolved: () => resolved
|
|
56
55
|
});
|
|
57
56
|
var import_ast = require("@zenstackhq/language/ast");
|
|
58
|
-
|
|
57
|
+
var import_utils = require("@zenstackhq/language/utils");
|
|
58
|
+
function isIdField(field, contextModel) {
|
|
59
59
|
if (hasAttribute(field, "@id")) {
|
|
60
60
|
return true;
|
|
61
61
|
}
|
|
62
|
-
const
|
|
63
|
-
const modelLevelIds = getModelIdFields(model);
|
|
62
|
+
const modelLevelIds = (0, import_utils.getModelIdFields)(contextModel);
|
|
64
63
|
if (modelLevelIds.map((f) => f.name).includes(field.name)) {
|
|
65
64
|
return true;
|
|
66
65
|
}
|
|
67
|
-
|
|
66
|
+
const allFields = (0, import_utils.getAllFields)(contextModel);
|
|
67
|
+
if (allFields.some((f) => hasAttribute(f, "@id")) || modelLevelIds.length > 0) {
|
|
68
68
|
return false;
|
|
69
69
|
}
|
|
70
|
-
const firstUniqueField =
|
|
70
|
+
const firstUniqueField = allFields.find((f) => hasAttribute(f, "@unique"));
|
|
71
71
|
if (firstUniqueField) {
|
|
72
72
|
return firstUniqueField.name === field.name;
|
|
73
73
|
}
|
|
74
|
-
const modelLevelUnique = getModelUniqueFields(
|
|
74
|
+
const modelLevelUnique = (0, import_utils.getModelUniqueFields)(contextModel);
|
|
75
75
|
if (modelLevelUnique.map((f) => f.name).includes(field.name)) {
|
|
76
76
|
return true;
|
|
77
77
|
}
|
|
@@ -86,67 +86,6 @@ function getAttribute(decl, name) {
|
|
|
86
86
|
return decl.attributes.find((attr) => attr.decl.$refText === name);
|
|
87
87
|
}
|
|
88
88
|
__name(getAttribute, "getAttribute");
|
|
89
|
-
function getModelIdFields(model) {
|
|
90
|
-
const modelsToCheck = model.$baseMerged ? [
|
|
91
|
-
model
|
|
92
|
-
] : [
|
|
93
|
-
model,
|
|
94
|
-
...getRecursiveBases(model)
|
|
95
|
-
];
|
|
96
|
-
for (const modelToCheck of modelsToCheck) {
|
|
97
|
-
const idAttr = modelToCheck.attributes.find((attr) => attr.decl.$refText === "@@id");
|
|
98
|
-
if (!idAttr) {
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
const fieldsArg = idAttr.args.find((a) => a.$resolvedParam?.name === "fields");
|
|
102
|
-
if (!fieldsArg || !(0, import_ast.isArrayExpr)(fieldsArg.value)) {
|
|
103
|
-
continue;
|
|
104
|
-
}
|
|
105
|
-
return fieldsArg.value.items.filter((item) => (0, import_ast.isReferenceExpr)(item)).map((item) => item.target.ref);
|
|
106
|
-
}
|
|
107
|
-
return [];
|
|
108
|
-
}
|
|
109
|
-
__name(getModelIdFields, "getModelIdFields");
|
|
110
|
-
function getModelUniqueFields(model) {
|
|
111
|
-
const modelsToCheck = model.$baseMerged ? [
|
|
112
|
-
model
|
|
113
|
-
] : [
|
|
114
|
-
model,
|
|
115
|
-
...getRecursiveBases(model)
|
|
116
|
-
];
|
|
117
|
-
for (const modelToCheck of modelsToCheck) {
|
|
118
|
-
const uniqueAttr = modelToCheck.attributes.find((attr) => attr.decl.$refText === "@@unique");
|
|
119
|
-
if (!uniqueAttr) {
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
const fieldsArg = uniqueAttr.args.find((a) => a.$resolvedParam?.name === "fields");
|
|
123
|
-
if (!fieldsArg || !(0, import_ast.isArrayExpr)(fieldsArg.value)) {
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
return fieldsArg.value.items.filter((item) => (0, import_ast.isReferenceExpr)(item)).map((item) => item.target.ref);
|
|
127
|
-
}
|
|
128
|
-
return [];
|
|
129
|
-
}
|
|
130
|
-
__name(getModelUniqueFields, "getModelUniqueFields");
|
|
131
|
-
function getRecursiveBases(dataModel, includeDelegate = true, seen = /* @__PURE__ */ new Set()) {
|
|
132
|
-
const result = [];
|
|
133
|
-
if (seen.has(dataModel)) {
|
|
134
|
-
return result;
|
|
135
|
-
}
|
|
136
|
-
seen.add(dataModel);
|
|
137
|
-
dataModel.superTypes.forEach((superType) => {
|
|
138
|
-
const baseDecl = superType.ref;
|
|
139
|
-
if (baseDecl) {
|
|
140
|
-
if (!includeDelegate && isDelegateModel(baseDecl)) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
result.push(baseDecl);
|
|
144
|
-
result.push(...getRecursiveBases(baseDecl, includeDelegate, seen));
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
__name(getRecursiveBases, "getRecursiveBases");
|
|
150
89
|
function isDelegateModel(node) {
|
|
151
90
|
return (0, import_ast.isDataModel)(node) && hasAttribute(node, "@@delegate");
|
|
152
91
|
}
|
|
@@ -189,9 +128,16 @@ function getAuthDecl(model) {
|
|
|
189
128
|
return found;
|
|
190
129
|
}
|
|
191
130
|
__name(getAuthDecl, "getAuthDecl");
|
|
131
|
+
function getIdFields(dm) {
|
|
132
|
+
return (0, import_utils.getAllFields)(dm).filter((f) => isIdField(f, dm)).map((f) => f.name);
|
|
133
|
+
}
|
|
134
|
+
__name(getIdFields, "getIdFields");
|
|
135
|
+
var DELEGATE_AUX_RELATION_PREFIX = "delegate_aux";
|
|
192
136
|
|
|
193
137
|
// src/prisma/prisma-schema-generator.ts
|
|
138
|
+
var import_common_helpers = require("@zenstackhq/common-helpers");
|
|
194
139
|
var import_ast2 = require("@zenstackhq/language/ast");
|
|
140
|
+
var import_utils2 = require("@zenstackhq/language/utils");
|
|
195
141
|
var import_langium = require("langium");
|
|
196
142
|
var import_ts_pattern = require("ts-pattern");
|
|
197
143
|
|
|
@@ -586,6 +532,7 @@ var EnumField = class extends DeclarationBase {
|
|
|
586
532
|
};
|
|
587
533
|
|
|
588
534
|
// src/prisma/prisma-schema-generator.ts
|
|
535
|
+
var IDENTIFIER_NAME_MAX_LENGTH = 50 - DELEGATE_AUX_RELATION_PREFIX.length;
|
|
589
536
|
var PrismaSchemaGenerator = class {
|
|
590
537
|
static {
|
|
591
538
|
__name(this, "PrismaSchemaGenerator");
|
|
@@ -597,6 +544,8 @@ var PrismaSchemaGenerator = class {
|
|
|
597
544
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
598
545
|
|
|
599
546
|
`;
|
|
547
|
+
// a mapping from full names to shortened names
|
|
548
|
+
shortNameMap = /* @__PURE__ */ new Map();
|
|
600
549
|
constructor(zmodel) {
|
|
601
550
|
this.zmodel = zmodel;
|
|
602
551
|
}
|
|
@@ -660,16 +609,22 @@ var PrismaSchemaGenerator = class {
|
|
|
660
609
|
}
|
|
661
610
|
generateModel(prisma, decl) {
|
|
662
611
|
const model = decl.isView ? prisma.addView(decl.name) : prisma.addModel(decl.name);
|
|
663
|
-
|
|
612
|
+
const allFields = (0, import_utils2.getAllFields)(decl, true);
|
|
613
|
+
for (const field of allFields) {
|
|
664
614
|
if (model_utils_exports.hasAttribute(field, "@computed")) {
|
|
665
615
|
continue;
|
|
666
616
|
}
|
|
667
|
-
this.
|
|
617
|
+
if (model_utils_exports.isIdField(field, decl) || !this.isInheritedFromDelegate(field, decl)) {
|
|
618
|
+
this.generateModelField(model, field, decl);
|
|
619
|
+
}
|
|
668
620
|
}
|
|
669
|
-
|
|
621
|
+
const allAttributes = (0, import_utils2.getAllAttributes)(decl);
|
|
622
|
+
for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
|
|
670
623
|
this.generateContainerAttribute(model, attr);
|
|
671
624
|
}
|
|
672
625
|
decl.comments.forEach((c) => model.addComment(c));
|
|
626
|
+
this.generateDelegateRelationForBase(model, decl);
|
|
627
|
+
this.generateDelegateRelationForConcrete(model, decl);
|
|
673
628
|
}
|
|
674
629
|
isPrismaAttribute(attr) {
|
|
675
630
|
if (!attr.decl.ref) {
|
|
@@ -692,7 +647,7 @@ var PrismaSchemaGenerator = class {
|
|
|
692
647
|
getStringLiteral(node) {
|
|
693
648
|
return (0, import_ast2.isStringLiteral)(node) ? node.value : void 0;
|
|
694
649
|
}
|
|
695
|
-
generateModelField(model, field, addToFront = false) {
|
|
650
|
+
generateModelField(model, field, contextModel, addToFront = false) {
|
|
696
651
|
let fieldType;
|
|
697
652
|
if (field.type.type) {
|
|
698
653
|
fieldType = field.type.type;
|
|
@@ -718,7 +673,7 @@ var PrismaSchemaGenerator = class {
|
|
|
718
673
|
const type = new ModelFieldType(fieldType, isArray, field.type.optional);
|
|
719
674
|
const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithPluginInvocation(attr)).filter((attr) => (
|
|
720
675
|
// when building physical schema, exclude `@default` for id fields inherited from delegate base
|
|
721
|
-
!(model_utils_exports.isIdField(field) && this.isInheritedFromDelegate(field) && attr.decl.$refText === "@default")
|
|
676
|
+
!(model_utils_exports.isIdField(field, contextModel) && this.isInheritedFromDelegate(field, contextModel) && attr.decl.$refText === "@default")
|
|
722
677
|
)).map((attr) => this.makeFieldAttribute(attr));
|
|
723
678
|
const docs = [
|
|
724
679
|
...field.comments
|
|
@@ -740,16 +695,8 @@ var PrismaSchemaGenerator = class {
|
|
|
740
695
|
const model = import_langium.AstUtils.getContainerOfType(node, import_ast2.isModel);
|
|
741
696
|
return !!model && !!model.$document && model.$document.uri.path.endsWith("plugin.zmodel");
|
|
742
697
|
}
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
throw new Error(`Unsupported field type with default value: ${field.type.type}`);
|
|
746
|
-
});
|
|
747
|
-
result.attributes.push(new FieldAttribute("@default", [
|
|
748
|
-
new AttributeArg(void 0, dummyDefaultValue)
|
|
749
|
-
]));
|
|
750
|
-
}
|
|
751
|
-
isInheritedFromDelegate(field) {
|
|
752
|
-
return field.$inheritedFrom && model_utils_exports.isDelegateModel(field.$inheritedFrom);
|
|
698
|
+
isInheritedFromDelegate(field, contextModel) {
|
|
699
|
+
return field.$container !== contextModel && model_utils_exports.isDelegateModel(field.$container);
|
|
753
700
|
}
|
|
754
701
|
makeFieldAttribute(attr) {
|
|
755
702
|
const attrName = attr.decl.ref.name;
|
|
@@ -806,12 +753,66 @@ var PrismaSchemaGenerator = class {
|
|
|
806
753
|
];
|
|
807
754
|
_enum.addField(field.name, attributes, docs);
|
|
808
755
|
}
|
|
756
|
+
generateDelegateRelationForBase(model, decl) {
|
|
757
|
+
if (!(0, import_utils2.isDelegateModel)(decl)) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
const concreteModels = this.getConcreteModels(decl);
|
|
761
|
+
concreteModels.forEach((concrete) => {
|
|
762
|
+
const auxName = this.truncate(`${DELEGATE_AUX_RELATION_PREFIX}_${(0, import_common_helpers.lowerCaseFirst)(concrete.name)}`);
|
|
763
|
+
model.addField(auxName, new ModelFieldType(concrete.name, false, true));
|
|
764
|
+
});
|
|
765
|
+
}
|
|
766
|
+
generateDelegateRelationForConcrete(model, concreteDecl) {
|
|
767
|
+
const base = concreteDecl.baseModel?.ref;
|
|
768
|
+
if (!base) {
|
|
769
|
+
return;
|
|
770
|
+
}
|
|
771
|
+
const idFields = getIdFields(base);
|
|
772
|
+
const relationField = this.truncate(`${DELEGATE_AUX_RELATION_PREFIX}_${(0, import_common_helpers.lowerCaseFirst)(base.name)}`);
|
|
773
|
+
model.addField(relationField, base.name, [
|
|
774
|
+
new FieldAttribute("@relation", [
|
|
775
|
+
new AttributeArg("fields", new AttributeArgValue("Array", idFields.map((idField) => new AttributeArgValue("FieldReference", new FieldReference(idField))))),
|
|
776
|
+
new AttributeArg("references", new AttributeArgValue("Array", idFields.map((idField) => new AttributeArgValue("FieldReference", new FieldReference(idField))))),
|
|
777
|
+
new AttributeArg("onDelete", new AttributeArgValue("FieldReference", new FieldReference("Cascade"))),
|
|
778
|
+
new AttributeArg("onUpdate", new AttributeArgValue("FieldReference", new FieldReference("Cascade")))
|
|
779
|
+
])
|
|
780
|
+
]);
|
|
781
|
+
}
|
|
782
|
+
getConcreteModels(dataModel) {
|
|
783
|
+
if (!(0, import_utils2.isDelegateModel)(dataModel)) {
|
|
784
|
+
return [];
|
|
785
|
+
}
|
|
786
|
+
return dataModel.$container.declarations.filter((d) => (0, import_ast2.isDataModel)(d) && d !== dataModel && d.baseModel?.ref === dataModel);
|
|
787
|
+
}
|
|
788
|
+
truncate(name) {
|
|
789
|
+
if (name.length <= IDENTIFIER_NAME_MAX_LENGTH) {
|
|
790
|
+
return name;
|
|
791
|
+
}
|
|
792
|
+
const existing = this.shortNameMap.get(name);
|
|
793
|
+
if (existing) {
|
|
794
|
+
return existing;
|
|
795
|
+
}
|
|
796
|
+
const baseName = name.slice(0, IDENTIFIER_NAME_MAX_LENGTH);
|
|
797
|
+
let index = 0;
|
|
798
|
+
let shortName = `${baseName}_${index}`;
|
|
799
|
+
while (true) {
|
|
800
|
+
const conflict = Array.from(this.shortNameMap.values()).find((v) => v === shortName);
|
|
801
|
+
if (!conflict) {
|
|
802
|
+
this.shortNameMap.set(name, shortName);
|
|
803
|
+
break;
|
|
804
|
+
}
|
|
805
|
+
index++;
|
|
806
|
+
shortName = `${baseName}_${index}`;
|
|
807
|
+
}
|
|
808
|
+
return shortName;
|
|
809
|
+
}
|
|
809
810
|
};
|
|
810
811
|
|
|
811
812
|
// src/ts-schema-generator.ts
|
|
812
|
-
var
|
|
813
|
-
var import_language = require("@zenstackhq/language");
|
|
813
|
+
var import_common_helpers2 = require("@zenstackhq/common-helpers");
|
|
814
814
|
var import_ast3 = require("@zenstackhq/language/ast");
|
|
815
|
+
var import_utils3 = require("@zenstackhq/language/utils");
|
|
815
816
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
816
817
|
var import_node_path = __toESM(require("path"), 1);
|
|
817
818
|
var import_ts_pattern2 = require("ts-pattern");
|
|
@@ -820,26 +821,23 @@ var TsSchemaGenerator = class {
|
|
|
820
821
|
static {
|
|
821
822
|
__name(this, "TsSchemaGenerator");
|
|
822
823
|
}
|
|
823
|
-
async generate(
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
824
|
+
async generate(model, outputDir) {
|
|
825
|
+
import_node_fs.default.mkdirSync(outputDir, {
|
|
826
|
+
recursive: true
|
|
827
|
+
});
|
|
828
|
+
this.generateSchema(model, outputDir);
|
|
829
|
+
this.generateModelsAndTypeDefs(model, outputDir);
|
|
830
|
+
this.generateInputTypes(model, outputDir);
|
|
831
|
+
}
|
|
832
|
+
generateSchema(model, outputDir) {
|
|
829
833
|
const statements = [];
|
|
830
834
|
this.generateSchemaStatements(model, statements);
|
|
831
835
|
this.generateBannerComments(statements);
|
|
832
|
-
const
|
|
836
|
+
const schemaOutputFile = import_node_path.default.join(outputDir, "schema.ts");
|
|
837
|
+
const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
833
838
|
const printer = ts.createPrinter();
|
|
834
839
|
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
835
|
-
import_node_fs.default.
|
|
836
|
-
recursive: true
|
|
837
|
-
});
|
|
838
|
-
import_node_fs.default.writeFileSync(outputFile, result);
|
|
839
|
-
return {
|
|
840
|
-
model,
|
|
841
|
-
warnings
|
|
842
|
-
};
|
|
840
|
+
import_node_fs.default.writeFileSync(schemaOutputFile, result);
|
|
843
841
|
}
|
|
844
842
|
generateSchemaStatements(model, statements) {
|
|
845
843
|
const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
|
|
@@ -867,7 +865,11 @@ var TsSchemaGenerator = class {
|
|
|
867
865
|
// provider
|
|
868
866
|
ts.factory.createPropertyAssignment("provider", this.createProviderObject(model)),
|
|
869
867
|
// models
|
|
870
|
-
ts.factory.createPropertyAssignment("models", this.createModelsObject(model))
|
|
868
|
+
ts.factory.createPropertyAssignment("models", this.createModelsObject(model)),
|
|
869
|
+
// typeDefs
|
|
870
|
+
...model.declarations.some(import_ast3.isTypeDef) ? [
|
|
871
|
+
ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model))
|
|
872
|
+
] : []
|
|
871
873
|
];
|
|
872
874
|
const enums = model.declarations.filter(import_ast3.isEnum);
|
|
873
875
|
if (enums.length > 0) {
|
|
@@ -893,18 +895,43 @@ var TsSchemaGenerator = class {
|
|
|
893
895
|
createModelsObject(model) {
|
|
894
896
|
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => (0, import_ast3.isDataModel)(d) && !hasAttribute(d, "@@ignore")).map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm))), true);
|
|
895
897
|
}
|
|
898
|
+
createTypeDefsObject(model) {
|
|
899
|
+
return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => (0, import_ast3.isTypeDef)(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
|
|
900
|
+
}
|
|
896
901
|
createDataModelObject(dm) {
|
|
902
|
+
const allFields = (0, import_utils3.getAllFields)(dm);
|
|
903
|
+
const allAttributes = (0, import_utils3.getAllAttributes)(dm).filter((attr) => {
|
|
904
|
+
if (attr.decl.$refText === "@@delegate" && attr.$container !== dm) {
|
|
905
|
+
return false;
|
|
906
|
+
}
|
|
907
|
+
return true;
|
|
908
|
+
});
|
|
909
|
+
const subModels = this.getSubModels(dm);
|
|
897
910
|
const fields = [
|
|
911
|
+
// name
|
|
912
|
+
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(dm.name)),
|
|
913
|
+
// baseModel
|
|
914
|
+
...dm.baseModel ? [
|
|
915
|
+
ts.factory.createPropertyAssignment("baseModel", ts.factory.createStringLiteral(dm.baseModel.$refText))
|
|
916
|
+
] : [],
|
|
898
917
|
// fields
|
|
899
|
-
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(
|
|
918
|
+
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm))), true)),
|
|
900
919
|
// attributes
|
|
901
|
-
...
|
|
902
|
-
ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(
|
|
920
|
+
...allAttributes.length > 0 ? [
|
|
921
|
+
ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
|
|
903
922
|
] : [],
|
|
904
923
|
// idFields
|
|
905
|
-
ts.factory.createPropertyAssignment("idFields", ts.factory.createArrayLiteralExpression(
|
|
924
|
+
ts.factory.createPropertyAssignment("idFields", ts.factory.createArrayLiteralExpression(getIdFields(dm).map((idField) => ts.factory.createStringLiteral(idField)))),
|
|
906
925
|
// uniqueFields
|
|
907
|
-
ts.factory.createPropertyAssignment("uniqueFields", this.createUniqueFieldsObject(dm))
|
|
926
|
+
ts.factory.createPropertyAssignment("uniqueFields", this.createUniqueFieldsObject(dm)),
|
|
927
|
+
// isDelegate
|
|
928
|
+
...isDelegateModel(dm) ? [
|
|
929
|
+
ts.factory.createPropertyAssignment("isDelegate", ts.factory.createTrue())
|
|
930
|
+
] : [],
|
|
931
|
+
// subModels
|
|
932
|
+
...subModels.length > 0 ? [
|
|
933
|
+
ts.factory.createPropertyAssignment("subModels", ts.factory.createArrayLiteralExpression(subModels.map((subModel) => ts.factory.createStringLiteral(subModel))))
|
|
934
|
+
] : []
|
|
908
935
|
];
|
|
909
936
|
const computedFields = dm.fields.filter((f) => hasAttribute(f, "@computed"));
|
|
910
937
|
if (computedFields.length > 0) {
|
|
@@ -912,8 +939,31 @@ var TsSchemaGenerator = class {
|
|
|
912
939
|
}
|
|
913
940
|
return ts.factory.createObjectLiteralExpression(fields, true);
|
|
914
941
|
}
|
|
942
|
+
getSubModels(dm) {
|
|
943
|
+
return dm.$container.declarations.filter(import_ast3.isDataModel).filter((d) => d.baseModel?.ref === dm).map((d) => d.name);
|
|
944
|
+
}
|
|
945
|
+
createTypeDefObject(td) {
|
|
946
|
+
const allFields = (0, import_utils3.getAllFields)(td);
|
|
947
|
+
const allAttributes = (0, import_utils3.getAllAttributes)(td);
|
|
948
|
+
const fields = [
|
|
949
|
+
// name
|
|
950
|
+
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(td.name)),
|
|
951
|
+
// fields
|
|
952
|
+
ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0))), true)),
|
|
953
|
+
// attributes
|
|
954
|
+
...allAttributes.length > 0 ? [
|
|
955
|
+
ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
|
|
956
|
+
] : []
|
|
957
|
+
];
|
|
958
|
+
return ts.factory.createObjectLiteralExpression(fields, true);
|
|
959
|
+
}
|
|
915
960
|
createComputedFieldsObject(fields) {
|
|
916
|
-
return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [
|
|
961
|
+
return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [
|
|
962
|
+
// parameter: `context: { currentModel: string }`
|
|
963
|
+
ts.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, ts.factory.createTypeLiteralNode([
|
|
964
|
+
ts.factory.createPropertySignature(void 0, "currentModel", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
|
|
965
|
+
]), void 0)
|
|
966
|
+
], ts.factory.createTypeReferenceNode("OperandExpression", [
|
|
917
967
|
ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type))
|
|
918
968
|
]), ts.factory.createBlock([
|
|
919
969
|
ts.factory.createThrowStatement(ts.factory.createNewExpression(ts.factory.createIdentifier("Error"), void 0, [
|
|
@@ -931,11 +981,14 @@ var TsSchemaGenerator = class {
|
|
|
931
981
|
}
|
|
932
982
|
return result;
|
|
933
983
|
}
|
|
934
|
-
|
|
984
|
+
createDataFieldObject(field, contextModel) {
|
|
935
985
|
const objectFields = [
|
|
986
|
+
// name
|
|
987
|
+
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
|
|
988
|
+
// type
|
|
936
989
|
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
937
990
|
];
|
|
938
|
-
if (isIdField(field)) {
|
|
991
|
+
if (contextModel && model_utils_exports.isIdField(field, contextModel)) {
|
|
939
992
|
objectFields.push(ts.factory.createPropertyAssignment("id", ts.factory.createTrue()));
|
|
940
993
|
}
|
|
941
994
|
if (isUniqueField(field)) {
|
|
@@ -950,6 +1003,13 @@ var TsSchemaGenerator = class {
|
|
|
950
1003
|
if (hasAttribute(field, "@updatedAt")) {
|
|
951
1004
|
objectFields.push(ts.factory.createPropertyAssignment("updatedAt", ts.factory.createTrue()));
|
|
952
1005
|
}
|
|
1006
|
+
if (contextModel && // id fields are duplicated in inherited models
|
|
1007
|
+
!isIdField(field, contextModel) && field.$container !== contextModel && isDelegateModel(field.$container)) {
|
|
1008
|
+
objectFields.push(ts.factory.createPropertyAssignment("originModel", ts.factory.createStringLiteral(field.$container.name)));
|
|
1009
|
+
}
|
|
1010
|
+
if (this.isDiscriminatorField(field)) {
|
|
1011
|
+
objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
|
|
1012
|
+
}
|
|
953
1013
|
if (field.attributes.length > 0) {
|
|
954
1014
|
objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
|
|
955
1015
|
}
|
|
@@ -993,31 +1053,19 @@ var TsSchemaGenerator = class {
|
|
|
993
1053
|
}
|
|
994
1054
|
return ts.factory.createObjectLiteralExpression(objectFields, true);
|
|
995
1055
|
}
|
|
1056
|
+
isDiscriminatorField(field) {
|
|
1057
|
+
const origin = field.$container;
|
|
1058
|
+
return getAttribute(origin, "@@delegate")?.args.some((arg) => arg.$resolvedParam.name === "discriminator" && (0, import_utils3.isDataFieldReference)(arg.value) && arg.value.target.ref === field);
|
|
1059
|
+
}
|
|
996
1060
|
getDataSourceProvider(model) {
|
|
997
1061
|
const dataSource = model.declarations.find(import_ast3.isDataSource);
|
|
998
|
-
(0,
|
|
1062
|
+
(0, import_common_helpers2.invariant)(dataSource, "No data source found in the model");
|
|
999
1063
|
const providerExpr = dataSource.fields.find((f) => f.name === "provider")?.value;
|
|
1000
|
-
(0,
|
|
1064
|
+
(0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(providerExpr), "Provider must be a literal");
|
|
1001
1065
|
const type = providerExpr.value;
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
return {
|
|
1006
|
-
type,
|
|
1007
|
-
url: urlExpr.value,
|
|
1008
|
-
env: void 0
|
|
1009
|
-
};
|
|
1010
|
-
} else if ((0, import_ast3.isInvocationExpr)(urlExpr)) {
|
|
1011
|
-
(0, import_common_helpers.invariant)(urlExpr.function.$refText === "env", 'only "env" function is supported');
|
|
1012
|
-
(0, import_common_helpers.invariant)(urlExpr.args.length === 1, "env function must have one argument");
|
|
1013
|
-
return {
|
|
1014
|
-
type,
|
|
1015
|
-
env: urlExpr.args[0].value.value,
|
|
1016
|
-
url: void 0
|
|
1017
|
-
};
|
|
1018
|
-
} else {
|
|
1019
|
-
throw new Error("Unsupported URL type");
|
|
1020
|
-
}
|
|
1066
|
+
return {
|
|
1067
|
+
type
|
|
1068
|
+
};
|
|
1021
1069
|
}
|
|
1022
1070
|
getFieldMappedDefault(field) {
|
|
1023
1071
|
const defaultAttr = getAttribute(field, "@default");
|
|
@@ -1025,7 +1073,7 @@ var TsSchemaGenerator = class {
|
|
|
1025
1073
|
return void 0;
|
|
1026
1074
|
}
|
|
1027
1075
|
const defaultValue = defaultAttr.args[0]?.value;
|
|
1028
|
-
(0,
|
|
1076
|
+
(0, import_common_helpers2.invariant)(defaultValue, "Expected a default value");
|
|
1029
1077
|
return this.getMappedValue(defaultValue, field.type);
|
|
1030
1078
|
}
|
|
1031
1079
|
getMappedValue(expr, fieldType) {
|
|
@@ -1150,44 +1198,43 @@ var TsSchemaGenerator = class {
|
|
|
1150
1198
|
if (relation) {
|
|
1151
1199
|
const nameArg = relation.args.find((arg) => arg.$resolvedParam.name === "name");
|
|
1152
1200
|
if (nameArg) {
|
|
1153
|
-
(0,
|
|
1201
|
+
(0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(nameArg.value), "name must be a literal");
|
|
1154
1202
|
return nameArg.value.value;
|
|
1155
1203
|
}
|
|
1156
1204
|
}
|
|
1157
1205
|
return void 0;
|
|
1158
1206
|
}
|
|
1159
|
-
getIdFields(dm) {
|
|
1160
|
-
return dm.fields.filter(isIdField).map((f) => f.name);
|
|
1161
|
-
}
|
|
1162
1207
|
createUniqueFieldsObject(dm) {
|
|
1163
1208
|
const properties = [];
|
|
1164
|
-
|
|
1209
|
+
const allFields = (0, import_utils3.getAllFields)(dm);
|
|
1210
|
+
for (const field of allFields) {
|
|
1165
1211
|
if (hasAttribute(field, "@id") || hasAttribute(field, "@unique")) {
|
|
1166
1212
|
properties.push(ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
|
|
1167
1213
|
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
1168
1214
|
])));
|
|
1169
1215
|
}
|
|
1170
1216
|
}
|
|
1217
|
+
const allAttributes = (0, import_utils3.getAllAttributes)(dm);
|
|
1171
1218
|
const seenKeys = /* @__PURE__ */ new Set();
|
|
1172
|
-
for (const attr of
|
|
1219
|
+
for (const attr of allAttributes) {
|
|
1173
1220
|
if (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@unique") {
|
|
1174
1221
|
const fieldNames = this.getReferenceNames(attr.args[0].value);
|
|
1175
1222
|
if (!fieldNames) {
|
|
1176
1223
|
continue;
|
|
1177
1224
|
}
|
|
1178
1225
|
if (fieldNames.length === 1) {
|
|
1179
|
-
const fieldDef =
|
|
1226
|
+
const fieldDef = allFields.find((f) => f.name === fieldNames[0]);
|
|
1180
1227
|
properties.push(ts.factory.createPropertyAssignment(fieldNames[0], ts.factory.createObjectLiteralExpression([
|
|
1181
1228
|
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1182
1229
|
])));
|
|
1183
1230
|
} else {
|
|
1184
|
-
const key =
|
|
1231
|
+
const key = this.getCompoundUniqueKey(attr, fieldNames);
|
|
1185
1232
|
if (seenKeys.has(key)) {
|
|
1186
1233
|
continue;
|
|
1187
1234
|
}
|
|
1188
1235
|
seenKeys.add(key);
|
|
1189
|
-
properties.push(ts.factory.createPropertyAssignment(
|
|
1190
|
-
const fieldDef =
|
|
1236
|
+
properties.push(ts.factory.createPropertyAssignment(key, ts.factory.createObjectLiteralExpression(fieldNames.map((field) => {
|
|
1237
|
+
const fieldDef = allFields.find((f) => f.name === field);
|
|
1191
1238
|
return ts.factory.createPropertyAssignment(field, ts.factory.createObjectLiteralExpression([
|
|
1192
1239
|
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1193
1240
|
]));
|
|
@@ -1197,8 +1244,16 @@ var TsSchemaGenerator = class {
|
|
|
1197
1244
|
}
|
|
1198
1245
|
return ts.factory.createObjectLiteralExpression(properties, true);
|
|
1199
1246
|
}
|
|
1247
|
+
getCompoundUniqueKey(attr, fieldNames) {
|
|
1248
|
+
const nameArg = attr.args.find((arg) => arg.$resolvedParam.name === "name");
|
|
1249
|
+
if (nameArg && (0, import_ast3.isLiteralExpr)(nameArg.value)) {
|
|
1250
|
+
return nameArg.value.value;
|
|
1251
|
+
} else {
|
|
1252
|
+
return fieldNames.join("_");
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1200
1255
|
generateFieldTypeLiteral(field) {
|
|
1201
|
-
(0,
|
|
1256
|
+
(0, import_common_helpers2.invariant)(field.type.type || field.type.reference || field.type.unsupported, "Field type must be a primitive, reference, or Unsupported");
|
|
1202
1257
|
return field.type.type ? ts.factory.createStringLiteral(field.type.type) : field.type.reference ? ts.factory.createStringLiteral(field.type.reference.$refText) : ts.factory.createStringLiteral("Unsupported");
|
|
1203
1258
|
}
|
|
1204
1259
|
createEnumObject(e) {
|
|
@@ -1322,7 +1377,7 @@ var TsSchemaGenerator = class {
|
|
|
1322
1377
|
]);
|
|
1323
1378
|
}
|
|
1324
1379
|
createRefExpression(expr) {
|
|
1325
|
-
if ((0, import_ast3.
|
|
1380
|
+
if ((0, import_ast3.isDataField)(expr.target.ref)) {
|
|
1326
1381
|
return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.field"), void 0, [
|
|
1327
1382
|
this.createLiteralNode(expr.target.$refText)
|
|
1328
1383
|
]);
|
|
@@ -1351,6 +1406,150 @@ var TsSchemaGenerator = class {
|
|
|
1351
1406
|
throw new Error(`Unsupported literal type: ${type}`);
|
|
1352
1407
|
});
|
|
1353
1408
|
}
|
|
1409
|
+
generateModelsAndTypeDefs(model, outputDir) {
|
|
1410
|
+
const statements = [];
|
|
1411
|
+
statements.push(this.generateSchemaImport(model, true, true));
|
|
1412
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
1413
|
+
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
|
|
1414
|
+
...model.declarations.some(import_ast3.isTypeDef) ? [
|
|
1415
|
+
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`TypeDefResult as $TypeDefResult`))
|
|
1416
|
+
] : []
|
|
1417
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1418
|
+
const dataModels = model.declarations.filter(import_ast3.isDataModel);
|
|
1419
|
+
for (const dm of dataModels) {
|
|
1420
|
+
let modelType = ts.factory.createTypeAliasDeclaration([
|
|
1421
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1422
|
+
], dm.name, void 0, ts.factory.createTypeReferenceNode("$ModelResult", [
|
|
1423
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1424
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1425
|
+
]));
|
|
1426
|
+
if (dm.comments.length > 0) {
|
|
1427
|
+
modelType = this.generateDocs(modelType, dm);
|
|
1428
|
+
}
|
|
1429
|
+
statements.push(modelType);
|
|
1430
|
+
}
|
|
1431
|
+
const typeDefs = model.declarations.filter(import_ast3.isTypeDef);
|
|
1432
|
+
for (const td of typeDefs) {
|
|
1433
|
+
let typeDef = ts.factory.createTypeAliasDeclaration([
|
|
1434
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1435
|
+
], td.name, void 0, ts.factory.createTypeReferenceNode("$TypeDefResult", [
|
|
1436
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1437
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(td.name))
|
|
1438
|
+
]));
|
|
1439
|
+
if (td.comments.length > 0) {
|
|
1440
|
+
typeDef = this.generateDocs(typeDef, td);
|
|
1441
|
+
}
|
|
1442
|
+
statements.push(typeDef);
|
|
1443
|
+
}
|
|
1444
|
+
const enums = model.declarations.filter(import_ast3.isEnum);
|
|
1445
|
+
for (const e of enums) {
|
|
1446
|
+
let enumDecl = ts.factory.createVariableStatement([
|
|
1447
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1448
|
+
], ts.factory.createVariableDeclarationList([
|
|
1449
|
+
ts.factory.createVariableDeclaration(e.name, void 0, void 0, ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("$schema"), ts.factory.createIdentifier("enums")), ts.factory.createIdentifier(e.name)))
|
|
1450
|
+
], ts.NodeFlags.Const));
|
|
1451
|
+
if (e.comments.length > 0) {
|
|
1452
|
+
enumDecl = this.generateDocs(enumDecl, e);
|
|
1453
|
+
}
|
|
1454
|
+
statements.push(enumDecl);
|
|
1455
|
+
let typeAlias = ts.factory.createTypeAliasDeclaration([
|
|
1456
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1457
|
+
], e.name, void 0, ts.factory.createIndexedAccessTypeNode(ts.factory.createTypeQueryNode(ts.factory.createIdentifier(e.name)), ts.factory.createTypeOperatorNode(ts.SyntaxKind.KeyOfKeyword, ts.factory.createTypeQueryNode(ts.factory.createIdentifier(e.name)))));
|
|
1458
|
+
if (e.comments.length > 0) {
|
|
1459
|
+
typeAlias = this.generateDocs(typeAlias, e);
|
|
1460
|
+
}
|
|
1461
|
+
statements.push(typeAlias);
|
|
1462
|
+
}
|
|
1463
|
+
this.generateBannerComments(statements);
|
|
1464
|
+
const outputFile = import_node_path.default.join(outputDir, "models.ts");
|
|
1465
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1466
|
+
const printer = ts.createPrinter();
|
|
1467
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1468
|
+
import_node_fs.default.writeFileSync(outputFile, result);
|
|
1469
|
+
}
|
|
1470
|
+
generateSchemaImport(model, schemaObject, schemaType) {
|
|
1471
|
+
const importSpecifiers = [];
|
|
1472
|
+
if (schemaObject) {
|
|
1473
|
+
if (model.declarations.some(import_ast3.isEnum)) {
|
|
1474
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("schema"), ts.factory.createIdentifier("$schema")));
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
if (schemaType) {
|
|
1478
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
|
|
1479
|
+
}
|
|
1480
|
+
return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
|
|
1481
|
+
}
|
|
1482
|
+
generateDocs(tsDecl, decl) {
|
|
1483
|
+
return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
|
|
1484
|
+
* ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
|
|
1485
|
+
`, true);
|
|
1486
|
+
}
|
|
1487
|
+
generateInputTypes(model, outputDir) {
|
|
1488
|
+
const dataModels = model.declarations.filter(import_ast3.isDataModel);
|
|
1489
|
+
const statements = [];
|
|
1490
|
+
statements.push(this.generateSchemaImport(model, false, true));
|
|
1491
|
+
const inputTypes = [
|
|
1492
|
+
"FindManyArgs",
|
|
1493
|
+
"FindUniqueArgs",
|
|
1494
|
+
"FindFirstArgs",
|
|
1495
|
+
"CreateArgs",
|
|
1496
|
+
"CreateManyArgs",
|
|
1497
|
+
"CreateManyAndReturnArgs",
|
|
1498
|
+
"UpdateArgs",
|
|
1499
|
+
"UpdateManyArgs",
|
|
1500
|
+
"UpdateManyAndReturnArgs",
|
|
1501
|
+
"UpsertArgs",
|
|
1502
|
+
"DeleteArgs",
|
|
1503
|
+
"DeleteManyArgs",
|
|
1504
|
+
"CountArgs",
|
|
1505
|
+
"AggregateArgs",
|
|
1506
|
+
"GroupByArgs",
|
|
1507
|
+
"WhereInput",
|
|
1508
|
+
"SelectInput",
|
|
1509
|
+
"IncludeInput",
|
|
1510
|
+
"OmitInput"
|
|
1511
|
+
];
|
|
1512
|
+
const inputTypeNameFixes = {
|
|
1513
|
+
SelectInput: "Select",
|
|
1514
|
+
IncludeInput: "Include",
|
|
1515
|
+
OmitInput: "Omit"
|
|
1516
|
+
};
|
|
1517
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports(inputTypes.map((inputType) => ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier(`${inputType} as $${inputType}`))))), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1518
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports([
|
|
1519
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SimplifiedModelResult as $SimplifiedModelResult")),
|
|
1520
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SelectIncludeOmit as $SelectIncludeOmit"))
|
|
1521
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1522
|
+
for (const dm of dataModels) {
|
|
1523
|
+
for (const inputType of inputTypes) {
|
|
1524
|
+
const exportName = inputTypeNameFixes[inputType] ? `${dm.name}${inputTypeNameFixes[inputType]}` : `${dm.name}${inputType}`;
|
|
1525
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1526
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1527
|
+
], exportName, void 0, ts.factory.createTypeReferenceNode(`$${inputType}`, [
|
|
1528
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1529
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1530
|
+
])));
|
|
1531
|
+
}
|
|
1532
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1533
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1534
|
+
], `${dm.name}GetPayload`, [
|
|
1535
|
+
ts.factory.createTypeParameterDeclaration(void 0, "Args", ts.factory.createTypeReferenceNode("$SelectIncludeOmit", [
|
|
1536
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1537
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1538
|
+
ts.factory.createLiteralTypeNode(ts.factory.createTrue())
|
|
1539
|
+
]))
|
|
1540
|
+
], ts.factory.createTypeReferenceNode("$SimplifiedModelResult", [
|
|
1541
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1542
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1543
|
+
ts.factory.createTypeReferenceNode("Args")
|
|
1544
|
+
])));
|
|
1545
|
+
}
|
|
1546
|
+
this.generateBannerComments(statements);
|
|
1547
|
+
const outputFile = import_node_path.default.join(outputDir, "input.ts");
|
|
1548
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1549
|
+
const printer = ts.createPrinter();
|
|
1550
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1551
|
+
import_node_fs.default.writeFileSync(outputFile, result);
|
|
1552
|
+
}
|
|
1354
1553
|
};
|
|
1355
1554
|
|
|
1356
1555
|
// src/zmodel-code-generator.ts
|
|
@@ -1442,21 +1641,21 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
|
1442
1641
|
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
1443
1642
|
}
|
|
1444
1643
|
_generateDataModel(ast) {
|
|
1445
|
-
return `${ast.
|
|
1644
|
+
return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.ref?.name).join(", ") : ""} {
|
|
1446
1645
|
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
|
|
1447
1646
|
}`;
|
|
1448
1647
|
}
|
|
1449
|
-
|
|
1648
|
+
_generateDataField(ast) {
|
|
1450
1649
|
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
1451
1650
|
}
|
|
1452
1651
|
fieldType(type) {
|
|
1453
|
-
const baseType = type.type ? type.type : type.$type == "
|
|
1652
|
+
const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
|
|
1454
1653
|
return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
|
|
1455
1654
|
}
|
|
1456
1655
|
_generateDataModelAttribute(ast) {
|
|
1457
1656
|
return this.attribute(ast);
|
|
1458
1657
|
}
|
|
1459
|
-
|
|
1658
|
+
_generateDataFieldAttribute(ast) {
|
|
1460
1659
|
return this.attribute(ast);
|
|
1461
1660
|
}
|
|
1462
1661
|
attribute(ast) {
|
|
@@ -1540,9 +1739,6 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attribu
|
|
|
1540
1739
|
${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attributes.length > 0 ? "\n\n" + ast.attributes.map((x) => this.indent + this.generate(x)).join("\n") : ""}
|
|
1541
1740
|
}`;
|
|
1542
1741
|
}
|
|
1543
|
-
_generateTypeDefField(ast) {
|
|
1544
|
-
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
1545
|
-
}
|
|
1546
1742
|
argument(ast) {
|
|
1547
1743
|
return this.generate(ast.value);
|
|
1548
1744
|
}
|
|
@@ -1668,13 +1864,13 @@ _ts_decorate([
|
|
|
1668
1864
|
_ts_metadata("design:returntype", void 0)
|
|
1669
1865
|
], ZModelCodeGenerator.prototype, "_generateDataModel", null);
|
|
1670
1866
|
_ts_decorate([
|
|
1671
|
-
gen(import_ast4.
|
|
1867
|
+
gen(import_ast4.DataField),
|
|
1672
1868
|
_ts_metadata("design:type", Function),
|
|
1673
1869
|
_ts_metadata("design:paramtypes", [
|
|
1674
|
-
typeof import_ast4.
|
|
1870
|
+
typeof import_ast4.DataField === "undefined" ? Object : import_ast4.DataField
|
|
1675
1871
|
]),
|
|
1676
1872
|
_ts_metadata("design:returntype", void 0)
|
|
1677
|
-
], ZModelCodeGenerator.prototype, "
|
|
1873
|
+
], ZModelCodeGenerator.prototype, "_generateDataField", null);
|
|
1678
1874
|
_ts_decorate([
|
|
1679
1875
|
gen(import_ast4.DataModelAttribute),
|
|
1680
1876
|
_ts_metadata("design:type", Function),
|
|
@@ -1684,13 +1880,13 @@ _ts_decorate([
|
|
|
1684
1880
|
_ts_metadata("design:returntype", void 0)
|
|
1685
1881
|
], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
|
|
1686
1882
|
_ts_decorate([
|
|
1687
|
-
gen(import_ast4.
|
|
1883
|
+
gen(import_ast4.DataFieldAttribute),
|
|
1688
1884
|
_ts_metadata("design:type", Function),
|
|
1689
1885
|
_ts_metadata("design:paramtypes", [
|
|
1690
|
-
typeof import_ast4.
|
|
1886
|
+
typeof import_ast4.DataFieldAttribute === "undefined" ? Object : import_ast4.DataFieldAttribute
|
|
1691
1887
|
]),
|
|
1692
1888
|
_ts_metadata("design:returntype", void 0)
|
|
1693
|
-
], ZModelCodeGenerator.prototype, "
|
|
1889
|
+
], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
|
|
1694
1890
|
_ts_decorate([
|
|
1695
1891
|
gen(import_ast4.AttributeArg),
|
|
1696
1892
|
_ts_metadata("design:type", Function),
|
|
@@ -1855,14 +2051,6 @@ _ts_decorate([
|
|
|
1855
2051
|
]),
|
|
1856
2052
|
_ts_metadata("design:returntype", void 0)
|
|
1857
2053
|
], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
|
|
1858
|
-
_ts_decorate([
|
|
1859
|
-
gen(import_ast4.TypeDefField),
|
|
1860
|
-
_ts_metadata("design:type", Function),
|
|
1861
|
-
_ts_metadata("design:paramtypes", [
|
|
1862
|
-
typeof import_ast4.TypeDefField === "undefined" ? Object : import_ast4.TypeDefField
|
|
1863
|
-
]),
|
|
1864
|
-
_ts_metadata("design:returntype", void 0)
|
|
1865
|
-
], ZModelCodeGenerator.prototype, "_generateTypeDefField", null);
|
|
1866
2054
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1867
2055
|
0 && (module.exports = {
|
|
1868
2056
|
ModelUtils,
|