@@ -101,7 +101,7 @@ module.exports = function (schema, options) {
101101
102102 if ( options . overrideMethods ) {
103103 var overrideItems = options . overrideMethods ;
104- var overridableMethods = [ 'count' , 'find' , 'findOne' , 'findOneAndUpdate' , 'update' ] ;
104+ var overridableMethods = [ 'count' , 'countDocuments' , ' find', 'findOne' , 'findOneAndUpdate' , 'update' ] ;
105105 var finalList = [ ] ;
106106
107107 if ( ( typeof overrideItems === 'string' || overrideItems instanceof String ) && overrideItems === 'all' ) {
@@ -121,15 +121,23 @@ module.exports = function (schema, options) {
121121 }
122122
123123 finalList . forEach ( function ( method ) {
124- if ( method === 'count' || method === 'find' || method === 'findOne' ) {
124+ if ( [ 'count' , 'countDocuments' , 'find' , 'findOne' ] . indexOf ( method ) > - 1 ) {
125+ var modelMethodName = method ;
126+
127+ // countDocuments do not exist in Mongoose v4
128+ /* istanbul ignore next */
129+ if ( method === 'countDocuments' && typeof Model . countDocuments !== 'function' ) {
130+ modelMethodName = 'count' ;
131+ }
132+
125133 schema . statics [ method ] = function ( ) {
126- return Model [ method ] . apply ( this , arguments ) . where ( 'deleted' ) . ne ( true ) ;
134+ return Model [ modelMethodName ] . apply ( this , arguments ) . where ( 'deleted' ) . ne ( true ) ;
127135 } ;
128136 schema . statics [ method + 'Deleted' ] = function ( ) {
129- return Model [ method ] . apply ( this , arguments ) . where ( 'deleted' ) . ne ( false ) ;
137+ return Model [ modelMethodName ] . apply ( this , arguments ) . where ( 'deleted' ) . ne ( false ) ;
130138 } ;
131139 schema . statics [ method + 'WithDeleted' ] = function ( ) {
132- return Model [ method ] . apply ( this , arguments ) ;
140+ return Model [ modelMethodName ] . apply ( this , arguments ) ;
133141 } ;
134142 } else {
135143 schema . statics [ method ] = function ( ) {
@@ -208,6 +216,19 @@ module.exports = function (schema, options) {
208216 }
209217 } ;
210218
219+ schema . statics . deleteById = function ( id , deletedBy , callback ) {
220+ if ( arguments . length === 0 || typeof id === 'function' ) {
221+ var msg = 'First argument is mandatory and must not be a function.' ;
222+ throw new TypeError ( msg ) ;
223+ }
224+
225+ var conditions = {
226+ _id : id
227+ } ;
228+
229+ return this . delete ( conditions , deletedBy , callback ) ;
230+ } ;
231+
211232 schema . methods . restore = function ( callback ) {
212233 this . deleted = false ;
213234 this . deletedAt = undefined ;
0 commit comments