@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.cjs CHANGED
@@ -570,6 +570,9 @@ var PrismaSchemaGenerator = class {
570
570
  break;
571
571
  }
572
572
  }
573
+ if (!this.zmodel.declarations.some(import_ast2.isGeneratorDecl)) {
574
+ this.generateDefaultGenerator(prisma);
575
+ }
573
576
  return this.PRELUDE + prisma.toString();
574
577
  }
575
578
  generateDataSource(prisma, dataSource) {
@@ -610,6 +613,21 @@ var PrismaSchemaGenerator = class {
610
613
  text: this.configExprToText(f.value)
611
614
  })));
612
615
  }
616
+ generateDefaultGenerator(prisma) {
617
+ const gen = prisma.addGenerator("client", [
618
+ {
619
+ name: "provider",
620
+ text: '"prisma-client-js"'
621
+ }
622
+ ]);
623
+ const dataSource = this.zmodel.declarations.find(import_ast2.isDataSource);
624
+ if (dataSource?.fields.some((f) => f.name === "extensions")) {
625
+ gen.fields.push({
626
+ name: "previewFeatures",
627
+ text: '["postgresqlExtensions"]'
628
+ });
629
+ }
630
+ }
613
631
  generateModel(prisma, decl) {
614
632
  const model = decl.isView ? prisma.addView(decl.name) : prisma.addModel(decl.name);
615
633
  const allFields = (0, import_utils2.getAllFields)(decl, true);
@@ -760,9 +778,15 @@ var PrismaSchemaGenerator = class {
760
778
  for (const field of decl.fields) {
761
779
  this.generateEnumField(_enum, field);
762
780
  }
763
- for (const attr of decl.attributes.filter((attr2) => this.isPrismaAttribute(attr2))) {
781
+ const allAttributes = decl.attributes.filter((attr) => this.isPrismaAttribute(attr));
782
+ for (const attr of allAttributes) {
764
783
  this.generateContainerAttribute(_enum, attr);
765
784
  }
785
+ if (this.datasourceHasSchemasSetting(decl.$container) && !allAttributes.some((attr) => attr.decl.ref?.name === "@@schema")) {
786
+ _enum.addAttribute("@@schema", [
787
+ new AttributeArg(void 0, new AttributeArgValue("String", this.getDefaultPostgresSchemaName(decl.$container)))
788
+ ]);
789
+ }
766
790
  decl.comments.forEach((c) => _enum.addComment(c));
767
791
  }
768
792
  generateEnumField(_enum, field) {
@@ -888,16 +912,27 @@ var TsSchemaGenerator = class {
888
912
  ] : []
889
913
  ])), ts.factory.createStringLiteral("@zenstackhq/orm/schema"));
890
914
  statements.push(runtimeImportDecl);
891
- const declaration = ts.factory.createVariableStatement([
915
+ const _schemaDecl = ts.factory.createVariableStatement([], ts.factory.createVariableDeclarationList([
916
+ ts.factory.createVariableDeclaration("_schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
917
+ ], ts.NodeFlags.Const));
918
+ statements.push(_schemaDecl);
919
+ const brandedSchemaType = ts.factory.createTypeAliasDeclaration(void 0, "Schema", void 0, ts.factory.createIntersectionTypeNode([
920
+ ts.factory.createTypeQueryNode(ts.factory.createIdentifier("_schema")),
921
+ ts.factory.createTypeLiteralNode([
922
+ ts.factory.createPropertySignature(void 0, "__brand", ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createLiteralTypeNode(ts.factory.createStringLiteral("schema")))
923
+ ])
924
+ ]));
925
+ statements.push(brandedSchemaType);
926
+ const schemaExportDecl = ts.factory.createVariableStatement([
892
927
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
893
928
  ], ts.factory.createVariableDeclarationList([
894
- ts.factory.createVariableDeclaration("schema", void 0, void 0, ts.factory.createSatisfiesExpression(ts.factory.createAsExpression(schemaObject, ts.factory.createTypeReferenceNode("const")), ts.factory.createTypeReferenceNode("SchemaDef")))
929
+ ts.factory.createVariableDeclaration("schema", void 0, ts.factory.createTypeReferenceNode("Schema"), ts.factory.createIdentifier("_schema"))
895
930
  ], ts.NodeFlags.Const));
896
- statements.push(declaration);
897
- const typeDeclaration = ts.factory.createTypeAliasDeclaration([
931
+ statements.push(schemaExportDecl);
932
+ const schemaTypeDeclaration = ts.factory.createTypeAliasDeclaration([
898
933
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
899
- ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("typeof schema"));
900
- statements.push(typeDeclaration);
934
+ ], "SchemaType", void 0, ts.factory.createTypeReferenceNode("Schema"));
935
+ statements.push(schemaTypeDeclaration);
901
936
  }
902
937
  createExpressionUtilsCall(method, args) {
903
938
  this.usedExpressionUtils = true;
@@ -1364,7 +1399,10 @@ var TsSchemaGenerator = class {
1364
1399
  }
1365
1400
  }
1366
1401
  createLiteralNode(arg) {
1367
- 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;
1402
+ 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;
1403
+ }
1404
+ createNumberLiteral(arg) {
1405
+ return arg < 0 ? ts.factory.createPrefixUnaryExpression(ts.SyntaxKind.MinusToken, ts.factory.createNumericLiteral(-arg)) : ts.factory.createNumericLiteral(arg);
1368
1406
  }
1369
1407
  createProceduresObject(procedures) {
1370
1408
  return ts.factory.createObjectLiteralExpression(procedures.map((proc) => ts.factory.createPropertyAssignment(proc.name, this.createProcedureObject(proc))), true);
@@ -1498,7 +1536,7 @@ var TsSchemaGenerator = class {
1498
1536
  }
1499
1537
  generateModelsAndTypeDefs(model, options) {
1500
1538
  const statements = [];
1501
- statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly)));
1539
+ statements.push(this.generateSchemaImport(model, true, true, !!(options.lite || options.liteOnly), options.importWithFileExtension));
1502
1540
  statements.push(ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports([
1503
1541
  ts.factory.createImportSpecifier(true, void 0, ts.factory.createIdentifier(`ModelResult as $ModelResult`)),
1504
1542
  ...model.declarations.some(import_ast3.isTypeDef) ? [
@@ -1557,7 +1595,7 @@ var TsSchemaGenerator = class {
1557
1595
  const result = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(statements), sourceFile);
1558
1596
  import_node_fs.default.writeFileSync(outputFile, result);
1559
1597
  }
1560
- generateSchemaImport(model, schemaObject, schemaType, useLite) {
1598
+ generateSchemaImport(model, schemaObject, schemaType, useLite, importWithFileExtension) {
1561
1599
  const importSpecifiers = [];
1562
1600
  if (schemaObject) {
1563
1601
  if (model.declarations.some(import_ast3.isEnum)) {
@@ -1567,7 +1605,11 @@ var TsSchemaGenerator = class {
1567
1605
  if (schemaType) {
1568
1606
  importSpecifiers.push(ts.factory.createImportSpecifier(true, ts.factory.createIdentifier("SchemaType"), ts.factory.createIdentifier("$Schema")));
1569
1607
  }
1570
- return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(useLite ? "./schema-lite" : "./schema"));
1608
+ let importFrom = useLite ? "./schema-lite" : "./schema";
1609
+ if (importWithFileExtension) {
1610
+ importFrom += importWithFileExtension.startsWith(".") ? importWithFileExtension : `.${importWithFileExtension}`;
1611
+ }
1612
+ return ts.factory.createImportDeclaration(void 0, ts.factory.createImportClause(false, void 0, ts.factory.createNamedImports(importSpecifiers)), ts.factory.createStringLiteral(importFrom));
1571
1613
  }
1572
1614
  generateDocs(tsDecl, decl) {
1573
1615
  return ts.addSyntheticLeadingComment(tsDecl, ts.SyntaxKind.MultiLineCommentTrivia, `*
@@ -1577,7 +1619,7 @@ var TsSchemaGenerator = class {
1577
1619
  generateInputTypes(model, options) {
1578
1620
  const dataModels = model.declarations.filter(import_ast3.isDataModel);
1579
1621
  const statements = [];
1580
- statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly)));
1622
+ statements.push(this.generateSchemaImport(model, false, true, !!(options.lite || options.liteOnly), options.importWithFileExtension));
1581
1623
  const inputTypes = [
1582
1624
  "FindManyArgs",
1583
1625
  "FindUniqueArgs",