Skip to content
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

Models are not being included on delete method. #19

Merged
merged 2 commits into from
Jan 15, 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: 14 additions & 1 deletion src/lib/actionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,24 @@ function createDeleteParams(
};
}

if (!!params.scope) {
return {
...params,
action: "update",
args: {
where: params.args,
data: {
[field]: createValue(true),
},
},
};
}

return {
...params,
action: "update",
args: {
where: params.args?.where || params.args,
...params.args,
data: {
[field]: createValue(true),
},
Expand Down
43 changes: 40 additions & 3 deletions test/unit/include.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { set } from "lodash";
import faker from "faker";

import { createSoftDeleteMiddleware } from "../../src";
import { createParams } from "./utils/createParams";
import { createParams, ActionByModel } from "./utils/createParams";

describe("include", () => {
it("does not change include params if model is not in the list", async () => {
Expand All @@ -21,6 +21,44 @@ describe("include", () => {
expect(next).toHaveBeenCalledWith(params);
});

it.each([
"delete",
"update",
"upsert",
"findFirst",
"findFirstOrThrow",
"findUnique",
"findUniqueOrThrow",
"findMany",
] as Array<ActionByModel<"User">>)(
"can include records for configured models in %s",
async (action) => {
const middleware = createSoftDeleteMiddleware({
models: { User: true },
});

const params = createParams("User", action, {
where: { id: 1 },
include: {
comments: true,
},
});

const next = jest.fn(() =>
Promise.resolve({
comments: [{ deleted: true }, { deleted: false }],
})
);

await middleware(params, next);

// @ts-expect-error - ts doesn't know there has been a call
expect(next.mock.calls[0][0]?.args?.include).toEqual({
comments: true,
});
}
);

it("uses params to exclude deleted records from toMany includes", async () => {
const middleware = createSoftDeleteMiddleware({
models: { Comment: true },
Expand Down Expand Up @@ -138,7 +176,7 @@ describe("include", () => {
expect(next).toHaveBeenCalledWith(params);
expect(result).toEqual({ author: null });
});

it("does not manually exclude non-deleted records from toOne include with nested includes", async () => {
const middleware = createSoftDeleteMiddleware({
models: { User: true },
Expand Down Expand Up @@ -263,7 +301,6 @@ describe("include", () => {
});
});


it("allows explicitly including deleted records using include", async () => {
const middleware = createSoftDeleteMiddleware({
models: { Comment: true },
Expand Down
40 changes: 39 additions & 1 deletion test/unit/select.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { set } from "lodash";
import faker from "faker";

import { createSoftDeleteMiddleware } from "../../src";
import { createParams } from "./utils/createParams";
import { ActionByModel, createParams } from "./utils/createParams";

describe("select", () => {
it("does not change select params if model is not in the list", async () => {
Expand All @@ -21,6 +21,44 @@ describe("select", () => {
expect(next).toHaveBeenCalledWith(params);
});

it.each([
"delete",
"update",
"upsert",
"findFirst",
"findFirstOrThrow",
"findUnique",
"findUniqueOrThrow",
"findMany",
] as Array<ActionByModel<"User">>)(
"can select records for configured models in %s",
async (action) => {
const middleware = createSoftDeleteMiddleware({
models: { User: true },
});

const params = createParams("User", action, {
where: { id: 1 },
select: {
comments: true,
},
});

const next = jest.fn(() =>
Promise.resolve({
comments: [{ deleted: true }, { deleted: false }],
})
);

await middleware(params, next);

// @ts-expect-error - ts doesn't know there has been a call
expect(next.mock.calls[0][0]?.args?.select).toEqual({
comments: true,
});
}
);

it("excludes deleted records from selects", async () => {
const middleware = createSoftDeleteMiddleware({
models: { Comment: true },
Expand Down
2 changes: 1 addition & 1 deletion test/unit/utils/createParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type IncludeByModel<Model extends Prisma.ModelName> = Model extends "User"
? Prisma.CommentInclude
: never;

type ActionByModel<Model extends Prisma.ModelName> =
export type ActionByModel<Model extends Prisma.ModelName> =
| keyof DelegateByModel<Model>
| "connectOrCreate"
| "select"
Expand Down
Loading