Skip to content

Commit ea42cc1

Browse files
DoctorFTBymc9claude
authored
fix(orm): handle mapping enum array (#2819)
Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 2724ff6 commit ea42cc1

2 files changed

Lines changed: 124 additions & 6 deletions

File tree

‎packages/orm/src/client/executor/name-mapper.ts‎

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
type OrderByItemNode,
1818
PrimitiveValueListNode,
1919
type QueryId,
20+
RawNode,
2021
ReferenceNode,
2122
ReturningNode,
2223
SelectAllNode,
@@ -810,6 +811,15 @@ export class QueryNameMapper extends OperationNodeTransformer {
810811
if (mappedValue) {
811812
return mappedValue;
812813
}
814+
} else if (
815+
this.isOperationNode(value) &&
816+
ValueNode.is(value) &&
817+
Array.isArray(value.value) &&
818+
value.value.every((v) => typeof v === 'string')
819+
) {
820+
return ValueNode.create(value.value.map((v) => enumValueMapping[v] ?? v));
821+
} else if (Array.isArray(value) && value.every((v) => typeof v === 'string')) {
822+
return value.map((v) => enumValueMapping[v] ?? v);
813823
}
814824

815825
return value;
@@ -839,23 +849,39 @@ export class QueryNameMapper extends OperationNodeTransformer {
839849
return selection;
840850
}
841851

852+
const shouldUseArray = fieldDef.array;
853+
842854
const eb = expressionBuilder();
843-
const caseBuilder = eb.case();
855+
const caseNode = shouldUseArray ? ColumnNode.create('t') : node;
844856
let caseWhen: CaseWhenBuilder<any, any, any, any> | undefined;
857+
845858
for (const [key, value] of Object.entries(enumValueMapping)) {
846859
if (!caseWhen) {
847-
caseWhen = caseBuilder.when(new ExpressionWrapper(node), '=', value).then(key);
860+
caseWhen = eb.case().when(new ExpressionWrapper(caseNode), '=', value).then(key);
848861
} else {
849-
caseWhen = caseWhen.when(new ExpressionWrapper(node), '=', value).then(key);
862+
caseWhen = caseWhen.when(new ExpressionWrapper(caseNode), '=', value).then(key);
850863
}
851864
}
852865

853866
// the explicit cast to "text" is needed to address postgres's case-when type inference issue
854-
const finalExpr = caseWhen!.else(this.dialect.castText(new ExpressionWrapper(node))).end();
867+
const caseExpr = caseWhen!.else(this.dialect.castText(new ExpressionWrapper(caseNode))).end();
868+
869+
if (!shouldUseArray) {
870+
if (aliasName) {
871+
return caseExpr.as(aliasName).toOperationNode() as SelectionNodeChild;
872+
} else {
873+
return caseExpr.toOperationNode() as SelectionNodeChild;
874+
}
875+
}
876+
877+
const arrayExpr = new ExpressionWrapper(
878+
RawNode.create(['ARRAY(SELECT ', ' FROM unnest( ', ' ) AS t)'], [caseExpr.toOperationNode(), node]),
879+
);
880+
855881
if (aliasName) {
856-
return finalExpr.as(aliasName).toOperationNode() as SelectionNodeChild;
882+
return arrayExpr.as(aliasName).toOperationNode() as SelectionNodeChild;
857883
} else {
858-
return finalExpr.toOperationNode() as SelectionNodeChild;
884+
return arrayExpr.toOperationNode() as SelectionNodeChild;
859885
}
860886
}
861887

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { createTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
// https://github.com/zenstackhq/zenstack/issues/2818
5+
describe('Regression for issue #2818', () => {
6+
it('supported enum array', async () => {
7+
const schema = `
8+
enum TestTag {
9+
INFO @map("info")
10+
WARN @map("warn")
11+
12+
@@map("post_tag")
13+
}
14+
15+
model Test {
16+
id Int @id
17+
tag TestTag?
18+
tags TestTag[]
19+
20+
@@map("test")
21+
}
22+
`;
23+
24+
const db = await createTestClient(schema, { usePrismaPush: true, provider: 'postgresql' });
25+
26+
await db.test.create({ data: { id: 0 } });
27+
await db.test.create({ data: { id: 1, tag: 'INFO' } });
28+
await db.test.create({ data: { id: 2, tags: [] } });
29+
await db.test.create({ data: { id: 3, tags: ['INFO'] } });
30+
await db.test.create({ data: { id: 4, tags: ['INFO', 'WARN'] } });
31+
32+
await expect(db.test.findMany({ orderBy: { id: 'asc' } })).resolves.toEqual([
33+
{ id: 0, tag: null, tags: [] },
34+
{ id: 1, tag: 'INFO', tags: [] },
35+
{ id: 2, tag: null, tags: [] },
36+
{ id: 3, tag: null, tags: ['INFO'] },
37+
{ id: 4, tag: null, tags: ['INFO', 'WARN'] },
38+
]);
39+
40+
await db.$qb.updateTable('test').set({ tag: 'WARN' }).where('id', '=', 2).executeTakeFirst();
41+
await db.$qb
42+
.updateTable('test')
43+
.set({ tags: ['INFO', 'WARN'] })
44+
.where('id', '=', 2)
45+
.executeTakeFirst();
46+
await db.$qb.updateTable('test').set({ tags: [] }).where('id', '=', 4).executeTakeFirst();
47+
48+
await expect(db.test.findMany({ orderBy: { id: 'asc' } })).resolves.toEqual([
49+
{ id: 0, tag: null, tags: [] },
50+
{ id: 1, tag: 'INFO', tags: [] },
51+
{ id: 2, tag: 'WARN', tags: ['INFO', 'WARN'] },
52+
{ id: 3, tag: null, tags: ['INFO'] },
53+
{ id: 4, tag: null, tags: [] },
54+
]);
55+
});
56+
57+
it('supports enum array with partially mapped values', async () => {
58+
const schema = `
59+
enum TestTag {
60+
INFO @map("info")
61+
WARN
62+
63+
@@map("post_tag")
64+
}
65+
66+
model Test {
67+
id Int @id
68+
tag TestTag?
69+
tags TestTag[]
70+
71+
@@map("test")
72+
}
73+
`;
74+
75+
const db = await createTestClient(schema, { usePrismaPush: true, provider: 'postgresql' });
76+
77+
await db.test.create({ data: { id: 1, tag: 'WARN', tags: ['INFO', 'WARN'] } });
78+
await db.test.create({ data: { id: 2, tags: ['WARN'] } });
79+
80+
await expect(db.test.findMany({ orderBy: { id: 'asc' } })).resolves.toEqual([
81+
{ id: 1, tag: 'WARN', tags: ['INFO', 'WARN'] },
82+
{ id: 2, tag: null, tags: ['WARN'] },
83+
]);
84+
85+
await db.test.update({ where: { id: 2 }, data: { tags: { push: 'INFO' } } });
86+
await expect(db.test.findUnique({ where: { id: 2 } })).resolves.toEqual({
87+
id: 2,
88+
tag: null,
89+
tags: ['WARN', 'INFO'],
90+
});
91+
});
92+
});

0 commit comments

Comments
 (0)