@zenstackhq/sdk 3.0.0-beta.8 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -33,8 +33,7 @@ var src_exports = {};
33
33
  __export(src_exports, {
34
34
  ModelUtils: () => model_utils_exports,
35
35
  PrismaSchemaGenerator: () => PrismaSchemaGenerator,
36
- TsSchemaGenerator: () => TsSchemaGenerator,
37
- ZModelCodeGenerator: () => ZModelCodeGenerator
36
+ TsSchemaGenerator: () => TsSchemaGenerator
38
37
  });
39
38
  module.exports = __toCommonJS(src_exports);
40
39
 
@@ -123,7 +122,7 @@ __name(resolved, "resolved");
123
122
  function getAuthDecl(model) {
124
123
  let found = model.declarations.find((d) => ((0, import_ast.isDataModel)(d) || (0, import_ast.isTypeDef)(d)) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
125
124
  if (!found) {
126
- found = model.declarations.find((d) => (0, import_ast.isDataModel)(d) && d.name === "User");
125
+ found = model.declarations.find((d) => ((0, import_ast.isDataModel)(d) || (0, import_ast.isTypeDef)(d)) && d.name === "User");
127
126
  }
128
127
  return found;
129
128
  }
@@ -136,6 +135,7 @@ var DELEGATE_AUX_RELATION_PREFIX = "delegate_aux";
136
135
 
137
136
  // src/prisma/prisma-schema-generator.ts
138
137
  var import_common_helpers = require("@zenstackhq/common-helpers");
138
+ var import_language = require("@zenstackhq/language");
139
139
  var import_ast2 = require("@zenstackhq/language/ast");
140
140
  var import_utils2 = require("@zenstackhq/language/utils");
141
141
  var import_langium = require("langium");
@@ -533,6 +533,9 @@ var EnumField = class extends DeclarationBase {
533
533
 
534
534
  // src/prisma/prisma-schema-generator.ts
535
535
  var IDENTIFIER_NAME_MAX_LENGTH = 50 - DELEGATE_AUX_RELATION_PREFIX.length;
536
+ var NON_PRISMA_DATASOURCE_FIELDS = [
537
+ "defaultSchema"
538
+ ];
536
539
  var PrismaSchemaGenerator = class {
537
540
  static {
538
541
  __name(this, "PrismaSchemaGenerator");
@@ -567,10 +570,13 @@ var PrismaSchemaGenerator = class {
567
570
  break;
568
571
  }
569
572
  }
573
+ if (!this.zmodel.declarations.some(import_ast2.isGeneratorDecl)) {
574
+ this.generateDefaultGenerator(prisma);
575
+ }
570
576
  return this.PRELUDE + prisma.toString();
571
577
  }
572
578
  generateDataSource(prisma, dataSource) {
573
- const fields = dataSource.fields.map((f) => ({
579
+ const fields = dataSource.fields.filter((f) => !NON_PRISMA_DATASOURCE_FIELDS.includes(f.name)).map((f) => ({
574
580
  name: f.name,
575
581
  text: this.configExprToText(f.value)
576
582
  }));
@@ -607,6 +613,21 @@ var PrismaSchemaGenerator = class {
607
613
  text: this.configExprToText(f.value)
608
614
  })));
609
615
  }
616
+ generateDefaultGenerator(prisma) {
617
+ const gen = prisma.addGenerator("client", [
618
+ {
619
+ name: "provider",
620
+ text: '"prisma-client-js"'
621
+ }
622
+ ]);
623
+ const dataSource = this.zmodel.declarations.find(import_ast2.isDataSource);
624
+ if (dataSource?.fields.some((f) => f.name === "extensions")) {
625
+ gen.fields.push({
626
+ name: "previewFeatures",
627
+ text: '["postgresqlExtensions"]'
628
+ });
629
+ }
630
+ }
610
631
  generateModel(prisma, decl) {
611
632
  const model = decl.isView ? prisma.addView(decl.name) : prisma.addModel(decl.name);
612
633
  const allFields = (0, import_utils2.getAllFields)(decl, true);
@@ -618,14 +639,30 @@ var PrismaSchemaGenerator = class {
618
639
  this.generateModelField(model, field, decl);
619
640
  }
620
641
  }
621
- const allAttributes = (0, import_utils2.getAllAttributes)(decl);
622
- for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
642
+ const allAttributes = (0, import_utils2.getAllAttributes)(decl).filter((attr) => this.isPrismaAttribute(attr));
643
+ for (const attr of allAttributes) {
623
644
  this.generateContainerAttribute(model, attr);
624
645
  }
646
+ if (this.datasourceHasSchemasSetting(decl.$container) && !allAttributes.some((attr) => attr.decl.ref?.name === "@@schema")) {
647
+ model.addAttribute("@@schema", [
648
+ new AttributeArg(void 0, new AttributeArgValue("String", this.getDefaultPostgresSchemaName(decl.$container)))
649
+ ]);
650
+ }
625
651
  decl.comments.forEach((c) => model.addComment(c));
626
652
  this.generateDelegateRelationForBase(model, decl);
627
653
  this.generateDelegateRelationForConcrete(model, decl);
628
654
  }
655
+ getDatasourceField(zmodel, fieldName) {
656
+ const dataSource = zmodel.declarations.find(import_ast2.isDataSource);
657
+ return dataSource?.fields.find((f) => f.name === fieldName);
658
+ }
659
+ datasourceHasSchemasSetting(zmodel) {
660
+ return !!this.getDatasourceField(zmodel, "schemas");
661
+ }
662
+ getDefaultPostgresSchemaName(zmodel) {
663
+ const defaultSchemaField = this.getDatasourceField(zmodel, "defaultSchema");
664
+ return (0, import_utils2.getStringLiteral)(defaultSchemaField?.value) ?? "public";
665
+ }
629
666
  isPrismaAttribute(attr) {
630
667
  if (!attr.decl.ref) {
631
668
  return false;
@@ -634,7 +671,7 @@ var PrismaSchemaGenerator = class {
634
671
  }
635
672
  getUnsupportedFieldType(fieldType) {
636
673
  if (fieldType.unsupported) {
637
- const value = this.getStringLiteral(fieldType.unsupported.value);
674
+ const value = (0, import_utils2.getStringLiteral)(fieldType.unsupported.value);
638
675
  if (value) {
639
676
  return `Unsupported("${value}")`;
640
677
  } else {
@@ -644,9 +681,6 @@ var PrismaSchemaGenerator = class {
644
681
  return void 0;
645
682
  }
646
683
  }
647
- getStringLiteral(node) {
648
- return (0, import_ast2.isStringLiteral)(node) ? node.value : void 0;
649
- }
650
684
  generateModelField(model, field, contextModel, addToFront = false) {
651
685
  let fieldType;
652
686
  if (field.type.type) {
@@ -716,7 +750,7 @@ var PrismaSchemaGenerator = class {
716
750
  }
717
751
  }
718
752
  exprToText(expr) {
719
- return new ZModelCodeGenerator({
753
+ return new import_language.ZModelCodeGenerator({
720
754
  quote: "double"
721
755
  }).generate(expr);
722
756
  }
@@ -737,9 +771,15 @@ var PrismaSchemaGenerator = class {
737
771
  for (const field of decl.fields) {
738
772
  this.generateEnumField(_enum, field);
739
773
  }
740
- for (const attr of decl.attributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
774
+ const allAttributes = decl.attributes.filter((attr) => this.isPrismaAttribute(attr));
775
+ for (const attr of allAttributes) {
741
776
  this.generateContainerAttribute(_enum, attr);
742
777
  }
778
+ if (this.datasourceHasSchemasSetting(decl.$container) && !allAttributes.some((attr) => attr.decl.ref?.name === "@@schema")) {
779
+ _enum.addAttribute("@@schema", [
780
+ new AttributeArg(void 0, new AttributeArgValue("String", this.getDefaultPostgresSchemaName(decl.$container)))
781
+ ]);
782
+ }
743
783
  decl.comments.forEach((c) => _enum.addComment(c));
744
784
  }
745
785
  generateEnumField(_enum, field) {
@@ -818,29 +858,44 @@ var TsSchemaGenerator = class {
818
858
  __name(this, "TsSchemaGenerator");
819
859
  }
820
860
  usedExpressionUtils = false;
821
- async generate(model, outputDir) {
822
- import_node_fs.default.mkdirSync(outputDir, {
861
+ async generate(model, options) {
862
+ import_node_fs.default.mkdirSync(options.outDir, {
823
863
  recursive: true
824
864
  });
825
865
  this.usedExpressionUtils = false;
826
- this.generateSchema(model, outputDir);
827
- this.generateModelsAndTypeDefs(model, outputDir);
828
- this.generateInputTypes(model, outputDir);
829
- }
830
- generateSchema(model, outputDir) {
831
- const statements = [];
832
- this.generateSchemaStatements(model, statements);
833
- this.generateBannerComments(statements);
834
- const schemaOutputFile = import_node_path.default.join(outputDir, "schema.ts");
835
- const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
836
- const printer = ts.createPrinter();
837
- const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
838
- import_node_fs.default.writeFileSync(schemaOutputFile, result);
866
+ this.generateSchema(model, options);
867
+ this.generateModelsAndTypeDefs(model, options);
868
+ this.generateInputTypes(model, options);
869
+ }
870
+ generateSchema(model, options) {
871
+ const targets = [];
872
+ if (!options.liteOnly) {
873
+ targets.push({
874
+ lite: false,
875
+ file: "schema.ts"
876
+ });
877
+ }
878
+ if (options.lite || options.liteOnly) {
879
+ targets.push({
880
+ lite: true,
881
+ file: "schema-lite.ts"
882
+ });
883
+ }
884
+ for (const { lite, file } of targets) {
885
+ const statements = [];
886
+ this.generateSchemaStatements(model, statements, lite);
887
+ this.generateBannerComments(statements);
888
+ const schemaOutputFile = import_node_path.default.join(options.outDir, file);
889
+ const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
890
+ const printer = ts.createPrinter();
891
+ const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
892
+ import_node_fs.default.writeFileSync(schemaOutputFile, result);
893
+ }
839
894
  }
840
- generateSchemaStatements(model, statements) {
895
+ generateSchemaStatements(model, statements, lite) {
841
896
  const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
842
- const schemaObject = this.createSchemaObject(model);
843
- const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
897
+ const schemaClass = this.createSchemaClass(model, lite);
898
+ const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(void 0, void 0, ts.factory.createNamedImports([
844
899
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
845
900
  ...hasComputedFields ? [
846
901
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("OperandExpression"))
@@ -848,64 +903,81 @@ var TsSchemaGenerator = class {
848
903
  ...this.usedExpressionUtils ? [
849
904
  ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
850
905
  ] : []
851
- ])), ts.factory.createStringLiteral("@zenstackhq/runtime/schema"));
906
+ ])), ts.factory.createStringLiteral("@zenstackhq/orm/schema"));
852
907
  statements.push(runtimeImportDecl);
853
- const declaration = ts.factory.createVariableStatement([
908
+ statements.push(schemaClass);
909
+ const schemaDecl = ts.factory.createVariableStatement([
854
910
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
855
911
  ], ts.factory.createVariableDeclarationList([
856
- ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
912
+ ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createNewExpression(ts.factory.createIdentifier("SchemaType"), void 0, []))
857
913
  ], ts.NodeFlags.Const));
858
- statements.push(declaration);
859
- const typeDeclaration = ts.factory.createTypeAliasDeclaration([
860
- ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
861
- ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
862
- statements.push(typeDeclaration);
914
+ statements.push(schemaDecl);
863
915
  }
864
916
  createExpressionUtilsCall(method, args) {
865
917
  this.usedExpressionUtils = true;
866
918
  return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
867
919
  }
868
- createSchemaObject(model) {
869
- const properties = [
920
+ createSchemaClass(model, lite) {
921
+ const members = [
870
922
  // provider
871
- ts.factory.createPropertyAssignment("provider", this.createProviderObject(model)),
923
+ ts.factory.createPropertyDeclaration(void 0, "provider", void 0, void 0, this.createAsConst(this.createProviderObject(model))),
872
924
  // models
873
- ts.factory.createPropertyAssignment("models", this.createModelsObject(model)),
925
+ ts.factory.createPropertyDeclaration(void 0, "models", void 0, void 0, this.createAsConst(this.createModelsObject(model, lite))),
874
926
  // typeDefs
875
927
  ...model.declarations.some(import_ast3.isTypeDef) ? [
876
- ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model))
928
+ ts.factory.createPropertyDeclaration(void 0, "typeDefs", void 0, void 0, this.createAsConst(this.createTypeDefsObject(model, lite)))
877
929
  ] : []
878
930
  ];
879
931
  const enums = model.declarations.filter(import_ast3.isEnum);
880
932
  if (enums.length > 0) {
881
- properties.push(ts.factory.createPropertyAssignment("enums", ts.factory.createObjectLiteralExpression(enums.map((e) => ts.factory.createPropertyAssignment(e.name, this.createEnumObject(e))), true)));
933
+ members.push(ts.factory.createPropertyDeclaration(void 0, "enums", void 0, void 0, this.createAsConst(ts.factory.createObjectLiteralExpression(enums.map((e) => ts.factory.createPropertyAssignment(e.name, this.createEnumObject(e))), true))));
882
934
  }
883
935
  const authType = getAuthDecl(model);
884
936
  if (authType) {
885
- properties.push(ts.factory.createPropertyAssignment("authType", this.createLiteralNode(authType.name)));
937
+ members.push(ts.factory.createPropertyDeclaration(void 0, "authType", void 0, void 0, this.createAsConst(this.createLiteralNode(authType.name))));
886
938
  }
887
939
  const procedures = model.declarations.filter(import_ast3.isProcedure);
888
940
  if (procedures.length > 0) {
889
- properties.push(ts.factory.createPropertyAssignment("procedures", this.createProceduresObject(procedures)));
941
+ members.push(ts.factory.createPropertyDeclaration(void 0, "procedures", void 0, void 0, this.createAsConst(this.createProceduresObject(procedures))));
890
942
  }
891
- properties.push(ts.factory.createPropertyAssignment("plugins", ts.factory.createObjectLiteralExpression([], true)));
892
- return ts.factory.createObjectLiteralExpression(properties, true);
943
+ members.push(ts.factory.createPropertyDeclaration(void 0, "plugins", void 0, void 0, ts.factory.createObjectLiteralExpression([], true)));
944
+ const schemaClass = ts.factory.createClassDeclaration([
945
+ ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
946
+ ], "SchemaType", void 0, [
947
+ ts.factory.createHeritageClause(ts.SyntaxKind.ImplementsKeyword, [
948
+ ts.factory.createExpressionWithTypeArguments(ts.factory.createIdentifier("SchemaDef"), void 0)
949
+ ])
950
+ ], members);
951
+ return schemaClass;
952
+ }
953
+ createAsConst(expr) {
954
+ return ts.factory.createAsExpression(expr, ts.factory.createTypeReferenceNode("const"));
893
955
  }
894
956
  createProviderObject(model) {
895
957
  const dsProvider = this.getDataSourceProvider(model);
958
+ const defaultSchema = this.getDataSourceDefaultSchema(model);
896
959
  return ts.factory.createObjectLiteralExpression([
897
- ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(dsProvider.type))
960
+ ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(dsProvider)),
961
+ ...defaultSchema ? [
962
+ ts.factory.createPropertyAssignment("defaultSchema", ts.factory.createStringLiteral(defaultSchema))
963
+ ] : []
898
964
  ], true);
899
965
  }
900
- createModelsObject(model) {
901
- 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);
966
+ createModelsObject(model, lite) {
967
+ return ts.factory.createObjectLiteralExpression(this.getAllDataModels(model).map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm, lite))), true);
968
+ }
969
+ getAllDataModels(model) {
970
+ return model.declarations.filter((d) => (0, import_ast3.isDataModel)(d) && !hasAttribute(d, "@@ignore"));
902
971
  }
903
- createTypeDefsObject(model) {
904
- return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => (0, import_ast3.isTypeDef)(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
972
+ getAllTypeDefs(model) {
973
+ return model.declarations.filter((d) => (0, import_ast3.isTypeDef)(d) && !hasAttribute(d, "@@ignore"));
905
974
  }
906
- createDataModelObject(dm) {
975
+ createTypeDefsObject(model, lite) {
976
+ return ts.factory.createObjectLiteralExpression(this.getAllTypeDefs(model).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite))), true);
977
+ }
978
+ createDataModelObject(dm, lite) {
907
979
  const allFields = (0, import_utils3.getAllFields)(dm);
908
- const allAttributes = (0, import_utils3.getAllAttributes)(dm).filter((attr) => {
980
+ const allAttributes = lite ? [] : (0, import_utils3.getAllAttributes)(dm).filter((attr) => {
909
981
  if (attr.decl.$refText === "@@delegate" && attr.$container !== dm) {
910
982
  return false;
911
983
  }
@@ -920,7 +992,7 @@ var TsSchemaGenerator = class {
920
992
  ts.factory.createPropertyAssignment("baseModel", ts.factory.createStringLiteral(dm.baseModel.$refText))
921
993
  ] : [],
922
994
  // fields
923
- ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm))), true)),
995
+ ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm, lite))), true)),
924
996
  // attributes
925
997
  ...allAttributes.length > 0 ? [
926
998
  ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
@@ -950,14 +1022,14 @@ var TsSchemaGenerator = class {
950
1022
  getSubModels(dm) {
951
1023
  return dm.$container.declarations.filter(import_ast3.isDataModel).filter((d) => d.baseModel?.ref === dm).map((d) => d.name);
952
1024
  }
953
- createTypeDefObject(td) {
1025
+ createTypeDefObject(td, lite) {
954
1026
  const allFields = (0, import_utils3.getAllFields)(td);
955
1027
  const allAttributes = (0, import_utils3.getAllAttributes)(td);
956
1028
  const fields = [
957
1029
  // name
958
1030
  ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(td.name)),
959
1031
  // fields
960
- ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0))), true)),
1032
+ ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0, lite))), true)),
961
1033
  // attributes
962
1034
  ...allAttributes.length > 0 ? [
963
1035
  ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
@@ -989,7 +1061,7 @@ var TsSchemaGenerator = class {
989
1061
  }
990
1062
  return result;
991
1063
  }
992
- createDataFieldObject(field, contextModel) {
1064
+ createDataFieldObject(field, contextModel, lite) {
993
1065
  const objectFields = [
994
1066
  // name
995
1067
  ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
@@ -1011,6 +1083,9 @@ var TsSchemaGenerator = class {
1011
1083
  if (hasAttribute(field, "@updatedAt")) {
1012
1084
  objectFields.push(ts.factory.createPropertyAssignment("updatedAt", ts.factory.createTrue()));
1013
1085
  }
1086
+ if (hasAttribute(field, "@omit")) {
1087
+ objectFields.push(ts.factory.createPropertyAssignment("omit", ts.factory.createTrue()));
1088
+ }
1014
1089
  if (contextModel && // id fields are duplicated in inherited models
1015
1090
  !isIdField(field, contextModel) && field.$container !== contextModel && isDelegateModel(field.$container)) {
1016
1091
  objectFields.push(ts.factory.createPropertyAssignment("originModel", ts.factory.createStringLiteral(field.$container.name)));
@@ -1018,7 +1093,7 @@ var TsSchemaGenerator = class {
1018
1093
  if (this.isDiscriminatorField(field)) {
1019
1094
  objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
1020
1095
  }
1021
- if (field.attributes.length > 0) {
1096
+ if (!lite && field.attributes.length > 0) {
1022
1097
  objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
1023
1098
  }
1024
1099
  const defaultValue = this.getFieldMappedDefault(field);
@@ -1028,7 +1103,9 @@ var TsSchemaGenerator = class {
1028
1103
  objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1029
1104
  ts.factory.createStringLiteral(defaultValue.call),
1030
1105
  ...defaultValue.args.length > 0 ? [
1031
- ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
1106
+ ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createExpressionUtilsCall("literal", [
1107
+ this.createLiteralNode(arg)
1108
+ ])))
1032
1109
  ] : []
1033
1110
  ])));
1034
1111
  } else if ("authMember" in defaultValue) {
@@ -1069,11 +1146,18 @@ var TsSchemaGenerator = class {
1069
1146
  const dataSource = model.declarations.find(import_ast3.isDataSource);
1070
1147
  (0, import_common_helpers2.invariant)(dataSource, "No data source found in the model");
1071
1148
  const providerExpr = dataSource.fields.find((f) => f.name === "provider")?.value;
1072
- (0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(providerExpr), "Provider must be a literal");
1073
- const type = providerExpr.value;
1074
- return {
1075
- type
1076
- };
1149
+ (0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(providerExpr) && typeof providerExpr.value === "string", "Provider must be a string literal");
1150
+ return providerExpr.value;
1151
+ }
1152
+ getDataSourceDefaultSchema(model) {
1153
+ const dataSource = model.declarations.find(import_ast3.isDataSource);
1154
+ (0, import_common_helpers2.invariant)(dataSource, "No data source found in the model");
1155
+ const defaultSchemaExpr = dataSource.fields.find((f) => f.name === "defaultSchema")?.value;
1156
+ if (!defaultSchemaExpr) {
1157
+ return void 0;
1158
+ }
1159
+ (0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(defaultSchemaExpr) && typeof defaultSchemaExpr.value === "string", "Default schema must be a string literal");
1160
+ return defaultSchemaExpr.value;
1077
1161
  }
1078
1162
  getFieldMappedDefault(field) {
1079
1163
  const defaultAttr = getAttribute(field, "@default");
@@ -1143,12 +1227,16 @@ var TsSchemaGenerator = class {
1143
1227
  relationFields.push(ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(relationName)));
1144
1228
  }
1145
1229
  const relation = getAttribute(field, "@relation");
1230
+ const fkFields = [];
1146
1231
  if (relation) {
1147
1232
  for (const arg of relation.args) {
1148
1233
  const param = arg.$resolvedParam.name;
1149
1234
  if (param === "fields" || param === "references") {
1150
1235
  const fieldNames = this.getReferenceNames(arg.value);
1151
1236
  if (fieldNames) {
1237
+ if (param === "fields") {
1238
+ fkFields.push(...fieldNames);
1239
+ }
1152
1240
  relationFields.push(ts.factory.createPropertyAssignment(param, ts.factory.createArrayLiteralExpression(fieldNames.map((el) => ts.factory.createStringLiteral(el)))));
1153
1241
  }
1154
1242
  }
@@ -1158,6 +1246,15 @@ var TsSchemaGenerator = class {
1158
1246
  }
1159
1247
  }
1160
1248
  }
1249
+ if (fkFields.length > 0) {
1250
+ const allHaveDefault = fkFields.every((fieldName) => {
1251
+ const fieldDef = field.$container.fields.find((f) => f.name === fieldName);
1252
+ return fieldDef && hasAttribute(fieldDef, "@default");
1253
+ });
1254
+ if (allHaveDefault) {
1255
+ relationFields.push(ts.factory.createPropertyAssignment("hasDefault", ts.factory.createTrue()));
1256
+ }
1257
+ }
1161
1258
  return ts.factory.createObjectLiteralExpression(relationFields);
1162
1259
  }
1163
1260
  getReferenceNames(expr) {
@@ -1226,7 +1323,11 @@ var TsSchemaGenerator = class {
1226
1323
  const seenKeys = /* @__PURE__ */ new Set();
1227
1324
  for (const attr of allAttributes) {
1228
1325
  if (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@unique") {
1229
- const fieldNames = this.getReferenceNames(attr.args[0].value);
1326
+ const fieldsArg = (0, import_utils3.getAttributeArg)(attr, "fields");
1327
+ if (!fieldsArg) {
1328
+ continue;
1329
+ }
1330
+ const fieldNames = this.getReferenceNames(fieldsArg);
1230
1331
  if (!fieldNames) {
1231
1332
  continue;
1232
1333
  }
@@ -1265,7 +1366,21 @@ var TsSchemaGenerator = class {
1265
1366
  return field.type.type ? ts.factory.createStringLiteral(field.type.type) : field.type.reference ? ts.factory.createStringLiteral(field.type.reference.$refText) : ts.factory.createStringLiteral("Unsupported");
1266
1367
  }
1267
1368
  createEnumObject(e) {
1268
- return ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createStringLiteral(field.name))), true);
1369
+ return ts.factory.createObjectLiteralExpression([
1370
+ ts.factory.createPropertyAssignment("values", ts.factory.createObjectLiteralExpression(e.fields.map((f) => ts.factory.createPropertyAssignment(f.name, ts.factory.createStringLiteral(f.name))), true)),
1371
+ // only generate `fields` if there are attributes on the fields
1372
+ ...e.fields.some((f) => f.attributes.length > 0) ? [
1373
+ ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
1374
+ ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
1375
+ ...field.attributes.length > 0 ? [
1376
+ ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes?.map((attr) => this.createAttributeObject(attr)) ?? [], true))
1377
+ ] : []
1378
+ ], true))), true))
1379
+ ] : [],
1380
+ ...e.attributes.length > 0 ? [
1381
+ ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(e.attributes.map((attr) => this.createAttributeObject(attr)), true))
1382
+ ] : []
1383
+ ], true);
1269
1384
  }
1270
1385
  getLiteral(expr) {
1271
1386
  if (!(0, import_ast3.isLiteralExpr)(expr)) {
@@ -1282,7 +1397,10 @@ var TsSchemaGenerator = class {
1282
1397
  }
1283
1398
  }
1284
1399
  createLiteralNode(arg) {
1285
- return arg === null ? ts.factory.createNull() : typeof arg === "string" ? ts.factory.createStringLiteral(arg) : typeof arg === "number" ? ts.factory.createNumericLiteral(arg) : arg === true ? ts.factory.createTrue() : arg === false ? ts.factory.createFalse() : void 0;
1400
+ return arg === null ? ts.factory.createNull() : typeof arg === "string" ? ts.factory.createStringLiteral(arg) : typeof arg === "number" ? this.createNumberLiteral(arg) : arg === true ? ts.factory.createTrue() : arg === false ? ts.factory.createFalse() : void 0;
1401
+ }
1402
+ createNumberLiteral(arg) {
1403
+ return arg < 0 ? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral(-arg)) : ts.factory.createNumericLiteral(arg);
1286
1404
  }
1287
1405
  createProceduresObject(procedures) {
1288
1406
  return ts.factory.createObjectLiteralExpression(procedures.map((proc) => ts.factory.createPropertyAssignment(proc.name, this.createProcedureObject(proc))), true);
@@ -1414,16 +1532,16 @@ var TsSchemaGenerator = class {
1414
1532
  throw new Error(`Unsupported literal type: ${type}`);
1415
1533
  });
1416
1534
  }
1417
- generateModelsAndTypeDefs(model, outputDir) {
1535
+ generateModelsAndTypeDefs(model, options) {
1418
1536
  const statements = [];
1419
- statements.push(this.generateSchemaImport(model, true, true));
1537
+ statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly), options.importWithFileExtension));
1420
1538
  statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
1421
1539
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
1422
1540
  ...model.declarations.some(import_ast3.isTypeDef) ? [
1423
1541
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`TypeDefResult as $TypeDefResult`))
1424
1542
  ] : []
1425
- ])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
1426
- const dataModels = model.declarations.filter(import_ast3.isDataModel);
1543
+ ])), ts.factory.createStringLiteral("@zenstackhq/orm")));
1544
+ const dataModels = this.getAllDataModels(model);
1427
1545
  for (const dm of dataModels) {
1428
1546
  let modelType = ts.factory.createTypeAliasDeclaration([
1429
1547
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
@@ -1436,7 +1554,7 @@ var TsSchemaGenerator = class {
1436
1554
  }
1437
1555
  statements.push(modelType);
1438
1556
  }
1439
- const typeDefs = model.declarations.filter(import_ast3.isTypeDef);
1557
+ const typeDefs = this.getAllTypeDefs(model);
1440
1558
  for (const td of typeDefs) {
1441
1559
  let typeDef = ts.factory.createTypeAliasDeclaration([
1442
1560
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
@@ -1454,7 +1572,7 @@ var TsSchemaGenerator = class {
1454
1572
  let enumDecl = ts.factory.createVariableStatement([
1455
1573
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
1456
1574
  ], ts.factory.createVariableDeclarationList([
1457
- 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)))
1575
+ ts.factory.createVariableDeclaration(e.name, void 0, void 0, ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("$schema"), ts.factory.createIdentifier("enums")), ts.factory.createIdentifier(e.name)), ts.factory.createIdentifier("values")))
1458
1576
  ], ts.NodeFlags.Const));
1459
1577
  if (e.comments.length > 0) {
1460
1578
  enumDecl = this.generateDocs(enumDecl, e);
@@ -1469,13 +1587,13 @@ var TsSchemaGenerator = class {
1469
1587
  statements.push(typeAlias);
1470
1588
  }
1471
1589
  this.generateBannerComments(statements);
1472
- const outputFile = import_node_path.default.join(outputDir, "models.ts");
1590
+ const outputFile = import_node_path.default.join(options.outDir, "models.ts");
1473
1591
  const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
1474
1592
  const printer = ts.createPrinter();
1475
1593
  const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
1476
1594
  import_node_fs.default.writeFileSync(outputFile, result);
1477
1595
  }
1478
- generateSchemaImport(model, schemaObject, schemaType) {
1596
+ generateSchemaImport(model, schemaObject, schemaType, useLite, importWithFileExtension) {
1479
1597
  const importSpecifiers = [];
1480
1598
  if (schemaObject) {
1481
1599
  if (model.declarations.some(import_ast3.isEnum)) {
@@ -1485,17 +1603,21 @@ var TsSchemaGenerator = class {
1485
1603
  if (schemaType) {
1486
1604
  importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
1487
1605
  }
1488
- return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
1606
+ let importFrom = useLite ? "./schema-lite" : "./schema";
1607
+ if (importWithFileExtension) {
1608
+ importFrom += importWithFileExtension.startsWith(".") ? importWithFileExtension : `.${importWithFileExtension}`;
1609
+ }
1610
+ return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(importFrom));
1489
1611
  }
1490
1612
  generateDocs(tsDecl, decl) {
1491
1613
  return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
1492
1614
  * ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
1493
1615
  `, true);
1494
1616
  }
1495
- generateInputTypes(model, outputDir) {
1496
- const dataModels = model.declarations.filter(import_ast3.isDataModel);
1617
+ generateInputTypes(model, options) {
1618
+ const dataModels = this.getAllDataModels(model);
1497
1619
  const statements = [];
1498
- statements.push(this.generateSchemaImport(model, false, true));
1620
+ statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly), options.importWithFileExtension));
1499
1621
  const inputTypes = [
1500
1622
  "FindManyArgs",
1501
1623
  "FindUniqueArgs",
@@ -1522,11 +1644,14 @@ var TsSchemaGenerator = class {
1522
1644
  IncludeInput: "Include",
1523
1645
  OmitInput: "Omit"
1524
1646
  };
1525
- 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")));
1526
1647
  statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports([
1527
- ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SimplifiedModelResult as $SimplifiedModelResult")),
1648
+ ...inputTypes.map((inputType) => ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier(`${inputType} as $${inputType}`))),
1649
+ ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("QueryOptions as $QueryOptions"))
1650
+ ])), ts.factory.createStringLiteral("@zenstackhq/orm")));
1651
+ statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(true, void 0, ts.factory.createNamedImports([
1652
+ ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SimplifiedPlainResult as $Result")),
1528
1653
  ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("SelectIncludeOmit as $SelectIncludeOmit"))
1529
- ])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
1654
+ ])), ts.factory.createStringLiteral("@zenstackhq/orm")));
1530
1655
  for (const dm of dataModels) {
1531
1656
  for (const inputType of inputTypes) {
1532
1657
  const exportName = inputTypeNameFixes[inputType] ? `${dm.name}${inputTypeNameFixes[inputType]}` : `${dm.name}${inputType}`;
@@ -1544,526 +1669,31 @@ var TsSchemaGenerator = class {
1544
1669
  ts.factory.createTypeReferenceNode("$Schema"),
1545
1670
  ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
1546
1671
  ts.factory.createLiteralTypeNode(ts.factory.createTrue())
1672
+ ])),
1673
+ ts.factory.createTypeParameterDeclaration(void 0, "Options", ts.factory.createTypeReferenceNode("$QueryOptions", [
1674
+ ts.factory.createTypeReferenceNode("$Schema")
1675
+ ]), ts.factory.createTypeReferenceNode("$QueryOptions", [
1676
+ ts.factory.createTypeReferenceNode("$Schema")
1547
1677
  ]))
1548
- ], ts.factory.createTypeReferenceNode("$SimplifiedModelResult", [
1678
+ ], ts.factory.createTypeReferenceNode("$Result", [
1549
1679
  ts.factory.createTypeReferenceNode("$Schema"),
1550
1680
  ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral(dm.name)),
1551
- ts.factory.createTypeReferenceNode("Args")
1681
+ ts.factory.createTypeReferenceNode("Args"),
1682
+ ts.factory.createTypeReferenceNode("Options")
1552
1683
  ])));
1553
1684
  }
1554
1685
  this.generateBannerComments(statements);
1555
- const outputFile = import_node_path.default.join(outputDir, "input.ts");
1686
+ const outputFile = import_node_path.default.join(options.outDir, "input.ts");
1556
1687
  const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
1557
1688
  const printer = ts.createPrinter();
1558
1689
  const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
1559
1690
  import_node_fs.default.writeFileSync(outputFile, result);
1560
1691
  }
1561
1692
  };
1562
-
1563
- // src/zmodel-code-generator.ts
1564
- var import_ast4 = require("@zenstackhq/language/ast");
1565
- function _ts_decorate(decorators, target, key, desc) {
1566
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
1567
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
1568
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
1569
- return c > 3 && r && Object.defineProperty(target, key, r), r;
1570
- }
1571
- __name(_ts_decorate, "_ts_decorate");
1572
- function _ts_metadata(k, v) {
1573
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
1574
- }
1575
- __name(_ts_metadata, "_ts_metadata");
1576
- var generationHandlers = /* @__PURE__ */ new Map();
1577
- function gen(name) {
1578
- return function(_target, _propertyKey, descriptor) {
1579
- if (!generationHandlers.get(name)) {
1580
- generationHandlers.set(name, descriptor);
1581
- }
1582
- return descriptor;
1583
- };
1584
- }
1585
- __name(gen, "gen");
1586
- var ZModelCodeGenerator = class {
1587
- static {
1588
- __name(this, "ZModelCodeGenerator");
1589
- }
1590
- options;
1591
- constructor(options) {
1592
- this.options = {
1593
- binaryExprNumberOfSpaces: options?.binaryExprNumberOfSpaces ?? 1,
1594
- unaryExprNumberOfSpaces: options?.unaryExprNumberOfSpaces ?? 0,
1595
- indent: options?.indent ?? 4,
1596
- quote: options?.quote ?? "single"
1597
- };
1598
- }
1599
- /**
1600
- * Generates ZModel source code from AST.
1601
- */
1602
- generate(ast) {
1603
- const handler = generationHandlers.get(ast.$type);
1604
- if (!handler) {
1605
- throw new Error(`No generation handler found for ${ast.$type}`);
1606
- }
1607
- return handler.value.call(this, ast);
1608
- }
1609
- _generateModel(ast) {
1610
- return ast.declarations.map((d) => this.generate(d)).join("\n\n");
1611
- }
1612
- _generateDataSource(ast) {
1613
- return `datasource ${ast.name} {
1614
- ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
1615
- }`;
1616
- }
1617
- _generateEnum(ast) {
1618
- return `enum ${ast.name} {
1619
- ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
1620
- }`;
1621
- }
1622
- _generateEnumField(ast) {
1623
- return `${ast.name}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
1624
- }
1625
- _generateGenerator(ast) {
1626
- return `generator ${ast.name} {
1627
- ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
1628
- }`;
1629
- }
1630
- _generateConfigField(ast) {
1631
- return `${ast.name} = ${this.generate(ast.value)}`;
1632
- }
1633
- _generateConfigArrayExpr(ast) {
1634
- return `[${ast.items.map((x) => this.generate(x)).join(", ")}]`;
1635
- }
1636
- _generateConfigInvocationExpr(ast) {
1637
- if (ast.args.length === 0) {
1638
- return ast.name;
1639
- } else {
1640
- return `${ast.name}(${ast.args.map((x) => (x.name ? x.name + ": " : "") + this.generate(x.value)).join(", ")})`;
1641
- }
1642
- }
1643
- _generatePlugin(ast) {
1644
- return `plugin ${ast.name} {
1645
- ${ast.fields.map((x) => this.indent + this.generate(x)).join("\n")}
1646
- }`;
1647
- }
1648
- _generatePluginField(ast) {
1649
- return `${ast.name} = ${this.generate(ast.value)}`;
1650
- }
1651
- _generateDataModel(ast) {
1652
- return `${ast.isView ? "view" : "model"} ${ast.name}${ast.mixins.length > 0 ? " mixes " + ast.mixins.map((x) => x.ref?.name).join(", ") : ""} {
1653
- ${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") : ""}
1654
- }`;
1655
- }
1656
- _generateDataField(ast) {
1657
- return `${ast.name} ${this.fieldType(ast.type)}${ast.attributes.length > 0 ? " " + ast.attributes.map((x) => this.generate(x)).join(" ") : ""}`;
1658
- }
1659
- fieldType(type) {
1660
- const baseType = type.type ? type.type : type.$type == "DataFieldType" && type.unsupported ? "Unsupported(" + this.generate(type.unsupported.value) + ")" : type.reference?.$refText;
1661
- return `${baseType}${type.array ? "[]" : ""}${type.optional ? "?" : ""}`;
1662
- }
1663
- _generateDataModelAttribute(ast) {
1664
- return this.attribute(ast);
1665
- }
1666
- _generateDataFieldAttribute(ast) {
1667
- return this.attribute(ast);
1668
- }
1669
- attribute(ast) {
1670
- const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
1671
- return `${resolved(ast.decl).name}${args}`;
1672
- }
1673
- _generateAttributeArg(ast) {
1674
- if (ast.name) {
1675
- return `${ast.name}: ${this.generate(ast.value)}`;
1676
- } else {
1677
- return this.generate(ast.value);
1678
- }
1679
- }
1680
- _generateObjectExpr(ast) {
1681
- return `{ ${ast.fields.map((field) => this.objectField(field)).join(", ")} }`;
1682
- }
1683
- objectField(field) {
1684
- return `${field.name}: ${this.generate(field.value)}`;
1685
- }
1686
- _generateArrayExpr(ast) {
1687
- return `[${ast.items.map((item) => this.generate(item)).join(", ")}]`;
1688
- }
1689
- _generateLiteralExpr(ast) {
1690
- return this.options.quote === "single" ? `'${ast.value}'` : `"${ast.value}"`;
1691
- }
1692
- _generateNumberLiteral(ast) {
1693
- return ast.value.toString();
1694
- }
1695
- _generateBooleanLiteral(ast) {
1696
- return ast.value.toString();
1697
- }
1698
- _generateUnaryExpr(ast) {
1699
- return `${ast.operator}${this.unaryExprSpace}${this.generate(ast.operand)}`;
1700
- }
1701
- _generateBinaryExpr(ast) {
1702
- const operator = ast.operator;
1703
- const isCollectionPredicate = this.isCollectionPredicateOperator(operator);
1704
- const rightExpr = this.generate(ast.right);
1705
- const { left: isLeftParenthesis, right: isRightParenthesis } = this.isParenthesesNeededForBinaryExpr(ast);
1706
- return `${isLeftParenthesis ? "(" : ""}${this.generate(ast.left)}${isLeftParenthesis ? ")" : ""}${isCollectionPredicate ? "" : this.binaryExprSpace}${operator}${isCollectionPredicate ? "" : this.binaryExprSpace}${isRightParenthesis ? "(" : ""}${isCollectionPredicate ? `[${rightExpr}]` : rightExpr}${isRightParenthesis ? ")" : ""}`;
1707
- }
1708
- _generateReferenceExpr(ast) {
1709
- const args = ast.args.length ? `(${ast.args.map((x) => this.generate(x)).join(", ")})` : "";
1710
- return `${ast.target.ref?.name}${args}`;
1711
- }
1712
- _generateReferenceArg(ast) {
1713
- return `${ast.name}:${this.generate(ast.value)}`;
1714
- }
1715
- _generateMemberExpr(ast) {
1716
- return `${this.generate(ast.operand)}.${ast.member.ref?.name}`;
1717
- }
1718
- _generateInvocationExpr(ast) {
1719
- return `${ast.function.ref?.name}(${ast.args.map((x) => this.argument(x)).join(", ")})`;
1720
- }
1721
- _generateNullExpr() {
1722
- return "null";
1723
- }
1724
- _generateThisExpr() {
1725
- return "this";
1726
- }
1727
- _generateAttribute(ast) {
1728
- return `attribute ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")})`;
1729
- }
1730
- _generateAttributeParam(ast) {
1731
- return `${ast.default ? "_ " : ""}${ast.name}: ${this.generate(ast.type)}`;
1732
- }
1733
- _generateAttributeParamType(ast) {
1734
- return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}${ast.optional ? "?" : ""}`;
1735
- }
1736
- _generateFunctionDecl(ast) {
1737
- return `function ${ast.name}(${ast.params.map((x) => this.generate(x)).join(", ")}) ${ast.returnType ? ": " + this.generate(ast.returnType) : ""} {}`;
1738
- }
1739
- _generateFunctionParam(ast) {
1740
- return `${ast.name}: ${this.generate(ast.type)}`;
1741
- }
1742
- _generateFunctionParamType(ast) {
1743
- return `${ast.type ?? ast.reference?.$refText}${ast.array ? "[]" : ""}`;
1744
- }
1745
- _generateTypeDef(ast) {
1746
- return `type ${ast.name} {
1747
- ${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") : ""}
1748
- }`;
1749
- }
1750
- argument(ast) {
1751
- return this.generate(ast.value);
1752
- }
1753
- get binaryExprSpace() {
1754
- return " ".repeat(this.options.binaryExprNumberOfSpaces);
1755
- }
1756
- get unaryExprSpace() {
1757
- return " ".repeat(this.options.unaryExprNumberOfSpaces);
1758
- }
1759
- get indent() {
1760
- return " ".repeat(this.options.indent);
1761
- }
1762
- isParenthesesNeededForBinaryExpr(ast) {
1763
- const result = {
1764
- left: false,
1765
- right: false
1766
- };
1767
- const operator = ast.operator;
1768
- const isCollectionPredicate = this.isCollectionPredicateOperator(operator);
1769
- const currentPriority = import_ast4.BinaryExprOperatorPriority[operator];
1770
- if (ast.left.$type === import_ast4.BinaryExpr && import_ast4.BinaryExprOperatorPriority[ast.left["operator"]] < currentPriority) {
1771
- result.left = true;
1772
- }
1773
- if (!isCollectionPredicate && ast.right.$type === import_ast4.BinaryExpr && import_ast4.BinaryExprOperatorPriority[ast.right["operator"]] <= currentPriority) {
1774
- result.right = true;
1775
- }
1776
- return result;
1777
- }
1778
- isCollectionPredicateOperator(op) {
1779
- return [
1780
- "?",
1781
- "!",
1782
- "^"
1783
- ].includes(op);
1784
- }
1785
- };
1786
- _ts_decorate([
1787
- gen(import_ast4.Model),
1788
- _ts_metadata("design:type", Function),
1789
- _ts_metadata("design:paramtypes", [
1790
- typeof import_ast4.Model === "undefined" ? Object : import_ast4.Model
1791
- ]),
1792
- _ts_metadata("design:returntype", void 0)
1793
- ], ZModelCodeGenerator.prototype, "_generateModel", null);
1794
- _ts_decorate([
1795
- gen(import_ast4.DataSource),
1796
- _ts_metadata("design:type", Function),
1797
- _ts_metadata("design:paramtypes", [
1798
- typeof import_ast4.DataSource === "undefined" ? Object : import_ast4.DataSource
1799
- ]),
1800
- _ts_metadata("design:returntype", void 0)
1801
- ], ZModelCodeGenerator.prototype, "_generateDataSource", null);
1802
- _ts_decorate([
1803
- gen(import_ast4.Enum),
1804
- _ts_metadata("design:type", Function),
1805
- _ts_metadata("design:paramtypes", [
1806
- typeof import_ast4.Enum === "undefined" ? Object : import_ast4.Enum
1807
- ]),
1808
- _ts_metadata("design:returntype", void 0)
1809
- ], ZModelCodeGenerator.prototype, "_generateEnum", null);
1810
- _ts_decorate([
1811
- gen(import_ast4.EnumField),
1812
- _ts_metadata("design:type", Function),
1813
- _ts_metadata("design:paramtypes", [
1814
- typeof import_ast4.EnumField === "undefined" ? Object : import_ast4.EnumField
1815
- ]),
1816
- _ts_metadata("design:returntype", void 0)
1817
- ], ZModelCodeGenerator.prototype, "_generateEnumField", null);
1818
- _ts_decorate([
1819
- gen(import_ast4.GeneratorDecl),
1820
- _ts_metadata("design:type", Function),
1821
- _ts_metadata("design:paramtypes", [
1822
- typeof import_ast4.GeneratorDecl === "undefined" ? Object : import_ast4.GeneratorDecl
1823
- ]),
1824
- _ts_metadata("design:returntype", void 0)
1825
- ], ZModelCodeGenerator.prototype, "_generateGenerator", null);
1826
- _ts_decorate([
1827
- gen(import_ast4.ConfigField),
1828
- _ts_metadata("design:type", Function),
1829
- _ts_metadata("design:paramtypes", [
1830
- typeof import_ast4.ConfigField === "undefined" ? Object : import_ast4.ConfigField
1831
- ]),
1832
- _ts_metadata("design:returntype", void 0)
1833
- ], ZModelCodeGenerator.prototype, "_generateConfigField", null);
1834
- _ts_decorate([
1835
- gen(import_ast4.ConfigArrayExpr),
1836
- _ts_metadata("design:type", Function),
1837
- _ts_metadata("design:paramtypes", [
1838
- typeof import_ast4.ConfigArrayExpr === "undefined" ? Object : import_ast4.ConfigArrayExpr
1839
- ]),
1840
- _ts_metadata("design:returntype", void 0)
1841
- ], ZModelCodeGenerator.prototype, "_generateConfigArrayExpr", null);
1842
- _ts_decorate([
1843
- gen(import_ast4.ConfigInvocationExpr),
1844
- _ts_metadata("design:type", Function),
1845
- _ts_metadata("design:paramtypes", [
1846
- typeof import_ast4.ConfigInvocationExpr === "undefined" ? Object : import_ast4.ConfigInvocationExpr
1847
- ]),
1848
- _ts_metadata("design:returntype", void 0)
1849
- ], ZModelCodeGenerator.prototype, "_generateConfigInvocationExpr", null);
1850
- _ts_decorate([
1851
- gen(import_ast4.Plugin),
1852
- _ts_metadata("design:type", Function),
1853
- _ts_metadata("design:paramtypes", [
1854
- typeof import_ast4.Plugin === "undefined" ? Object : import_ast4.Plugin
1855
- ]),
1856
- _ts_metadata("design:returntype", void 0)
1857
- ], ZModelCodeGenerator.prototype, "_generatePlugin", null);
1858
- _ts_decorate([
1859
- gen(import_ast4.PluginField),
1860
- _ts_metadata("design:type", Function),
1861
- _ts_metadata("design:paramtypes", [
1862
- typeof import_ast4.PluginField === "undefined" ? Object : import_ast4.PluginField
1863
- ]),
1864
- _ts_metadata("design:returntype", void 0)
1865
- ], ZModelCodeGenerator.prototype, "_generatePluginField", null);
1866
- _ts_decorate([
1867
- gen(import_ast4.DataModel),
1868
- _ts_metadata("design:type", Function),
1869
- _ts_metadata("design:paramtypes", [
1870
- typeof import_ast4.DataModel === "undefined" ? Object : import_ast4.DataModel
1871
- ]),
1872
- _ts_metadata("design:returntype", void 0)
1873
- ], ZModelCodeGenerator.prototype, "_generateDataModel", null);
1874
- _ts_decorate([
1875
- gen(import_ast4.DataField),
1876
- _ts_metadata("design:type", Function),
1877
- _ts_metadata("design:paramtypes", [
1878
- typeof import_ast4.DataField === "undefined" ? Object : import_ast4.DataField
1879
- ]),
1880
- _ts_metadata("design:returntype", void 0)
1881
- ], ZModelCodeGenerator.prototype, "_generateDataField", null);
1882
- _ts_decorate([
1883
- gen(import_ast4.DataModelAttribute),
1884
- _ts_metadata("design:type", Function),
1885
- _ts_metadata("design:paramtypes", [
1886
- typeof import_ast4.DataModelAttribute === "undefined" ? Object : import_ast4.DataModelAttribute
1887
- ]),
1888
- _ts_metadata("design:returntype", void 0)
1889
- ], ZModelCodeGenerator.prototype, "_generateDataModelAttribute", null);
1890
- _ts_decorate([
1891
- gen(import_ast4.DataFieldAttribute),
1892
- _ts_metadata("design:type", Function),
1893
- _ts_metadata("design:paramtypes", [
1894
- typeof import_ast4.DataFieldAttribute === "undefined" ? Object : import_ast4.DataFieldAttribute
1895
- ]),
1896
- _ts_metadata("design:returntype", void 0)
1897
- ], ZModelCodeGenerator.prototype, "_generateDataFieldAttribute", null);
1898
- _ts_decorate([
1899
- gen(import_ast4.AttributeArg),
1900
- _ts_metadata("design:type", Function),
1901
- _ts_metadata("design:paramtypes", [
1902
- typeof import_ast4.AttributeArg === "undefined" ? Object : import_ast4.AttributeArg
1903
- ]),
1904
- _ts_metadata("design:returntype", void 0)
1905
- ], ZModelCodeGenerator.prototype, "_generateAttributeArg", null);
1906
- _ts_decorate([
1907
- gen(import_ast4.ObjectExpr),
1908
- _ts_metadata("design:type", Function),
1909
- _ts_metadata("design:paramtypes", [
1910
- typeof import_ast4.ObjectExpr === "undefined" ? Object : import_ast4.ObjectExpr
1911
- ]),
1912
- _ts_metadata("design:returntype", void 0)
1913
- ], ZModelCodeGenerator.prototype, "_generateObjectExpr", null);
1914
- _ts_decorate([
1915
- gen(import_ast4.ArrayExpr),
1916
- _ts_metadata("design:type", Function),
1917
- _ts_metadata("design:paramtypes", [
1918
- typeof import_ast4.ArrayExpr === "undefined" ? Object : import_ast4.ArrayExpr
1919
- ]),
1920
- _ts_metadata("design:returntype", void 0)
1921
- ], ZModelCodeGenerator.prototype, "_generateArrayExpr", null);
1922
- _ts_decorate([
1923
- gen(import_ast4.StringLiteral),
1924
- _ts_metadata("design:type", Function),
1925
- _ts_metadata("design:paramtypes", [
1926
- typeof import_ast4.LiteralExpr === "undefined" ? Object : import_ast4.LiteralExpr
1927
- ]),
1928
- _ts_metadata("design:returntype", void 0)
1929
- ], ZModelCodeGenerator.prototype, "_generateLiteralExpr", null);
1930
- _ts_decorate([
1931
- gen(import_ast4.NumberLiteral),
1932
- _ts_metadata("design:type", Function),
1933
- _ts_metadata("design:paramtypes", [
1934
- typeof import_ast4.NumberLiteral === "undefined" ? Object : import_ast4.NumberLiteral
1935
- ]),
1936
- _ts_metadata("design:returntype", void 0)
1937
- ], ZModelCodeGenerator.prototype, "_generateNumberLiteral", null);
1938
- _ts_decorate([
1939
- gen(import_ast4.BooleanLiteral),
1940
- _ts_metadata("design:type", Function),
1941
- _ts_metadata("design:paramtypes", [
1942
- typeof import_ast4.BooleanLiteral === "undefined" ? Object : import_ast4.BooleanLiteral
1943
- ]),
1944
- _ts_metadata("design:returntype", void 0)
1945
- ], ZModelCodeGenerator.prototype, "_generateBooleanLiteral", null);
1946
- _ts_decorate([
1947
- gen(import_ast4.UnaryExpr),
1948
- _ts_metadata("design:type", Function),
1949
- _ts_metadata("design:paramtypes", [
1950
- typeof import_ast4.UnaryExpr === "undefined" ? Object : import_ast4.UnaryExpr
1951
- ]),
1952
- _ts_metadata("design:returntype", void 0)
1953
- ], ZModelCodeGenerator.prototype, "_generateUnaryExpr", null);
1954
- _ts_decorate([
1955
- gen(import_ast4.BinaryExpr),
1956
- _ts_metadata("design:type", Function),
1957
- _ts_metadata("design:paramtypes", [
1958
- typeof import_ast4.BinaryExpr === "undefined" ? Object : import_ast4.BinaryExpr
1959
- ]),
1960
- _ts_metadata("design:returntype", void 0)
1961
- ], ZModelCodeGenerator.prototype, "_generateBinaryExpr", null);
1962
- _ts_decorate([
1963
- gen(import_ast4.ReferenceExpr),
1964
- _ts_metadata("design:type", Function),
1965
- _ts_metadata("design:paramtypes", [
1966
- typeof import_ast4.ReferenceExpr === "undefined" ? Object : import_ast4.ReferenceExpr
1967
- ]),
1968
- _ts_metadata("design:returntype", void 0)
1969
- ], ZModelCodeGenerator.prototype, "_generateReferenceExpr", null);
1970
- _ts_decorate([
1971
- gen(import_ast4.ReferenceArg),
1972
- _ts_metadata("design:type", Function),
1973
- _ts_metadata("design:paramtypes", [
1974
- typeof import_ast4.ReferenceArg === "undefined" ? Object : import_ast4.ReferenceArg
1975
- ]),
1976
- _ts_metadata("design:returntype", void 0)
1977
- ], ZModelCodeGenerator.prototype, "_generateReferenceArg", null);
1978
- _ts_decorate([
1979
- gen(import_ast4.MemberAccessExpr),
1980
- _ts_metadata("design:type", Function),
1981
- _ts_metadata("design:paramtypes", [
1982
- typeof import_ast4.MemberAccessExpr === "undefined" ? Object : import_ast4.MemberAccessExpr
1983
- ]),
1984
- _ts_metadata("design:returntype", void 0)
1985
- ], ZModelCodeGenerator.prototype, "_generateMemberExpr", null);
1986
- _ts_decorate([
1987
- gen(import_ast4.InvocationExpr),
1988
- _ts_metadata("design:type", Function),
1989
- _ts_metadata("design:paramtypes", [
1990
- typeof import_ast4.InvocationExpr === "undefined" ? Object : import_ast4.InvocationExpr
1991
- ]),
1992
- _ts_metadata("design:returntype", void 0)
1993
- ], ZModelCodeGenerator.prototype, "_generateInvocationExpr", null);
1994
- _ts_decorate([
1995
- gen(import_ast4.NullExpr),
1996
- _ts_metadata("design:type", Function),
1997
- _ts_metadata("design:paramtypes", []),
1998
- _ts_metadata("design:returntype", void 0)
1999
- ], ZModelCodeGenerator.prototype, "_generateNullExpr", null);
2000
- _ts_decorate([
2001
- gen(import_ast4.ThisExpr),
2002
- _ts_metadata("design:type", Function),
2003
- _ts_metadata("design:paramtypes", []),
2004
- _ts_metadata("design:returntype", void 0)
2005
- ], ZModelCodeGenerator.prototype, "_generateThisExpr", null);
2006
- _ts_decorate([
2007
- gen(import_ast4.Attribute),
2008
- _ts_metadata("design:type", Function),
2009
- _ts_metadata("design:paramtypes", [
2010
- typeof import_ast4.Attribute === "undefined" ? Object : import_ast4.Attribute
2011
- ]),
2012
- _ts_metadata("design:returntype", void 0)
2013
- ], ZModelCodeGenerator.prototype, "_generateAttribute", null);
2014
- _ts_decorate([
2015
- gen(import_ast4.AttributeParam),
2016
- _ts_metadata("design:type", Function),
2017
- _ts_metadata("design:paramtypes", [
2018
- typeof import_ast4.AttributeParam === "undefined" ? Object : import_ast4.AttributeParam
2019
- ]),
2020
- _ts_metadata("design:returntype", void 0)
2021
- ], ZModelCodeGenerator.prototype, "_generateAttributeParam", null);
2022
- _ts_decorate([
2023
- gen(import_ast4.AttributeParamType),
2024
- _ts_metadata("design:type", Function),
2025
- _ts_metadata("design:paramtypes", [
2026
- typeof import_ast4.AttributeParamType === "undefined" ? Object : import_ast4.AttributeParamType
2027
- ]),
2028
- _ts_metadata("design:returntype", void 0)
2029
- ], ZModelCodeGenerator.prototype, "_generateAttributeParamType", null);
2030
- _ts_decorate([
2031
- gen(import_ast4.FunctionDecl),
2032
- _ts_metadata("design:type", Function),
2033
- _ts_metadata("design:paramtypes", [
2034
- typeof import_ast4.FunctionDecl === "undefined" ? Object : import_ast4.FunctionDecl
2035
- ]),
2036
- _ts_metadata("design:returntype", void 0)
2037
- ], ZModelCodeGenerator.prototype, "_generateFunctionDecl", null);
2038
- _ts_decorate([
2039
- gen(import_ast4.FunctionParam),
2040
- _ts_metadata("design:type", Function),
2041
- _ts_metadata("design:paramtypes", [
2042
- typeof import_ast4.FunctionParam === "undefined" ? Object : import_ast4.FunctionParam
2043
- ]),
2044
- _ts_metadata("design:returntype", void 0)
2045
- ], ZModelCodeGenerator.prototype, "_generateFunctionParam", null);
2046
- _ts_decorate([
2047
- gen(import_ast4.FunctionParamType),
2048
- _ts_metadata("design:type", Function),
2049
- _ts_metadata("design:paramtypes", [
2050
- typeof import_ast4.FunctionParamType === "undefined" ? Object : import_ast4.FunctionParamType
2051
- ]),
2052
- _ts_metadata("design:returntype", void 0)
2053
- ], ZModelCodeGenerator.prototype, "_generateFunctionParamType", null);
2054
- _ts_decorate([
2055
- gen(import_ast4.TypeDef),
2056
- _ts_metadata("design:type", Function),
2057
- _ts_metadata("design:paramtypes", [
2058
- typeof import_ast4.TypeDef === "undefined" ? Object : import_ast4.TypeDef
2059
- ]),
2060
- _ts_metadata("design:returntype", void 0)
2061
- ], ZModelCodeGenerator.prototype, "_generateTypeDef", null);
2062
1693
  // Annotate the CommonJS export names for ESM import in node:
2063
1694
  0 && (module.exports = {
2064
1695
  ModelUtils,
2065
1696
  PrismaSchemaGenerator,
2066
- TsSchemaGenerator,
2067
- ZModelCodeGenerator
1697
+ TsSchemaGenerator
2068
1698
  });
2069
1699
  //# sourceMappingURL=index.cjs.map