bajo-extra 0.2.6 → 0.2.8
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.
- package/bajo/helper/fetch-and-save.js +22 -14
- package/package.json +1 -1
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
async function fetchAndSave ({ source = {}, converter, coll, current = {}, options = {} } = {}) {
|
|
2
|
-
const { print, setImmediate, importPkg } = this.bajo.helper
|
|
2
|
+
const { print, setImmediate, importPkg, getConfig } = this.bajo.helper
|
|
3
3
|
const { isEmpty, isFunction } = await importPkg('lodash-es')
|
|
4
4
|
const { fetch } = this.bajoExtra.helper
|
|
5
5
|
const { recordCreate, recordFind, recordUpdate, validationErrorMessage } = this.bajoDb.helper
|
|
6
|
-
const
|
|
6
|
+
const config = getConfig()
|
|
7
|
+
const opts = { type: options.returnEarly ? 'log' : 'bora', pkg: options.pkg }
|
|
8
|
+
const showDatetime = !config.tool
|
|
9
|
+
const spinner = print.bora('Fetching starts...', { showCounter: true, showDatetime, isSilent: options.returnEarly }).start()
|
|
10
|
+
if (options.returnEarly) print.succeed('Fetching starts...', opts)
|
|
7
11
|
const resp = await fetch(source.url, source.options ?? {})
|
|
8
12
|
if (isEmpty(resp)) spinner.fatal('No result from server, aborted!')
|
|
9
13
|
if (source.abort) {
|
|
@@ -11,34 +15,38 @@ async function fetchAndSave ({ source = {}, converter, coll, current = {}, optio
|
|
|
11
15
|
if (aborted) spinner.fatal(aborted)
|
|
12
16
|
}
|
|
13
17
|
let count = 0
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
const iterator = isFunction(source.iterator) ? await source.iterator.call(this, resp) : resp[source.iterator]
|
|
19
|
+
spinner.setText('Got %d records, processing...', iterator.length)
|
|
16
20
|
for (let r of iterator) {
|
|
17
21
|
await setImmediate()
|
|
18
22
|
if (converter) r = await converter.call(this, r, options)
|
|
23
|
+
if (isEmpty(r)) continue
|
|
19
24
|
try {
|
|
20
25
|
await recordCreate(coll, r)
|
|
21
26
|
if (current.coll && current.query) {
|
|
22
27
|
const query = await current.query.call(this, r)
|
|
23
|
-
const recs = await recordFind(current.coll, { query })
|
|
28
|
+
const recs = await recordFind(current.coll, { query }, { skipCache: true })
|
|
24
29
|
const rc = current.converter ? await current.converter.call(this, r, options) : r
|
|
25
|
-
if (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
+
if (rc) {
|
|
31
|
+
if (recs.length > 0) {
|
|
32
|
+
const id = recs[0].id
|
|
33
|
+
await recordUpdate(current.coll, id, rc)
|
|
34
|
+
} else {
|
|
35
|
+
await recordCreate(current.coll, rc)
|
|
36
|
+
}
|
|
30
37
|
}
|
|
31
38
|
}
|
|
32
|
-
if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spinner.getElapsed()}] Batch line %d/%d`, count,
|
|
33
|
-
else spinner.setText('Record %d/%d...', count,
|
|
39
|
+
if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spinner.getElapsed()}] Batch line %d/%d`, count, iterator.length, opts)
|
|
40
|
+
else spinner.setText('Record %d/%d...', count, iterator.length)
|
|
34
41
|
count++
|
|
35
42
|
} catch (err) {
|
|
36
43
|
console.log(err)
|
|
37
44
|
spinner.setText(validationErrorMessage(err) + ', continue')
|
|
38
45
|
}
|
|
39
46
|
}
|
|
40
|
-
spinner.info(`${count}/${
|
|
41
|
-
|
|
47
|
+
spinner.info(`${count}/${iterator.length} records processed`)
|
|
48
|
+
if (options.returnEarly) print.info(`${count}/${iterator.length} records processed`, opts)
|
|
49
|
+
else spinner.succeed('Done!')
|
|
42
50
|
}
|
|
43
51
|
|
|
44
52
|
export default fetchAndSave
|