@zenstackhq/sdk 3.0.0-alpha.3 → 3.0.0-alpha.6

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
@@ -318,9 +318,9 @@ var Model = class extends ContainerDeclaration {
318
318
  }
319
319
  name;
320
320
  isView;
321
- fields;
321
+ fields = [];
322
322
  constructor(name, isView, documentations = []) {
323
- super(documentations), this.name = name, this.isView = isView, this.fields = [];
323
+ super(documentations), this.name = name, this.isView = isView;
324
324
  }
325
325
  addField(name, type, attributes = [], documentations = [], addToFront = false) {
326
326
  const field = new ModelField(name, type, attributes, documentations);
@@ -540,9 +540,9 @@ var Enum = class extends ContainerDeclaration {
540
540
  __name(this, "Enum");
541
541
  }
542
542
  name;
543
- fields;
543
+ fields = [];
544
544
  constructor(name, documentations = []) {
545
- super(documentations), this.name = name, this.fields = [];
545
+ super(documentations), this.name = name;
546
546
  }
547
547
  addField(name, attributes = [], documentations = []) {
548
548
  const field = new EnumField(name, attributes, documentations);
@@ -591,15 +591,14 @@ var PrismaSchemaGenerator = class {
591
591
  __name(this, "PrismaSchemaGenerator");
592
592
  }
593
593
  zmodel;
594
- PRELUDE;
595
- constructor(zmodel) {
596
- this.zmodel = zmodel;
597
- this.PRELUDE = `//////////////////////////////////////////////////////////////////////////////////////////////
594
+ PRELUDE = `//////////////////////////////////////////////////////////////////////////////////////////////
598
595
  // DO NOT MODIFY THIS FILE //
599
596
  // This file is automatically generated by ZenStack CLI and should not be manually updated. //
600
597
  //////////////////////////////////////////////////////////////////////////////////////////////
601
598
 
602
599
  `;
600
+ constructor(zmodel) {
601
+ this.zmodel = zmodel;
603
602
  }
604
603
  async generate() {
605
604
  const prisma = new PrismaModel();
@@ -810,11 +809,11 @@ var PrismaSchemaGenerator = class {
810
809
  };
811
810
 
812
811
  // src/ts-schema-generator.ts
812
+ var import_common_helpers = require("@zenstackhq/common-helpers");
813
813
  var import_language = require("@zenstackhq/language");
814
814
  var import_ast3 = require("@zenstackhq/language/ast");
815
815
  var import_node_fs = __toESM(require("fs"), 1);
816
816
  var import_node_path = __toESM(require("path"), 1);
817
- var import_tiny_invariant = __toESM(require("tiny-invariant"), 1);
818
817
  var import_ts_pattern2 = require("ts-pattern");
819
818
  var ts = __toESM(require("typescript"), 1);
820
819
  var TsSchemaGenerator = class {
@@ -915,15 +914,22 @@ var TsSchemaGenerator = class {
915
914
  }
916
915
  createComputedFieldsObject(fields) {
917
916
  return ts.factory.createObjectLiteralExpression(fields.map((field) => ts.factory.createMethodDeclaration(void 0, void 0, field.name, void 0, void 0, [], ts.factory.createTypeReferenceNode("OperandExpression", [
918
- ts.factory.createKeywordTypeNode(this.mapTypeToTSSyntaxKeyword(field.type.type))
917
+ ts.factory.createTypeReferenceNode(this.mapFieldTypeToTSType(field.type))
919
918
  ]), ts.factory.createBlock([
920
919
  ts.factory.createThrowStatement(ts.factory.createNewExpression(ts.factory.createIdentifier("Error"), void 0, [
921
920
  ts.factory.createStringLiteral("This is a stub for computed field")
922
921
  ]))
923
922
  ], true))), true);
924
923
  }
925
- mapTypeToTSSyntaxKeyword(type) {
926
- return (0, import_ts_pattern2.match)(type).with("String", () => ts.SyntaxKind.StringKeyword).with("Boolean", () => ts.SyntaxKind.BooleanKeyword).with("Int", () => ts.SyntaxKind.NumberKeyword).with("Float", () => ts.SyntaxKind.NumberKeyword).with("BigInt", () => ts.SyntaxKind.BigIntKeyword).with("Decimal", () => ts.SyntaxKind.NumberKeyword).otherwise(() => ts.SyntaxKind.UnknownKeyword);
924
+ mapFieldTypeToTSType(type) {
925
+ let result = (0, import_ts_pattern2.match)(type.type).with("String", () => "string").with("Boolean", () => "boolean").with("Int", () => "number").with("Float", () => "number").with("BigInt", () => "bigint").with("Decimal", () => "number").otherwise(() => "unknown");
926
+ if (type.array) {
927
+ result = `${result}[]`;
928
+ }
929
+ if (type.optional) {
930
+ result = `${result} | null`;
931
+ }
932
+ return result;
927
933
  }
928
934
  createDataModelFieldObject(field) {
929
935
  const objectFields = [
@@ -985,12 +991,12 @@ var TsSchemaGenerator = class {
985
991
  }
986
992
  getDataSourceProvider(model) {
987
993
  const dataSource = model.declarations.find(import_ast3.isDataSource);
988
- (0, import_tiny_invariant.default)(dataSource, "No data source found in the model");
994
+ (0, import_common_helpers.invariant)(dataSource, "No data source found in the model");
989
995
  const providerExpr = dataSource.fields.find((f) => f.name === "provider")?.value;
990
- (0, import_tiny_invariant.default)((0, import_ast3.isLiteralExpr)(providerExpr), "Provider must be a literal");
996
+ (0, import_common_helpers.invariant)((0, import_ast3.isLiteralExpr)(providerExpr), "Provider must be a literal");
991
997
  const type = providerExpr.value;
992
998
  const urlExpr = dataSource.fields.find((f) => f.name === "url")?.value;
993
- (0, import_tiny_invariant.default)((0, import_ast3.isLiteralExpr)(urlExpr) || (0, import_ast3.isInvocationExpr)(urlExpr), "URL must be a literal or env function");
999
+ (0, import_common_helpers.invariant)((0, import_ast3.isLiteralExpr)(urlExpr) || (0, import_ast3.isInvocationExpr)(urlExpr), "URL must be a literal or env function");
994
1000
  if ((0, import_ast3.isLiteralExpr)(urlExpr)) {
995
1001
  return {
996
1002
  type,
@@ -998,8 +1004,8 @@ var TsSchemaGenerator = class {
998
1004
  env: void 0
999
1005
  };
1000
1006
  } else if ((0, import_ast3.isInvocationExpr)(urlExpr)) {
1001
- (0, import_tiny_invariant.default)(urlExpr.function.$refText === "env", 'only "env" function is supported');
1002
- (0, import_tiny_invariant.default)(urlExpr.args.length === 1, "env function must have one argument");
1007
+ (0, import_common_helpers.invariant)(urlExpr.function.$refText === "env", 'only "env" function is supported');
1008
+ (0, import_common_helpers.invariant)(urlExpr.args.length === 1, "env function must have one argument");
1003
1009
  return {
1004
1010
  type,
1005
1011
  env: urlExpr.args[0].value.value,
@@ -1015,7 +1021,7 @@ var TsSchemaGenerator = class {
1015
1021
  return void 0;
1016
1022
  }
1017
1023
  const defaultValue = defaultAttr.args[0]?.value;
1018
- (0, import_tiny_invariant.default)(defaultValue, "Expected a default value");
1024
+ (0, import_common_helpers.invariant)(defaultValue, "Expected a default value");
1019
1025
  if ((0, import_ast3.isLiteralExpr)(defaultValue)) {
1020
1026
  const lit = defaultValue.value;
1021
1027
  return field.type.type === "Boolean" ? lit : [
@@ -1135,7 +1141,7 @@ var TsSchemaGenerator = class {
1135
1141
  if (relation) {
1136
1142
  const nameArg = relation.args.find((arg) => arg.$resolvedParam.name === "name");
1137
1143
  if (nameArg) {
1138
- (0, import_tiny_invariant.default)((0, import_ast3.isLiteralExpr)(nameArg.value), "name must be a literal");
1144
+ (0, import_common_helpers.invariant)((0, import_ast3.isLiteralExpr)(nameArg.value), "name must be a literal");
1139
1145
  return nameArg.value.value;
1140
1146
  }
1141
1147
  }
@@ -1230,6 +1236,8 @@ var TsSchemaGenerator = class {
1230
1236
  // This file is automatically generated by ZenStack CLI and should not be manually updated. //
1231
1237
  //////////////////////////////////////////////////////////////////////////////////////////////
1232
1238
 
1239
+ /* eslint-disable */
1240
+
1233
1241
  `;
1234
1242
  ts.addSyntheticLeadingComment(statements[0], ts.SyntaxKind.SingleLineCommentTrivia, banner);
1235
1243
  }