@zenstackhq/sdk 3.0.0-beta.3 → 3.0.0-beta.5

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.d.cts CHANGED
@@ -122,9 +122,11 @@ declare class PrismaSchemaGenerator {
122
122
  }
123
123
 
124
124
  declare class TsSchemaGenerator {
125
+ private usedExpressionUtils;
125
126
  generate(model: Model, outputDir: string): Promise<void>;
126
127
  private generateSchema;
127
128
  private generateSchemaStatements;
129
+ private createExpressionUtilsCall;
128
130
  private createSchemaObject;
129
131
  private createProviderObject;
130
132
  private createModelsObject;
package/dist/index.d.ts CHANGED
@@ -122,9 +122,11 @@ declare class PrismaSchemaGenerator {
122
122
  }
123
123
 
124
124
  declare class TsSchemaGenerator {
125
+ private usedExpressionUtils;
125
126
  generate(model: Model, outputDir: string): Promise<void>;
126
127
  private generateSchema;
127
128
  private generateSchemaStatements;
129
+ private createExpressionUtilsCall;
128
130
  private createSchemaObject;
129
131
  private createProviderObject;
130
132
  private createModelsObject;
package/dist/index.js CHANGED
@@ -20,7 +20,7 @@ __export(model_utils_exports, {
20
20
  isUniqueField: () => isUniqueField,
21
21
  resolved: () => resolved
22
22
  });
23
- import { isDataModel, isLiteralExpr, isModel } from "@zenstackhq/language/ast";
23
+ import { isDataModel, isLiteralExpr, isModel, isTypeDef } from "@zenstackhq/language/ast";
24
24
  import { getAllFields, getModelIdFields, getModelUniqueFields } from "@zenstackhq/language/utils";
25
25
  function isIdField(field, contextModel) {
26
26
  if (hasAttribute(field, "@id")) {
@@ -88,7 +88,7 @@ function resolved(ref) {
88
88
  }
89
89
  __name(resolved, "resolved");
90
90
  function getAuthDecl(model) {
91
- let found = model.declarations.find((d) => isDataModel(d) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
91
+ let found = model.declarations.find((d) => (isDataModel(d) || isTypeDef(d)) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
92
92
  if (!found) {
93
93
  found = model.declarations.find((d) => isDataModel(d) && d.name === "User");
94
94
  }
@@ -103,7 +103,7 @@ var DELEGATE_AUX_RELATION_PREFIX = "delegate_aux";
103
103
 
104
104
  // src/prisma/prisma-schema-generator.ts
105
105
  import { lowerCaseFirst } from "@zenstackhq/common-helpers";
106
- import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isModel as isModel2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
106
+ import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isModel as isModel2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef as isTypeDef2, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
107
107
  import { getAllAttributes, getAllFields as getAllFields2, isDelegateModel as isDelegateModel2 } from "@zenstackhq/language/utils";
108
108
  import { AstUtils } from "langium";
109
109
  import { match } from "ts-pattern";
@@ -619,7 +619,7 @@ var PrismaSchemaGenerator = class {
619
619
  if (field.type.type) {
620
620
  fieldType = field.type.type;
621
621
  } else if (field.type.reference?.ref) {
622
- if (isTypeDef(field.type.reference.ref)) {
622
+ if (isTypeDef2(field.type.reference.ref)) {
623
623
  fieldType = "Json";
624
624
  } else {
625
625
  fieldType = field.type.reference.ref.name;
@@ -635,7 +635,7 @@ var PrismaSchemaGenerator = class {
635
635
  }
636
636
  const isArray = (
637
637
  // typed-JSON fields should be translated to scalar Json type
638
- isTypeDef(field.type.reference?.ref) ? false : field.type.array
638
+ isTypeDef2(field.type.reference?.ref) ? false : field.type.array
639
639
  );
640
640
  const type = new ModelFieldType(fieldType, isArray, field.type.optional);
641
641
  const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithPluginInvocation(attr)).filter((attr) => (
@@ -778,7 +778,7 @@ var PrismaSchemaGenerator = class {
778
778
 
779
779
  // src/ts-schema-generator.ts
780
780
  import { invariant } from "@zenstackhq/common-helpers";
781
- import { isArrayExpr as isArrayExpr2, isBinaryExpr, isDataField, isDataModel as isDataModel3, isDataSource, isEnum, isEnumField, isInvocationExpr as isInvocationExpr2, isLiteralExpr as isLiteralExpr3, isMemberAccessExpr, isNullExpr as isNullExpr2, isProcedure, isReferenceExpr as isReferenceExpr2, isThisExpr, isTypeDef as isTypeDef2, isUnaryExpr } from "@zenstackhq/language/ast";
781
+ import { isArrayExpr as isArrayExpr2, isBinaryExpr, isDataField, isDataModel as isDataModel3, isDataSource, isEnum, isEnumField, isInvocationExpr as isInvocationExpr2, isLiteralExpr as isLiteralExpr3, isMemberAccessExpr, isNullExpr as isNullExpr2, isProcedure, isReferenceExpr as isReferenceExpr2, isThisExpr, isTypeDef as isTypeDef3, isUnaryExpr } from "@zenstackhq/language/ast";
782
782
  import { getAllAttributes as getAllAttributes2, getAllFields as getAllFields3, isDataFieldReference } from "@zenstackhq/language/utils";
783
783
  import fs from "fs";
784
784
  import path from "path";
@@ -788,10 +788,12 @@ var TsSchemaGenerator = class {
788
788
  static {
789
789
  __name(this, "TsSchemaGenerator");
790
790
  }
791
+ usedExpressionUtils = false;
791
792
  async generate(model, outputDir) {
792
793
  fs.mkdirSync(outputDir, {
793
794
  recursive: true
794
795
  });
796
+ this.usedExpressionUtils = false;
795
797
  this.generateSchema(model, outputDir);
796
798
  this.generateModelsAndTypeDefs(model, outputDir);
797
799
  this.generateInputTypes(model, outputDir);
@@ -808,18 +810,21 @@ var TsSchemaGenerator = class {
808
810
  }
809
811
  generateSchemaStatements(model, statements) {
810
812
  const hasComputedFields = model.declarations.some((d) => isDataModel3(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
813
+ const schemaObject = this.createSchemaObject(model);
811
814
  const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
812
815
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
813
816
  ...hasComputedFields ? [
814
817
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("OperandExpression"))
815
818
  ] : [],
816
- ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
819
+ ...this.usedExpressionUtils ? [
820
+ ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
821
+ ] : []
817
822
  ])), ts.factory.createStringLiteral("@zenstackhq/runtime/schema"));
818
823
  statements.push(runtimeImportDecl);
819
824
  const declaration = ts.factory.createVariableStatement([
820
825
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
821
826
  ], ts.factory.createVariableDeclarationList([
822
- ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(this.createSchemaObject(model), ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
827
+ ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
823
828
  ], ts.NodeFlags.Const));
824
829
  statements.push(declaration);
825
830
  const typeDeclaration = ts.factory.createTypeAliasDeclaration([
@@ -827,6 +832,10 @@ var TsSchemaGenerator = class {
827
832
  ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
828
833
  statements.push(typeDeclaration);
829
834
  }
835
+ createExpressionUtilsCall(method, args) {
836
+ this.usedExpressionUtils = true;
837
+ return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
838
+ }
830
839
  createSchemaObject(model) {
831
840
  const properties = [
832
841
  // provider
@@ -834,7 +843,7 @@ var TsSchemaGenerator = class {
834
843
  // models
835
844
  ts.factory.createPropertyAssignment("models", this.createModelsObject(model)),
836
845
  // typeDefs
837
- ...model.declarations.some(isTypeDef2) ? [
846
+ ...model.declarations.some(isTypeDef3) ? [
838
847
  ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model))
839
848
  ] : []
840
849
  ];
@@ -863,7 +872,7 @@ var TsSchemaGenerator = class {
863
872
  return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isDataModel3(d) && !hasAttribute(d, "@@ignore")).map((dm) => ts.factory.createPropertyAssignment(dm.name, this.createDataModelObject(dm))), true);
864
873
  }
865
874
  createTypeDefsObject(model) {
866
- return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isTypeDef2(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
875
+ return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => isTypeDef3(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
867
876
  }
868
877
  createDataModelObject(dm) {
869
878
  const allFields = getAllFields3(dm);
@@ -898,6 +907,9 @@ var TsSchemaGenerator = class {
898
907
  // subModels
899
908
  ...subModels.length > 0 ? [
900
909
  ts.factory.createPropertyAssignment("subModels", ts.factory.createArrayLiteralExpression(subModels.map((subModel) => ts.factory.createStringLiteral(subModel))))
910
+ ] : [],
911
+ ...dm.isView ? [
912
+ ts.factory.createPropertyAssignment("isView", ts.factory.createTrue())
901
913
  ] : []
902
914
  ];
903
915
  const computedFields = dm.fields.filter((f) => hasAttribute(f, "@computed"));
@@ -926,9 +938,9 @@ var TsSchemaGenerator = class {
926
938
  }
927
939
  createComputedFieldsObject(fields) {
928
940
  return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [
929
- // parameter: `context: { currentModel: string }`
941
+ // parameter: `context: { modelAlias: string }`
930
942
  ts.factory.createParameterDeclaration(void 0, void 0, "_context", void 0, ts.factory.createTypeLiteralNode([
931
- ts.factory.createPropertySignature(void 0, "currentModel", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
943
+ ts.factory.createPropertySignature(void 0, "modelAlias", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
932
944
  ]), void 0)
933
945
  ], ts.factory.createTypeReferenceNode("OperandExpression", [
934
946
  ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type))
@@ -984,15 +996,15 @@ var TsSchemaGenerator = class {
984
996
  if (defaultValue !== void 0) {
985
997
  if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
986
998
  if ("call" in defaultValue) {
987
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
999
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
988
1000
  ts.factory.createStringLiteral(defaultValue.call),
989
1001
  ...defaultValue.args.length > 0 ? [
990
1002
  ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
991
1003
  ] : []
992
1004
  ])));
993
1005
  } else if ("authMember" in defaultValue) {
994
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, [
995
- ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1006
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
1007
+ this.createExpressionUtilsCall("call", [
996
1008
  ts.factory.createStringLiteral("auth")
997
1009
  ]),
998
1010
  ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
@@ -1306,7 +1318,7 @@ var TsSchemaGenerator = class {
1306
1318
  });
1307
1319
  }
1308
1320
  createThisExpression() {
1309
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._this"), void 0, []);
1321
+ return this.createExpressionUtilsCall("_this");
1310
1322
  }
1311
1323
  createMemberExpression(expr) {
1312
1324
  const members = [];
@@ -1320,32 +1332,32 @@ var TsSchemaGenerator = class {
1320
1332
  this.createExpression(receiver),
1321
1333
  ts.factory.createArrayLiteralExpression(members.map((m) => ts.factory.createStringLiteral(m)))
1322
1334
  ];
1323
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, args);
1335
+ return this.createExpressionUtilsCall("member", args);
1324
1336
  }
1325
1337
  createNullExpression() {
1326
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._null"), void 0, []);
1338
+ return this.createExpressionUtilsCall("_null");
1327
1339
  }
1328
1340
  createBinaryExpression(expr) {
1329
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.binary"), void 0, [
1341
+ return this.createExpressionUtilsCall("binary", [
1330
1342
  this.createExpression(expr.left),
1331
1343
  this.createLiteralNode(expr.operator),
1332
1344
  this.createExpression(expr.right)
1333
1345
  ]);
1334
1346
  }
1335
1347
  createUnaryExpression(expr) {
1336
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.unary"), void 0, [
1348
+ return this.createExpressionUtilsCall("unary", [
1337
1349
  this.createLiteralNode(expr.operator),
1338
1350
  this.createExpression(expr.operand)
1339
1351
  ]);
1340
1352
  }
1341
1353
  createArrayExpression(expr) {
1342
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.array"), void 0, [
1354
+ return this.createExpressionUtilsCall("array", [
1343
1355
  ts.factory.createArrayLiteralExpression(expr.items.map((item) => this.createExpression(item)))
1344
1356
  ]);
1345
1357
  }
1346
1358
  createRefExpression(expr) {
1347
1359
  if (isDataField(expr.target.ref)) {
1348
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.field"), void 0, [
1360
+ return this.createExpressionUtilsCall("field", [
1349
1361
  this.createLiteralNode(expr.target.$refText)
1350
1362
  ]);
1351
1363
  } else if (isEnumField(expr.target.ref)) {
@@ -1355,7 +1367,7 @@ var TsSchemaGenerator = class {
1355
1367
  }
1356
1368
  }
1357
1369
  createCallExpression(expr) {
1358
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1370
+ return this.createExpressionUtilsCall("call", [
1359
1371
  ts.factory.createStringLiteral(expr.function.$refText),
1360
1372
  ...expr.args.length > 0 ? [
1361
1373
  ts.factory.createArrayLiteralExpression(expr.args.map((arg) => this.createExpression(arg.value)))
@@ -1363,11 +1375,11 @@ var TsSchemaGenerator = class {
1363
1375
  ]);
1364
1376
  }
1365
1377
  createLiteralExpression(type, value) {
1366
- return match2(type).with("BooleanLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1378
+ return match2(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1367
1379
  this.createLiteralNode(value)
1368
- ])).with("NumberLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1380
+ ])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
1369
1381
  ts.factory.createIdentifier(value)
1370
- ])).with("StringLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1382
+ ])).with("StringLiteral", () => this.createExpressionUtilsCall("literal", [
1371
1383
  this.createLiteralNode(value)
1372
1384
  ])).otherwise(() => {
1373
1385
  throw new Error(`Unsupported literal type: ${type}`);
@@ -1378,7 +1390,7 @@ var TsSchemaGenerator = class {
1378
1390
  statements.push(this.generateSchemaImport(model, true, true));
1379
1391
  statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
1380
1392
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
1381
- ...model.declarations.some(isTypeDef2) ? [
1393
+ ...model.declarations.some(isTypeDef3) ? [
1382
1394
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`TypeDefResult as $TypeDefResult`))
1383
1395
  ] : []
1384
1396
  ])), ts.factory.createStringLiteral("@zenstackhq/runtime")));
@@ -1395,7 +1407,7 @@ var TsSchemaGenerator = class {
1395
1407
  }
1396
1408
  statements.push(modelType);
1397
1409
  }
1398
- const typeDefs = model.declarations.filter(isTypeDef2);
1410
+ const typeDefs = model.declarations.filter(isTypeDef3);
1399
1411
  for (const td of typeDefs) {
1400
1412
  let typeDef = ts.factory.createTypeAliasDeclaration([
1401
1413
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)