bajo-extra 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
async function fetchAndSave ({ source = {}, converter, coll, current = {}, options = {} } = {}) {
|
|
2
|
-
const { print, setImmediate } = this.bajo.helper
|
|
2
|
+
const { print, setImmediate, importPkg } = this.bajo.helper
|
|
3
|
+
const { isEmpty, isFunction } = await importPkg('lodash-es')
|
|
3
4
|
const { fetch } = this.bajoExtra.helper
|
|
4
5
|
const { recordCreate, recordFind, recordUpdate, validationErrorMessage } = this.bajoDb.helper
|
|
5
6
|
const spinner = print.bora('Fetching starts...', { showCounter: true, showDatetime: true }).start()
|
|
6
7
|
const resp = await fetch(source.url, source.options ?? {})
|
|
8
|
+
if (isEmpty(resp)) spinner.fatal('No result from server, aborted!')
|
|
9
|
+
if (source.abort) {
|
|
10
|
+
const aborted = await source.abort.call(this, resp)
|
|
11
|
+
if (aborted) spinner.fatal(aborted)
|
|
12
|
+
}
|
|
7
13
|
let count = 0
|
|
8
14
|
spinner.setText('Got %d records, processing...', resp.response.length)
|
|
9
|
-
|
|
15
|
+
const iterator = isFunction(source.dataKey) ? await source.dataKey.call(this, resp) : resp[source.dataKey]
|
|
16
|
+
for (let r of iterator) {
|
|
10
17
|
await setImmediate()
|
|
11
18
|
if (converter) r = await converter.call(this, r, options)
|
|
12
19
|
try {
|
|
@@ -20,7 +27,7 @@ async function fetchAndSave ({ source = {}, converter, coll, current = {}, optio
|
|
|
20
27
|
await recordCreate(current.coll, r)
|
|
21
28
|
}
|
|
22
29
|
}
|
|
23
|
-
if (options.printCount && (count % options.printCount === 0)) print.succeed(
|
|
30
|
+
if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spinner.getElapsed()}] Batch line %d/%d`, count, resp.response.length, { showDatetime: true })
|
|
24
31
|
else spinner.setText('Record %d/%d...', count, resp.response.length)
|
|
25
32
|
count++
|
|
26
33
|
} catch (err) {
|