diff --git a/src/lib/types.ts b/src/lib/types.ts index 90b5aad..70a9bc3 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -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 = @@ -20,6 +21,7 @@ export type NestedWriteAction = export type NestedAction = | Prisma.PrismaAction + | MissingPrismaAction | NestedWriteAction | NestedReadAction | NestedQueryAction; diff --git a/test/e2e/smoke.test.ts b/test/e2e/smoke.test.ts index 68a623c..f9f942e 100644 --- a/test/e2e/smoke.test.ts +++ b/test/e2e/smoke.test.ts @@ -10,9 +10,6 @@ describe("smoke", () => { let secondUser: User; let post: Post; - beforeAll(() => { - testClient = new PrismaClient(); - }); beforeEach(async () => { firstUser = await client.user.create({ data: { @@ -70,6 +67,7 @@ describe("smoke", () => { describe("vanilla", () => { beforeAll(() => { + testClient = new PrismaClient(); testClient.$use( createNestedMiddleware((params, next) => { return next(params); @@ -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); + }); + }); }); diff --git a/test/unit/calls.test.ts b/test/unit/calls.test.ts index 9b91210..df91710 100644 --- a/test/unit/calls.test.ts +++ b/test/unit/calls.test.ts @@ -23,7 +23,8 @@ type MiddlewareCall = { | "findMany" | "include" | "select" - | "where"; + | "where" + | "groupBy"; argsPath: string; scope?: MiddlewareCall; relations: { @@ -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", { @@ -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", { diff --git a/test/unit/helpers/createParams.ts b/test/unit/helpers/createParams.ts index 562198c..8352bf3 100644 --- a/test/unit/helpers/createParams.ts +++ b/test/unit/helpers/createParams.ts @@ -64,6 +64,8 @@ type ArgsByAction< ? Parameters["count"]>[0] : Action extends "aggregate" ? Parameters["aggregate"]>[0] + : Action extends "groupBy" + ? Parameters["groupBy"]>[0] : Action extends "connectOrCreate" ? { where: Parameters["findUnique"]>[0];