@@ -33,12 +33,12 @@ import {
3333} from './types' ;
3434
3535import logger from './utils/logger' ;
36- import { addUpdatedOnField , generateId , getPrivateFindOptions , omitPrivateFields } from './utils/helpers' ;
36+ import { addUpdatedOnField , generateId , omitPrivateFields } from './utils/helpers' ;
3737import PopulateUtil from './utils/populate' ;
3838
3939import { inMemoryPublisher } from './events/in-memory' ;
4040
41- const defaultOptions : ServiceOptions = {
41+ const defaultOptions : ServiceOptions < IDocument > = {
4242 skipDeletedOnDocs : true ,
4343 publishEvents : true ,
4444 outbox : false ,
@@ -57,7 +57,7 @@ class Service<T extends IDocument> {
5757
5858 private _collectionName : string ;
5959
60- private options : ServiceOptions ;
60+ private options : ServiceOptions < T > ;
6161
6262 private db ;
6363
@@ -68,7 +68,7 @@ class Service<T extends IDocument> {
6868 constructor (
6969 collectionName : string ,
7070 db : IDatabase ,
71- options : ServiceOptions = { } ,
71+ options : ServiceOptions < T > = { } ,
7272 ) {
7373 this . _collectionName = collectionName ;
7474 this . db = db ;
@@ -240,21 +240,19 @@ class Service<T extends IDocument> {
240240 readConfig : ReadConfig = { } ,
241241 findOptions : FindOptions = { } ,
242242 ) : Promise < ( U & PopulateTypes ) | U | null > {
243- const { mode = 'private' , populate } = readConfig ;
243+ const { populate } = readConfig ;
244244
245245 const collection = await this . getCollection < U > ( ) ;
246246
247247 filter = this . handleReadOperations ( filter , readConfig ) ;
248248
249- const privateFindOptions = getPrivateFindOptions ( { mode, findOptions, privateFields : this . options . privateFields } ) ;
250-
251249 if ( populate ) {
252- const docs = await this . populateAggregate < U , PopulateTypes > ( collection , filter , readConfig , privateFindOptions ) ;
250+ const docs = await this . populateAggregate < U , PopulateTypes > ( collection , filter , readConfig , findOptions ) ;
253251
254252 return docs [ 0 ] || null ;
255253 }
256254
257- return collection . findOne < U > ( filter , privateFindOptions ) ;
255+ return collection . findOne < U > ( filter , findOptions ) ;
258256 }
259257
260258 // Method overloading for find
@@ -275,19 +273,17 @@ class Service<T extends IDocument> {
275273 readConfig : ReadConfig & { page ?: number ; perPage ?: number } = { } ,
276274 findOptions : FindOptions = { } ,
277275 ) : Promise < FindResult < U & PopulateTypes > | FindResult < U > > {
278- const { mode = 'private' , populate, page, perPage } = readConfig ;
276+ const { populate, page, perPage } = readConfig ;
279277
280278 const collection = await this . getCollection < U > ( ) ;
281279 const hasPaging = ! ! page && ! ! perPage ;
282280
283281 filter = this . handleReadOperations ( filter , readConfig ) ;
284282
285- const privateFindOptions = getPrivateFindOptions ( { mode, findOptions, privateFields : this . options . privateFields } ) ;
286-
287283 if ( ! hasPaging ) {
288284 const results = populate
289- ? await this . populateAggregate < U , PopulateTypes > ( collection , filter , readConfig , privateFindOptions )
290- : await collection . find < U > ( filter , privateFindOptions ) . toArray ( ) ;
285+ ? await this . populateAggregate < U , PopulateTypes > ( collection , filter , readConfig , findOptions )
286+ : await collection . find < U > ( filter , findOptions ) . toArray ( ) ;
291287
292288 return {
293289 pagesCount : 1 ,
@@ -296,13 +292,13 @@ class Service<T extends IDocument> {
296292 } ;
297293 }
298294
299- privateFindOptions . skip = ( page - 1 ) * perPage ;
300- privateFindOptions . limit = perPage ;
295+ findOptions . skip = ( page - 1 ) * perPage ;
296+ findOptions . limit = perPage ;
301297
302298 const [ results , count ] = await Promise . all ( [
303299 populate
304- ? this . populateAggregate < U , PopulateTypes > ( collection , filter , readConfig , privateFindOptions )
305- : collection . find < U > ( filter , privateFindOptions ) . toArray ( ) ,
300+ ? this . populateAggregate < U , PopulateTypes > ( collection , filter , readConfig , findOptions )
301+ : collection . find < U > ( filter , findOptions ) . toArray ( ) ,
306302 collection . countDocuments ( filter ) ,
307303 ] ) ;
308304
@@ -355,7 +351,7 @@ class Service<T extends IDocument> {
355351 createConfig : CreateConfig = { } ,
356352 insertOneOptions : InsertOneOptions = { } ,
357353 ) : Promise < U > => {
358- const { mode = 'private' , publishEvents } = createConfig ;
354+ const { publishEvents } = createConfig ;
359355 const collection = await this . getCollection < U > ( ) ;
360356
361357 const validEntity = await this . validateCreateOperation < U > ( object , createConfig ) ;
@@ -385,19 +381,15 @@ class Service<T extends IDocument> {
385381 await collection . insertOne ( validEntity as OptionalUnlessRequiredId < U > , insertOneOptions ) ;
386382 }
387383
388- if ( mode === 'public' ) {
389- return validEntity ;
390- }
391-
392- return omitPrivateFields < U > ( validEntity , this . options . privateFields ) ;
384+ return validEntity ;
393385 } ;
394386
395387 insertMany = async < U extends T = T > (
396388 objects : Partial < U > [ ] ,
397389 createConfig : CreateConfig = { } ,
398390 bulkWriteOptions : BulkWriteOptions = { } ,
399- ) : Promise < Array < U > > => {
400- const { mode = 'private' , publishEvents } = createConfig ;
391+ ) : Promise < U [ ] > => {
392+ const { publishEvents } = createConfig ;
401393
402394 const collection = await this . getCollection < U > ( ) ;
403395
@@ -430,11 +422,7 @@ class Service<T extends IDocument> {
430422 await collection . insertMany ( validEntities as OptionalUnlessRequiredId < U > [ ] , bulkWriteOptions ) ;
431423 }
432424
433- if ( mode === 'public' ) {
434- return validEntities ;
435- }
436-
437- return validEntities . map ( ( doc ) => omitPrivateFields < U > ( doc , this . options . privateFields ) ) ;
425+ return validEntities ;
438426 } ;
439427
440428 replaceOne = async (
@@ -474,7 +462,7 @@ class Service<T extends IDocument> {
474462 updateConfig : UpdateConfig = { } ,
475463 updateOptions : UpdateOptions = { } ,
476464 ) : Promise < U | null > {
477- const { mode = 'private' , validateSchema, publishEvents } = updateConfig ;
465+ const { validateSchema, publishEvents } = updateConfig ;
478466
479467 const collection = await this . getCollection < U > ( ) ;
480468
@@ -577,11 +565,7 @@ class Service<T extends IDocument> {
577565 ) ;
578566 }
579567
580- if ( mode === 'public' ) {
581- return newDoc ;
582- }
583-
584- return omitPrivateFields < U > ( newDoc , this . options . privateFields ) ;
568+ return newDoc ;
585569 }
586570
587571 updateMany < U extends T = T > (
@@ -604,7 +588,7 @@ class Service<T extends IDocument> {
604588 updateConfig : UpdateConfig = { } ,
605589 updateOptions : UpdateOptions = { } ,
606590 ) : Promise < U [ ] > {
607- const { mode = 'private' , validateSchema, publishEvents } = updateConfig ;
591+ const { validateSchema, publishEvents } = updateConfig ;
608592
609593 const collection = await this . getCollection < U > ( ) ;
610594
@@ -728,11 +712,7 @@ class Service<T extends IDocument> {
728712 await collection . bulkWrite ( bulkWriteQuery , updateOptions ) ;
729713 }
730714
731- if ( mode === 'public' ) {
732- return updated . map ( ( u ) => u ?. doc ) as U [ ] ;
733- }
734-
735- return updated . map ( ( u ) => omitPrivateFields < U > ( u ?. doc as U , this . options . privateFields ) ) as U [ ] ;
715+ return updated . map ( ( u ) => u ?. doc ) . filter ( Boolean ) as U [ ] ;
736716 }
737717
738718 deleteOne = async < U extends T = T > (
@@ -1008,6 +988,10 @@ class Service<T extends IDocument> {
1008988 this . collection = null ;
1009989 }
1010990 } ;
991+
992+ getPublic = < U extends T = T > ( doc : U | null ) : Partial < U > | null => {
993+ return omitPrivateFields < U > ( doc , this . options . privateFields || [ ] ) ;
994+ } ;
1011995}
1012996
1013997export default Service ;
0 commit comments