Skip to content

Commit a4d3f15

Browse files
authored
fix: @omit doesn't remove fields inside to-many relation (#993)
1 parent da33881 commit a4d3f15

File tree

3 files changed

+104
-4
lines changed

3 files changed

+104
-4
lines changed

packages/runtime/src/enhancements/omit.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,18 @@ class OmitHandler extends DefaultPrismaProxyHandler {
5454
continue;
5555
}
5656

57-
if (fieldInfo.attributes?.find((attr) => attr.name === '@omit')) {
57+
const shouldOmit = fieldInfo.attributes?.find((attr) => attr.name === '@omit');
58+
if (shouldOmit) {
5859
delete entityData[field];
59-
} else if (fieldInfo.isDataModel) {
60-
// recurse
61-
await this.doPostProcess(entityData[field], fieldInfo.type);
60+
}
61+
62+
if (fieldInfo.isDataModel) {
63+
const items =
64+
fieldInfo.isArray && Array.isArray(entityData[field]) ? entityData[field] : [entityData[field]];
65+
for (const item of items) {
66+
// recurse
67+
await this.doPostProcess(item, fieldInfo.type);
68+
}
6269
}
6370
}
6471
}

tests/integration/tests/enhancements/with-omit/with-omit.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,52 @@ describe('Omit test', () => {
105105
expect(r1.password).toBeUndefined();
106106
expect(r1.profile.image).toBeUndefined();
107107
});
108+
109+
it('to-many', async () => {
110+
const { withOmit } = await loadSchema(
111+
`
112+
model User {
113+
id String @id @default(cuid())
114+
posts Post[]
115+
116+
@@allow('all', true)
117+
}
118+
119+
model Post {
120+
id String @id @default(cuid())
121+
user User @relation(fields: [userId], references: [id])
122+
userId String
123+
images Image[]
124+
125+
@@allow('all', true)
126+
}
127+
128+
model Image {
129+
id String @id @default(cuid())
130+
post Post @relation(fields: [postId], references: [id])
131+
postId String
132+
url String @omit
133+
134+
@@allow('all', true)
135+
}
136+
`
137+
);
138+
139+
const db = withOmit();
140+
const r = await db.user.create({
141+
include: { posts: { include: { images: true } } },
142+
data: {
143+
posts: {
144+
create: [
145+
{ images: { create: { url: 'img1' } } },
146+
{ images: { create: [{ url: 'img2' }, { url: 'img3' }] } },
147+
],
148+
},
149+
},
150+
});
151+
152+
expect(r.posts[0].images[0].url).toBeUndefined();
153+
expect(r.posts[1].images[0].url).toBeUndefined();
154+
expect(r.posts[1].images[1].url).toBeUndefined();
155+
});
108156
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Regression: issue 992', () => {
4+
it('regression', async () => {
5+
const { enhance, prisma } = await loadSchema(
6+
`
7+
model Product {
8+
id String @id @default(cuid())
9+
category Category @relation(fields: [categoryId], references: [id])
10+
categoryId String
11+
12+
deleted Int @default(0) @omit
13+
@@deny('read', deleted != 0)
14+
@@allow('all', true)
15+
}
16+
17+
model Category {
18+
id String @id @default(cuid())
19+
products Product[]
20+
@@allow('all', true)
21+
}
22+
`
23+
);
24+
25+
await prisma.category.create({
26+
data: {
27+
products: {
28+
create: [
29+
{
30+
deleted: 0,
31+
},
32+
{
33+
deleted: 0,
34+
},
35+
],
36+
},
37+
},
38+
});
39+
40+
const db = enhance();
41+
const category = await db.category.findFirst({ include: { products: true } });
42+
expect(category.products[0].deleted).toBeUndefined();
43+
expect(category.products[1].deleted).toBeUndefined();
44+
});
45+
});

0 commit comments

Comments
 (0)