Skip to content

Commit 16090dc

Browse files
Merge pull request #26 from nour-karoui/mohamed/cover-more-query-hooks
chore: handle missing query method hooks
2 parents 9cf04dd + d2f2082 commit 16090dc

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

src/soft-delete-plugin.ts

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
import mongoose, { CallbackError, SaveOptions } from 'mongoose';
1+
import mongoose, { CallbackError, MongooseQueryMiddleware, SaveOptions } from 'mongoose';
2+
3+
const QUERY_HOOK_METHODS: MongooseQueryMiddleware[] = [
4+
'find',
5+
'findOne',
6+
'count',
7+
'countDocuments',
8+
'updateMany',
9+
'updateOne',
10+
'findOneAndUpdate',
11+
'distinct',
12+
];
213

314
export const softDeletePlugin = (schema: mongoose.Schema) => {
415
schema.add({
@@ -14,7 +25,7 @@ export const softDeletePlugin = (schema: mongoose.Schema) => {
1425
});
1526

1627
// @ts-ignore
17-
schema.pre('find',
28+
schema.pre(QUERY_HOOK_METHODS,
1829
async function (this, next: (err?: CallbackError) => void) {
1930
if (this.getFilter().isDeleted === true) {
2031
return next();
@@ -24,26 +35,6 @@ export const softDeletePlugin = (schema: mongoose.Schema) => {
2435
},
2536
);
2637

27-
// @ts-ignore
28-
schema.pre('count',
29-
async function (this, next: (err?: CallbackError) => void) {
30-
if (this.getFilter().isDeleted === true) {
31-
return next();
32-
}
33-
this.setQuery({ ...this.getFilter(), isDeleted: { $ne: true } });
34-
next();
35-
})
36-
37-
// @ts-ignore
38-
schema.pre('countDocuments',
39-
async function (this, next: (err?: CallbackError) => void) {
40-
if (this.getFilter().isDeleted === true) {
41-
return next();
42-
}
43-
this.setQuery({ ...this.getFilter(), isDeleted: { $ne: true } });
44-
next();
45-
})
46-
4738
schema.static('findDeleted', async function () {
4839
return this.find({ isDeleted: true });
4940
});

tests/index.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ describe('soft delete plugin', () => {
3939
// get this user after we performed soft delete
4040
const userAfterDelete = await userModel.find({ _id: user._id });
4141
expect(userAfterDelete?.length).toBe(0);
42+
43+
const usersCount = await userModel.countDocuments();
44+
expect(usersCount).toBe(0);
45+
46+
//soft deleted documents should not be updated by updateOne
47+
const updatedUser = await userModel.updateOne({ _id: user._id }, { $set: { name: 'james' } });
48+
expect(updatedUser.modifiedCount).toBe(0);
49+
50+
//soft deleted documents should not be updated by updateMany
51+
const updatedUsers = await userModel.updateMany({ _id: user._id }, { $set: { name: 'james2' } });
52+
expect(updatedUsers.modifiedCount).toBe(0);
53+
54+
const allUserIds = await userModel.distinct('_id');
55+
expect(allUserIds.length).toBe(0);
56+
57+
const allUserIdsWithDeleted = await userModel.distinct('_id', { isDeleted: true });
58+
expect(allUserIdsWithDeleted.length).toBe(1);
4259
});
4360

4461
test('restore should be successed', async () => {

0 commit comments

Comments
 (0)