@@ -156,6 +156,19 @@ const DsCoreExtensions = superclass =>
156156 } )
157157 }
158158
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+
159172 /**
160173 * Returns the set of objects satisfying the `filter` if specified;
161174 * otherwise returns all objects. If a `writable` stream is provided and
@@ -173,24 +186,16 @@ const DsCoreExtensions = superclass =>
173186 if ( options ?. query ?. __count ) return this . count ( )
174187 if ( options ?. query ?. __cached ) return this . listSync ( options . query )
175188
176- const opts = {
177- ...options ,
178- streamResult :
179- options ?. writable && ! options ?. query ?. __aggregate ? true : false
180- }
189+ const opts = { ...options , streamResult : this . streamResult ( ) }
181190 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 )
185191
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 ) )
191196 } catch ( error ) {
192197 console . error ( { fn : this . list . name , error } )
193- throw error
198+ throw new DsError ( error , 500 )
194199 }
195200 }
196201
@@ -286,7 +291,6 @@ const DataSourceFactory = (() => {
286291 const { memoryOnly, ephemeral, adapterName } = options
287292
288293 if ( memoryOnly || ephemeral ) return dsClasses [ 'DataSourceMemory' ]
289-
290294 if ( adapterName ) return adapters [ adapterName ] || DefaultDataSource
291295
292296 if ( spec ?. datasource ?. factory ) {
0 commit comments