@@ -304,33 +304,32 @@ class Service extends AdapterService {
304304 query . collation = params . collation ;
305305 }
306306
307- if ( id !== null ) {
308- query . $and = ( query . $and || [ ] ) . concat ( { [ this . id ] : id } ) ;
309-
310- return this . Model . findOneAndDelete ( query , params . mongoose ) . lean ( this . lean )
311- . exec ( ) . then ( result => {
312- if ( result === null ) {
313- throw new errors . NotFound ( `No record found for id '${ id } '` , query ) ;
314- }
315-
316- return result ;
317- } ) . then ( select ( params , this . id ) ) . catch ( errorHandler ) ;
318- }
319-
320307 const findParams = Object . assign ( { } , params , {
321308 paginate : false ,
322309 query
323310 } ) ;
324311
312+ if ( id !== null ) {
313+ query . $and = ( query . $and || [ ] ) . concat ( { [ this . id ] : id } ) ;
314+ }
315+
325316 // NOTE (EK): First fetch the record(s) so that we can return
326317 // it/them when we delete it/them.
327- return this . _getOrFind ( id , findParams ) . then ( data =>
328- this . Model . deleteMany ( query , params . mongoose )
318+ return this . _getOrFind ( id , findParams ) . then ( ( data ) => {
319+ if ( id !== null ) {
320+ return this . Model . deleteOne ( query , params . mongoose )
321+ . lean ( this . lean )
322+ . exec ( )
323+ . then ( ( ) => data )
324+ . then ( select ( params , this . id ) ) ;
325+ }
326+
327+ return this . Model . deleteMany ( query , params . mongoose )
329328 . lean ( this . lean )
330329 . exec ( )
331330 . then ( ( ) => data )
332- . then ( select ( params , this . id ) )
333- ) . catch ( errorHandler ) ;
331+ . then ( select ( params , this . id ) ) ;
332+ } ) . catch ( errorHandler ) ;
334333 }
335334}
336335
0 commit comments