1
1
import {
2
2
FilterQuery ,
3
3
Model ,
4
- MongooseQueryOptions ,
4
+ MongooseBaseQueryOptions ,
5
5
QueryOptions ,
6
6
QueryWithHelpers ,
7
7
UpdateWriteOpResult
@@ -18,8 +18,8 @@ import {
18
18
import { DeleteSchemaOptions } from './types/DeleteSchemaOptions' ;
19
19
import mergeOptions from './utils/mergeOptions' ;
20
20
21
- type DeleteOptions < RawDocType > = MongoDbDeleteOptions & Omit < MongooseQueryOptions < RawDocType > , 'lean' | 'timestamps' > ;
22
- type UpdateOptions < RawDocType > = MongoDbUpdateOptions & Omit < MongooseQueryOptions < RawDocType > , 'lean' > ;
21
+ type DeleteOptions < RawDocType > = MongoDbDeleteOptions & Omit < MongooseBaseQueryOptions < RawDocType > , 'lean' | 'timestamps' > ;
22
+ type UpdateOptions < RawDocType > = MongoDbUpdateOptions & Omit < MongooseBaseQueryOptions < RawDocType > , 'lean' > ;
23
23
24
24
export interface DeletedStaticMethods < DocType , THelpers = { } , RawDocType = DocType > {
25
25
restoreOne (
@@ -54,23 +54,25 @@ export default function(
54
54
) : void {
55
55
schema . statics . deleteOne = async function < T > ( this : Model < any > , filter ?: FilterQuery < T > , options ?: DeleteOptions < T > | null ) {
56
56
const update = staticDelete ( deletedFieldOptions ) ;
57
- const result = await this . updateOne . call ( this , filter , update , mergeOptions ( options , schemaOptions ) ) ;
57
+ const updateOneFn = this . updateOne . bind ( this ) ;
58
+ const result = await updateOneFn ( filter ?? { } , update , mergeOptions ( options , schemaOptions ) ) ;
58
59
return convertToDeleteResult ( result ) ;
59
60
} ;
60
61
schema . statics . deleteMany = async function < T > ( this : Model < any > , filter ?: FilterQuery < T > , options ?: DeleteOptions < T > | null ) {
61
62
const update = staticDelete ( deletedFieldOptions ) ;
62
- const result = await this . updateMany . call ( this , filter , update , mergeOptions ( options , schemaOptions ) ) ;
63
+ const result = await this . updateMany . call ( this , filter ?? { } , update , mergeOptions ( options , schemaOptions ) ) ;
63
64
return convertToDeleteResult ( result ) ;
64
65
} ;
65
66
66
67
schema . statics . deleteOneByUser = async function < TUser , T > ( this : Model < any > , user : TUser , filter ?: FilterQuery < T > , options ?: DeleteOptions < T > | null ) {
67
68
const update = staticDelete ( deletedFieldOptions , user ) ;
68
- const result = await this . updateOne . call ( this , filter , update , mergeOptions ( options , schemaOptions ) ) ;
69
+ const updateOneFn = this . updateOne . bind ( this ) ;
70
+ const result = await updateOneFn ( filter ?? { } , update , mergeOptions ( options , schemaOptions ) ) ;
69
71
return convertToDeleteResult ( result ) ;
70
72
} ;
71
73
schema . statics . deleteManyByUser = async function < TUser , T > ( this : Model < any > , user : TUser , filter ?: FilterQuery < T > , options ?: DeleteOptions < T > | null ) {
72
74
const update = staticDelete ( deletedFieldOptions , user ) ;
73
- const result = await this . updateMany . call ( this , filter , update , mergeOptions ( options , schemaOptions ) ) ;
75
+ const result = await this . updateMany . call ( this , filter ?? { } , update , mergeOptions ( options , schemaOptions ) ) ;
74
76
return convertToDeleteResult ( result ) ;
75
77
} ;
76
78
@@ -85,7 +87,8 @@ export default function(
85
87
86
88
schema . statics . restoreOne = function < T > ( this : Model < any > , filter ?: FilterQuery < T > , options ?: UpdateOptions < T > | null ) {
87
89
const update = staticRestore ( deletedFieldOptions ) ;
88
- return this . updateOne . call ( this , { ...filter , deleted : true } , update , mergeOptions ( options , schemaOptions ) ) ;
90
+ const updateOneFn = this . updateOne . bind ( this ) ;
91
+ return updateOneFn ( { ...filter , deleted : true } , update , mergeOptions ( options , schemaOptions ) ) ;
89
92
} ;
90
93
schema . statics . restoreMany = function < T > ( this : Model < any > , filter ?: FilterQuery < T > , options ?: UpdateOptions < T > | null ) {
91
94
const update = staticRestore ( deletedFieldOptions ) ;
0 commit comments