Skip to content

Commit

Permalink
fix: allow non-scalar values when using client expressions
Browse files Browse the repository at this point in the history
This fixes an issue where client expressions fail if the value is not
a scalar, e.g. if using `some`.

Signed-off-by: Lucian Buzzo <[email protected]>
  • Loading branch information
LucianBuzzo committed Dec 14, 2023
1 parent 7802d6b commit 2dbaf09
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions test/integration/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,5 +625,96 @@ describe("expressions", () => {

expect(post.id).toBeDefined();
});

it.only("should be able to handle expressions that are multi-level objects", async () => {
const initial = new PrismaClient();

const role = `USER_${uuid()}`;

const client = await setup({
prisma: initial,
customAbilities: {
Post: {
customReadAbility: {
description: "Read user that made a post with a tag labeled with the tag context value",
operation: "SELECT",
expression: (client: PrismaClient, row, context) => {
return client.post.findFirst({
where: {
authorId: row("id"),
tags: {
some: {
label: context("tag_label"),
},
},
},
});
},
},
},
},
getRoles(abilities) {
return {
[role]: [abilities.Post.create, abilities.Post.customReadAbility, abilities.Tag.read, abilities.Tag.create],
};
},
getContext: () => ({
role,
context: {
tag_label: "foo",
},
}),
});

const testTitle = `test_${uuid()}`;

const user1 = await adminClient.user.create({
data: {
email: `test-user-${uuid()}@example.com`,
posts: {
create: {
title: testTitle,
tags: {
create: {
label: "foo",
},
},
},
},
},
});

const user2 = await adminClient.user.create({
data: {
email: `test-user-${uuid()}@example.com`,
posts: {
create: {
title: testTitle,
tags: {
create: {
label: "bar",
},
},
},
},
},
});

const result1 = await client.user.findFirst({
where: {
id: user1.id,
},
});

expect(result1).not.toBeNull();

const result2 = await client.user.findFirst({
where: {
id: user2.id,
},
});

expect(result2).toBeNull();
});
});
});

0 comments on commit 2dbaf09

Please sign in to comment.