@@ -7,24 +7,50 @@ type DataSourceOperation = "findOne" | "find" | "count";
77export default class MongoDataSource < TSchema extends Document = Document >
88 implements DataSource
99{
10- context : any ;
11- collection : Collection < TSchema > ;
12-
13- cachePrefix : string ;
10+ /**
11+ * The prefix for the cache key
12+ * @default "mongodb"
13+ */
14+ cachePrefix = "mongodb" ;
1415
1516 private pendingResults : { key : string ; promise : Promise < any > } [ ] = [ ] ;
1617
17- constructor ( collection : Collection < TSchema > , public cache ?: KeyValueCache ) {
18- this . collection = collection ;
18+ private defaultTTL = 60 ;
19+
20+ constructor (
21+ /**
22+ * MongoDB collection for the data source.
23+ */
24+ public collection : Collection < TSchema > ,
25+ /**
26+ * Cache instance
27+ */
28+ public cache ?: KeyValueCache ,
29+ /**
30+ * Options for the DataSource
31+ */
32+ options ?: {
33+ /**
34+ * The default TTL for the cache
35+ */
36+ defaultTTL ?: number ;
37+ /**
38+ * The prefix for the cache key
39+ */
40+ cachePrefix ?: string ;
41+ }
42+ ) {
43+ this . cachePrefix = `${ this . cachePrefix } -${ this . collection . dbName } -${ this . collection . collectionName } -` ;
1944
20- this . cachePrefix = `mongo-${ this . collection . dbName } -${ this . collection . collectionName } -` ;
45+ if ( options ?. defaultTTL ) this . defaultTTL = options . defaultTTL ;
46+ if ( options ?. cachePrefix ) this . cachePrefix = options . cachePrefix ;
2147 }
2248
23- initialize ( { cache } : { context : any ; cache : KeyValueCache } ) {
49+ initialize ( { cache } : { cache : KeyValueCache } ) {
2450 this . cache = cache ;
2551 }
2652
27- async count ( query : { } = { } , options = { ttl : 60 } ) {
53+ async count ( query : { } = { } , options = { ttl : this . defaultTTL } ) {
2854 const cacheKey = this . getCacheKey ( "count" , query ) ,
2955 cacheDoc = await this . cache ?. get ( cacheKey ) ;
3056 if ( cacheDoc ) return JSON . parse ( cacheDoc ) ;
@@ -42,7 +68,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
4268 async find (
4369 fields : any = { } ,
4470 options : { ttl : number ; findOptions ?: FindOptions < Document > } = {
45- ttl : 60
71+ ttl : this . defaultTTL
4672 }
4773 ) : Promise < TSchema [ ] > {
4874 const cacheKey = this . getCacheKey ( "find" , fields , options ) ,
@@ -54,7 +80,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
5480 const r = await this . collection
5581 . find ( fields , options . findOptions )
5682 . toArray ( ) ;
57- this . cache ?. set ( cacheKey , JSON . stringify ( r ) , options ) ;
83+ await this . cache ?. set ( cacheKey , JSON . stringify ( r ) , options ) ;
5884
5985 return r ;
6086 } ) ;
@@ -65,7 +91,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
6591 async findOne (
6692 fields : any = { } ,
6793 options : { ttl : number ; findOptions ?: FindOptions } = {
68- ttl : 60
94+ ttl : this . defaultTTL
6995 }
7096 ) : Promise < TSchema | null > {
7197 const cacheKey = this . getCacheKey ( "findOne" , fields , options ) ,
@@ -75,7 +101,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
75101
76102 const docs = await this . antiSpam ( cacheKey , async ( ) => {
77103 const r = await this . collection . findOne ( fields , options . findOptions ) ;
78- this . cache ?. set ( cacheKey , JSON . stringify ( r ) , options ) ;
104+ await this . cache ?. set ( cacheKey , JSON . stringify ( r ) , options ) ;
79105
80106 return r ;
81107 } ) ;
@@ -85,7 +111,7 @@ export default class MongoDataSource<TSchema extends Document = Document>
85111
86112 async delete (
87113 type : DataSourceOperation ,
88- fields : any ,
114+ fields : any = { } ,
89115 options : { findOptions ?: FindOptions } = { }
90116 ) {
91117 return await this . cache ?. delete ( this . getCacheKey ( type , fields , options ) ) ;
0 commit comments