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

feat: allow overwrite in AND, OR in where queries #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
24 changes: 10 additions & 14 deletions src/lib/actionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NestedParams, NestedMiddleware } from "prisma-nested-middleware";
import { ModelConfig } from "./types";
import {
addDeletedToSelect,
isDeletedFieldOverWritten,
stripDeletedFieldFromResults,
} from "./utils/nestedReads";
import {
Expand Down Expand Up @@ -136,8 +137,7 @@ function createUpdateManyParams(
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down Expand Up @@ -268,8 +268,7 @@ function createFindFirstParams(
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand All @@ -283,6 +282,7 @@ export function createFindFirstMiddleware(
};
}


/* FindMany middleware */

function createFindManyParams(
Expand All @@ -297,8 +297,7 @@ function createFindManyParams(
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down Expand Up @@ -326,8 +325,7 @@ function createGroupByParams(
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down Expand Up @@ -359,7 +357,7 @@ function createCountParams(
where: {
...where,
// allow overriding the deleted field in where
[config.field]: where[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down Expand Up @@ -387,7 +385,7 @@ function createAggregateParams(
where: {
...where,
// allow overriding the deleted field in where
[config.field]: where[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down Expand Up @@ -456,8 +454,7 @@ function createIncludeParams(
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down Expand Up @@ -518,8 +515,7 @@ function createSelectParams(
where: {
...params.args?.where,
// allow overriding the deleted field in where
[config.field]:
params.args?.where?.[config.field] || config.createValue(false),
...(isDeletedFieldOverWritten(config.field, params.args?.where) ? {} : { [config.field]: config.createValue(false) })
},
},
};
Expand Down
26 changes: 26 additions & 0 deletions src/lib/utils/nestedReads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,29 @@ export function stripDeletedFieldFromResults(

return results;
}

export function isDeletedFieldOverWritten(field: string, where?: any): boolean {
if (!where) {
return false
}
if (where[field] !== undefined) {
return true
}
if (where.OR && Array.isArray(where.OR)) {
const isDeletedFieldOverWrittenInOR = where.OR.some((arg: any) => {
return isDeletedFieldOverWritten(field, arg)
})
if (isDeletedFieldOverWrittenInOR) {
return true
}
}
if (where.AND && Array.isArray(where.AND)) {
const isDeletedFieldOverWrittenInAND = where.AND.some((arg: any) => {
return isDeletedFieldOverWritten(field, arg)
})
if (isDeletedFieldOverWrittenInAND) {
return true
}
}
return false
}
9 changes: 9 additions & 0 deletions test/e2e/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ describe("queries", () => {
[firstUser.id, secondUser.id].sort()
);
});
it("findMany keeps soft deleted records when field is overwritten", async () => {
const foundUsers = await testClient.user.findMany({
where: { name: { contains: "J" }, OR: [{ deleted: false }, { deleted: true }] },
});
expect(foundUsers).toHaveLength(3);
expect(foundUsers.map(({ id }) => id).sort()).toEqual(
[firstUser.id, secondUser.id, deletedUser.id].sort()
);
})
});

describe("count", () => {
Expand Down
64 changes: 64 additions & 0 deletions test/unit/findMany.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,68 @@ describe("findMany", () => {
// params have not been modified
expect(next).toHaveBeenCalledWith(params);
});

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

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

await middleware(params, next);

// params have not been modified
expect(next).toHaveBeenCalledWith(params);
})

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

const params = createParams("User", "findMany", {
where: { id: 1, OR: [{ name: 'name' }, { OR: [{ deleted: { not: false } }] }] },
});
const next = jest.fn(() => Promise.resolve({}));

await middleware(params, next);

// params have not been modified
expect(next).toHaveBeenCalledWith(params);
})

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

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

await middleware(params, next);

// params have not been modified
expect(next).toHaveBeenCalledWith(params);
})

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

const params = createParams("User", "findMany", {
where: { id: 1, AND: [{ name: 'name' }, { OR: [{ deleted: { not: false } }] }] },
});
const next = jest.fn(() => Promise.resolve({}));

await middleware(params, next);

// params have not been modified
expect(next).toHaveBeenCalledWith(params);
})
});
51 changes: 51 additions & 0 deletions test/unit/utils/nestedReads.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { isDeletedFieldOverWritten } from '../../../src/lib/utils/nestedReads'

describe('nestedReads', () => {
describe('isDeletedFieldOverWritten', () => {
const DELETED_FIELD = 'deleted'

it.each([
[false, null],
[false, undefined],
[false, {}],
[false, { field: false }],
[false, { field: false, OR: [] }],
[false, { field: false, AND: [] }],
[true, { [DELETED_FIELD]: false }],
[true, { [DELETED_FIELD]: true }]
])
("should return %p when where field is %s", (expectedResult, where) => {
const result = isDeletedFieldOverWritten(DELETED_FIELD, where)

expect(result).toBe(expectedResult)
})

it("should return false when OR field doesn't contain the deleted field", () => {
const where = { OR: [{ field: 'value' }, { anotherField: 'value' }] }
const result = isDeletedFieldOverWritten(DELETED_FIELD, where);

expect(result).toBe(false)
})

it("should return false when AND field doesn't contain the deleted field", () => {
const where = { AND: [{ field: 'value' }, { anotherField: 'value' }] }
const result = isDeletedFieldOverWritten(DELETED_FIELD, where);

expect(result).toBe(false)
})

it("should return true when OR field contains the deleted field", () => {
const where = { OR: [{ field: 'value' }, { [DELETED_FIELD]: false }] }
const result = isDeletedFieldOverWritten(DELETED_FIELD, where);

expect(result).toBe(true)
})

it("should return true when AND field contains the deleted field", () => {
const where = { AND: [{ [DELETED_FIELD]: false }, { anotherField: 'value' }] }
const result = isDeletedFieldOverWritten(DELETED_FIELD, where);

expect(result).toBe(true)
})
})
})