@zenstackhq/sdk 3.0.0-beta.1 → 3.0.0-beta.10

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
@@ -121,7 +121,7 @@ function resolved(ref) {
121
121
  }
122
122
  __name(resolved, "resolved");
123
123
  function getAuthDecl(model) {
124
- let found = model.declarations.find((d) => (0, import_ast.isDataModel)(d) && d.attributes.some((attr) => attr.decl.$refText === "@@auth"));
124
+ 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
125
  if (!found) {
126
126
  found = model.declarations.find((d) => (0, import_ast.isDataModel)(d) && d.name === "User");
127
127
  }
@@ -619,13 +619,20 @@ var PrismaSchemaGenerator = class {
619
619
  }
620
620
  }
621
621
  const allAttributes = (0, import_utils2.getAllAttributes)(decl);
622
- for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
622
+ for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2) && !this.isInheritedMapAttribute(attr2, decl))) {
623
623
  this.generateContainerAttribute(model, attr);
624
624
  }
625
625
  decl.comments.forEach((c) => model.addComment(c));
626
626
  this.generateDelegateRelationForBase(model, decl);
627
627
  this.generateDelegateRelationForConcrete(model, decl);
628
628
  }
629
+ isInheritedMapAttribute(attr, contextModel) {
630
+ if (attr.$container === contextModel) {
631
+ return false;
632
+ }
633
+ const attrName = attr.decl.ref?.name ?? attr.decl.$refText;
634
+ return attrName === "@@map";
635
+ }
629
636
  isPrismaAttribute(attr) {
630
637
  if (!attr.decl.ref) {
631
638
  return false;
@@ -671,7 +678,7 @@ var PrismaSchemaGenerator = class {
671
678
  (0, import_ast2.isTypeDef)(field.type.reference?.ref) ? false : field.type.array
672
679
  );
673
680
  const type = new ModelFieldType(fieldType, isArray, field.type.optional);
674
- const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithPluginInvocation(attr)).filter((attr) => (
681
+ const attributes = field.attributes.filter((attr) => this.isPrismaAttribute(attr)).filter((attr) => !this.isDefaultWithAuthInvocation(attr)).filter((attr) => (
675
682
  // when building physical schema, exclude `@default` for id fields inherited from delegate base
676
683
  !(model_utils_exports.isIdField(field, contextModel) && this.isInheritedFromDelegate(field, contextModel) && attr.decl.$refText === "@default")
677
684
  )).map((attr) => this.makeFieldAttribute(attr));
@@ -681,7 +688,7 @@ var PrismaSchemaGenerator = class {
681
688
  const result = model.addField(field.name, type, attributes, docs, addToFront);
682
689
  return result;
683
690
  }
684
- isDefaultWithPluginInvocation(attr) {
691
+ isDefaultWithAuthInvocation(attr) {
685
692
  if (attr.decl.ref?.name !== "@default") {
686
693
  return false;
687
694
  }
@@ -689,11 +696,7 @@ var PrismaSchemaGenerator = class {
689
696
  if (!expr) {
690
697
  return false;
691
698
  }
692
- return import_langium.AstUtils.streamAst(expr).some((node) => (0, import_ast2.isInvocationExpr)(node) && this.isFromPlugin(node.function.ref));
693
- }
694
- isFromPlugin(node) {
695
- const model = import_langium.AstUtils.getContainerOfType(node, import_ast2.isModel);
696
- return !!model && !!model.$document && model.$document.uri.path.endsWith("plugin.zmodel");
699
+ return import_langium.AstUtils.streamAst(expr).some(import_utils2.isAuthInvocation);
697
700
  }
698
701
  isInheritedFromDelegate(field, contextModel) {
699
702
  return field.$container !== contextModel && model_utils_exports.isDelegateModel(field.$container);
@@ -821,10 +824,12 @@ var TsSchemaGenerator = class {
821
824
  static {
822
825
  __name(this, "TsSchemaGenerator");
823
826
  }
827
+ usedExpressionUtils = false;
824
828
  async generate(model, outputDir) {
825
829
  import_node_fs.default.mkdirSync(outputDir, {
826
830
  recursive: true
827
831
  });
832
+ this.usedExpressionUtils = false;
828
833
  this.generateSchema(model, outputDir);
829
834
  this.generateModelsAndTypeDefs(model, outputDir);
830
835
  this.generateInputTypes(model, outputDir);
@@ -841,18 +846,21 @@ var TsSchemaGenerator = class {
841
846
  }
842
847
  generateSchemaStatements(model, statements) {
843
848
  const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
849
+ const schemaObject = this.createSchemaObject(model);
844
850
  const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
845
851
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
846
852
  ...hasComputedFields ? [
847
853
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("OperandExpression"))
848
854
  ] : [],
849
- ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
855
+ ...this.usedExpressionUtils ? [
856
+ ts.factory.createImportSpecifier(false, void 0, ts.factory.createIdentifier("ExpressionUtils"))
857
+ ] : []
850
858
  ])), ts.factory.createStringLiteral("@zenstackhq/runtime/schema"));
851
859
  statements.push(runtimeImportDecl);
852
860
  const declaration = ts.factory.createVariableStatement([
853
861
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
854
862
  ], ts.factory.createVariableDeclarationList([
855
- 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")))
863
+ ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
856
864
  ], ts.NodeFlags.Const));
857
865
  statements.push(declaration);
858
866
  const typeDeclaration = ts.factory.createTypeAliasDeclaration([
@@ -860,6 +868,10 @@ var TsSchemaGenerator = class {
860
868
  ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
861
869
  statements.push(typeDeclaration);
862
870
  }
871
+ createExpressionUtilsCall(method, args) {
872
+ this.usedExpressionUtils = true;
873
+ return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
874
+ }
863
875
  createSchemaObject(model) {
864
876
  const properties = [
865
877
  // provider
@@ -931,6 +943,9 @@ var TsSchemaGenerator = class {
931
943
  // subModels
932
944
  ...subModels.length > 0 ? [
933
945
  ts.factory.createPropertyAssignment("subModels", ts.factory.createArrayLiteralExpression(subModels.map((subModel) => ts.factory.createStringLiteral(subModel))))
946
+ ] : [],
947
+ ...dm.isView ? [
948
+ ts.factory.createPropertyAssignment("isView", ts.factory.createTrue())
934
949
  ] : []
935
950
  ];
936
951
  const computedFields = dm.fields.filter((f) => hasAttribute(f, "@computed"));
@@ -959,9 +974,9 @@ var TsSchemaGenerator = class {
959
974
  }
960
975
  createComputedFieldsObject(fields) {
961
976
  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 }`
977
+ // parameter: `context: { modelAlias: string }`
963
978
  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))
979
+ ts.factory.createPropertySignature(void 0, "modelAlias", void 0, ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword))
965
980
  ]), void 0)
966
981
  ], ts.factory.createTypeReferenceNode("OperandExpression", [
967
982
  ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type))
@@ -1017,15 +1032,17 @@ var TsSchemaGenerator = class {
1017
1032
  if (defaultValue !== void 0) {
1018
1033
  if (typeof defaultValue === "object" && !Array.isArray(defaultValue)) {
1019
1034
  if ("call" in defaultValue) {
1020
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1035
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("call", [
1021
1036
  ts.factory.createStringLiteral(defaultValue.call),
1022
1037
  ...defaultValue.args.length > 0 ? [
1023
- ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createLiteralNode(arg)))
1038
+ ts.factory.createArrayLiteralExpression(defaultValue.args.map((arg) => this.createExpressionUtilsCall("literal", [
1039
+ this.createLiteralNode(arg)
1040
+ ])))
1024
1041
  ] : []
1025
1042
  ])));
1026
1043
  } else if ("authMember" in defaultValue) {
1027
- objectFields.push(ts.factory.createPropertyAssignment("default", ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, [
1028
- ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1044
+ objectFields.push(ts.factory.createPropertyAssignment("default", this.createExpressionUtilsCall("member", [
1045
+ this.createExpressionUtilsCall("call", [
1029
1046
  ts.factory.createStringLiteral("auth")
1030
1047
  ]),
1031
1048
  ts.factory.createArrayLiteralExpression(defaultValue.authMember.map((m) => ts.factory.createStringLiteral(m)))
@@ -1339,7 +1356,7 @@ var TsSchemaGenerator = class {
1339
1356
  });
1340
1357
  }
1341
1358
  createThisExpression() {
1342
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._this"), void 0, []);
1359
+ return this.createExpressionUtilsCall("_this");
1343
1360
  }
1344
1361
  createMemberExpression(expr) {
1345
1362
  const members = [];
@@ -1353,32 +1370,32 @@ var TsSchemaGenerator = class {
1353
1370
  this.createExpression(receiver),
1354
1371
  ts.factory.createArrayLiteralExpression(members.map((m) => ts.factory.createStringLiteral(m)))
1355
1372
  ];
1356
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.member"), void 0, args);
1373
+ return this.createExpressionUtilsCall("member", args);
1357
1374
  }
1358
1375
  createNullExpression() {
1359
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils._null"), void 0, []);
1376
+ return this.createExpressionUtilsCall("_null");
1360
1377
  }
1361
1378
  createBinaryExpression(expr) {
1362
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.binary"), void 0, [
1379
+ return this.createExpressionUtilsCall("binary", [
1363
1380
  this.createExpression(expr.left),
1364
1381
  this.createLiteralNode(expr.operator),
1365
1382
  this.createExpression(expr.right)
1366
1383
  ]);
1367
1384
  }
1368
1385
  createUnaryExpression(expr) {
1369
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.unary"), void 0, [
1386
+ return this.createExpressionUtilsCall("unary", [
1370
1387
  this.createLiteralNode(expr.operator),
1371
1388
  this.createExpression(expr.operand)
1372
1389
  ]);
1373
1390
  }
1374
1391
  createArrayExpression(expr) {
1375
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.array"), void 0, [
1392
+ return this.createExpressionUtilsCall("array", [
1376
1393
  ts.factory.createArrayLiteralExpression(expr.items.map((item) => this.createExpression(item)))
1377
1394
  ]);
1378
1395
  }
1379
1396
  createRefExpression(expr) {
1380
1397
  if ((0, import_ast3.isDataField)(expr.target.ref)) {
1381
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.field"), void 0, [
1398
+ return this.createExpressionUtilsCall("field", [
1382
1399
  this.createLiteralNode(expr.target.$refText)
1383
1400
  ]);
1384
1401
  } else if ((0, import_ast3.isEnumField)(expr.target.ref)) {
@@ -1388,7 +1405,7 @@ var TsSchemaGenerator = class {
1388
1405
  }
1389
1406
  }
1390
1407
  createCallExpression(expr) {
1391
- return ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.call"), void 0, [
1408
+ return this.createExpressionUtilsCall("call", [
1392
1409
  ts.factory.createStringLiteral(expr.function.$refText),
1393
1410
  ...expr.args.length > 0 ? [
1394
1411
  ts.factory.createArrayLiteralExpression(expr.args.map((arg) => this.createExpression(arg.value)))
@@ -1396,11 +1413,11 @@ var TsSchemaGenerator = class {
1396
1413
  ]);
1397
1414
  }
1398
1415
  createLiteralExpression(type, value) {
1399
- return (0, import_ts_pattern2.match)(type).with("BooleanLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1416
+ return (0, import_ts_pattern2.match)(type).with("BooleanLiteral", () => this.createExpressionUtilsCall("literal", [
1400
1417
  this.createLiteralNode(value)
1401
- ])).with("NumberLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1418
+ ])).with("NumberLiteral", () => this.createExpressionUtilsCall("literal", [
1402
1419
  ts.factory.createIdentifier(value)
1403
- ])).with("StringLiteral", () => ts.factory.createCallExpression(ts.factory.createIdentifier("ExpressionUtils.literal"), void 0, [
1420
+ ])).with("StringLiteral", () => this.createExpressionUtilsCall("literal", [
1404
1421
  this.createLiteralNode(value)
1405
1422
  ])).otherwise(() => {
1406
1423
  throw new Error(`Unsupported literal type: ${type}`);