@@ -34,7 +34,7 @@ const connections = []
34
34
35
35
const mongoOpts = {
36
36
//useNewUrlParser: true,
37
- useUnifiedTopology : true ,
37
+ // useUnifiedTopology: true
38
38
}
39
39
40
40
/**
@@ -52,7 +52,7 @@ export class DataSourceMongoDb extends DataSource {
52
52
this . url = url
53
53
}
54
54
55
- connect ( client ) {
55
+ connect ( client ) {
56
56
return async function ( ) {
57
57
let timeout = false
58
58
const timerId = setTimeout ( ( ) => {
@@ -64,7 +64,24 @@ export class DataSourceMongoDb extends DataSource {
64
64
}
65
65
}
66
66
67
- async connection ( ) {
67
+ async connectionPool ( ) {
68
+ return new Promise ( ( resolve , reject ) => {
69
+ if ( this . db ) return resolve ( this . db )
70
+ MongoClient . connect (
71
+ this . url ,
72
+ {
73
+ ...this . mongoOpts ,
74
+ poolSize : dsOptions . numConns || 2
75
+ } ,
76
+ ( err , database ) => {
77
+ if ( err ) return reject ( err )
78
+ resolve ( ( this . db = database . db ( this . namespace ) ) )
79
+ }
80
+ )
81
+ } )
82
+ }
83
+
84
+ async connection ( ) {
68
85
try {
69
86
while ( connections . length < ( dsOptions . numConns || 1 ) ) {
70
87
const client = new MongoClient ( this . url , this . mongoOpts )
@@ -73,12 +90,12 @@ export class DataSourceMongoDb extends DataSource {
73
90
errorRate : 1 ,
74
91
callVolume : 1 ,
75
92
intervalMs : 10000 ,
76
- testDelay : 300000 ,
93
+ testDelay : 300000
77
94
//fallbackFn: () => client.emit('connectionClosed')
78
- } ,
95
+ }
79
96
}
80
97
const breaker = CircuitBreaker (
81
- 'mongo.conn ' ,
98
+ 'mongodb.connect ' ,
82
99
this . connect ( client ) ,
83
100
thresholds
84
101
)
@@ -224,15 +241,13 @@ export class DataSourceMongoDb extends DataSource {
224
241
/**
225
242
*
226
243
* @param {Object } filter Supposed to be a valid Mongo Filter
227
- * @param {Object } options Options to sort limit aggregate etc...
228
- * @param {Object } options.sort a valid Mongo sort object
229
- * @param {Number } options.limit a valid Mongo limit
230
- * @param {Object } options.aggregate a valid Mongo aggregate object
244
+ * @param {Object } sort a valid Mongo sort object
245
+ * @param {Number } limit a valid Mongo limit
246
+ * @param {Object } aggregate a valid Mongo aggregate object
231
247
*
232
- * @returns
248
+ * @returns { Promise<import('mongodb').AbstractCursor> }
233
249
*/
234
-
235
- async mongoFind ( { filter, sort, limit, skip, aggregate } = { } ) {
250
+ async mongoFind ( { filter, sort, limit, aggregate, skip } = { } ) {
236
251
console . log ( { fn : this . mongoFind . name , filter } )
237
252
let cursor = aggregate
238
253
? ( await this . collection ( ) ) . aggregate ( aggregate )
@@ -363,40 +378,12 @@ export class DataSourceMongoDb extends DataSource {
363
378
}
364
379
365
380
/**
366
- * Returns the set of objects satisfying the `filter` if specified;
367
- * otherwise returns all objects. If a `writable`stream is provided and `cached`
368
- * is false, the list is streamed. Otherwise the list is returned in
369
- * an array. A custom transform can be specified to modify the streamed
370
- * results. Using {@link createWriteStream} updates can be streamed back
371
- * to the db. With streams, we can support queries of very large tables,
372
- * with minimal memory overhead on the node server.
373
381
*
374
382
* @override
375
- * @param {{key1:string, keyN:string} } filter - e.g. http query
376
- * @param {{
377
- * writable: WritableStream,
378
- * cached: boolean,
379
- * serialize: boolean,
380
- * transform: Transform
381
- * }} params
382
- * - details
383
- * - `serialize` seriailize input to writable
384
- * - `cached` list cache only
385
- * - `transform` transform stream before writing
386
- * - `writable` writable stream for output
383
+ * @param {import('../../domain/datasource').listOptions } param
387
384
*/
388
- async list ( param = { } ) {
389
- const {
390
- writable = null ,
391
- transform = null ,
392
- serialize = false ,
393
- query = { } ,
394
- } = param
395
- let result
385
+ async list ( param ) {
396
386
try {
397
- if ( query . __cached ) return super . listSync ( query )
398
- if ( query . __count ) return this . count ( )
399
-
400
387
const options = this . processOptions ( param )
401
388
if ( 0 < ~ ~ query . __page ) {
402
389
// qpm > processOptions weeds out __page - add it back properly as an integer
@@ -433,7 +420,11 @@ export class DataSourceMongoDb extends DataSource {
433
420
}
434
421
}
435
422
436
- async count ( ) {
423
+ /**
424
+ *
425
+ * @override
426
+ */
427
+ async count ( ) {
437
428
return {
438
429
total : await this . countDb ( ) ,
439
430
cached : this . getCacheSize ( ) ,
0 commit comments