Skip to content

Commit 1243422

Browse files
authored
fix(delegate): fields from delegate models used in logical groups inside filter are not translated (#1418)
1 parent 153dd4f commit 1243422

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

packages/runtime/src/enhancements/delegate.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
9999
}
100100

101101
Object.entries(where).forEach(([field, value]) => {
102+
if (['AND', 'OR', 'NOT'].includes(field)) {
103+
// recurse into logical group
104+
enumerate(value).forEach((item) => this.injectWhereHierarchy(model, item));
105+
return;
106+
}
107+
102108
const fieldInfo = resolveField(this.options.modelMeta, model, field);
103109
if (!fieldInfo?.inheritedFrom) {
104110
return;
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 1410', () => {
4+
it('regression', async () => {
5+
const { enhance } = await loadSchema(
6+
`
7+
model Drink {
8+
id Int @id @default(autoincrement())
9+
slug String @unique
10+
11+
manufacturer_id Int
12+
manufacturer Manufacturer @relation(fields: [manufacturer_id], references: [id])
13+
14+
type String
15+
16+
name String @unique
17+
description String
18+
abv Float
19+
image String?
20+
21+
gluten Boolean
22+
lactose Boolean
23+
organic Boolean
24+
25+
containers Container[]
26+
27+
@@delegate(type)
28+
29+
@@allow('all', true)
30+
}
31+
32+
model Beer extends Drink {
33+
style_id Int
34+
style BeerStyle @relation(fields: [style_id], references: [id])
35+
36+
ibu Float?
37+
38+
@@allow('all', true)
39+
}
40+
41+
model BeerStyle {
42+
id Int @id @default(autoincrement())
43+
44+
name String @unique
45+
color String
46+
47+
beers Beer[]
48+
49+
@@allow('all', true)
50+
}
51+
52+
model Wine extends Drink {
53+
style_id Int
54+
style WineStyle @relation(fields: [style_id], references: [id])
55+
56+
heavy_score Int?
57+
tannine_score Int?
58+
dry_score Int?
59+
fresh_score Int?
60+
notes String?
61+
62+
@@allow('all', true)
63+
}
64+
65+
model WineStyle {
66+
id Int @id @default(autoincrement())
67+
68+
name String @unique
69+
color String
70+
71+
wines Wine[]
72+
73+
@@allow('all', true)
74+
}
75+
76+
model Soda extends Drink {
77+
carbonated Boolean
78+
79+
@@allow('all', true)
80+
}
81+
82+
model Cocktail extends Drink {
83+
mix Boolean
84+
85+
@@allow('all', true)
86+
}
87+
88+
model Container {
89+
barcode String @id
90+
91+
drink_id Int
92+
drink Drink @relation(fields: [drink_id], references: [id])
93+
94+
type String
95+
volume Int
96+
portions Int?
97+
98+
inventory Int @default(0)
99+
100+
@@allow('all', true)
101+
}
102+
103+
model Manufacturer {
104+
id Int @id @default(autoincrement())
105+
106+
country_id String
107+
country Country @relation(fields: [country_id], references: [code])
108+
109+
name String @unique
110+
description String?
111+
image String?
112+
113+
drinks Drink[]
114+
115+
@@allow('all', true)
116+
}
117+
118+
model Country {
119+
code String @id
120+
name String
121+
122+
manufacturers Manufacturer[]
123+
124+
@@allow('all', true)
125+
}
126+
`
127+
);
128+
129+
const db = enhance();
130+
131+
await db.beer.findMany({
132+
include: { style: true, manufacturer: true },
133+
where: { NOT: { gluten: true } },
134+
});
135+
136+
await db.beer.findMany({
137+
include: { style: true, manufacturer: true },
138+
where: { AND: [{ gluten: true }, { abv: { gt: 50 } }] },
139+
});
140+
141+
await db.beer.findMany({
142+
include: { style: true, manufacturer: true },
143+
where: { OR: [{ AND: [{ NOT: { gluten: true } }] }, { abv: { gt: 50 } }] },
144+
});
145+
});
146+
});

0 commit comments

Comments
 (0)