@zenstackhq/sdk 3.0.0-beta.20 → 3.0.0-beta.21

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
@@ -533,6 +533,9 @@ var EnumField = class extends DeclarationBase {
533
533
 
534
534
  // src/prisma/prisma-schema-generator.ts
535
535
  var IDENTIFIER_NAME_MAX_LENGTH = 50 - DELEGATE_AUX_RELATION_PREFIX.length;
536
+ var NON_PRISMA_DATASOURCE_FIELDS = [
537
+ "defaultSchema"
538
+ ];
536
539
  var PrismaSchemaGenerator = class {
537
540
  static {
538
541
  __name(this, "PrismaSchemaGenerator");
@@ -570,7 +573,7 @@ var PrismaSchemaGenerator = class {
570
573
  return this.PRELUDE + prisma.toString();
571
574
  }
572
575
  generateDataSource(prisma, dataSource) {
573
- const fields = dataSource.fields.map((f) => ({
576
+ const fields = dataSource.fields.filter((f) => !NON_PRISMA_DATASOURCE_FIELDS.includes(f.name)).map((f) => ({
574
577
  name: f.name,
575
578
  text: this.configExprToText(f.value)
576
579
  }));
@@ -618,14 +621,30 @@ var PrismaSchemaGenerator = class {
618
621
  this.generateModelField(model, field, decl);
619
622
  }
620
623
  }
621
- const allAttributes = (0, import_utils2.getAllAttributes)(decl);
622
- for (const attr of allAttributes.filter((attr2) => this.isPrismaAttribute(attr2) && !this.isInheritedMapAttribute(attr2, decl))) {
624
+ const allAttributes = (0, import_utils2.getAllAttributes)(decl).filter((attr) => this.isPrismaAttribute(attr) && !this.isInheritedMapAttribute(attr, decl));
625
+ for (const attr of allAttributes) {
623
626
  this.generateContainerAttribute(model, attr);
624
627
  }
628
+ if (this.datasourceHasSchemasSetting(decl.$container) && !allAttributes.some((attr) => attr.decl.ref?.name === "@@schema")) {
629
+ model.addAttribute("@@schema", [
630
+ new AttributeArg(void 0, new AttributeArgValue("String", this.getDefaultPostgresSchemaName(decl.$container)))
631
+ ]);
632
+ }
625
633
  decl.comments.forEach((c) => model.addComment(c));
626
634
  this.generateDelegateRelationForBase(model, decl);
627
635
  this.generateDelegateRelationForConcrete(model, decl);
628
636
  }
637
+ getDatasourceField(zmodel, fieldName) {
638
+ const dataSource = zmodel.declarations.find(import_ast2.isDataSource);
639
+ return dataSource?.fields.find((f) => f.name === fieldName);
640
+ }
641
+ datasourceHasSchemasSetting(zmodel) {
642
+ return !!this.getDatasourceField(zmodel, "schemas");
643
+ }
644
+ getDefaultPostgresSchemaName(zmodel) {
645
+ const defaultSchemaField = this.getDatasourceField(zmodel, "defaultSchema");
646
+ return (0, import_utils2.getStringLiteral)(defaultSchemaField?.value) ?? "public";
647
+ }
629
648
  isInheritedMapAttribute(attr, contextModel) {
630
649
  if (attr.$container === contextModel) {
631
650
  return false;
@@ -641,7 +660,7 @@ var PrismaSchemaGenerator = class {
641
660
  }
642
661
  getUnsupportedFieldType(fieldType) {
643
662
  if (fieldType.unsupported) {
644
- const value = this.getStringLiteral(fieldType.unsupported.value);
663
+ const value = (0, import_utils2.getStringLiteral)(fieldType.unsupported.value);
645
664
  if (value) {
646
665
  return `Unsupported("${value}")`;
647
666
  } else {
@@ -651,9 +670,6 @@ var PrismaSchemaGenerator = class {
651
670
  return void 0;
652
671
  }
653
672
  }
654
- getStringLiteral(node) {
655
- return (0, import_ast2.isStringLiteral)(node) ? node.value : void 0;
656
- }
657
673
  generateModelField(model, field, contextModel, addToFront = false) {
658
674
  let fieldType;
659
675
  if (field.type.type) {
@@ -915,8 +931,12 @@ var TsSchemaGenerator = class {
915
931
  }
916
932
  createProviderObject(model) {
917
933
  const dsProvider = this.getDataSourceProvider(model);
934
+ const defaultSchema = this.getDataSourceDefaultSchema(model);
918
935
  return ts.factory.createObjectLiteralExpression([
919
- ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(dsProvider.type))
936
+ ts.factory.createPropertyAssignment("type", ts.factory.createStringLiteral(dsProvider)),
937
+ ...defaultSchema ? [
938
+ ts.factory.createPropertyAssignment("defaultSchema", ts.factory.createStringLiteral(defaultSchema))
939
+ ] : []
920
940
  ], true);
921
941
  }
922
942
  createModelsObject(model, lite) {
@@ -1093,11 +1113,18 @@ var TsSchemaGenerator = class {
1093
1113
  const dataSource = model.declarations.find(import_ast3.isDataSource);
1094
1114
  (0, import_common_helpers2.invariant)(dataSource, "No data source found in the model");
1095
1115
  const providerExpr = dataSource.fields.find((f) => f.name === "provider")?.value;
1096
- (0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(providerExpr), "Provider must be a literal");
1097
- const type = providerExpr.value;
1098
- return {
1099
- type
1100
- };
1116
+ (0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(providerExpr) && typeof providerExpr.value === "string", "Provider must be a string literal");
1117
+ return providerExpr.value;
1118
+ }
1119
+ getDataSourceDefaultSchema(model) {
1120
+ const dataSource = model.declarations.find(import_ast3.isDataSource);
1121
+ (0, import_common_helpers2.invariant)(dataSource, "No data source found in the model");
1122
+ const defaultSchemaExpr = dataSource.fields.find((f) => f.name === "defaultSchema")?.value;
1123
+ if (!defaultSchemaExpr) {
1124
+ return void 0;
1125
+ }
1126
+ (0, import_common_helpers2.invariant)((0, import_ast3.isLiteralExpr)(defaultSchemaExpr) && typeof defaultSchemaExpr.value === "string", "Default schema must be a string literal");
1127
+ return defaultSchemaExpr.value;
1101
1128
  }
1102
1129
  getFieldMappedDefault(field) {
1103
1130
  const defaultAttr = getAttribute(field, "@default");
@@ -1263,7 +1290,11 @@ var TsSchemaGenerator = class {
1263
1290
  const seenKeys = /* @__PURE__ */ new Set();
1264
1291
  for (const attr of allAttributes) {
1265
1292
  if (attr.decl.$refText === "@@id" || attr.decl.$refText === "@@unique") {
1266
- const fieldNames = this.getReferenceNames(attr.args[0].value);
1293
+ const fieldsArg = (0, import_utils3.getAttributeArg)(attr, "fields");
1294
+ if (!fieldsArg) {
1295
+ continue;
1296
+ }
1297
+ const fieldNames = this.getReferenceNames(fieldsArg);
1267
1298
  if (!fieldNames) {
1268
1299
  continue;
1269
1300
  }
@@ -1302,7 +1333,21 @@ var TsSchemaGenerator = class {
1302
1333
  return field.type.type ? ts.factory.createStringLiteral(field.type.type) : field.type.reference ? ts.factory.createStringLiteral(field.type.reference.$refText) : ts.factory.createStringLiteral("Unsupported");
1303
1334
  }
1304
1335
  createEnumObject(e) {
1305
- return ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createStringLiteral(field.name))), true);
1336
+ return ts.factory.createObjectLiteralExpression([
1337
+ ts.factory.createPropertyAssignment("values", ts.factory.createObjectLiteralExpression(e.fields.map((f) => ts.factory.createPropertyAssignment(f.name, ts.factory.createStringLiteral(f.name))), true)),
1338
+ // only generate `fields` if there are attributes on the fields
1339
+ ...e.fields.some((f) => f.attributes.length > 0) ? [
1340
+ ts.factory.createPropertyAssignment("fields", ts.factory.createObjectLiteralExpression(e.fields.map((field) => ts.factory.createPropertyAssignment(field.name, ts.factory.createObjectLiteralExpression([
1341
+ ts.factory.createPropertyAssignment("name", ts.factory.createStringLiteral(field.name)),
1342
+ ...field.attributes.length > 0 ? [
1343
+ ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(field.attributes?.map((attr) => this.createAttributeObject(attr)) ?? [], true))
1344
+ ] : []
1345
+ ], true))), true))
1346
+ ] : [],
1347
+ ...e.attributes.length > 0 ? [
1348
+ ts.factory.createPropertyAssignment("attributes", ts.factory.createArrayLiteralExpression(e.attributes.map((attr) => this.createAttributeObject(attr)), true))
1349
+ ] : []
1350
+ ], true);
1306
1351
  }
1307
1352
  getLiteral(expr) {
1308
1353
  if (!(0, import_ast3.isLiteralExpr)(expr)) {
@@ -1491,7 +1536,7 @@ var TsSchemaGenerator = class {
1491
1536
  let enumDecl = ts.factory.createVariableStatement([
1492
1537
  ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)
1493
1538
  ], ts.factory.createVariableDeclarationList([
1494
- ts.factory.createVariableDeclaration(e.name, void 0, void 0, ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("$schema"), ts.factory.createIdentifier("enums")), ts.factory.createIdentifier(e.name)))
1539
+ ts.factory.createVariableDeclaration(e.name, void 0, void 0, ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createPropertyAccessExpression(ts.factory.createIdentifier("$schema"), ts.factory.createIdentifier("enums")), ts.factory.createIdentifier(e.name)), ts.factory.createIdentifier("values")))
1495
1540
  ], ts.NodeFlags.Const));
1496
1541
  if (e.comments.length > 0) {
1497
1542
  enumDecl = this.generateDocs(enumDecl, e);