Skip to content
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
fix: groupBy not included in NestedAction type
Prisma client does not include groupBy in the Prisma.PrismaAction types.
This appears to be an omission because middleware is called with the
action set to 'groupBy'.

Include it in NestedAction types while we wait for it to be added to
Prisma.PrismaAction.
  • Loading branch information
olivierwilkinson committed May 25, 2023
commit fafca3931d26abfde17cead18f9809d367d77606
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DeferredPromise } from "@open-draft/deferred-promise";
export type Modifier = "is" | "isNot" | "some" | "none" | "every";
export type LogicalOperator = "AND" | "OR" | "NOT";

export type MissingPrismaAction = "groupBy";
export type NestedQueryAction = "where";
export type NestedReadAction = "include" | "select";
export type NestedWriteAction =
Expand All @@ -20,6 +21,7 @@ export type NestedWriteAction =

export type NestedAction =
| Prisma.PrismaAction
| MissingPrismaAction
| NestedWriteAction
| NestedReadAction
| NestedQueryAction;
Expand Down
33 changes: 30 additions & 3 deletions test/e2e/smoke.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ describe("smoke", () => {
let secondUser: User;
let post: Post;

beforeAll(() => {
testClient = new PrismaClient();
});
beforeEach(async () => {
firstUser = await client.user.create({
data: {
Expand Down Expand Up @@ -70,6 +67,7 @@ describe("smoke", () => {

describe("vanilla", () => {
beforeAll(() => {
testClient = new PrismaClient();
testClient.$use(
createNestedMiddleware((params, next) => {
return next(params);
Expand Down Expand Up @@ -153,4 +151,33 @@ describe("smoke", () => {
expect(profile!.meta).toBe(null);
});
});

describe('groupBy', () => {
beforeAll(() => {
testClient = new PrismaClient();
testClient.$use(
createNestedMiddleware((params, next) => {
if (params.action !== 'groupBy') {
throw new Error('expected groupBy action')
}
return next(params);
})
);
});

it('calls middleware with groupBy action', async () => {
await expect(testClient.comment.findMany()).rejects.toThrowError(
'expected groupBy action'
);

const groupBy = await testClient.comment.groupBy({
by: ['authorId'],
orderBy: {
authorId: 'asc',
},
});

expect(groupBy).toHaveLength(1);
});
});
});
36 changes: 35 additions & 1 deletion test/unit/calls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type MiddlewareCall<Model extends Prisma.ModelName> = {
| "findMany"
| "include"
| "select"
| "where";
| "where"
| "groupBy";
argsPath: string;
scope?: MiddlewareCall<any>;
relations: {
Expand Down Expand Up @@ -100,6 +101,13 @@ describe("calls", () => {
description: "aggregate",
rootParams: createParams("User", "aggregate", {}),
},
{
description: "groupBy",
rootParams: createParams("User", "groupBy", {
by: ["email"],
orderBy: { email: "asc" },
})
},
{
description: "nested create in create",
rootParams: createParams("User", "create", {
Expand Down Expand Up @@ -4048,6 +4056,32 @@ describe("calls", () => {
},
],
},
{
description: "where in groupBy",
rootParams: createParams("User", "groupBy", {
by: ["id"],
orderBy: { id: "asc" },
where: {
posts: {
some: {
title: "foo",
},
},
},
}),
nestedCalls: [
{
action: "where",
model: "Post",
argsPath: "args.where.posts.some",
modifier: "some",
relations: {
to: getModelRelation("User", "posts"),
from: getModelRelation("Post", "author"),
},
},
],
},
{
description: "correct relations for relations between same model",
rootParams: createParams("Comment", "findMany", {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/helpers/createParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ type ArgsByAction<
? Parameters<DelegateByModel<Model>["count"]>[0]
: Action extends "aggregate"
? Parameters<DelegateByModel<Model>["aggregate"]>[0]
: Action extends "groupBy"
? Parameters<DelegateByModel<Model>["groupBy"]>[0]
: Action extends "connectOrCreate"
? {
where: Parameters<DelegateByModel<Model>["findUnique"]>[0];
Expand Down