Skip to content

Commit 60fa883

Browse files
authored
Merge pull request #273 from module-federation/release-0-1-0
refactored for legibility
2 parents aa54875 + 88e9daa commit 60fa883

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

src/domain/datasource-factory.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)