Skip to content

fix: @omit doesn't remove fields inside to-many relation #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/runtime/src/enhancements/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ class OmitHandler extends DefaultPrismaProxyHandler {
continue;
}

if (fieldInfo.attributes?.find((attr) => attr.name === '@omit')) {
const shouldOmit = fieldInfo.attributes?.find((attr) => attr.name === '@omit');
if (shouldOmit) {
delete entityData[field];
} else if (fieldInfo.isDataModel) {
// recurse
await this.doPostProcess(entityData[field], fieldInfo.type);
}

if (fieldInfo.isDataModel) {
const items =
fieldInfo.isArray && Array.isArray(entityData[field]) ? entityData[field] : [entityData[field]];
for (const item of items) {
// recurse
await this.doPostProcess(item, fieldInfo.type);
}
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions tests/integration/tests/enhancements/with-omit/with-omit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,52 @@ describe('Omit test', () => {
expect(r1.password).toBeUndefined();
expect(r1.profile.image).toBeUndefined();
});

it('to-many', async () => {
const { withOmit } = await loadSchema(
`
model User {
id String @id @default(cuid())
posts Post[]

@@allow('all', true)
}

model Post {
id String @id @default(cuid())
user User @relation(fields: [userId], references: [id])
userId String
images Image[]

@@allow('all', true)
}

model Image {
id String @id @default(cuid())
post Post @relation(fields: [postId], references: [id])
postId String
url String @omit

@@allow('all', true)
}
`
);

const db = withOmit();
const r = await db.user.create({
include: { posts: { include: { images: true } } },
data: {
posts: {
create: [
{ images: { create: { url: 'img1' } } },
{ images: { create: [{ url: 'img2' }, { url: 'img3' }] } },
],
},
},
});

expect(r.posts[0].images[0].url).toBeUndefined();
expect(r.posts[1].images[0].url).toBeUndefined();
expect(r.posts[1].images[1].url).toBeUndefined();
});
});
45 changes: 45 additions & 0 deletions tests/integration/tests/regression/issue-992.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression: issue 992', () => {
it('regression', async () => {
const { enhance, prisma } = await loadSchema(
`
model Product {
id String @id @default(cuid())
category Category @relation(fields: [categoryId], references: [id])
categoryId String

deleted Int @default(0) @omit
@@deny('read', deleted != 0)
@@allow('all', true)
}

model Category {
id String @id @default(cuid())
products Product[]
@@allow('all', true)
}
`
);

await prisma.category.create({
data: {
products: {
create: [
{
deleted: 0,
},
{
deleted: 0,
},
],
},
},
});

const db = enhance();
const category = await db.category.findFirst({ include: { products: true } });
expect(category.products[0].deleted).toBeUndefined();
expect(category.products[1].deleted).toBeUndefined();
});
});