@@ -4,7 +4,7 @@ import { TransformableModel } from '../types/TransformableModel';
4
4
import logger from '../logger' ;
5
5
import config from '../config' ;
6
6
import { LoggifyClass } from '../decorators/Loggify' ;
7
- import { MongoClient , Db , FindOneOptions } from 'mongodb' ;
7
+ import { MongoClient , Db , FindOneOptions , Cursor } from 'mongodb' ;
8
8
import '../models' ;
9
9
10
10
export type StreamingFindOptions < T > = Partial < {
@@ -63,7 +63,7 @@ export class StorageService {
63
63
stop ( ) { }
64
64
65
65
validPagingProperty < T > ( model : TransformableModel < T > , property : keyof T ) {
66
- return model . allowedPaging . some ( ( prop ) => prop . key === property ) ;
66
+ return model . allowedPaging . some ( prop => prop . key === property ) ;
67
67
}
68
68
69
69
/**
@@ -93,21 +93,7 @@ export class StorageService {
93
93
return typecastedValue ;
94
94
}
95
95
96
- apiStreamingFind < T > ( model : TransformableModel < T > , query : any , options : StreamingFindOptions < T > , res : Response ) {
97
- if ( options . since !== undefined && options . paging && this . validPagingProperty ( model , options . paging ) ) {
98
- options . since = this . typecastForDb ( model , options . paging , options . since ) ;
99
- if ( options . direction && Number ( options . direction ) === 1 ) {
100
- query [ options . paging ] = { $gt : options . since } ;
101
- options . sort = { [ options . paging ] : 1 } ;
102
- } else {
103
- query [ options . paging ] = { $lt : options . since } ;
104
- options . sort = { [ options . paging ] : - 1 } ;
105
- }
106
- }
107
- options . limit = Math . min ( options . limit || 100 , 1000 ) ;
108
- let cursor = model . collection . find ( query , options ) . stream ( {
109
- transform : model . _apiTransform
110
- } ) ;
96
+ apiStream < T > ( cursor : Cursor < T > , res : Response ) {
111
97
cursor . on ( 'error' , function ( err ) {
112
98
return res . status ( 500 ) . end ( err . message ) ;
113
99
} ) ;
@@ -132,6 +118,24 @@ export class StorageService {
132
118
res . end ( ) ;
133
119
} ) ;
134
120
}
121
+
122
+ apiStreamingFind < T > ( model : TransformableModel < T > , query : any , options : StreamingFindOptions < T > , res : Response ) {
123
+ if ( options . since !== undefined && options . paging && this . validPagingProperty ( model , options . paging ) ) {
124
+ options . since = this . typecastForDb ( model , options . paging , options . since ) ;
125
+ if ( options . direction && Number ( options . direction ) === 1 ) {
126
+ query [ options . paging ] = { $gt : options . since } ;
127
+ options . sort = { [ options . paging ] : 1 } ;
128
+ } else {
129
+ query [ options . paging ] = { $lt : options . since } ;
130
+ options . sort = { [ options . paging ] : - 1 } ;
131
+ }
132
+ }
133
+ options . limit = Math . min ( options . limit || 100 , 1000 ) ;
134
+ let cursor = model . collection . find ( query , options ) . stream ( {
135
+ transform : model . _apiTransform
136
+ } ) ;
137
+ this . apiStream ( cursor , res ) ;
138
+ }
135
139
}
136
140
137
141
export let Storage = new StorageService ( ) ;
0 commit comments