bajo-extra 0.2.2 → 0.2.4

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,26 +1,34 @@
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
- for (let r of resp.response) {
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 {
13
20
  await recordCreate(coll, r)
14
21
  if (current.coll && current.query) {
15
22
  const recs = await recordFind(current.coll, current.query)
23
+ const rc = current.converter ? await current.converter.call(this, r, options) : r
16
24
  if (recs.length > 0) {
17
25
  const id = recs[0].id
18
- await recordUpdate(current.coll, id, r)
26
+ await recordUpdate(current.coll, id, rc)
19
27
  } else {
20
- await recordCreate(current.coll, r)
28
+ await recordCreate(current.coll, rc)
21
29
  }
22
30
  }
23
- if (options.printCount && (count % options.printCount === 0)) print.succeed('Batch tag %d/%d OK', count, resp.response.length, { showDatetime: true })
31
+ if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spinner.getElapsed()}] Batch line %d/%d`, count, resp.response.length, { showDatetime: true })
24
32
  else spinner.setText('Record %d/%d...', count, resp.response.length)
25
33
  count++
26
34
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {