Skip to content

Commit

Permalink
feat: add support for groupBy queries (#7)
Browse files Browse the repository at this point in the history
Update version of prisma-nested-middleware to v3.0.3 so 'groupBy' is included as an option for NestedAction.
  • Loading branch information
gkothe authored May 26, 2023
1 parent 82561f6 commit 35f2bc7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"author": "Olivier Wilkinson",
"license": "Apache-2.0",
"dependencies": {
"prisma-nested-middleware": "^3.0.0"
"prisma-nested-middleware": "^3.0.3"
},
"peerDependencies": {
"@prisma/client": "*"
Expand Down
33 changes: 32 additions & 1 deletion src/lib/actionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,38 @@ export function createFindManyMiddleware(
};
}

/* Count middlware */

/*GroupBy middleware */
function createGroupByParams(
params: NestedParams,
config: ModelConfig
): NestedParams {
return {
...params,
action: "groupBy",
args: {
...params.args,
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
},
},
};
}

export function createGroupByMiddleware(
config: ModelConfig
): NestedMiddleware {
return function groupByMiddleware(params, next) {
return next(createGroupByParams(params, config));
};
}



/* Count middleware */

function createCountParams(
params: NestedParams,
Expand Down
2 changes: 2 additions & 0 deletions src/lib/createSoftDeleteMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createUpdateMiddleware,
createUpsertMiddleware,
createWhereMiddleware,
createGroupByMiddleware,
} from "./actionMiddleware";

import { Config, ModelConfig } from "./types";
Expand Down Expand Up @@ -71,6 +72,7 @@ export function createSoftDeleteMiddleware({
where: createWhereMiddleware(config),
include: createIncludeMiddleware(config),
select: createSelectMiddleware(config),
groupBy: createGroupByMiddleware(config),
},
};
}, {});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ export type ModelConfig = {
export type Config = {
models: Partial<Record<Prisma.ModelName, ModelConfig | boolean>>;
defaultConfig?: ModelConfig;
};
};
65 changes: 65 additions & 0 deletions test/unit/groupBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { createSoftDeleteMiddleware } from "../../src";
import { createParams } from "./utils/createParams";

describe("groupBy", () => {

//group by must always have by and order by, else we get an error,
it("does not change groupBy action if model is not in the list", async () => {
const middleware = createSoftDeleteMiddleware({ models: {} });

const params = createParams("User", "groupBy", { where: { id: 1 }, by: ['id'], orderBy: {}, });
const next = jest.fn(() => Promise.resolve({}));
await middleware(params, next);
// params have not been modified
expect(next).toHaveBeenCalledWith(params);
});

it("does not modify groupBy results", async () => {
const middleware = createSoftDeleteMiddleware({ models: { User: true } });
const params = createParams("User", "groupBy", { where: { id: 1 }, by: ['id'], orderBy: {}, });
const next = jest.fn(() => Promise.resolve([{ id: 1, deleted: true }]));
expect(await middleware(params, next)).toEqual([{ id: 1, deleted: true }]);
});

it("excludes deleted records from groupBy", async () => {
const middleware = createSoftDeleteMiddleware({
models: { User: true },
});

const params = createParams("User", "groupBy", { where: { id: 1 },by: ['id'], orderBy: {} });
const next = jest.fn(() => Promise.resolve({}));

await middleware(params, next);

// params have been modified
expect(next).toHaveBeenCalledWith({
...params,
action: "groupBy",
args: {
by: ['id'],
orderBy: { },
where: {
id: 1,
deleted: false,
},
},
});
});


it("allows explicitly querying for deleted records using groupBy", async () => {
const middleware = createSoftDeleteMiddleware({
models: { User: true },
});

const params = createParams("User", "groupBy", {
where: { id: 1, deleted: true }, by: ['id'], orderBy: {}
});
const next = jest.fn(() => Promise.resolve({}));

await middleware(params, next);

// params have not been modified
expect(next).toHaveBeenCalledWith(params);
});
});
2 changes: 2 additions & 0 deletions test/unit/utils/createParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type ArgsByAction<
? Parameters<DelegateByModel<Model>["updateMany"]>[0]
: Action extends "findUnique"
? Parameters<DelegateByModel<Model>["findUnique"]>[0]
: Action extends "groupBy"
? Parameters<DelegateByModel<Model>["groupBy"]>[0]
: Action extends "findFirst"
? Parameters<DelegateByModel<Model>["findFirst"]>[0]
: Action extends "findMany"
Expand Down

0 comments on commit 35f2bc7

Please sign in to comment.