@zenstackhq/sdk 3.0.0-beta.22 → 3.0.0-beta.24

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
@@ -99,6 +99,7 @@ declare class PrismaSchemaGenerator {
99
99
  private configInvocationArgToText;
100
100
  private literalToText;
101
101
  private generateGenerator;
102
+ private generateDefaultGenerator;
102
103
  private generateModel;
103
104
  private getDatasourceField;
104
105
  private datasourceHasSchemasSetting;
@@ -127,6 +128,7 @@ type TsSchemaGeneratorOptions = {
127
128
  outDir: string;
128
129
  lite?: boolean;
129
130
  liteOnly?: boolean;
131
+ importWithFileExtension?: string;
130
132
  };
131
133
  declare class TsSchemaGenerator {
132
134
  private usedExpressionUtils;
@@ -163,6 +165,7 @@ declare class TsSchemaGenerator {
163
165
  private createEnumObject;
164
166
  private getLiteral;
165
167
  private createLiteralNode;
168
+ private createNumberLiteral;
166
169
  private createProceduresObject;
167
170
  private createProcedureObject;
168
171
  private generateBannerComments;
package/dist/index.d.ts CHANGED
@@ -99,6 +99,7 @@ declare class PrismaSchemaGenerator {
99
99
  private configInvocationArgToText;
100
100
  private literalToText;
101
101
  private generateGenerator;
102
+ private generateDefaultGenerator;
102
103
  private generateModel;
103
104
  private getDatasourceField;
104
105
  private datasourceHasSchemasSetting;
@@ -127,6 +128,7 @@ type TsSchemaGeneratorOptions = {
127
128
  outDir: string;
128
129
  lite?: boolean;
129
130
  liteOnly?: boolean;
131
+ importWithFileExtension?: string;
130
132
  };
131
133
  declare class TsSchemaGenerator {
132
134
  private usedExpressionUtils;
@@ -163,6 +165,7 @@ declare class TsSchemaGenerator {
163
165
  private createEnumObject;
164
166
  private getLiteral;
165
167
  private createLiteralNode;
168
+ private createNumberLiteral;
166
169
  private createProceduresObject;
167
170
  private createProcedureObject;
168
171
  private generateBannerComments;
package/dist/index.js CHANGED
@@ -104,7 +104,7 @@ var DELEGATE_AUX_RELATION_PREFIX = "delegate_aux";
104
104
  // src/prisma/prisma-schema-generator.ts
105
105
  import { lowerCaseFirst } from "@zenstackhq/common-helpers";
106
106
  import { ZModelCodeGenerator } from "@zenstackhq/language";
107
- import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isDataSource, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef as isTypeDef2, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
107
+ import { BooleanLiteral, DataModel, DataSource as DataSource2, Enum as Enum2, GeneratorDecl, isArrayExpr, isDataModel as isDataModel2, isDataSource, isGeneratorDecl, isInvocationExpr, isLiteralExpr as isLiteralExpr2, isNullExpr, isReferenceExpr, isStringLiteral, isTypeDef as isTypeDef2, NumberLiteral, StringLiteral } from "@zenstackhq/language/ast";
108
108
  import { getAllAttributes, getAllFields as getAllFields2, getStringLiteral, isAuthInvocation, isDelegateModel as isDelegateModel2 } from "@zenstackhq/language/utils";
109
109
  import { AstUtils } from "langium";
110
110
  import { match } from "ts-pattern";
@@ -538,6 +538,9 @@ var PrismaSchemaGenerator = class {
538
538
  break;
539
539
  }
540
540
  }
541
+ if (!this.zmodel.declarations.some(isGeneratorDecl)) {
542
+ this.generateDefaultGenerator(prisma);
543
+ }
541
544
  return this.PRELUDE + prisma.toString();
542
545
  }
543
546
  generateDataSource(prisma, dataSource) {
@@ -578,6 +581,21 @@ var PrismaSchemaGenerator = class {
578
581
  text: this.configExprToText(f.value)
579
582
  })));
580
583
  }
584
+ generateDefaultGenerator(prisma) {
585
+ const gen = prisma.addGenerator("client", [
586
+ {
587
+ name: "provider",
588
+ text: '"prisma-client-js"'
589
+ }
590
+ ]);
591
+ const dataSource = this.zmodel.declarations.find(isDataSource);
592
+ if (dataSource?.fields.some((f) => f.name === "extensions")) {
593
+ gen.fields.push({
594
+ name: "previewFeatures",
595
+ text: '["postgresqlExtensions"]'
596
+ });
597
+ }
598
+ }
581
599
  generateModel(prisma, decl) {
582
600
  const model = decl.isView ? prisma.addView(decl.name) : prisma.addModel(decl.name);
583
601
  const allFields = getAllFields2(decl, true);
@@ -728,9 +746,15 @@ var PrismaSchemaGenerator = class {
728
746
  for (const field of decl.fields) {
729
747
  this.generateEnumField(_enum, field);
730
748
  }
731
- for (const attr of decl.attributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
749
+ const allAttributes = decl.attributes.filter((attr) => this.isPrismaAttribute(attr));
750
+ for (const attr of allAttributes) {
732
751
  this.generateContainerAttribute(_enum, attr);
733
752
  }
753
+ if (this.datasourceHasSchemasSetting(decl.$container) && !allAttributes.some((attr) => attr.decl.ref?.name === "@@schema")) {
754
+ _enum.addAttribute("@@schema", [
755
+ new AttributeArg(void 0, new AttributeArgValue("String", this.getDefaultPostgresSchemaName(decl.$container)))
756
+ ]);
757
+ }
734
758
  decl.comments.forEach((c) => _enum.addComment(c));
735
759
  }
736
760
  generateEnumField(_enum, field) {
@@ -856,16 +880,27 @@ var TsSchemaGenerator = class {
856
880
  ] : []
857
881
  ])), ts.factory.createStringLiteral("@zenstackhq/orm/schema"));
858
882
  statements.push(runtimeImportDecl);
859
- const declaration = ts.factory.createVariableStatement([
883
+ const _schemaDecl = ts.factory.createVariableStatement([], ts.factory.createVariableDeclarationList([
884
+ ts.factory.createVariableDeclaration("_schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
885
+ ], ts.NodeFlags.Const));
886
+ statements.push(_schemaDecl);
887
+ const brandedSchemaType = ts.factory.createTypeAliasDeclaration(void 0, "Schema", void 0, ts.factory.createIntersectionTypeNode([
888
+ ts.factory.createTypeQueryNode(ts.factory.createIdentifier("_schema")),
889
+ ts.factory.createTypeLiteralNode([
890
+ ts.factory.createPropertySignature(void 0, "__brand", ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral("schema")))
891
+ ])
892
+ ]));
893
+ statements.push(brandedSchemaType);
894
+ const schemaExportDecl = ts.factory.createVariableStatement([
860
895
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
861
896
  ], ts.factory.createVariableDeclarationList([
862
- ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
897
+ ts.factory.createVariableDeclaration("schema", void 0, ts.factory.createTypeReferenceNode("Schema"), ts.factory.createIdentifier("_schema"))
863
898
  ], ts.NodeFlags.Const));
864
- statements.push(declaration);
865
- const typeDeclaration = ts.factory.createTypeAliasDeclaration([
899
+ statements.push(schemaExportDecl);
900
+ const schemaTypeDeclaration = ts.factory.createTypeAliasDeclaration([
866
901
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
867
- ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
868
- statements.push(typeDeclaration);
902
+ ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("Schema"));
903
+ statements.push(schemaTypeDeclaration);
869
904
  }
870
905
  createExpressionUtilsCall(method, args) {
871
906
  this.usedExpressionUtils = true;
@@ -1332,7 +1367,10 @@ var TsSchemaGenerator = class {
1332
1367
  }
1333
1368
  }
1334
1369
  createLiteralNode(arg) {
1335
- 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;
1370
+ 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;
1371
+ }
1372
+ createNumberLiteral(arg) {
1373
+ return arg < 0 ? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral(-arg)) : ts.factory.createNumericLiteral(arg);
1336
1374
  }
1337
1375
  createProceduresObject(procedures) {
1338
1376
  return ts.factory.createObjectLiteralExpression(procedures.map((proc) => ts.factory.createPropertyAssignment(proc.name, this.createProcedureObject(proc))), true);
@@ -1466,7 +1504,7 @@ var TsSchemaGenerator = class {
1466
1504
  }
1467
1505
  generateModelsAndTypeDefs(model, options) {
1468
1506
  const statements = [];
1469
- statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly)));
1507
+ statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly), options.importWithFileExtension));
1470
1508
  statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
1471
1509
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
1472
1510
  ...model.declarations.some(isTypeDef3) ? [
@@ -1525,7 +1563,7 @@ var TsSchemaGenerator = class {
1525
1563
  const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
1526
1564
  fs.writeFileSync(outputFile, result);
1527
1565
  }
1528
- generateSchemaImport(model, schemaObject, schemaType, useLite) {
1566
+ generateSchemaImport(model, schemaObject, schemaType, useLite, importWithFileExtension) {
1529
1567
  const importSpecifiers = [];
1530
1568
  if (schemaObject) {
1531
1569
  if (model.declarations.some(isEnum)) {
@@ -1535,7 +1573,11 @@ var TsSchemaGenerator = class {
1535
1573
  if (schemaType) {
1536
1574
  importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
1537
1575
  }
1538
- return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(useLite ? "./schema-lite" : "./schema"));
1576
+ let importFrom = useLite ? "./schema-lite" : "./schema";
1577
+ if (importWithFileExtension) {
1578
+ importFrom += importWithFileExtension.startsWith(".") ? importWithFileExtension : `.${importWithFileExtension}`;
1579
+ }
1580
+ return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(importFrom));
1539
1581
  }
1540
1582
  generateDocs(tsDecl, decl) {
1541
1583
  return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
@@ -1545,7 +1587,7 @@ var TsSchemaGenerator = class {
1545
1587
  generateInputTypes(model, options) {
1546
1588
  const dataModels = model.declarations.filter(isDataModel3);
1547
1589
  const statements = [];
1548
- statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly)));
1590
+ statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly), options.importWithFileExtension));
1549
1591
  const inputTypes = [
1550
1592
  "FindManyArgs",
1551
1593
  "FindUniqueArgs",