11'use strict'
22
3- import CircuitBreaker from '../../domain/circuit-breaker'
4- import DataSource from '../../domain/datasource'
3+ const CircuitBreaker = require ( '../../domain/circuit-breaker' ) . default
4+ const DataSource = require ( '../../domain/datasource' ) . default
55
66const HIGHWATERMARK = 50
77
@@ -55,6 +55,23 @@ export class DataSourceMongoDb extends DataSource {
5555 }
5656 }
5757
58+ async connectionPool ( ) {
59+ return new Promise ( ( resolve , reject ) => {
60+ if ( this . db ) return resolve ( this . db )
61+ MongoClient . connect (
62+ this . url ,
63+ {
64+ ...this . mongoOpts ,
65+ poolSize : dsOptions . numConns || 2
66+ } ,
67+ ( err , database ) => {
68+ if ( err ) return reject ( err )
69+ resolve ( ( this . db = database . db ( this . namespace ) ) )
70+ }
71+ )
72+ } )
73+ }
74+
5875 async connection ( ) {
5976 try {
6077 while ( connections . length < ( dsOptions . numConns || 1 ) ) {
@@ -69,7 +86,7 @@ export class DataSourceMongoDb extends DataSource {
6986 }
7087 }
7188 const breaker = CircuitBreaker (
72- 'mongo.conn ' ,
89+ 'mongodb.connect ' ,
7390 this . connect ( client ) ,
7491 thresholds
7592 )
@@ -186,14 +203,12 @@ export class DataSourceMongoDb extends DataSource {
186203 /**
187204 *
188205 * @param {Object } filter Supposed to be a valid Mongo Filter
189- * @param {Object } options Options to sort limit aggregate etc...
190- * @param {Object } options.sort a valid Mongo sort object
191- * @param {Number } options.limit a valid Mongo limit
192- * @param {Object } options.aggregate a valid Mongo aggregate object
206+ * @param {Object } sort a valid Mongo sort object
207+ * @param {Number } limit a valid Mongo limit
208+ * @param {Object } aggregate a valid Mongo aggregate object
193209 *
194- * @returns
210+ * @returns { Promise<import('mongodb').AbstractCursor> }
195211 */
196-
197212 async mongoFind ( { filter, sort, limit, aggregate, skip } = { } ) {
198213 console . log ( { fn : this . mongoFind . name , filter } )
199214 let cursor = ( await this . collection ( ) ) . find ( filter )
@@ -204,112 +219,23 @@ export class DataSourceMongoDb extends DataSource {
204219 return cursor
205220 }
206221
207- /**
208- * Pipes to writable and streams list. List can be filtered. Stream
209- * is serialized by default. Stream can be modified by transform.
210- *
211- * @param {{
212- * filter:*
213- * transform:Transform
214- * serialize:boolean
215- * }} param0
216- * @returns
217- */
218- streamList ( { writable, serialize, transform, options } ) {
219- try {
220- let first = true
221-
222- const serializer = new Transform ( {
223- writableObjectMode : true ,
224-
225- // start of array
226- construct ( callback ) {
227- this . push ( '[' )
228- callback ( )
229- } ,
230-
231- // each chunk is a record
232- transform ( chunk , _encoding , next ) {
233- // comma-separate
234- if ( first ) first = false
235- else this . push ( ',' )
236-
237- // serialize record
238- this . push ( JSON . stringify ( chunk ) )
239- next ( )
240- } ,
241-
242- // end of array
243- flush ( callback ) {
244- this . push ( ']' )
245- callback ( )
246- }
247- } )
248-
249- return new Promise ( async ( resolve , reject ) => {
250- const readable = ( await this . mongoFind ( options ) ) . stream ( )
251-
252- readable . on ( 'error' , reject )
253- readable . on ( 'end' , resolve )
254-
255- // optionally transform db stream then pipe to output
256- if ( transform && serialize )
257- readable
258- . pipe ( transform )
259- . pipe ( serializer )
260- . pipe ( writable )
261- else if ( transform ) readable . pipe ( transform ) . pipe ( writable )
262- else if ( serialize ) readable . pipe ( serializer ) . pipe ( writable )
263- else readable . pipe ( writable )
264- } )
265- } catch ( error ) { }
266- }
267-
268222 processOptions ( param ) {
269223 const { options = { } , query = { } } = param
270224 return { ...options , ...processQuery ( query ) }
271225 }
272226
273227 /**
274- * Returns the set of objects satisfying the `filter` if specified;
275- * otherwise returns all objects. If a `writable`stream is provided and `cached`
276- * is false, the list is streamed. Otherwise the list is returned in
277- * an array. A custom transform can be specified to modify the streamed
278- * results. Using {@link createWriteStream} updates can be streamed back
279- * to the db. With streams, we can support queries of very large tables,
280- * with minimal memory overhead on the node server.
281228 *
282229 * @override
283- * @param {{key1:string, keyN:string} } filter - e.g. http query
284- * @param {{
285- * writable: WritableStream,
286- * cached: boolean,
287- * serialize: boolean,
288- * transform: Transform
289- * }} params
290- * - details
291- * - `serialize` seriailize input to writable
292- * - `cached` list cache only
293- * - `transform` transform stream before writing
294- * - `writable` writable stream for output
230+ * @param {import('../../domain/datasource').listOptions } param
295231 */
296- async list ( param = { } ) {
297- const {
298- writable = null ,
299- transform = null ,
300- serialize = false ,
301- query = { }
302- } = param
303-
232+ async list ( param ) {
304233 try {
305- if ( query . __cached ) return super . listSync ( query )
306- if ( query . __count ) return this . count ( )
307-
308234 const options = this . processOptions ( param )
309235 console . log ( { options } )
310236
311- if ( writable ) {
312- return this . streamList ( { writable , serialize , transform , options } )
237+ if ( param . streamRequested ) {
238+ return ( await this . mongoFind ( options ) ) . stream ( )
313239 }
314240
315241 return ( await this . mongoFind ( options ) ) . toArray ( )
@@ -318,6 +244,10 @@ export class DataSourceMongoDb extends DataSource {
318244 }
319245 }
320246
247+ /**
248+ *
249+ * @override
250+ */
321251 async count ( ) {
322252 return {
323253 total : await this . countDb ( ) ,
0 commit comments