@@ -21,11 +21,12 @@ const processQuery = qpm({
2121const url = process . env . MONGODB_URL || 'mongodb://localhost:27017'
2222
2323const dsOptions = configRoot . adapters . datasources . DataSourceMongoDb . options || {
24- runOffline : false ,
25- numConns : 2 ,
24+ runOffline : true ,
25+ numConns : 2
2626}
2727
2828const cacheSize = configRoot . adapters . cacheSize || 3000
29+ let connPool
2930
3031/**
3132 * @type {Map<string,MongoClient> }
@@ -43,7 +44,7 @@ const mongoOpts = {
4344 * even when the database is offline.
4445 */
4546export class DataSourceMongoDb extends DataSource {
46- constructor ( map , name , namespace , options = { } ) {
47+ constructor ( map , name , namespace , options = { } ) {
4748 super ( map , name , namespace , options )
4849 this . cacheSize = cacheSize
4950 this . mongoOpts = mongoOpts
@@ -52,47 +53,56 @@ export class DataSourceMongoDb extends DataSource {
5253 this . url = url
5354 }
5455
55- connect ( client ) {
56- return async function ( ) {
57- let timeout = false
58- const timerId = setTimeout ( ( ) => {
59- timeout = true
60- } , 500 )
61- await client . connect ( )
62- clearTimeout ( timerId )
63- if ( timeout ) throw new Error ( 'mongo conn timeout' )
64- }
65- }
66-
67- async connectionPool ( ) {
56+ /**
57+ *
58+ * @returns {Promise<import('mongodb').Db> }
59+ */
60+ async connectionPool ( ) {
6861 return new Promise ( ( resolve , reject ) => {
6962 if ( this . db ) return resolve ( this . db )
7063 MongoClient . connect (
7164 this . url ,
7265 {
7366 ...this . mongoOpts ,
7467 poolSize : dsOptions . numConns || 2 ,
68+ connectTimeoutMS : 500
7569 } ,
76- ( err , database ) => {
70+ ( err , db ) => {
7771 if ( err ) return reject ( err )
78- resolve ( ( this . db = database . db ( this . namespace ) ) )
72+ this . db = db ( this . namespace )
73+ resolve ( this . db )
7974 }
8075 )
8176 } )
8277 }
8378
84- async connection ( ) {
79+ connect ( client ) {
80+ return async function ( ) {
81+ let timeout = false
82+ const timerId = setTimeout ( ( ) => {
83+ timeout = true
84+ } , 500 )
85+ await client . connect ( )
86+ clearTimeout ( timerId )
87+ if ( timeout ) throw new Error ( 'mongo conn timeout' )
88+ }
89+ }
90+
91+ async connection ( ) {
8592 try {
8693 while ( connections . length < ( dsOptions . numConns || 1 ) ) {
87- const client = new MongoClient ( this . url , this . mongoOpts )
94+ const client = new MongoClient ( this . url , {
95+ ...this . mongoOpts ,
96+ connectTimeoutMS : 500
97+ } )
8898 const thresholds = {
8999 default : {
90100 errorRate : 1 ,
91101 callVolume : 1 ,
92102 intervalMs : 10000 ,
93103 testDelay : 300000 ,
94- // fallbackFn: () => client.emit('connectionClosed ')
95- } ,
104+ fallbackFn : ( ) => console . log ( 'circuit open ')
105+ }
96106 }
97107 const breaker = CircuitBreaker (
98108 'mongodb.connect' ,
@@ -145,7 +155,7 @@ export class DataSourceMongoDb extends DataSource {
145155 . createIndexes ( indexOperations )
146156 }
147157
148- async find ( id ) {
158+ async find ( id ) {
149159 try {
150160 return ( await this . collection ( ) ) . findOne ( { _id : id } )
151161 } catch ( error ) {
@@ -163,7 +173,7 @@ export class DataSourceMongoDb extends DataSource {
163173 * @param {* } id
164174 * @param {* } data
165175 */
166- async save ( id , data ) {
176+ async save ( id , data ) {
167177 try {
168178 await (
169179 await this . collection ( )
@@ -186,19 +196,19 @@ export class DataSourceMongoDb extends DataSource {
186196 * @param {number } highWaterMark num of docs per batch write
187197 * @returns
188198 */
189- createWriteStream ( filter = { } , highWaterMark = HIGHWATERMARK ) {
199+ createWriteStream ( filter = { } , highWaterMark = HIGHWATERMARK ) {
190200 try {
191201 let objects = [ ]
192202 const ctx = this
193203
194- async function upsert ( ) {
204+ async function upsert ( ) {
195205 const operations = objects . map ( obj => {
196206 return {
197207 replaceOne : {
198208 filter : { ...filter , _id : obj . id } ,
199209 replacement : { ...obj , _id : obj . id } ,
200- upsert : true ,
201- } ,
210+ upsert : true
211+ }
202212 }
203213 } )
204214
@@ -217,17 +227,17 @@ export class DataSourceMongoDb extends DataSource {
217227 const writable = new Writable ( {
218228 objectMode : true ,
219229
220- async write ( chunk , _encoding , next ) {
230+ async write ( chunk , _encoding , next ) {
221231 objects . push ( chunk )
222232 // if true time to flush buffer and write to db
223233 if ( objects . length >= highWaterMark ) await upsert ( )
224234 next ( )
225235 } ,
226236
227- end ( chunk , _ , done ) {
237+ end ( chunk , _ , done ) {
228238 objects . push ( chunk )
229239 done ( )
230- } ,
240+ }
231241 } )
232242
233243 writable . on ( 'finish' , async ( ) => await upsert ( ) )
@@ -247,7 +257,7 @@ export class DataSourceMongoDb extends DataSource {
247257 *
248258 * @returns {Promise<import('mongodb').AbstractCursor> }
249259 */
250- async mongoFind ( { filter, sort, limit, aggregate, skip } = { } ) {
260+ async mongoFind ( { filter, sort, limit, aggregate, skip } = { } ) {
251261 console . log ( { fn : this . mongoFind . name , filter } )
252262 let cursor = aggregate
253263 ? ( await this . collection ( ) ) . aggregate ( aggregate )
@@ -287,7 +297,6 @@ export class DataSourceMongoDb extends DataSource {
287297 */
288298 streamList ( { writable, serialize, transform, options } ) {
289299 try {
290- let pipeArgs = [ ]
291300
292301 const serializer = new Transform ( {
293302 writableObjectMode : true ,
@@ -340,15 +349,12 @@ export class DataSourceMongoDb extends DataSource {
340349 } )
341350
342351 return new Promise ( async ( resolve , reject ) => {
352+ const pipeArgs = [ ]
343353 const readable = ( await this . mongoFind ( options ) ) . stream ( )
344354
345- readable . on ( 'error' , reject )
346- readable . on ( 'end' , resolve )
347-
348355 // optionally transform db stream then pipe to output
349356 pipeArgs . push ( readable )
350- if ( transform ) pipeArgs . push ( transform )
351- if ( serialize ) pipeArgs . push ( serializer )
357+
352358 if ( options . page ) {
353359 const count = ~ ~ ( await this . mongoCount ( options . filter ) )
354360 pipeArgs . push ( paginate )
@@ -360,10 +366,9 @@ export class DataSourceMongoDb extends DataSource {
360366 ( ) => console . log ( 'paginated query' , options )
361367 )
362368 }
363- pipeArgs . push ( writable )
364-
365- // perform pipe operations
366- pipeArgs . reduce ( ( prevVal , currVal ) => prevVal . pipe ( currVal ) )
369+
370+ return pipeArgs
371+
367372 } )
368373 } catch ( error ) {
369374 console . error ( {
@@ -382,7 +387,7 @@ export class DataSourceMongoDb extends DataSource {
382387 * @override
383388 * @param {import('../../domain/datasource').listOptions } param
384389 */
385- async list ( param ) {
390+ async list ( param ) {
386391 try {
387392 const options = this . processOptions ( param )
388393 if ( 0 < ~ ~ query . __page ) {
@@ -401,7 +406,7 @@ export class DataSourceMongoDb extends DataSource {
401406 }
402407 console . log ( { options } )
403408
404- if ( writable && ! query . __json )
409+ if ( streamRequested && ! query . __json )
405410 this . streamList ( { writable, serialize, transform, options } )
406411 else {
407412 const data = ( await this . mongoFind ( options ) ) . toArray ( )
@@ -424,19 +429,19 @@ export class DataSourceMongoDb extends DataSource {
424429 *
425430 * @override
426431 */
427- async count ( ) {
432+ async count ( ) {
428433 return {
429434 total : await this . countDb ( ) ,
430435 cached : this . getCacheSize ( ) ,
431- bytes : this . getCacheSizeBytes ( ) ,
436+ bytes : this . getCacheSizeBytes ( )
432437 }
433438 }
434439
435440 /**
436441 * @override
437442 * @returns
438443 */
439- async countDb ( ) {
444+ async countDb ( ) {
440445 return ( await this . collection ( ) ) . countDocuments ( )
441446 }
442447
@@ -447,7 +452,7 @@ export class DataSourceMongoDb extends DataSource {
447452 * @override
448453 * @param {* } id
449454 */
450- async delete ( id ) {
455+ async delete ( id ) {
451456 try {
452457 await ( await this . collection ( ) ) . deleteOne ( { _id : id } )
453458 } catch ( error ) {
0 commit comments