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