@zenstackhq/sdk 3.0.0-beta.15 → 3.0.0-beta.17

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
@@ -825,28 +825,43 @@ var TsSchemaGenerator = class {
825
825
  __name(this, "TsSchemaGenerator");
826
826
  }
827
827
  usedExpressionUtils = false;
828
- async generate(model, outputDir) {
829
- import_node_fs.default.mkdirSync(outputDir, {
828
+ async generate(model, options) {
829
+ import_node_fs.default.mkdirSync(options.outDir, {
830
830
  recursive: true
831
831
  });
832
832
  this.usedExpressionUtils = false;
833
- this.generateSchema(model, outputDir);
834
- this.generateModelsAndTypeDefs(model, outputDir);
835
- this.generateInputTypes(model, outputDir);
836
- }
837
- generateSchema(model, outputDir) {
838
- const statements = [];
839
- this.generateSchemaStatements(model, statements);
840
- this.generateBannerComments(statements);
841
- const schemaOutputFile = import_node_path.default.join(outputDir, "schema.ts");
842
- const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
843
- const printer = ts.createPrinter();
844
- const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
845
- import_node_fs.default.writeFileSync(schemaOutputFile, result);
833
+ this.generateSchema(model, options);
834
+ this.generateModelsAndTypeDefs(model, options);
835
+ this.generateInputTypes(model, options);
836
+ }
837
+ generateSchema(model, options) {
838
+ const targets = [];
839
+ if (!options.liteOnly) {
840
+ targets.push({
841
+ lite: false,
842
+ file: "schema.ts"
843
+ });
844
+ }
845
+ if (options.lite || options.liteOnly) {
846
+ targets.push({
847
+ lite: true,
848
+ file: "schema-lite.ts"
849
+ });
850
+ }
851
+ for (const { lite, file } of targets) {
852
+ const statements = [];
853
+ this.generateSchemaStatements(model, statements, lite);
854
+ this.generateBannerComments(statements);
855
+ const schemaOutputFile = import_node_path.default.join(options.outDir, file);
856
+ const sourceFile = ts.createSourceFile(schemaOutputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
857
+ const printer = ts.createPrinter();
858
+ const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
859
+ import_node_fs.default.writeFileSync(schemaOutputFile, result);
860
+ }
846
861
  }
847
- generateSchemaStatements(model, statements) {
862
+ generateSchemaStatements(model, statements, lite) {
848
863
  const hasComputedFields = model.declarations.some((d) => (0, import_ast3.isDataModel)(d) && d.fields.some((f) => hasAttribute(f, "@computed")));
849
- const schemaObject = this.createSchemaObject(model);
864
+ const schemaObject = this.createSchemaObject(model, lite);
850
865
  const runtimeImportDecl = ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
851
866
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier("SchemaDef")),
852
867
  ...hasComputedFields ? [
@@ -872,15 +887,15 @@ var TsSchemaGenerator = class {
872
887
  this.usedExpressionUtils = true;
873
888
  return ts.factory.createCallExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("ExpressionUtils"), method), void 0, args || []);
874
889
  }
875
- createSchemaObject(model) {
890
+ createSchemaObject(model, lite) {
876
891
  const properties = [
877
892
  // provider
878
893
  ts.factory.createPropertyAssignment("provider", this.createProviderObject(model)),
879
894
  // models
880
- ts.factory.createPropertyAssignment("models", this.createModelsObject(model)),
895
+ ts.factory.createPropertyAssignment("models", this.createModelsObject(model, lite)),
881
896
  // typeDefs
882
897
  ...model.declarations.some(import_ast3.isTypeDef) ? [
883
- ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model))
898
+ ts.factory.createPropertyAssignment("typeDefs", this.createTypeDefsObject(model, lite))
884
899
  ] : []
885
900
  ];
886
901
  const enums = model.declarations.filter(import_ast3.isEnum);
@@ -904,15 +919,15 @@ var TsSchemaGenerator = class {
904
919
  ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(dsProvider.type))
905
920
  ], true);
906
921
  }
907
- createModelsObject(model) {
908
- 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);
922
+ createModelsObject(model, lite) {
923
+ 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, lite))), true);
909
924
  }
910
- createTypeDefsObject(model) {
911
- return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => (0, import_ast3.isTypeDef)(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td))), true);
925
+ createTypeDefsObject(model, lite) {
926
+ return ts.factory.createObjectLiteralExpression(model.declarations.filter((d) => (0, import_ast3.isTypeDef)(d)).map((td) => ts.factory.createPropertyAssignment(td.name, this.createTypeDefObject(td, lite))), true);
912
927
  }
913
- createDataModelObject(dm) {
928
+ createDataModelObject(dm, lite) {
914
929
  const allFields = (0, import_utils3.getAllFields)(dm);
915
- const allAttributes = (0, import_utils3.getAllAttributes)(dm).filter((attr) => {
930
+ const allAttributes = lite ? [] : (0, import_utils3.getAllAttributes)(dm).filter((attr) => {
916
931
  if (attr.decl.$refText === "@@delegate" && attr.$container !== dm) {
917
932
  return false;
918
933
  }
@@ -927,7 +942,7 @@ var TsSchemaGenerator = class {
927
942
  ts.factory.createPropertyAssignment("baseModel", ts.factory.createStringLiteral(dm.baseModel.$refText))
928
943
  ] : [],
929
944
  // fields
930
- ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm))), true)),
945
+ ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, dm, lite))), true)),
931
946
  // attributes
932
947
  ...allAttributes.length > 0 ? [
933
948
  ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
@@ -957,14 +972,14 @@ var TsSchemaGenerator = class {
957
972
  getSubModels(dm) {
958
973
  return dm.$container.declarations.filter(import_ast3.isDataModel).filter((d) => d.baseModel?.ref === dm).map((d) => d.name);
959
974
  }
960
- createTypeDefObject(td) {
975
+ createTypeDefObject(td, lite) {
961
976
  const allFields = (0, import_utils3.getAllFields)(td);
962
977
  const allAttributes = (0, import_utils3.getAllAttributes)(td);
963
978
  const fields = [
964
979
  // name
965
980
  ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(td.name)),
966
981
  // fields
967
- ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0))), true)),
982
+ ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(allFields.map((field) => ts.factory.createPropertyAssignment(field.name, this.createDataFieldObject(field, void 0, lite))), true)),
968
983
  // attributes
969
984
  ...allAttributes.length > 0 ? [
970
985
  ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(allAttributes.map((attr) => this.createAttributeObject(attr)), true))
@@ -996,7 +1011,7 @@ var TsSchemaGenerator = class {
996
1011
  }
997
1012
  return result;
998
1013
  }
999
- createDataFieldObject(field, contextModel) {
1014
+ createDataFieldObject(field, contextModel, lite) {
1000
1015
  const objectFields = [
1001
1016
  // name
1002
1017
  ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
@@ -1025,7 +1040,7 @@ var TsSchemaGenerator = class {
1025
1040
  if (this.isDiscriminatorField(field)) {
1026
1041
  objectFields.push(ts.factory.createPropertyAssignment("isDiscriminator", ts.factory.createTrue()));
1027
1042
  }
1028
- if (field.attributes.length > 0) {
1043
+ if (!lite && field.attributes.length > 0) {
1029
1044
  objectFields.push(ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes.map((attr) => this.createAttributeObject(attr)))));
1030
1045
  }
1031
1046
  const defaultValue = this.getFieldMappedDefault(field);
@@ -1423,9 +1438,9 @@ var TsSchemaGenerator = class {
1423
1438
  throw new Error(`Unsupported literal type: ${type}`);
1424
1439
  });
1425
1440
  }
1426
- generateModelsAndTypeDefs(model, outputDir) {
1441
+ generateModelsAndTypeDefs(model, options) {
1427
1442
  const statements = [];
1428
- statements.push(this.generateSchemaImport(model, true, true));
1443
+ statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly)));
1429
1444
  statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
1430
1445
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
1431
1446
  ...model.declarations.some(import_ast3.isTypeDef) ? [
@@ -1478,13 +1493,13 @@ var TsSchemaGenerator = class {
1478
1493
  statements.push(typeAlias);
1479
1494
  }
1480
1495
  this.generateBannerComments(statements);
1481
- const outputFile = import_node_path.default.join(outputDir, "models.ts");
1496
+ const outputFile = import_node_path.default.join(options.outDir, "models.ts");
1482
1497
  const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
1483
1498
  const printer = ts.createPrinter();
1484
1499
  const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
1485
1500
  import_node_fs.default.writeFileSync(outputFile, result);
1486
1501
  }
1487
- generateSchemaImport(model, schemaObject, schemaType) {
1502
+ generateSchemaImport(model, schemaObject, schemaType, useLite) {
1488
1503
  const importSpecifiers = [];
1489
1504
  if (schemaObject) {
1490
1505
  if (model.declarations.some(import_ast3.isEnum)) {
@@ -1494,17 +1509,17 @@ var TsSchemaGenerator = class {
1494
1509
  if (schemaType) {
1495
1510
  importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
1496
1511
  }
1497
- return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral("./schema"));
1512
+ return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(useLite ? "./schema-lite" : "./schema"));
1498
1513
  }
1499
1514
  generateDocs(tsDecl, decl) {
1500
1515
  return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
1501
1516
  * ${decl.comments.map((c) => c.replace(/^\s*\/*\s*/, "")).join("\n * ")}
1502
1517
  `, true);
1503
1518
  }
1504
- generateInputTypes(model, outputDir) {
1519
+ generateInputTypes(model, options) {
1505
1520
  const dataModels = model.declarations.filter(import_ast3.isDataModel);
1506
1521
  const statements = [];
1507
- statements.push(this.generateSchemaImport(model, false, true));
1522
+ statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly)));
1508
1523
  const inputTypes = [
1509
1524
  "FindManyArgs",
1510
1525
  "FindUniqueArgs",
@@ -1561,7 +1576,7 @@ var TsSchemaGenerator = class {
1561
1576
  ])));
1562
1577
  }
1563
1578
  this.generateBannerComments(statements);
1564
- const outputFile = import_node_path.default.join(outputDir, "input.ts");
1579
+ const outputFile = import_node_path.default.join(options.outDir, "input.ts");
1565
1580
  const sourceFile = ts.createSourceFile(outputFile, "", ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);
1566
1581
  const printer = ts.createPrinter();
1567
1582
  const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);