@zenstackhq/orm 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
@@ -4116,6 +4116,8 @@ var InputValidator = class {
4116
4116
  makePrimitiveSchema(type, attributes) {
4117
4117
  if (this.schema.typeDefs && type in this.schema.typeDefs) {
4118
4118
  return this.makeTypeDefSchema(type);
4119
+ } else if (this.schema.enums && type in this.schema.enums) {
4120
+ return this.makeEnumSchema(type);
4119
4121
  } else {
4120
4122
  return (0, import_ts_pattern13.match)(type).with("String", () => this.extraValidationsEnabled ? addStringValidation(import_zod2.z.string(), attributes) : import_zod2.z.string()).with("Int", () => this.extraValidationsEnabled ? addNumberValidation(import_zod2.z.number().int(), attributes) : import_zod2.z.number().int()).with("Float", () => this.extraValidationsEnabled ? addNumberValidation(import_zod2.z.number(), attributes) : import_zod2.z.number()).with("Boolean", () => import_zod2.z.boolean()).with("BigInt", () => import_zod2.z.union([
4121
4123
  this.extraValidationsEnabled ? addNumberValidation(import_zod2.z.number().int(), attributes) : import_zod2.z.number().int(),
@@ -4132,6 +4134,21 @@ var InputValidator = class {
4132
4134
  ])).with("Bytes", () => import_zod2.z.instanceof(Uint8Array)).otherwise(() => import_zod2.z.unknown());
4133
4135
  }
4134
4136
  }
4137
+ makeEnumSchema(type) {
4138
+ const key = (0, import_json_stable_stringify.default)({
4139
+ type: "enum",
4140
+ name: type
4141
+ });
4142
+ let schema = this.getSchemaCache(key);
4143
+ if (schema) {
4144
+ return schema;
4145
+ }
4146
+ const enumDef = getEnum(this.schema, type);
4147
+ (0, import_common_helpers7.invariant)(enumDef, `Enum "${type}" not found in schema`);
4148
+ schema = import_zod2.z.enum(Object.keys(enumDef.values));
4149
+ this.setSchemaCache(key, schema);
4150
+ return schema;
4151
+ }
4135
4152
  makeTypeDefSchema(type) {
4136
4153
  const key = (0, import_json_stable_stringify.default)({
4137
4154
  type: "typedef",
@@ -4142,9 +4159,9 @@ var InputValidator = class {
4142
4159
  if (schema) {
4143
4160
  return schema;
4144
4161
  }
4145
- const typeDef = this.schema.typeDefs?.[type];
4162
+ const typeDef = getTypeDef(this.schema, type);
4146
4163
  (0, import_common_helpers7.invariant)(typeDef, `Type definition "${type}" not found in schema`);
4147
- schema = import_zod2.z.object(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
4164
+ schema = import_zod2.z.looseObject(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
4148
4165
  let fieldSchema = this.makePrimitiveSchema(def.type);
4149
4166
  if (def.array) {
4150
4167
  fieldSchema = fieldSchema.array();
@@ -4156,7 +4173,7 @@ var InputValidator = class {
4156
4173
  field,
4157
4174
  fieldSchema
4158
4175
  ];
4159
- }))).passthrough();
4176
+ })));
4160
4177
  this.setSchemaCache(key, schema);
4161
4178
  return schema;
4162
4179
  }
@@ -4193,7 +4210,7 @@ var InputValidator = class {
4193
4210
  } else {
4194
4211
  const enumDef = getEnum(this.schema, fieldDef.type);
4195
4212
  if (enumDef) {
4196
- if (Object.keys(enumDef).length > 0) {
4213
+ if (Object.keys(enumDef.values).length > 0) {
4197
4214
  fieldSchema = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, withAggregations);
4198
4215
  }
4199
4216
  } else if (fieldDef.array) {
@@ -4215,7 +4232,7 @@ var InputValidator = class {
4215
4232
  let fieldSchema;
4216
4233
  const enumDef = getEnum(this.schema, def.type);
4217
4234
  if (enumDef) {
4218
- if (Object.keys(enumDef).length > 0) {
4235
+ if (Object.keys(enumDef.values).length > 0) {
4219
4236
  fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false);
4220
4237
  } else {
4221
4238
  fieldSchema = import_zod2.z.never();
@@ -4255,7 +4272,7 @@ var InputValidator = class {
4255
4272
  return result;
4256
4273
  }
4257
4274
  makeEnumFilterSchema(enumDef, optional, withAggregations) {
4258
- const baseSchema = import_zod2.z.enum(Object.keys(enumDef));
4275
+ const baseSchema = import_zod2.z.enum(Object.keys(enumDef.values));
4259
4276
  const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => import_zod2.z.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations)), [
4260
4277
  "equals",
4261
4278
  "in",
@@ -5303,13 +5320,26 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5303
5320
  if (!node.into) {
5304
5321
  return super.transformInsertQuery(node);
5305
5322
  }
5323
+ const model = extractModelName(node.into);
5324
+ (0, import_common_helpers8.invariant)(model, 'InsertQueryNode must have a model name in the "into" clause');
5306
5325
  return this.withScope({
5307
- model: node.into.table.identifier.name
5308
- }, () => ({
5309
- ...super.transformInsertQuery(node),
5310
- // map table name
5311
- into: this.processTableRef(node.into)
5312
- }));
5326
+ model
5327
+ }, () => {
5328
+ const baseResult = super.transformInsertQuery(node);
5329
+ let values = baseResult.values;
5330
+ if (node.columns && values) {
5331
+ values = this.processEnumMappingForColumns(model, node.columns, values);
5332
+ }
5333
+ return {
5334
+ ...baseResult,
5335
+ // map table name
5336
+ into: this.processTableRef(node.into),
5337
+ values
5338
+ };
5339
+ });
5340
+ }
5341
+ isOperationNode(value) {
5342
+ return !!value && typeof value === "object" && "kind" in value;
5313
5343
  }
5314
5344
  transformReturning(node) {
5315
5345
  return {
@@ -5332,7 +5362,7 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5332
5362
  mappedTableName = this.mapTableName(scope.model);
5333
5363
  }
5334
5364
  }
5335
- return import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(mappedFieldName), mappedTableName ? import_kysely6.TableNode.create(mappedTableName) : void 0);
5365
+ return import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(mappedFieldName), mappedTableName ? this.createTableNode(mappedTableName, void 0) : void 0);
5336
5366
  } else {
5337
5367
  return node;
5338
5368
  }
@@ -5353,12 +5383,24 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5353
5383
  if (!innerTable || !import_kysely6.TableNode.is(innerTable)) {
5354
5384
  return super.transformUpdateQuery(node);
5355
5385
  }
5386
+ const model = extractModelName(innerTable);
5387
+ (0, import_common_helpers8.invariant)(model, 'UpdateQueryNode must have a model name in the "table" clause');
5356
5388
  return this.withScope({
5357
- model: innerTable.table.identifier.name,
5389
+ model,
5358
5390
  alias
5359
5391
  }, () => {
5392
+ const baseResult = super.transformUpdateQuery(node);
5393
+ const updates = baseResult.updates?.map((update, i) => {
5394
+ if (import_kysely6.ColumnNode.is(update.column)) {
5395
+ const origColumn = node.updates[i].column;
5396
+ return import_kysely6.ColumnUpdateNode.create(update.column, this.processEnumMappingForValue(model, origColumn, update.value));
5397
+ } else {
5398
+ return update;
5399
+ }
5400
+ });
5360
5401
  return {
5361
- ...super.transformUpdateQuery(node),
5402
+ ...baseResult,
5403
+ updates,
5362
5404
  // map table name
5363
5405
  table: this.wrapAlias(this.processTableRef(innerTable), alias)
5364
5406
  };
@@ -5393,29 +5435,54 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5393
5435
  processSelectQuerySelections(node) {
5394
5436
  const selections = [];
5395
5437
  for (const selection of node.selections ?? []) {
5438
+ const processedSelections = [];
5396
5439
  if (import_kysely6.SelectAllNode.is(selection.selection)) {
5397
- const scope = this.scopes[this.scopes.length - 1];
5440
+ const scope = this.requireCurrentScope();
5398
5441
  if (scope?.model && !scope.namesMapped) {
5399
- selections.push(...this.createSelectAllFields(scope.model, scope.alias));
5442
+ processedSelections.push(...this.createSelectAllFields(scope.model, scope.alias));
5400
5443
  } else {
5401
- selections.push(super.transformSelection(selection));
5444
+ processedSelections.push({
5445
+ originalField: void 0,
5446
+ selection: super.transformSelection(selection)
5447
+ });
5402
5448
  }
5403
5449
  } else if (import_kysely6.ReferenceNode.is(selection.selection) || import_kysely6.ColumnNode.is(selection.selection)) {
5404
5450
  const transformed = this.transformNode(selection.selection);
5451
+ const originalField = extractFieldName(selection.selection);
5405
5452
  if (import_kysely6.AliasNode.is(transformed)) {
5406
- selections.push(import_kysely6.SelectionNode.create(transformed));
5453
+ processedSelections.push({
5454
+ originalField,
5455
+ selection: import_kysely6.SelectionNode.create(transformed)
5456
+ });
5407
5457
  } else {
5408
- const origFieldName = extractFieldName(selection.selection);
5409
5458
  const fieldName = extractFieldName(transformed);
5410
- if (fieldName !== origFieldName) {
5411
- selections.push(import_kysely6.SelectionNode.create(this.wrapAlias(transformed, origFieldName ? import_kysely6.IdentifierNode.create(origFieldName) : void 0)));
5459
+ if (fieldName !== originalField) {
5460
+ processedSelections.push({
5461
+ originalField,
5462
+ selection: import_kysely6.SelectionNode.create(this.wrapAlias(transformed, originalField ? import_kysely6.IdentifierNode.create(originalField) : void 0))
5463
+ });
5412
5464
  } else {
5413
- selections.push(import_kysely6.SelectionNode.create(transformed));
5465
+ processedSelections.push({
5466
+ originalField,
5467
+ selection: import_kysely6.SelectionNode.create(transformed)
5468
+ });
5414
5469
  }
5415
5470
  }
5416
5471
  } else {
5417
- selections.push(super.transformSelection(selection));
5472
+ const { node: innerNode } = stripAlias(selection.selection);
5473
+ processedSelections.push({
5474
+ originalField: extractFieldName(innerNode),
5475
+ selection: super.transformSelection(selection)
5476
+ });
5418
5477
  }
5478
+ const enumProcessedSelections = processedSelections.map(({ originalField, selection: selection2 }) => {
5479
+ if (!originalField) {
5480
+ return selection2;
5481
+ } else {
5482
+ return import_kysely6.SelectionNode.create(this.processEnumSelection(selection2.selection, originalField));
5483
+ }
5484
+ });
5485
+ selections.push(...enumProcessedSelections);
5419
5486
  }
5420
5487
  return selections;
5421
5488
  }
@@ -5479,7 +5546,9 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5479
5546
  if (!import_kysely6.TableNode.is(node)) {
5480
5547
  return super.transformNode(node);
5481
5548
  }
5482
- return import_kysely6.TableNode.create(this.mapTableName(node.table.identifier.name));
5549
+ const mappedName = this.mapTableName(node.table.identifier.name);
5550
+ const tableSchema = this.getTableSchema(node.table.identifier.name);
5551
+ return this.createTableNode(mappedName, tableSchema);
5483
5552
  }
5484
5553
  getMappedName(def) {
5485
5554
  const mapAttr = def.attributes?.find((attr) => attr.name === "@@map" || attr.name === "@map");
@@ -5519,8 +5588,9 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5519
5588
  const modelName = innerNode.table.identifier.name;
5520
5589
  const mappedName = this.mapTableName(modelName);
5521
5590
  const finalAlias = alias ?? (mappedName !== modelName ? import_kysely6.IdentifierNode.create(modelName) : void 0);
5591
+ const tableSchema = this.getTableSchema(modelName);
5522
5592
  return {
5523
- node: this.wrapAlias(import_kysely6.TableNode.create(mappedName), finalAlias),
5593
+ node: this.wrapAlias(this.createTableNode(mappedName, tableSchema), finalAlias),
5524
5594
  scope: {
5525
5595
  alias: alias ?? import_kysely6.IdentifierNode.create(modelName),
5526
5596
  model: modelName,
@@ -5538,6 +5608,20 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5538
5608
  };
5539
5609
  }
5540
5610
  }
5611
+ getTableSchema(model) {
5612
+ if (this.schema.provider.type !== "postgresql") {
5613
+ return void 0;
5614
+ }
5615
+ let schema = this.schema.provider.defaultSchema ?? "public";
5616
+ const schemaAttr = this.schema.models[model]?.attributes?.find((attr) => attr.name === "@@schema");
5617
+ if (schemaAttr) {
5618
+ const nameArg = schemaAttr.args?.find((arg) => arg.name === "map");
5619
+ if (nameArg && nameArg.value.kind === "literal") {
5620
+ schema = nameArg.value.value;
5621
+ }
5622
+ }
5623
+ return schema;
5624
+ }
5541
5625
  createSelectAllFields(model, alias) {
5542
5626
  const modelDef = requireModel(this.schema, model);
5543
5627
  return this.getModelFields(modelDef).map((fieldDef) => {
@@ -5545,9 +5629,15 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5545
5629
  const columnRef = import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(columnName), alias && import_kysely6.IdentifierNode.is(alias) ? import_kysely6.TableNode.create(alias.name) : void 0);
5546
5630
  if (columnName !== fieldDef.name) {
5547
5631
  const aliased = import_kysely6.AliasNode.create(columnRef, import_kysely6.IdentifierNode.create(fieldDef.name));
5548
- return import_kysely6.SelectionNode.create(aliased);
5632
+ return {
5633
+ originalField: fieldDef.name,
5634
+ selection: import_kysely6.SelectionNode.create(aliased)
5635
+ };
5549
5636
  } else {
5550
- return import_kysely6.SelectionNode.create(columnRef);
5637
+ return {
5638
+ originalField: fieldDef.name,
5639
+ selection: import_kysely6.SelectionNode.create(columnRef)
5640
+ };
5551
5641
  }
5552
5642
  });
5553
5643
  }
@@ -5571,26 +5661,155 @@ var QueryNameMapper = class extends import_kysely6.OperationNodeTransformer {
5571
5661
  return result;
5572
5662
  }
5573
5663
  processSelection(node) {
5574
- let alias;
5575
- if (!import_kysely6.AliasNode.is(node)) {
5576
- alias = extractFieldName(node);
5664
+ const { alias, node: innerNode } = stripAlias(node);
5665
+ const originalField = extractFieldName(innerNode);
5666
+ let result = super.transformNode(node);
5667
+ if (originalField) {
5668
+ result = this.processEnumSelection(result, originalField);
5669
+ }
5670
+ if (!import_kysely6.AliasNode.is(result)) {
5671
+ const addAlias = alias ?? (originalField ? import_kysely6.IdentifierNode.create(originalField) : void 0);
5672
+ if (addAlias) {
5673
+ result = this.wrapAlias(result, addAlias);
5674
+ }
5577
5675
  }
5578
- const result = super.transformNode(node);
5579
- return this.wrapAlias(result, alias ? import_kysely6.IdentifierNode.create(alias) : void 0);
5676
+ return result;
5580
5677
  }
5581
5678
  processSelectAll(node) {
5582
- const scope = this.scopes[this.scopes.length - 1];
5583
- (0, import_common_helpers8.invariant)(scope);
5584
- if (!scope.model || !this.hasMappedColumns(scope.model)) {
5679
+ const scope = this.requireCurrentScope();
5680
+ if (!scope.model || !(this.hasMappedColumns(scope.model) || this.modelUsesEnumWithMappedValues(scope.model))) {
5585
5681
  return super.transformSelectAll(node);
5586
5682
  }
5587
5683
  const modelDef = requireModel(this.schema, scope.model);
5588
5684
  return this.getModelFields(modelDef).map((fieldDef) => {
5589
5685
  const columnName = this.mapFieldName(modelDef.name, fieldDef.name);
5590
5686
  const columnRef = import_kysely6.ReferenceNode.create(import_kysely6.ColumnNode.create(columnName));
5591
- return columnName !== fieldDef.name ? this.wrapAlias(columnRef, import_kysely6.IdentifierNode.create(fieldDef.name)) : columnRef;
5687
+ const enumProcessed = this.processEnumSelection(columnRef, fieldDef.name);
5688
+ return columnName !== fieldDef.name && !import_kysely6.AliasNode.is(enumProcessed) ? this.wrapAlias(enumProcessed, import_kysely6.IdentifierNode.create(fieldDef.name)) : enumProcessed;
5689
+ });
5690
+ }
5691
+ createTableNode(tableName, schemaName) {
5692
+ return schemaName ? import_kysely6.TableNode.createWithSchema(schemaName, tableName) : import_kysely6.TableNode.create(tableName);
5693
+ }
5694
+ requireCurrentScope() {
5695
+ const scope = this.scopes[this.scopes.length - 1];
5696
+ (0, import_common_helpers8.invariant)(scope, "No scope available");
5697
+ return scope;
5698
+ }
5699
+ // #endregion
5700
+ // #region enum value mapping
5701
+ modelUsesEnumWithMappedValues(model) {
5702
+ const modelDef = getModel(this.schema, model);
5703
+ if (!modelDef) {
5704
+ return false;
5705
+ }
5706
+ return this.getModelFields(modelDef).some((fieldDef) => {
5707
+ const enumDef = getEnum(this.schema, fieldDef.type);
5708
+ if (!enumDef) {
5709
+ return false;
5710
+ }
5711
+ return Object.values(enumDef.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === "@map"));
5592
5712
  });
5593
5713
  }
5714
+ getEnumValueMapping(enumDef) {
5715
+ const mapping = {};
5716
+ for (const [key, field] of Object.entries(enumDef.fields ?? {})) {
5717
+ const mappedName = this.getMappedName(field);
5718
+ if (mappedName) {
5719
+ mapping[key] = mappedName;
5720
+ }
5721
+ }
5722
+ return mapping;
5723
+ }
5724
+ processEnumMappingForColumns(model, columns, values) {
5725
+ if (import_kysely6.ValuesNode.is(values)) {
5726
+ return import_kysely6.ValuesNode.create(values.values.map((valueItems) => {
5727
+ if (import_kysely6.PrimitiveValueListNode.is(valueItems)) {
5728
+ return import_kysely6.PrimitiveValueListNode.create(this.processEnumMappingForValues(model, columns, valueItems.values));
5729
+ } else {
5730
+ return import_kysely6.ValueListNode.create(this.processEnumMappingForValues(model, columns, valueItems.values));
5731
+ }
5732
+ }));
5733
+ } else if (import_kysely6.PrimitiveValueListNode.is(values)) {
5734
+ return import_kysely6.PrimitiveValueListNode.create(this.processEnumMappingForValues(model, columns, values.values));
5735
+ } else {
5736
+ return values;
5737
+ }
5738
+ }
5739
+ processEnumMappingForValues(model, columns, values) {
5740
+ const result = [];
5741
+ for (let i = 0; i < columns.length; i++) {
5742
+ const value = values[i];
5743
+ if (value === null || value === void 0) {
5744
+ result.push(value);
5745
+ continue;
5746
+ }
5747
+ result.push(this.processEnumMappingForValue(model, columns[i], value));
5748
+ }
5749
+ return result;
5750
+ }
5751
+ processEnumMappingForValue(model, column, value) {
5752
+ const fieldDef = getField(this.schema, model, column.column.name);
5753
+ if (!fieldDef) {
5754
+ return value;
5755
+ }
5756
+ if (!isEnum(this.schema, fieldDef.type)) {
5757
+ return value;
5758
+ }
5759
+ const enumDef = getEnum(this.schema, fieldDef.type);
5760
+ if (!enumDef) {
5761
+ return value;
5762
+ }
5763
+ const enumValueMapping = this.getEnumValueMapping(enumDef);
5764
+ if (this.isOperationNode(value) && import_kysely6.ValueNode.is(value) && typeof value.value === "string") {
5765
+ const mappedValue = enumValueMapping[value.value];
5766
+ if (mappedValue) {
5767
+ return import_kysely6.ValueNode.create(mappedValue);
5768
+ }
5769
+ } else if (typeof value === "string") {
5770
+ const mappedValue = enumValueMapping[value];
5771
+ if (mappedValue) {
5772
+ return mappedValue;
5773
+ }
5774
+ }
5775
+ return value;
5776
+ }
5777
+ processEnumSelection(selection, fieldName) {
5778
+ const { alias, node } = stripAlias(selection);
5779
+ const fieldScope = this.resolveFieldFromScopes(fieldName);
5780
+ if (!fieldScope || !fieldScope.model) {
5781
+ return selection;
5782
+ }
5783
+ const aliasName = alias && import_kysely6.IdentifierNode.is(alias) ? alias.name : fieldName;
5784
+ const fieldDef = getField(this.schema, fieldScope.model, fieldName);
5785
+ if (!fieldDef) {
5786
+ return selection;
5787
+ }
5788
+ const enumDef = getEnum(this.schema, fieldDef.type);
5789
+ if (!enumDef) {
5790
+ return selection;
5791
+ }
5792
+ const enumValueMapping = this.getEnumValueMapping(enumDef);
5793
+ if (Object.keys(enumValueMapping).length === 0) {
5794
+ return selection;
5795
+ }
5796
+ const eb = (0, import_kysely6.expressionBuilder)();
5797
+ const caseBuilder = eb.case();
5798
+ let caseWhen;
5799
+ for (const [key, value] of Object.entries(enumValueMapping)) {
5800
+ if (!caseWhen) {
5801
+ caseWhen = caseBuilder.when(new import_kysely6.ExpressionWrapper(node), "=", value).then(key);
5802
+ } else {
5803
+ caseWhen = caseWhen.when(new import_kysely6.ExpressionWrapper(node), "=", value).then(key);
5804
+ }
5805
+ }
5806
+ const finalExpr = caseWhen.else(eb.cast(new import_kysely6.ExpressionWrapper(node), "text")).end();
5807
+ if (aliasName) {
5808
+ return finalExpr.as(aliasName).toOperationNode();
5809
+ } else {
5810
+ return finalExpr.toOperationNode();
5811
+ }
5812
+ }
5594
5813
  };
5595
5814
 
5596
5815
  // src/client/executor/zenstack-query-executor.ts
@@ -5606,7 +5825,8 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends import_kysely7.
5606
5825
  nameMapper;
5607
5826
  constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
5608
5827
  super(compiler, adapter, connectionProvider, plugins), this.client = client, this.driver = driver, this.compiler = compiler, this.connectionProvider = connectionProvider, this.suppressMutationHooks = suppressMutationHooks;
5609
- if (this.schemaHasMappedNames(client.$schema)) {
5828
+ if (client.$schema.provider.type === "postgresql" || // postgres queries need to be schema-qualified
5829
+ this.schemaHasMappedNames(client.$schema)) {
5610
5830
  this.nameMapper = new QueryNameMapper(client.$schema);
5611
5831
  }
5612
5832
  }
@@ -6047,7 +6267,22 @@ var SchemaDbPusher = class {
6047
6267
  await this.kysely.transaction().execute(async (tx) => {
6048
6268
  if (this.schema.enums && this.schema.provider.type === "postgresql") {
6049
6269
  for (const [name, enumDef] of Object.entries(this.schema.enums)) {
6050
- const createEnum = tx.schema.createType(name).asEnum(Object.values(enumDef));
6270
+ let enumValues;
6271
+ if (enumDef.fields) {
6272
+ enumValues = Object.values(enumDef.fields).map((f) => {
6273
+ const mapAttr = f.attributes?.find((a) => a.name === "@map");
6274
+ if (!mapAttr || !mapAttr.args?.[0]) {
6275
+ return f.name;
6276
+ } else {
6277
+ const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
6278
+ (0, import_common_helpers11.invariant)(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
6279
+ return mappedName;
6280
+ }
6281
+ });
6282
+ } else {
6283
+ enumValues = Object.values(enumDef.values);
6284
+ }
6285
+ const createEnum = tx.schema.createType(name).asEnum(enumValues);
6051
6286
  await createEnum.execute();
6052
6287
  }
6053
6288
  }