@zenstackhq/orm 3.0.0-beta.20 → 3.0.0-beta.22
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 +274 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +54 -51
- package/dist/index.d.ts +54 -51
- package/dist/index.js +278 -43
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -4086,6 +4086,8 @@ var InputValidator = class {
|
|
|
4086
4086
|
makePrimitiveSchema(type, attributes) {
|
|
4087
4087
|
if (this.schema.typeDefs && type in this.schema.typeDefs) {
|
|
4088
4088
|
return this.makeTypeDefSchema(type);
|
|
4089
|
+
} else if (this.schema.enums && type in this.schema.enums) {
|
|
4090
|
+
return this.makeEnumSchema(type);
|
|
4089
4091
|
} else {
|
|
4090
4092
|
return match13(type).with("String", () => this.extraValidationsEnabled ? addStringValidation(z2.string(), attributes) : z2.string()).with("Int", () => this.extraValidationsEnabled ? addNumberValidation(z2.number().int(), attributes) : z2.number().int()).with("Float", () => this.extraValidationsEnabled ? addNumberValidation(z2.number(), attributes) : z2.number()).with("Boolean", () => z2.boolean()).with("BigInt", () => z2.union([
|
|
4091
4093
|
this.extraValidationsEnabled ? addNumberValidation(z2.number().int(), attributes) : z2.number().int(),
|
|
@@ -4102,6 +4104,21 @@ var InputValidator = class {
|
|
|
4102
4104
|
])).with("Bytes", () => z2.instanceof(Uint8Array)).otherwise(() => z2.unknown());
|
|
4103
4105
|
}
|
|
4104
4106
|
}
|
|
4107
|
+
makeEnumSchema(type) {
|
|
4108
|
+
const key = stableStringify({
|
|
4109
|
+
type: "enum",
|
|
4110
|
+
name: type
|
|
4111
|
+
});
|
|
4112
|
+
let schema = this.getSchemaCache(key);
|
|
4113
|
+
if (schema) {
|
|
4114
|
+
return schema;
|
|
4115
|
+
}
|
|
4116
|
+
const enumDef = getEnum(this.schema, type);
|
|
4117
|
+
invariant7(enumDef, `Enum "${type}" not found in schema`);
|
|
4118
|
+
schema = z2.enum(Object.keys(enumDef.values));
|
|
4119
|
+
this.setSchemaCache(key, schema);
|
|
4120
|
+
return schema;
|
|
4121
|
+
}
|
|
4105
4122
|
makeTypeDefSchema(type) {
|
|
4106
4123
|
const key = stableStringify({
|
|
4107
4124
|
type: "typedef",
|
|
@@ -4112,9 +4129,9 @@ var InputValidator = class {
|
|
|
4112
4129
|
if (schema) {
|
|
4113
4130
|
return schema;
|
|
4114
4131
|
}
|
|
4115
|
-
const typeDef = this.schema
|
|
4132
|
+
const typeDef = getTypeDef(this.schema, type);
|
|
4116
4133
|
invariant7(typeDef, `Type definition "${type}" not found in schema`);
|
|
4117
|
-
schema = z2.
|
|
4134
|
+
schema = z2.looseObject(Object.fromEntries(Object.entries(typeDef.fields).map(([field, def]) => {
|
|
4118
4135
|
let fieldSchema = this.makePrimitiveSchema(def.type);
|
|
4119
4136
|
if (def.array) {
|
|
4120
4137
|
fieldSchema = fieldSchema.array();
|
|
@@ -4126,7 +4143,7 @@ var InputValidator = class {
|
|
|
4126
4143
|
field,
|
|
4127
4144
|
fieldSchema
|
|
4128
4145
|
];
|
|
4129
|
-
})))
|
|
4146
|
+
})));
|
|
4130
4147
|
this.setSchemaCache(key, schema);
|
|
4131
4148
|
return schema;
|
|
4132
4149
|
}
|
|
@@ -4163,7 +4180,7 @@ var InputValidator = class {
|
|
|
4163
4180
|
} else {
|
|
4164
4181
|
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
4165
4182
|
if (enumDef) {
|
|
4166
|
-
if (Object.keys(enumDef).length > 0) {
|
|
4183
|
+
if (Object.keys(enumDef.values).length > 0) {
|
|
4167
4184
|
fieldSchema = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, withAggregations);
|
|
4168
4185
|
}
|
|
4169
4186
|
} else if (fieldDef.array) {
|
|
@@ -4185,7 +4202,7 @@ var InputValidator = class {
|
|
|
4185
4202
|
let fieldSchema;
|
|
4186
4203
|
const enumDef = getEnum(this.schema, def.type);
|
|
4187
4204
|
if (enumDef) {
|
|
4188
|
-
if (Object.keys(enumDef).length > 0) {
|
|
4205
|
+
if (Object.keys(enumDef.values).length > 0) {
|
|
4189
4206
|
fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false);
|
|
4190
4207
|
} else {
|
|
4191
4208
|
fieldSchema = z2.never();
|
|
@@ -4225,7 +4242,7 @@ var InputValidator = class {
|
|
|
4225
4242
|
return result;
|
|
4226
4243
|
}
|
|
4227
4244
|
makeEnumFilterSchema(enumDef, optional, withAggregations) {
|
|
4228
|
-
const baseSchema = z2.enum(Object.keys(enumDef));
|
|
4245
|
+
const baseSchema = z2.enum(Object.keys(enumDef.values));
|
|
4229
4246
|
const components = this.makeCommonPrimitiveFilterComponents(baseSchema, optional, () => z2.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations)), [
|
|
4230
4247
|
"equals",
|
|
4231
4248
|
"in",
|
|
@@ -5218,7 +5235,7 @@ import { match as match14 } from "ts-pattern";
|
|
|
5218
5235
|
|
|
5219
5236
|
// src/client/executor/name-mapper.ts
|
|
5220
5237
|
import { invariant as invariant8 } from "@zenstackhq/common-helpers";
|
|
5221
|
-
import { AliasNode as AliasNode2, ColumnNode as ColumnNode2, FromNode, IdentifierNode, OperationNodeTransformer, ReferenceNode as ReferenceNode2, SelectAllNode, SelectionNode, TableNode as TableNode2 } from "kysely";
|
|
5238
|
+
import { AliasNode as AliasNode2, ColumnNode as ColumnNode2, ColumnUpdateNode, expressionBuilder as expressionBuilder3, ExpressionWrapper, FromNode, IdentifierNode, OperationNodeTransformer, PrimitiveValueListNode, ReferenceNode as ReferenceNode2, SelectAllNode, SelectionNode, TableNode as TableNode2, ValueListNode, ValueNode, ValuesNode } from "kysely";
|
|
5222
5239
|
var QueryNameMapper = class extends OperationNodeTransformer {
|
|
5223
5240
|
static {
|
|
5224
5241
|
__name(this, "QueryNameMapper");
|
|
@@ -5273,13 +5290,26 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5273
5290
|
if (!node.into) {
|
|
5274
5291
|
return super.transformInsertQuery(node);
|
|
5275
5292
|
}
|
|
5293
|
+
const model = extractModelName(node.into);
|
|
5294
|
+
invariant8(model, 'InsertQueryNode must have a model name in the "into" clause');
|
|
5276
5295
|
return this.withScope({
|
|
5277
|
-
model
|
|
5278
|
-
}, () =>
|
|
5279
|
-
|
|
5280
|
-
|
|
5281
|
-
|
|
5282
|
-
|
|
5296
|
+
model
|
|
5297
|
+
}, () => {
|
|
5298
|
+
const baseResult = super.transformInsertQuery(node);
|
|
5299
|
+
let values = baseResult.values;
|
|
5300
|
+
if (node.columns && values) {
|
|
5301
|
+
values = this.processEnumMappingForColumns(model, node.columns, values);
|
|
5302
|
+
}
|
|
5303
|
+
return {
|
|
5304
|
+
...baseResult,
|
|
5305
|
+
// map table name
|
|
5306
|
+
into: this.processTableRef(node.into),
|
|
5307
|
+
values
|
|
5308
|
+
};
|
|
5309
|
+
});
|
|
5310
|
+
}
|
|
5311
|
+
isOperationNode(value) {
|
|
5312
|
+
return !!value && typeof value === "object" && "kind" in value;
|
|
5283
5313
|
}
|
|
5284
5314
|
transformReturning(node) {
|
|
5285
5315
|
return {
|
|
@@ -5302,7 +5332,7 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5302
5332
|
mappedTableName = this.mapTableName(scope.model);
|
|
5303
5333
|
}
|
|
5304
5334
|
}
|
|
5305
|
-
return ReferenceNode2.create(ColumnNode2.create(mappedFieldName), mappedTableName ?
|
|
5335
|
+
return ReferenceNode2.create(ColumnNode2.create(mappedFieldName), mappedTableName ? this.createTableNode(mappedTableName, void 0) : void 0);
|
|
5306
5336
|
} else {
|
|
5307
5337
|
return node;
|
|
5308
5338
|
}
|
|
@@ -5323,12 +5353,24 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5323
5353
|
if (!innerTable || !TableNode2.is(innerTable)) {
|
|
5324
5354
|
return super.transformUpdateQuery(node);
|
|
5325
5355
|
}
|
|
5356
|
+
const model = extractModelName(innerTable);
|
|
5357
|
+
invariant8(model, 'UpdateQueryNode must have a model name in the "table" clause');
|
|
5326
5358
|
return this.withScope({
|
|
5327
|
-
model
|
|
5359
|
+
model,
|
|
5328
5360
|
alias
|
|
5329
5361
|
}, () => {
|
|
5362
|
+
const baseResult = super.transformUpdateQuery(node);
|
|
5363
|
+
const updates = baseResult.updates?.map((update, i) => {
|
|
5364
|
+
if (ColumnNode2.is(update.column)) {
|
|
5365
|
+
const origColumn = node.updates[i].column;
|
|
5366
|
+
return ColumnUpdateNode.create(update.column, this.processEnumMappingForValue(model, origColumn, update.value));
|
|
5367
|
+
} else {
|
|
5368
|
+
return update;
|
|
5369
|
+
}
|
|
5370
|
+
});
|
|
5330
5371
|
return {
|
|
5331
|
-
...
|
|
5372
|
+
...baseResult,
|
|
5373
|
+
updates,
|
|
5332
5374
|
// map table name
|
|
5333
5375
|
table: this.wrapAlias(this.processTableRef(innerTable), alias)
|
|
5334
5376
|
};
|
|
@@ -5363,29 +5405,54 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5363
5405
|
processSelectQuerySelections(node) {
|
|
5364
5406
|
const selections = [];
|
|
5365
5407
|
for (const selection of node.selections ?? []) {
|
|
5408
|
+
const processedSelections = [];
|
|
5366
5409
|
if (SelectAllNode.is(selection.selection)) {
|
|
5367
|
-
const scope = this.
|
|
5410
|
+
const scope = this.requireCurrentScope();
|
|
5368
5411
|
if (scope?.model && !scope.namesMapped) {
|
|
5369
|
-
|
|
5412
|
+
processedSelections.push(...this.createSelectAllFields(scope.model, scope.alias));
|
|
5370
5413
|
} else {
|
|
5371
|
-
|
|
5414
|
+
processedSelections.push({
|
|
5415
|
+
originalField: void 0,
|
|
5416
|
+
selection: super.transformSelection(selection)
|
|
5417
|
+
});
|
|
5372
5418
|
}
|
|
5373
5419
|
} else if (ReferenceNode2.is(selection.selection) || ColumnNode2.is(selection.selection)) {
|
|
5374
5420
|
const transformed = this.transformNode(selection.selection);
|
|
5421
|
+
const originalField = extractFieldName(selection.selection);
|
|
5375
5422
|
if (AliasNode2.is(transformed)) {
|
|
5376
|
-
|
|
5423
|
+
processedSelections.push({
|
|
5424
|
+
originalField,
|
|
5425
|
+
selection: SelectionNode.create(transformed)
|
|
5426
|
+
});
|
|
5377
5427
|
} else {
|
|
5378
|
-
const origFieldName = extractFieldName(selection.selection);
|
|
5379
5428
|
const fieldName = extractFieldName(transformed);
|
|
5380
|
-
if (fieldName !==
|
|
5381
|
-
|
|
5429
|
+
if (fieldName !== originalField) {
|
|
5430
|
+
processedSelections.push({
|
|
5431
|
+
originalField,
|
|
5432
|
+
selection: SelectionNode.create(this.wrapAlias(transformed, originalField ? IdentifierNode.create(originalField) : void 0))
|
|
5433
|
+
});
|
|
5382
5434
|
} else {
|
|
5383
|
-
|
|
5435
|
+
processedSelections.push({
|
|
5436
|
+
originalField,
|
|
5437
|
+
selection: SelectionNode.create(transformed)
|
|
5438
|
+
});
|
|
5384
5439
|
}
|
|
5385
5440
|
}
|
|
5386
5441
|
} else {
|
|
5387
|
-
|
|
5442
|
+
const { node: innerNode } = stripAlias(selection.selection);
|
|
5443
|
+
processedSelections.push({
|
|
5444
|
+
originalField: extractFieldName(innerNode),
|
|
5445
|
+
selection: super.transformSelection(selection)
|
|
5446
|
+
});
|
|
5388
5447
|
}
|
|
5448
|
+
const enumProcessedSelections = processedSelections.map(({ originalField, selection: selection2 }) => {
|
|
5449
|
+
if (!originalField) {
|
|
5450
|
+
return selection2;
|
|
5451
|
+
} else {
|
|
5452
|
+
return SelectionNode.create(this.processEnumSelection(selection2.selection, originalField));
|
|
5453
|
+
}
|
|
5454
|
+
});
|
|
5455
|
+
selections.push(...enumProcessedSelections);
|
|
5389
5456
|
}
|
|
5390
5457
|
return selections;
|
|
5391
5458
|
}
|
|
@@ -5449,7 +5516,9 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5449
5516
|
if (!TableNode2.is(node)) {
|
|
5450
5517
|
return super.transformNode(node);
|
|
5451
5518
|
}
|
|
5452
|
-
|
|
5519
|
+
const mappedName = this.mapTableName(node.table.identifier.name);
|
|
5520
|
+
const tableSchema = this.getTableSchema(node.table.identifier.name);
|
|
5521
|
+
return this.createTableNode(mappedName, tableSchema);
|
|
5453
5522
|
}
|
|
5454
5523
|
getMappedName(def) {
|
|
5455
5524
|
const mapAttr = def.attributes?.find((attr) => attr.name === "@@map" || attr.name === "@map");
|
|
@@ -5489,8 +5558,9 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5489
5558
|
const modelName = innerNode.table.identifier.name;
|
|
5490
5559
|
const mappedName = this.mapTableName(modelName);
|
|
5491
5560
|
const finalAlias = alias ?? (mappedName !== modelName ? IdentifierNode.create(modelName) : void 0);
|
|
5561
|
+
const tableSchema = this.getTableSchema(modelName);
|
|
5492
5562
|
return {
|
|
5493
|
-
node: this.wrapAlias(
|
|
5563
|
+
node: this.wrapAlias(this.createTableNode(mappedName, tableSchema), finalAlias),
|
|
5494
5564
|
scope: {
|
|
5495
5565
|
alias: alias ?? IdentifierNode.create(modelName),
|
|
5496
5566
|
model: modelName,
|
|
@@ -5508,6 +5578,20 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5508
5578
|
};
|
|
5509
5579
|
}
|
|
5510
5580
|
}
|
|
5581
|
+
getTableSchema(model) {
|
|
5582
|
+
if (this.schema.provider.type !== "postgresql") {
|
|
5583
|
+
return void 0;
|
|
5584
|
+
}
|
|
5585
|
+
let schema = this.schema.provider.defaultSchema ?? "public";
|
|
5586
|
+
const schemaAttr = this.schema.models[model]?.attributes?.find((attr) => attr.name === "@@schema");
|
|
5587
|
+
if (schemaAttr) {
|
|
5588
|
+
const nameArg = schemaAttr.args?.find((arg) => arg.name === "map");
|
|
5589
|
+
if (nameArg && nameArg.value.kind === "literal") {
|
|
5590
|
+
schema = nameArg.value.value;
|
|
5591
|
+
}
|
|
5592
|
+
}
|
|
5593
|
+
return schema;
|
|
5594
|
+
}
|
|
5511
5595
|
createSelectAllFields(model, alias) {
|
|
5512
5596
|
const modelDef = requireModel(this.schema, model);
|
|
5513
5597
|
return this.getModelFields(modelDef).map((fieldDef) => {
|
|
@@ -5515,9 +5599,15 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5515
5599
|
const columnRef = ReferenceNode2.create(ColumnNode2.create(columnName), alias && IdentifierNode.is(alias) ? TableNode2.create(alias.name) : void 0);
|
|
5516
5600
|
if (columnName !== fieldDef.name) {
|
|
5517
5601
|
const aliased = AliasNode2.create(columnRef, IdentifierNode.create(fieldDef.name));
|
|
5518
|
-
return
|
|
5602
|
+
return {
|
|
5603
|
+
originalField: fieldDef.name,
|
|
5604
|
+
selection: SelectionNode.create(aliased)
|
|
5605
|
+
};
|
|
5519
5606
|
} else {
|
|
5520
|
-
return
|
|
5607
|
+
return {
|
|
5608
|
+
originalField: fieldDef.name,
|
|
5609
|
+
selection: SelectionNode.create(columnRef)
|
|
5610
|
+
};
|
|
5521
5611
|
}
|
|
5522
5612
|
});
|
|
5523
5613
|
}
|
|
@@ -5541,26 +5631,155 @@ var QueryNameMapper = class extends OperationNodeTransformer {
|
|
|
5541
5631
|
return result;
|
|
5542
5632
|
}
|
|
5543
5633
|
processSelection(node) {
|
|
5544
|
-
|
|
5545
|
-
|
|
5546
|
-
|
|
5634
|
+
const { alias, node: innerNode } = stripAlias(node);
|
|
5635
|
+
const originalField = extractFieldName(innerNode);
|
|
5636
|
+
let result = super.transformNode(node);
|
|
5637
|
+
if (originalField) {
|
|
5638
|
+
result = this.processEnumSelection(result, originalField);
|
|
5639
|
+
}
|
|
5640
|
+
if (!AliasNode2.is(result)) {
|
|
5641
|
+
const addAlias = alias ?? (originalField ? IdentifierNode.create(originalField) : void 0);
|
|
5642
|
+
if (addAlias) {
|
|
5643
|
+
result = this.wrapAlias(result, addAlias);
|
|
5644
|
+
}
|
|
5547
5645
|
}
|
|
5548
|
-
|
|
5549
|
-
return this.wrapAlias(result, alias ? IdentifierNode.create(alias) : void 0);
|
|
5646
|
+
return result;
|
|
5550
5647
|
}
|
|
5551
5648
|
processSelectAll(node) {
|
|
5552
|
-
const scope = this.
|
|
5553
|
-
|
|
5554
|
-
if (!scope.model || !this.hasMappedColumns(scope.model)) {
|
|
5649
|
+
const scope = this.requireCurrentScope();
|
|
5650
|
+
if (!scope.model || !(this.hasMappedColumns(scope.model) || this.modelUsesEnumWithMappedValues(scope.model))) {
|
|
5555
5651
|
return super.transformSelectAll(node);
|
|
5556
5652
|
}
|
|
5557
5653
|
const modelDef = requireModel(this.schema, scope.model);
|
|
5558
5654
|
return this.getModelFields(modelDef).map((fieldDef) => {
|
|
5559
5655
|
const columnName = this.mapFieldName(modelDef.name, fieldDef.name);
|
|
5560
5656
|
const columnRef = ReferenceNode2.create(ColumnNode2.create(columnName));
|
|
5561
|
-
|
|
5657
|
+
const enumProcessed = this.processEnumSelection(columnRef, fieldDef.name);
|
|
5658
|
+
return columnName !== fieldDef.name && !AliasNode2.is(enumProcessed) ? this.wrapAlias(enumProcessed, IdentifierNode.create(fieldDef.name)) : enumProcessed;
|
|
5659
|
+
});
|
|
5660
|
+
}
|
|
5661
|
+
createTableNode(tableName, schemaName) {
|
|
5662
|
+
return schemaName ? TableNode2.createWithSchema(schemaName, tableName) : TableNode2.create(tableName);
|
|
5663
|
+
}
|
|
5664
|
+
requireCurrentScope() {
|
|
5665
|
+
const scope = this.scopes[this.scopes.length - 1];
|
|
5666
|
+
invariant8(scope, "No scope available");
|
|
5667
|
+
return scope;
|
|
5668
|
+
}
|
|
5669
|
+
// #endregion
|
|
5670
|
+
// #region enum value mapping
|
|
5671
|
+
modelUsesEnumWithMappedValues(model) {
|
|
5672
|
+
const modelDef = getModel(this.schema, model);
|
|
5673
|
+
if (!modelDef) {
|
|
5674
|
+
return false;
|
|
5675
|
+
}
|
|
5676
|
+
return this.getModelFields(modelDef).some((fieldDef) => {
|
|
5677
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
5678
|
+
if (!enumDef) {
|
|
5679
|
+
return false;
|
|
5680
|
+
}
|
|
5681
|
+
return Object.values(enumDef.fields ?? {}).some((f) => f.attributes?.some((attr) => attr.name === "@map"));
|
|
5562
5682
|
});
|
|
5563
5683
|
}
|
|
5684
|
+
getEnumValueMapping(enumDef) {
|
|
5685
|
+
const mapping = {};
|
|
5686
|
+
for (const [key, field] of Object.entries(enumDef.fields ?? {})) {
|
|
5687
|
+
const mappedName = this.getMappedName(field);
|
|
5688
|
+
if (mappedName) {
|
|
5689
|
+
mapping[key] = mappedName;
|
|
5690
|
+
}
|
|
5691
|
+
}
|
|
5692
|
+
return mapping;
|
|
5693
|
+
}
|
|
5694
|
+
processEnumMappingForColumns(model, columns, values) {
|
|
5695
|
+
if (ValuesNode.is(values)) {
|
|
5696
|
+
return ValuesNode.create(values.values.map((valueItems) => {
|
|
5697
|
+
if (PrimitiveValueListNode.is(valueItems)) {
|
|
5698
|
+
return PrimitiveValueListNode.create(this.processEnumMappingForValues(model, columns, valueItems.values));
|
|
5699
|
+
} else {
|
|
5700
|
+
return ValueListNode.create(this.processEnumMappingForValues(model, columns, valueItems.values));
|
|
5701
|
+
}
|
|
5702
|
+
}));
|
|
5703
|
+
} else if (PrimitiveValueListNode.is(values)) {
|
|
5704
|
+
return PrimitiveValueListNode.create(this.processEnumMappingForValues(model, columns, values.values));
|
|
5705
|
+
} else {
|
|
5706
|
+
return values;
|
|
5707
|
+
}
|
|
5708
|
+
}
|
|
5709
|
+
processEnumMappingForValues(model, columns, values) {
|
|
5710
|
+
const result = [];
|
|
5711
|
+
for (let i = 0; i < columns.length; i++) {
|
|
5712
|
+
const value = values[i];
|
|
5713
|
+
if (value === null || value === void 0) {
|
|
5714
|
+
result.push(value);
|
|
5715
|
+
continue;
|
|
5716
|
+
}
|
|
5717
|
+
result.push(this.processEnumMappingForValue(model, columns[i], value));
|
|
5718
|
+
}
|
|
5719
|
+
return result;
|
|
5720
|
+
}
|
|
5721
|
+
processEnumMappingForValue(model, column, value) {
|
|
5722
|
+
const fieldDef = getField(this.schema, model, column.column.name);
|
|
5723
|
+
if (!fieldDef) {
|
|
5724
|
+
return value;
|
|
5725
|
+
}
|
|
5726
|
+
if (!isEnum(this.schema, fieldDef.type)) {
|
|
5727
|
+
return value;
|
|
5728
|
+
}
|
|
5729
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
5730
|
+
if (!enumDef) {
|
|
5731
|
+
return value;
|
|
5732
|
+
}
|
|
5733
|
+
const enumValueMapping = this.getEnumValueMapping(enumDef);
|
|
5734
|
+
if (this.isOperationNode(value) && ValueNode.is(value) && typeof value.value === "string") {
|
|
5735
|
+
const mappedValue = enumValueMapping[value.value];
|
|
5736
|
+
if (mappedValue) {
|
|
5737
|
+
return ValueNode.create(mappedValue);
|
|
5738
|
+
}
|
|
5739
|
+
} else if (typeof value === "string") {
|
|
5740
|
+
const mappedValue = enumValueMapping[value];
|
|
5741
|
+
if (mappedValue) {
|
|
5742
|
+
return mappedValue;
|
|
5743
|
+
}
|
|
5744
|
+
}
|
|
5745
|
+
return value;
|
|
5746
|
+
}
|
|
5747
|
+
processEnumSelection(selection, fieldName) {
|
|
5748
|
+
const { alias, node } = stripAlias(selection);
|
|
5749
|
+
const fieldScope = this.resolveFieldFromScopes(fieldName);
|
|
5750
|
+
if (!fieldScope || !fieldScope.model) {
|
|
5751
|
+
return selection;
|
|
5752
|
+
}
|
|
5753
|
+
const aliasName = alias && IdentifierNode.is(alias) ? alias.name : fieldName;
|
|
5754
|
+
const fieldDef = getField(this.schema, fieldScope.model, fieldName);
|
|
5755
|
+
if (!fieldDef) {
|
|
5756
|
+
return selection;
|
|
5757
|
+
}
|
|
5758
|
+
const enumDef = getEnum(this.schema, fieldDef.type);
|
|
5759
|
+
if (!enumDef) {
|
|
5760
|
+
return selection;
|
|
5761
|
+
}
|
|
5762
|
+
const enumValueMapping = this.getEnumValueMapping(enumDef);
|
|
5763
|
+
if (Object.keys(enumValueMapping).length === 0) {
|
|
5764
|
+
return selection;
|
|
5765
|
+
}
|
|
5766
|
+
const eb = expressionBuilder3();
|
|
5767
|
+
const caseBuilder = eb.case();
|
|
5768
|
+
let caseWhen;
|
|
5769
|
+
for (const [key, value] of Object.entries(enumValueMapping)) {
|
|
5770
|
+
if (!caseWhen) {
|
|
5771
|
+
caseWhen = caseBuilder.when(new ExpressionWrapper(node), "=", value).then(key);
|
|
5772
|
+
} else {
|
|
5773
|
+
caseWhen = caseWhen.when(new ExpressionWrapper(node), "=", value).then(key);
|
|
5774
|
+
}
|
|
5775
|
+
}
|
|
5776
|
+
const finalExpr = caseWhen.else(eb.cast(new ExpressionWrapper(node), "text")).end();
|
|
5777
|
+
if (aliasName) {
|
|
5778
|
+
return finalExpr.as(aliasName).toOperationNode();
|
|
5779
|
+
} else {
|
|
5780
|
+
return finalExpr.toOperationNode();
|
|
5781
|
+
}
|
|
5782
|
+
}
|
|
5564
5783
|
};
|
|
5565
5784
|
|
|
5566
5785
|
// src/client/executor/zenstack-query-executor.ts
|
|
@@ -5576,7 +5795,8 @@ var ZenStackQueryExecutor = class _ZenStackQueryExecutor extends DefaultQueryExe
|
|
|
5576
5795
|
nameMapper;
|
|
5577
5796
|
constructor(client, driver, compiler, adapter, connectionProvider, plugins = [], suppressMutationHooks = false) {
|
|
5578
5797
|
super(compiler, adapter, connectionProvider, plugins), this.client = client, this.driver = driver, this.compiler = compiler, this.connectionProvider = connectionProvider, this.suppressMutationHooks = suppressMutationHooks;
|
|
5579
|
-
if (
|
|
5798
|
+
if (client.$schema.provider.type === "postgresql" || // postgres queries need to be schema-qualified
|
|
5799
|
+
this.schemaHasMappedNames(client.$schema)) {
|
|
5580
5800
|
this.nameMapper = new QueryNameMapper(client.$schema);
|
|
5581
5801
|
}
|
|
5582
5802
|
}
|
|
@@ -5872,7 +6092,7 @@ __export(functions_exports, {
|
|
|
5872
6092
|
startsWith: () => startsWith
|
|
5873
6093
|
});
|
|
5874
6094
|
import { invariant as invariant10, lowerCaseFirst, upperCaseFirst } from "@zenstackhq/common-helpers";
|
|
5875
|
-
import { sql as sql5, ValueNode } from "kysely";
|
|
6095
|
+
import { sql as sql5, ValueNode as ValueNode2 } from "kysely";
|
|
5876
6096
|
import { match as match15 } from "ts-pattern";
|
|
5877
6097
|
var contains = /* @__PURE__ */ __name((eb, args, context) => textMatch(eb, args, context, "contains"), "contains");
|
|
5878
6098
|
var search = /* @__PURE__ */ __name((_eb, _args) => {
|
|
@@ -5981,7 +6201,7 @@ var currentOperation = /* @__PURE__ */ __name((_eb, args, { operation }) => {
|
|
|
5981
6201
|
}, "currentOperation");
|
|
5982
6202
|
function processCasing(casing, result, model) {
|
|
5983
6203
|
const opNode = casing.toOperationNode();
|
|
5984
|
-
invariant10(
|
|
6204
|
+
invariant10(ValueNode2.is(opNode) && typeof opNode.value === "string", '"casting" parameter must be a string value');
|
|
5985
6205
|
result = match15(opNode.value).with("original", () => model).with("upper", () => result.toUpperCase()).with("lower", () => result.toLowerCase()).with("capitalize", () => upperCaseFirst(result)).with("uncapitalize", () => lowerCaseFirst(result)).otherwise(() => {
|
|
5986
6206
|
throw new Error(`Invalid casing value: ${opNode.value}. Must be "original", "upper", "lower", "capitalize", or "uncapitalize".`);
|
|
5987
6207
|
});
|
|
@@ -5993,7 +6213,7 @@ function readBoolean(expr, defaultValue) {
|
|
|
5993
6213
|
return defaultValue;
|
|
5994
6214
|
}
|
|
5995
6215
|
const opNode = expr.toOperationNode();
|
|
5996
|
-
invariant10(
|
|
6216
|
+
invariant10(ValueNode2.is(opNode), "expression must be a literal value");
|
|
5997
6217
|
return !!opNode.value;
|
|
5998
6218
|
}
|
|
5999
6219
|
__name(readBoolean, "readBoolean");
|
|
@@ -6017,7 +6237,22 @@ var SchemaDbPusher = class {
|
|
|
6017
6237
|
await this.kysely.transaction().execute(async (tx) => {
|
|
6018
6238
|
if (this.schema.enums && this.schema.provider.type === "postgresql") {
|
|
6019
6239
|
for (const [name, enumDef] of Object.entries(this.schema.enums)) {
|
|
6020
|
-
|
|
6240
|
+
let enumValues;
|
|
6241
|
+
if (enumDef.fields) {
|
|
6242
|
+
enumValues = Object.values(enumDef.fields).map((f) => {
|
|
6243
|
+
const mapAttr = f.attributes?.find((a) => a.name === "@map");
|
|
6244
|
+
if (!mapAttr || !mapAttr.args?.[0]) {
|
|
6245
|
+
return f.name;
|
|
6246
|
+
} else {
|
|
6247
|
+
const mappedName = schema_exports.ExpressionUtils.getLiteralValue(mapAttr.args[0].value);
|
|
6248
|
+
invariant11(mappedName && typeof mappedName === "string", `Invalid @map attribute for enum field ${f.name}`);
|
|
6249
|
+
return mappedName;
|
|
6250
|
+
}
|
|
6251
|
+
});
|
|
6252
|
+
} else {
|
|
6253
|
+
enumValues = Object.values(enumDef.values);
|
|
6254
|
+
}
|
|
6255
|
+
const createEnum = tx.schema.createType(name).asEnum(enumValues);
|
|
6021
6256
|
await createEnum.execute();
|
|
6022
6257
|
}
|
|
6023
6258
|
}
|