@zenstackhq/sdk 3.0.0-alpha.3 → 3.0.0-alpha.30
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 +401 -194
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +77 -31
- package/dist/index.d.ts +77 -31
- package/dist/index.js +413 -206
- package/dist/index.js.map +1 -1
- package/dist/schema.d.cts +47 -19
- package/dist/schema.d.ts +47 -19
- package/package.json +10 -12
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
|
|
|
@@ -318,9 +264,9 @@ var Model = class extends ContainerDeclaration {
|
|
|
318
264
|
}
|
|
319
265
|
name;
|
|
320
266
|
isView;
|
|
321
|
-
fields;
|
|
267
|
+
fields = [];
|
|
322
268
|
constructor(name, isView, documentations = []) {
|
|
323
|
-
super(documentations), this.name = name, this.isView = isView
|
|
269
|
+
super(documentations), this.name = name, this.isView = isView;
|
|
324
270
|
}
|
|
325
271
|
addField(name, type, attributes = [], documentations = [], addToFront = false) {
|
|
326
272
|
const field = new ModelField(name, type, attributes, documentations);
|
|
@@ -540,9 +486,9 @@ var Enum = class extends ContainerDeclaration {
|
|
|
540
486
|
__name(this, "Enum");
|
|
541
487
|
}
|
|
542
488
|
name;
|
|
543
|
-
fields;
|
|
489
|
+
fields = [];
|
|
544
490
|
constructor(name, documentations = []) {
|
|
545
|
-
super(documentations), this.name = name
|
|
491
|
+
super(documentations), this.name = name;
|
|
546
492
|
}
|
|
547
493
|
addField(name, attributes = [], documentations = []) {
|
|
548
494
|
const field = new EnumField(name, attributes, documentations);
|
|
@@ -586,20 +532,22 @@ 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");
|
|
592
539
|
}
|
|
593
540
|
zmodel;
|
|
594
|
-
PRELUDE
|
|
595
|
-
constructor(zmodel) {
|
|
596
|
-
this.zmodel = zmodel;
|
|
597
|
-
this.PRELUDE = `//////////////////////////////////////////////////////////////////////////////////////////////
|
|
541
|
+
PRELUDE = `//////////////////////////////////////////////////////////////////////////////////////////////
|
|
598
542
|
// DO NOT MODIFY THIS FILE //
|
|
599
543
|
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
|
|
600
544
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
601
545
|
|
|
602
546
|
`;
|
|
547
|
+
// a mapping from full names to shortened names
|
|
548
|
+
shortNameMap = /* @__PURE__ */ new Map();
|
|
549
|
+
constructor(zmodel) {
|
|
550
|
+
this.zmodel = zmodel;
|
|
603
551
|
}
|
|
604
552
|
async generate() {
|
|
605
553
|
const prisma = new PrismaModel();
|
|
@@ -661,16 +609,22 @@ var PrismaSchemaGenerator = class {
|
|
|
661
609
|
}
|
|
662
610
|
generateModel(prisma, decl) {
|
|
663
611
|
const model = decl.isView ? prisma.addView(decl.name) : prisma.addModel(decl.name);
|
|
664
|
-
|
|
612
|
+
const allFields = (0, import_utils2.getAllFields)(decl, true);
|
|
613
|
+
for (const field of allFields) {
|
|
665
614
|
if (model_utils_exports.hasAttribute(field, "@computed")) {
|
|
666
615
|
continue;
|
|
667
616
|
}
|
|
668
|
-
this.
|
|
617
|
+
if (model_utils_exports.isIdField(field, decl) || !this.isInheritedFromDelegate(field, decl)) {
|
|
618
|
+
this.generateModelField(model, field, decl);
|
|
619
|
+
}
|
|
669
620
|
}
|
|
670
|
-
|
|
621
|
+
const allAttributes = (0, import_utils2.getAllAttributes)(decl);
|
|
622
|
+
for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
|
|
671
623
|
this.generateContainerAttribute(model, attr);
|
|
672
624
|
}
|
|
673
625
|
decl.comments.forEach((c) => model.addComment(c));
|
|
626
|
+
this.generateDelegateRelationForBase(model, decl);
|
|
627
|
+
this.generateDelegateRelationForConcrete(model, decl);
|
|
674
628
|
}
|
|
675
629
|
isPrismaAttribute(attr) {
|
|
676
630
|
if (!attr.decl.ref) {
|
|
@@ -693,7 +647,7 @@ var PrismaSchemaGenerator = class {
|
|
|
693
647
|
getStringLiteral(node) {
|
|
694
648
|
return (0, import_ast2.isStringLiteral)(node) ? node.value : void 0;
|
|
695
649
|
}
|
|
696
|
-
generateModelField(model, field, addToFront = false) {
|
|
650
|
+
generateModelField(model, field, contextModel, addToFront = false) {
|
|
697
651
|
let fieldType;
|
|
698
652
|
if (field.type.type) {
|
|
699
653
|
fieldType = field.type.type;
|
|
@@ -719,7 +673,7 @@ var PrismaSchemaGenerator = class {
|
|
|
719
673
|
const type = new ModelFieldType(fieldType, isArray, field.type.optional);
|
|
720
674
|
const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithPluginInvocation(attr)).filter((attr) => (
|
|
721
675
|
// when building physical schema, exclude `@default` for id fields inherited from delegate base
|
|
722
|
-
!(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")
|
|
723
677
|
)).map((attr) => this.makeFieldAttribute(attr));
|
|
724
678
|
const docs = [
|
|
725
679
|
...field.comments
|
|
@@ -741,16 +695,8 @@ var PrismaSchemaGenerator = class {
|
|
|
741
695
|
const model = import_langium.AstUtils.getContainerOfType(node, import_ast2.isModel);
|
|
742
696
|
return !!model && !!model.$document && model.$document.uri.path.endsWith("plugin.zmodel");
|
|
743
697
|
}
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
throw new Error(`Unsupported field type with default value: ${field.type.type}`);
|
|
747
|
-
});
|
|
748
|
-
result.attributes.push(new FieldAttribute("@default", [
|
|
749
|
-
new AttributeArg(void 0, dummyDefaultValue)
|
|
750
|
-
]));
|
|
751
|
-
}
|
|
752
|
-
isInheritedFromDelegate(field) {
|
|
753
|
-
return field.$inheritedFrom && model_utils_exports.isDelegateModel(field.$inheritedFrom);
|
|
698
|
+
isInheritedFromDelegate(field, contextModel) {
|
|
699
|
+
return field.$container !== contextModel && model_utils_exports.isDelegateModel(field.$container);
|
|
754
700
|
}
|
|
755
701
|
makeFieldAttribute(attr) {
|
|
756
702
|
const attrName = attr.decl.ref.name;
|
|
@@ -807,40 +753,91 @@ var PrismaSchemaGenerator = class {
|
|
|
807
753
|
];
|
|
808
754
|
_enum.addField(field.name, attributes, docs);
|
|
809
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
|
+
}
|
|
810
810
|
};
|
|
811
811
|
|
|
812
812
|
// src/ts-schema-generator.ts
|
|
813
|
-
var
|
|
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
|
-
var import_tiny_invariant = __toESM(require("tiny-invariant"), 1);
|
|
818
818
|
var import_ts_pattern2 = require("ts-pattern");
|
|
819
819
|
var ts = __toESM(require("typescript"), 1);
|
|
820
820
|
var TsSchemaGenerator = class {
|
|
821
821
|
static {
|
|
822
822
|
__name(this, "TsSchemaGenerator");
|
|
823
823
|
}
|
|
824
|
-
async generate(
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
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) {
|
|
830
833
|
const statements = [];
|
|
831
834
|
this.generateSchemaStatements(model, statements);
|
|
832
835
|
this.generateBannerComments(statements);
|
|
833
|
-
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);
|
|
834
838
|
const printer = ts.createPrinter();
|
|
835
839
|
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
836
|
-
import_node_fs.default.
|
|
837
|
-
recursive: true
|
|
838
|
-
});
|
|
839
|
-
import_node_fs.default.writeFileSync(outputFile, result);
|
|
840
|
-
return {
|
|
841
|
-
model,
|
|
842
|
-
warnings
|
|
843
|
-
};
|
|
840
|
+
import_node_fs.default.writeFileSync(schemaOutputFile, result);
|
|
844
841
|
}
|
|
845
842
|
generateSchemaStatements(model, statements) {
|
|
846
843
|
const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
|
|
@@ -868,7 +865,11 @@ var TsSchemaGenerator = class {
|
|
|
868
865
|
// provider
|
|
869
866
|
ts.factory.createPropertyAssignment("provider", this.createProviderObject(model)),
|
|
870
867
|
// models
|
|
871
|
-
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
|
+
] : []
|
|
872
873
|
];
|
|
873
874
|
const enums = model.declarations.filter(import_ast3.isEnum);
|
|
874
875
|
if (enums.length > 0) {
|
|
@@ -894,18 +895,43 @@ var TsSchemaGenerator = class {
|
|
|
894
895
|
createModelsObject(model) {
|
|
895
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);
|
|
896
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
|
+
}
|
|
897
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);
|
|
898
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
|
+
] : [],
|
|
899
917
|
// fields
|
|
900
|
-
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)),
|
|
901
919
|
// attributes
|
|
902
|
-
...
|
|
903
|
-
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))
|
|
904
922
|
] : [],
|
|
905
923
|
// idFields
|
|
906
|
-
ts.factory.createPropertyAssignment("idFields", ts.factory.createArrayLiteralExpression(
|
|
924
|
+
ts.factory.createPropertyAssignment("idFields", ts.factory.createArrayLiteralExpression(getIdFields(dm).map((idField) => ts.factory.createStringLiteral(idField)))),
|
|
907
925
|
// uniqueFields
|
|
908
|
-
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
|
+
] : []
|
|
909
935
|
];
|
|
910
936
|
const computedFields = dm.fields.filter((f) => hasAttribute(f, "@computed"));
|
|
911
937
|
if (computedFields.length > 0) {
|
|
@@ -913,23 +939,56 @@ var TsSchemaGenerator = class {
|
|
|
913
939
|
}
|
|
914
940
|
return ts.factory.createObjectLiteralExpression(fields, true);
|
|
915
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
|
+
}
|
|
916
960
|
createComputedFieldsObject(fields) {
|
|
917
|
-
return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [
|
|
918
|
-
|
|
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", [
|
|
967
|
+
ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type))
|
|
919
968
|
]), ts.factory.createBlock([
|
|
920
969
|
ts.factory.createThrowStatement(ts.factory.createNewExpression(ts.factory.createIdentifier("Error"), void 0, [
|
|
921
970
|
ts.factory.createStringLiteral("This is a stub for computed field")
|
|
922
971
|
]))
|
|
923
972
|
], true))), true);
|
|
924
973
|
}
|
|
925
|
-
|
|
926
|
-
|
|
974
|
+
mapFieldTypeToTSType(type) {
|
|
975
|
+
let result = (0, import_ts_pattern2.match)(type.type).with("String", () => "string").with("Boolean", () => "boolean").with("Int", () => "number").with("Float", () => "number").with("BigInt", () => "bigint").with("Decimal", () => "number").otherwise(() => "unknown");
|
|
976
|
+
if (type.array) {
|
|
977
|
+
result = `${result}[]`;
|
|
978
|
+
}
|
|
979
|
+
if (type.optional) {
|
|
980
|
+
result = `${result} | null`;
|
|
981
|
+
}
|
|
982
|
+
return result;
|
|
927
983
|
}
|
|
928
|
-
|
|
984
|
+
createDataFieldObject(field, contextModel) {
|
|
929
985
|
const objectFields = [
|
|
930
|
-
|
|
986
|
+
// name
|
|
987
|
+
ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
|
|
988
|
+
// type
|
|
989
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
931
990
|
];
|
|
932
|
-
if (isIdField(field)) {
|
|
991
|
+
if (contextModel && model_utils_exports.isIdField(field, contextModel)) {
|
|
933
992
|
objectFields.push(ts.factory.createPropertyAssignment("id", ts.factory.createTrue()));
|
|
934
993
|
}
|
|
935
994
|
if (isUniqueField(field)) {
|
|
@@ -944,12 +1003,19 @@ var TsSchemaGenerator = class {
|
|
|
944
1003
|
if (hasAttribute(field, "@updatedAt")) {
|
|
945
1004
|
objectFields.push(ts.factory.createPropertyAssignment("updatedAt", ts.factory.createTrue()));
|
|
946
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
|
+
}
|
|
947
1013
|
if (field.attributes.length > 0) {
|
|
948
1014
|
objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
|
|
949
1015
|
}
|
|
950
|
-
const defaultValue = this.
|
|
1016
|
+
const defaultValue = this.getFieldMappedDefault(field);
|
|
951
1017
|
if (defaultValue !== void 0) {
|
|
952
|
-
if (typeof defaultValue === "object") {
|
|
1018
|
+
if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
|
|
953
1019
|
if ("call" in defaultValue) {
|
|
954
1020
|
objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
|
|
955
1021
|
ts.factory.createStringLiteral(defaultValue.call),
|
|
@@ -968,7 +1034,11 @@ var TsSchemaGenerator = class {
|
|
|
968
1034
|
throw new Error(`Unsupported default value type for field ${field.name}`);
|
|
969
1035
|
}
|
|
970
1036
|
} else {
|
|
971
|
-
|
|
1037
|
+
if (Array.isArray(defaultValue)) {
|
|
1038
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createArrayLiteralExpression(defaultValue.map((item) => this.createLiteralNode(item)))));
|
|
1039
|
+
} else {
|
|
1040
|
+
objectFields.push(ts.factory.createPropertyAssignment("default", this.createLiteralNode(defaultValue)));
|
|
1041
|
+
}
|
|
972
1042
|
}
|
|
973
1043
|
}
|
|
974
1044
|
if (hasAttribute(field, "@computed")) {
|
|
@@ -983,60 +1053,53 @@ var TsSchemaGenerator = class {
|
|
|
983
1053
|
}
|
|
984
1054
|
return ts.factory.createObjectLiteralExpression(objectFields, true);
|
|
985
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
|
+
}
|
|
986
1060
|
getDataSourceProvider(model) {
|
|
987
1061
|
const dataSource = model.declarations.find(import_ast3.isDataSource);
|
|
988
|
-
(0,
|
|
1062
|
+
(0, import_common_helpers2.invariant)(dataSource, "No data source found in the model");
|
|
989
1063
|
const providerExpr = dataSource.fields.find((f) => f.name === "provider")?.value;
|
|
990
|
-
(0,
|
|
1064
|
+
(0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(providerExpr), "Provider must be a literal");
|
|
991
1065
|
const type = providerExpr.value;
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
return {
|
|
996
|
-
type,
|
|
997
|
-
url: urlExpr.value,
|
|
998
|
-
env: void 0
|
|
999
|
-
};
|
|
1000
|
-
} else if ((0, import_ast3.isInvocationExpr)(urlExpr)) {
|
|
1001
|
-
(0, import_tiny_invariant.default)(urlExpr.function.$refText === "env", 'only "env" function is supported');
|
|
1002
|
-
(0, import_tiny_invariant.default)(urlExpr.args.length === 1, "env function must have one argument");
|
|
1003
|
-
return {
|
|
1004
|
-
type,
|
|
1005
|
-
env: urlExpr.args[0].value.value,
|
|
1006
|
-
url: void 0
|
|
1007
|
-
};
|
|
1008
|
-
} else {
|
|
1009
|
-
throw new Error("Unsupported URL type");
|
|
1010
|
-
}
|
|
1066
|
+
return {
|
|
1067
|
+
type
|
|
1068
|
+
};
|
|
1011
1069
|
}
|
|
1012
|
-
|
|
1070
|
+
getFieldMappedDefault(field) {
|
|
1013
1071
|
const defaultAttr = getAttribute(field, "@default");
|
|
1014
1072
|
if (!defaultAttr) {
|
|
1015
1073
|
return void 0;
|
|
1016
1074
|
}
|
|
1017
1075
|
const defaultValue = defaultAttr.args[0]?.value;
|
|
1018
|
-
(0,
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1076
|
+
(0, import_common_helpers2.invariant)(defaultValue, "Expected a default value");
|
|
1077
|
+
return this.getMappedValue(defaultValue, field.type);
|
|
1078
|
+
}
|
|
1079
|
+
getMappedValue(expr, fieldType) {
|
|
1080
|
+
if ((0, import_ast3.isLiteralExpr)(expr)) {
|
|
1081
|
+
const lit = expr.value;
|
|
1082
|
+
return fieldType.type === "Boolean" ? lit : [
|
|
1022
1083
|
"Int",
|
|
1023
1084
|
"Float",
|
|
1024
1085
|
"Decimal",
|
|
1025
1086
|
"BigInt"
|
|
1026
|
-
].includes(
|
|
1027
|
-
} else if ((0, import_ast3.
|
|
1028
|
-
return
|
|
1029
|
-
} else if ((0, import_ast3.
|
|
1087
|
+
].includes(fieldType.type) ? Number(lit) : lit;
|
|
1088
|
+
} else if ((0, import_ast3.isArrayExpr)(expr)) {
|
|
1089
|
+
return expr.items.map((item) => this.getMappedValue(item, fieldType));
|
|
1090
|
+
} else if ((0, import_ast3.isReferenceExpr)(expr) && (0, import_ast3.isEnumField)(expr.target.ref)) {
|
|
1091
|
+
return expr.target.ref.name;
|
|
1092
|
+
} else if ((0, import_ast3.isInvocationExpr)(expr)) {
|
|
1030
1093
|
return {
|
|
1031
|
-
call:
|
|
1032
|
-
args:
|
|
1094
|
+
call: expr.function.$refText,
|
|
1095
|
+
args: expr.args.map((arg) => this.getLiteral(arg.value))
|
|
1033
1096
|
};
|
|
1034
|
-
} else if (this.isAuthMemberAccess(
|
|
1097
|
+
} else if (this.isAuthMemberAccess(expr)) {
|
|
1035
1098
|
return {
|
|
1036
|
-
authMember: this.getMemberAccessChain(
|
|
1099
|
+
authMember: this.getMemberAccessChain(expr)
|
|
1037
1100
|
};
|
|
1038
1101
|
} else {
|
|
1039
|
-
throw new Error(`Unsupported default value type for
|
|
1102
|
+
throw new Error(`Unsupported default value type for ${expr.$type}`);
|
|
1040
1103
|
}
|
|
1041
1104
|
}
|
|
1042
1105
|
getMemberAccessChain(expr) {
|
|
@@ -1135,40 +1198,45 @@ var TsSchemaGenerator = class {
|
|
|
1135
1198
|
if (relation) {
|
|
1136
1199
|
const nameArg = relation.args.find((arg) => arg.$resolvedParam.name === "name");
|
|
1137
1200
|
if (nameArg) {
|
|
1138
|
-
(0,
|
|
1201
|
+
(0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(nameArg.value), "name must be a literal");
|
|
1139
1202
|
return nameArg.value.value;
|
|
1140
1203
|
}
|
|
1141
1204
|
}
|
|
1142
1205
|
return void 0;
|
|
1143
1206
|
}
|
|
1144
|
-
getIdFields(dm) {
|
|
1145
|
-
return dm.fields.filter(isIdField).map((f) => f.name);
|
|
1146
|
-
}
|
|
1147
1207
|
createUniqueFieldsObject(dm) {
|
|
1148
1208
|
const properties = [];
|
|
1149
|
-
|
|
1209
|
+
const allFields = (0, import_utils3.getAllFields)(dm);
|
|
1210
|
+
for (const field of allFields) {
|
|
1150
1211
|
if (hasAttribute(field, "@id") || hasAttribute(field, "@unique")) {
|
|
1151
1212
|
properties.push(ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
|
|
1152
|
-
ts.factory.createPropertyAssignment("type",
|
|
1213
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(field))
|
|
1153
1214
|
])));
|
|
1154
1215
|
}
|
|
1155
1216
|
}
|
|
1156
|
-
|
|
1217
|
+
const allAttributes = (0, import_utils3.getAllAttributes)(dm);
|
|
1218
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
1219
|
+
for (const attr of allAttributes) {
|
|
1157
1220
|
if (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@unique") {
|
|
1158
1221
|
const fieldNames = this.getReferenceNames(attr.args[0].value);
|
|
1159
1222
|
if (!fieldNames) {
|
|
1160
1223
|
continue;
|
|
1161
1224
|
}
|
|
1162
1225
|
if (fieldNames.length === 1) {
|
|
1163
|
-
const fieldDef =
|
|
1226
|
+
const fieldDef = allFields.find((f) => f.name === fieldNames[0]);
|
|
1164
1227
|
properties.push(ts.factory.createPropertyAssignment(fieldNames[0], ts.factory.createObjectLiteralExpression([
|
|
1165
|
-
ts.factory.createPropertyAssignment("type",
|
|
1228
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1166
1229
|
])));
|
|
1167
1230
|
} else {
|
|
1231
|
+
const key = fieldNames.join("_");
|
|
1232
|
+
if (seenKeys.has(key)) {
|
|
1233
|
+
continue;
|
|
1234
|
+
}
|
|
1235
|
+
seenKeys.add(key);
|
|
1168
1236
|
properties.push(ts.factory.createPropertyAssignment(fieldNames.join("_"), ts.factory.createObjectLiteralExpression(fieldNames.map((field) => {
|
|
1169
|
-
const fieldDef =
|
|
1237
|
+
const fieldDef = allFields.find((f) => f.name === field);
|
|
1170
1238
|
return ts.factory.createPropertyAssignment(field, ts.factory.createObjectLiteralExpression([
|
|
1171
|
-
ts.factory.createPropertyAssignment("type",
|
|
1239
|
+
ts.factory.createPropertyAssignment("type", this.generateFieldTypeLiteral(fieldDef))
|
|
1172
1240
|
]));
|
|
1173
1241
|
}))));
|
|
1174
1242
|
}
|
|
@@ -1176,6 +1244,10 @@ var TsSchemaGenerator = class {
|
|
|
1176
1244
|
}
|
|
1177
1245
|
return ts.factory.createObjectLiteralExpression(properties, true);
|
|
1178
1246
|
}
|
|
1247
|
+
generateFieldTypeLiteral(field) {
|
|
1248
|
+
(0, import_common_helpers2.invariant)(field.type.type || field.type.reference || field.type.unsupported, "Field type must be a primitive, reference, or Unsupported");
|
|
1249
|
+
return field.type.type ? ts.factory.createStringLiteral(field.type.type) : field.type.reference ? ts.factory.createStringLiteral(field.type.reference.$refText) : ts.factory.createStringLiteral("Unsupported");
|
|
1250
|
+
}
|
|
1179
1251
|
createEnumObject(e) {
|
|
1180
1252
|
return ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createStringLiteral(field.name))), true);
|
|
1181
1253
|
}
|
|
@@ -1230,6 +1302,8 @@ var TsSchemaGenerator = class {
|
|
|
1230
1302
|
// This file is automatically generated by ZenStack CLI and should not be manually updated. //
|
|
1231
1303
|
//////////////////////////////////////////////////////////////////////////////////////////////
|
|
1232
1304
|
|
|
1305
|
+
/* eslint-disable */
|
|
1306
|
+
|
|
1233
1307
|
`;
|
|
1234
1308
|
ts.addSyntheticLeadingComment(statements[0], ts.SyntaxKind.SingleLineCommentTrivia, banner);
|
|
1235
1309
|
}
|
|
@@ -1295,7 +1369,7 @@ var TsSchemaGenerator = class {
|
|
|
1295
1369
|
]);
|
|
1296
1370
|
}
|
|
1297
1371
|
createRefExpression(expr) {
|
|
1298
|
-
if ((0, import_ast3.
|
|
1372
|
+
if ((0, import_ast3.isDataField)(expr.target.ref)) {
|
|
1299
1373
|
return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.field"), void 0, [
|
|
1300
1374
|
this.createLiteralNode(expr.target.$refText)
|
|
1301
1375
|
]);
|
|
@@ -1324,6 +1398,150 @@ var TsSchemaGenerator = class {
|
|
|
1324
1398
|
throw new Error(`Unsupported literal type: ${type}`);
|
|
1325
1399
|
});
|
|
1326
1400
|
}
|
|
1401
|
+
generateModelsAndTypeDefs(model, outputDir) {
|
|
1402
|
+
const statements = [];
|
|
1403
|
+
statements.push(this.generateSchemaImport(model, true, true));
|
|
1404
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
|
|
1405
|
+
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
|
|
1406
|
+
...model.declarations.some(import_ast3.isTypeDef) ? [
|
|
1407
|
+
ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`TypeDefResult as $TypeDefResult`))
|
|
1408
|
+
] : []
|
|
1409
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1410
|
+
const dataModels = model.declarations.filter(import_ast3.isDataModel);
|
|
1411
|
+
for (const dm of dataModels) {
|
|
1412
|
+
let modelType = ts.factory.createTypeAliasDeclaration([
|
|
1413
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1414
|
+
], dm.name, void 0, ts.factory.createTypeReferenceNode("$ModelResult", [
|
|
1415
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1416
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1417
|
+
]));
|
|
1418
|
+
if (dm.comments.length > 0) {
|
|
1419
|
+
modelType = this.generateDocs(modelType, dm);
|
|
1420
|
+
}
|
|
1421
|
+
statements.push(modelType);
|
|
1422
|
+
}
|
|
1423
|
+
const typeDefs = model.declarations.filter(import_ast3.isTypeDef);
|
|
1424
|
+
for (const td of typeDefs) {
|
|
1425
|
+
let typeDef = ts.factory.createTypeAliasDeclaration([
|
|
1426
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1427
|
+
], td.name, void 0, ts.factory.createTypeReferenceNode("$TypeDefResult", [
|
|
1428
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1429
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(td.name))
|
|
1430
|
+
]));
|
|
1431
|
+
if (td.comments.length > 0) {
|
|
1432
|
+
typeDef = this.generateDocs(typeDef, td);
|
|
1433
|
+
}
|
|
1434
|
+
statements.push(typeDef);
|
|
1435
|
+
}
|
|
1436
|
+
const enums = model.declarations.filter(import_ast3.isEnum);
|
|
1437
|
+
for (const e of enums) {
|
|
1438
|
+
let enumDecl = ts.factory.createVariableStatement([
|
|
1439
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1440
|
+
], ts.factory.createVariableDeclarationList([
|
|
1441
|
+
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)))
|
|
1442
|
+
], ts.NodeFlags.Const));
|
|
1443
|
+
if (e.comments.length > 0) {
|
|
1444
|
+
enumDecl = this.generateDocs(enumDecl, e);
|
|
1445
|
+
}
|
|
1446
|
+
statements.push(enumDecl);
|
|
1447
|
+
let typeAlias = ts.factory.createTypeAliasDeclaration([
|
|
1448
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1449
|
+
], 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)))));
|
|
1450
|
+
if (e.comments.length > 0) {
|
|
1451
|
+
typeAlias = this.generateDocs(typeAlias, e);
|
|
1452
|
+
}
|
|
1453
|
+
statements.push(typeAlias);
|
|
1454
|
+
}
|
|
1455
|
+
this.generateBannerComments(statements);
|
|
1456
|
+
const outputFile = import_node_path.default.join(outputDir, "models.ts");
|
|
1457
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1458
|
+
const printer = ts.createPrinter();
|
|
1459
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1460
|
+
import_node_fs.default.writeFileSync(outputFile, result);
|
|
1461
|
+
}
|
|
1462
|
+
generateSchemaImport(model, schemaObject, schemaType) {
|
|
1463
|
+
const importSpecifiers = [];
|
|
1464
|
+
if (schemaObject) {
|
|
1465
|
+
if (model.declarations.some(import_ast3.isEnum)) {
|
|
1466
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(false, ts.factory.createIdentifier("schema"), ts.factory.createIdentifier("$schema")));
|
|
1467
|
+
}
|
|
1468
|
+
}
|
|
1469
|
+
if (schemaType) {
|
|
1470
|
+
importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
|
|
1471
|
+
}
|
|
1472
|
+
return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
|
|
1473
|
+
}
|
|
1474
|
+
generateDocs(tsDecl, decl) {
|
|
1475
|
+
return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
|
|
1476
|
+
* ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
|
|
1477
|
+
`, true);
|
|
1478
|
+
}
|
|
1479
|
+
generateInputTypes(model, outputDir) {
|
|
1480
|
+
const dataModels = model.declarations.filter(import_ast3.isDataModel);
|
|
1481
|
+
const statements = [];
|
|
1482
|
+
statements.push(this.generateSchemaImport(model, false, true));
|
|
1483
|
+
const inputTypes = [
|
|
1484
|
+
"FindManyArgs",
|
|
1485
|
+
"FindUniqueArgs",
|
|
1486
|
+
"FindFirstArgs",
|
|
1487
|
+
"CreateArgs",
|
|
1488
|
+
"CreateManyArgs",
|
|
1489
|
+
"CreateManyAndReturnArgs",
|
|
1490
|
+
"UpdateArgs",
|
|
1491
|
+
"UpdateManyArgs",
|
|
1492
|
+
"UpdateManyAndReturnArgs",
|
|
1493
|
+
"UpsertArgs",
|
|
1494
|
+
"DeleteArgs",
|
|
1495
|
+
"DeleteManyArgs",
|
|
1496
|
+
"CountArgs",
|
|
1497
|
+
"AggregateArgs",
|
|
1498
|
+
"GroupByArgs",
|
|
1499
|
+
"WhereInput",
|
|
1500
|
+
"SelectInput",
|
|
1501
|
+
"IncludeInput",
|
|
1502
|
+
"OmitInput"
|
|
1503
|
+
];
|
|
1504
|
+
const inputTypeNameFixes = {
|
|
1505
|
+
SelectInput: "Select",
|
|
1506
|
+
IncludeInput: "Include",
|
|
1507
|
+
OmitInput: "Omit"
|
|
1508
|
+
};
|
|
1509
|
+
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")));
|
|
1510
|
+
statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports([
|
|
1511
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SimplifiedModelResult as $SimplifiedModelResult")),
|
|
1512
|
+
ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SelectIncludeOmit as $SelectIncludeOmit"))
|
|
1513
|
+
])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
|
|
1514
|
+
for (const dm of dataModels) {
|
|
1515
|
+
for (const inputType of inputTypes) {
|
|
1516
|
+
const exportName = inputTypeNameFixes[inputType] ? `${dm.name}${inputTypeNameFixes[inputType]}` : `${dm.name}${inputType}`;
|
|
1517
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1518
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1519
|
+
], exportName, void 0, ts.factory.createTypeReferenceNode(`$${inputType}`, [
|
|
1520
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1521
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name))
|
|
1522
|
+
])));
|
|
1523
|
+
}
|
|
1524
|
+
statements.push(ts.factory.createTypeAliasDeclaration([
|
|
1525
|
+
ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
|
|
1526
|
+
], `${dm.name}GetPayload`, [
|
|
1527
|
+
ts.factory.createTypeParameterDeclaration(void 0, "Args", ts.factory.createTypeReferenceNode("$SelectIncludeOmit", [
|
|
1528
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1529
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1530
|
+
ts.factory.createLiteralTypeNode(ts.factory.createTrue())
|
|
1531
|
+
]))
|
|
1532
|
+
], ts.factory.createTypeReferenceNode("$SimplifiedModelResult", [
|
|
1533
|
+
ts.factory.createTypeReferenceNode("$Schema"),
|
|
1534
|
+
ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
|
|
1535
|
+
ts.factory.createTypeReferenceNode("Args")
|
|
1536
|
+
])));
|
|
1537
|
+
}
|
|
1538
|
+
this.generateBannerComments(statements);
|
|
1539
|
+
const outputFile = import_node_path.default.join(outputDir, "input.ts");
|
|
1540
|
+
const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
|
|
1541
|
+
const printer = ts.createPrinter();
|
|
1542
|
+
const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
|
|
1543
|
+
import_node_fs.default.writeFileSync(outputFile, result);
|
|
1544
|
+
}
|
|
1327
1545
|
};
|
|
1328
1546
|
|
|
1329
1547
|
// src/zmodel-code-generator.ts
|
|
@@ -1415,21 +1633,21 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
|
|
|
1415
1633
|
return `${ast.name} = ${this.generate(ast.value)}`;
|
|
1416
1634
|
}
|
|
1417
1635
|
_generateDataModel(ast) {
|
|
1418
|
-
return `${ast.
|
|
1636
|
+
return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.ref?.name).join(", ") : ""} {
|
|
1419
1637
|
${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") : ""}
|
|
1420
1638
|
}`;
|
|
1421
1639
|
}
|
|
1422
|
-
|
|
1640
|
+
_generateDataField(ast) {
|
|
1423
1641
|
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
1424
1642
|
}
|
|
1425
1643
|
fieldType(type) {
|
|
1426
|
-
const baseType = type.type ? type.type : type.$type == "
|
|
1644
|
+
const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
|
|
1427
1645
|
return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
|
|
1428
1646
|
}
|
|
1429
1647
|
_generateDataModelAttribute(ast) {
|
|
1430
1648
|
return this.attribute(ast);
|
|
1431
1649
|
}
|
|
1432
|
-
|
|
1650
|
+
_generateDataFieldAttribute(ast) {
|
|
1433
1651
|
return this.attribute(ast);
|
|
1434
1652
|
}
|
|
1435
1653
|
attribute(ast) {
|
|
@@ -1513,9 +1731,6 @@ ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}${ast.attribu
|
|
|
1513
1731
|
${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") : ""}
|
|
1514
1732
|
}`;
|
|
1515
1733
|
}
|
|
1516
|
-
_generateTypeDefField(ast) {
|
|
1517
|
-
return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
|
|
1518
|
-
}
|
|
1519
1734
|
argument(ast) {
|
|
1520
1735
|
return this.generate(ast.value);
|
|
1521
1736
|
}
|
|
@@ -1641,13 +1856,13 @@ _ts_decorate([
|
|
|
1641
1856
|
_ts_metadata("design:returntype", void 0)
|
|
1642
1857
|
], ZModelCodeGenerator.prototype, "_generateDataModel", null);
|
|
1643
1858
|
_ts_decorate([
|
|
1644
|
-
gen(import_ast4.
|
|
1859
|
+
gen(import_ast4.DataField),
|
|
1645
1860
|
_ts_metadata("design:type", Function),
|
|
1646
1861
|
_ts_metadata("design:paramtypes", [
|
|
1647
|
-
typeof import_ast4.
|
|
1862
|
+
typeof import_ast4.DataField === "undefined" ? Object : import_ast4.DataField
|
|
1648
1863
|
]),
|
|
1649
1864
|
_ts_metadata("design:returntype", void 0)
|
|
1650
|
-
], ZModelCodeGenerator.prototype, "
|
|
1865
|
+
], ZModelCodeGenerator.prototype, "_generateDataField", null);
|
|
1651
1866
|
_ts_decorate([
|
|
1652
1867
|
gen(import_ast4.DataModelAttribute),
|
|
1653
1868
|
_ts_metadata("design:type", Function),
|
|
@@ -1657,13 +1872,13 @@ _ts_decorate([
|
|
|
1657
1872
|
_ts_metadata("design:returntype", void 0)
|
|
1658
1873
|
], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
|
|
1659
1874
|
_ts_decorate([
|
|
1660
|
-
gen(import_ast4.
|
|
1875
|
+
gen(import_ast4.DataFieldAttribute),
|
|
1661
1876
|
_ts_metadata("design:type", Function),
|
|
1662
1877
|
_ts_metadata("design:paramtypes", [
|
|
1663
|
-
typeof import_ast4.
|
|
1878
|
+
typeof import_ast4.DataFieldAttribute === "undefined" ? Object : import_ast4.DataFieldAttribute
|
|
1664
1879
|
]),
|
|
1665
1880
|
_ts_metadata("design:returntype", void 0)
|
|
1666
|
-
], ZModelCodeGenerator.prototype, "
|
|
1881
|
+
], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
|
|
1667
1882
|
_ts_decorate([
|
|
1668
1883
|
gen(import_ast4.AttributeArg),
|
|
1669
1884
|
_ts_metadata("design:type", Function),
|
|
@@ -1828,14 +2043,6 @@ _ts_decorate([
|
|
|
1828
2043
|
]),
|
|
1829
2044
|
_ts_metadata("design:returntype", void 0)
|
|
1830
2045
|
], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
|
|
1831
|
-
_ts_decorate([
|
|
1832
|
-
gen(import_ast4.TypeDefField),
|
|
1833
|
-
_ts_metadata("design:type", Function),
|
|
1834
|
-
_ts_metadata("design:paramtypes", [
|
|
1835
|
-
typeof import_ast4.TypeDefField === "undefined" ? Object : import_ast4.TypeDefField
|
|
1836
|
-
]),
|
|
1837
|
-
_ts_metadata("design:returntype", void 0)
|
|
1838
|
-
], ZModelCodeGenerator.prototype, "_generateTypeDefField", null);
|
|
1839
2046
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1840
2047
|
0 && (module.exports = {
|
|
1841
2048
|
ModelUtils,
|