@@ -156,6 +156,19 @@ const DsCoreExtensions = superclass =>
156
156
} )
157
157
}
158
158
159
+ /**@returns {boolean } */
160
+ streamResult ( options ) {
161
+ return options ?. writable && ! options ?. query ?. __aggregate ? true : false
162
+ }
163
+
164
+ isStream ( list ) {
165
+ return list [ 0 ] instanceof Readable || list [ 0 ] instanceof Transform
166
+ }
167
+
168
+ unmarshall ( model ) {
169
+ return ModelFactory . loadModel ( broker , this , model , this . name )
170
+ }
171
+
159
172
/**
160
173
* Returns the set of objects satisfying the `filter` if specified;
161
174
* otherwise returns all objects. If a `writable` stream is provided and
@@ -173,24 +186,16 @@ const DsCoreExtensions = superclass =>
173
186
if ( options ?. query ?. __count ) return this . count ( )
174
187
if ( options ?. query ?. __cached ) return this . listSync ( options . query )
175
188
176
- const opts = {
177
- ...options ,
178
- streamResult :
179
- options ?. writable && ! options ?. query ?. __aggregate ? true : false
180
- }
189
+ const opts = { ...options , streamResult : this . streamResult ( ) }
181
190
const list = [ await super . list ( opts ) ] . flat ( )
182
- if ( list . length < 1 ) throw new Error ( )
183
- if ( list [ 0 ] instanceof Readable || list [ 0 ] instanceof Transform )
184
- return this . stream ( list , options )
185
191
186
- return isMainThread
187
- ? list
188
- : list . map ( model =>
189
- ModelFactory . loadModel ( broker , this , model , this . name )
190
- )
192
+ if ( list . length < 1 ) throw new DsError ( 'Not Found' , 404 )
193
+ if ( this . isStream ( list ) ) return this . stream ( list , options )
194
+
195
+ return isMainThread ? list : list . map ( model => this . unmarshall ( model ) )
191
196
} catch ( error ) {
192
197
console . error ( { fn : this . list . name , error } )
193
- throw error
198
+ throw new DsError ( error , 500 )
194
199
}
195
200
}
196
201
@@ -286,7 +291,6 @@ const DataSourceFactory = (() => {
286
291
const { memoryOnly, ephemeral, adapterName } = options
287
292
288
293
if ( memoryOnly || ephemeral ) return dsClasses [ 'DataSourceMemory' ]
289
-
290
294
if ( adapterName ) return adapters [ adapterName ] || DefaultDataSource
291
295
292
296
if ( spec ?. datasource ?. factory ) {
0 commit comments