bajo-extra 0.2.14 → 0.2.15

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.
@@ -6,7 +6,7 @@ import { createGzip } from 'zlib'
6
6
  const { json, ndjson, csv, xlsx } = format
7
7
  const { DataStream } = scramjet
8
8
 
9
- const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx']
9
+ const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx', '.tsv']
10
10
 
11
11
  async function getFile (dest, ensureDir) {
12
12
  const { importPkg, error, getPluginDataDir } = this.bajo.helper
@@ -38,18 +38,19 @@ async function getData ({ source, filter, count, stream, progressFn }) {
38
38
  let cnt = count ?? 0
39
39
  const { recordFind } = this.bajoDb.helper
40
40
  for (;;) {
41
- const { data, pages, page } = await recordFind(source, filter, { dataOnly: false })
41
+ const batchStart = new Date()
42
+ const { data, page } = await recordFind(source, filter, { dataOnly: false })
42
43
  if (data.length === 0) break
43
44
  cnt += data.length
44
45
  await stream.pull(data)
45
- if (progressFn) await progressFn.call(this, { batchTotal: pages, batchNo: page, data })
46
+ if (progressFn) await progressFn.call(this, { batchNo: page, data, batchStart, batchEnd: new Date() })
46
47
  filter.page++
47
48
  }
48
49
  await stream.end()
49
50
  return cnt
50
51
  }
51
52
 
52
- function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, batch = 500, progressFn } = {}) {
53
+ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, batch = 500, progressFn } = {}, opts = {}) {
53
54
  const { error, importPkg, getConfig } = this.bajo.helper
54
55
  const cfg = getConfig('bajoExtra')
55
56
  if (!this.bajoDb) throw error('Bajo DB isn\'t loaded')
@@ -63,6 +64,7 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
63
64
  const { getInfo } = this.bajoDb.helper
64
65
  let count = 0
65
66
  let fs
67
+ let merge
66
68
  let file
67
69
  let ext
68
70
  let stream
@@ -70,6 +72,10 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
70
72
  let writer
71
73
  getInfo(source)
72
74
  .then(() => {
75
+ return importPkg('lodash-es')
76
+ })
77
+ .then(l => {
78
+ merge = l.merge
73
79
  return importPkg('fs-extra')
74
80
  })
75
81
  .then(res => {
@@ -90,10 +96,11 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
90
96
  stream = new DataStream()
91
97
  stream = stream.flatMap(items => (items))
92
98
  const pipes = []
93
- if (ext === '.json') pipes.push(json.stringify())
94
- else if (['.ndjson', '.jsonl'].includes(ext)) pipes.push(ndjson.stringify())
95
- else if (ext === '.csv') pipes.push(csv.stringify({ headers: useHeader }))
96
- else if (ext === '.xlsx') pipes.push(xlsx.stringify({ header: useHeader }))
99
+ if (ext === '.json') pipes.push(json.stringify(opts))
100
+ else if (['.ndjson', '.jsonl'].includes(ext)) pipes.push(ndjson.stringify(opts))
101
+ else if (ext === '.csv') pipes.push(csv.stringify(merge({}, { headers: useHeader }, opts)))
102
+ else if (ext === '.tsv') pipes.push(csv.stringify(merge({}, { headers: useHeader }, merge({}, opts, { delimiter: '\t' }))))
103
+ else if (ext === '.xlsx') pipes.push(xlsx.stringify(merge({}, { header: useHeader }, opts)))
97
104
  if (compress) pipes.push(createGzip())
98
105
  DataStream.pipeline(stream, ...pipes).pipe(writer)
99
106
  return getData.call(this, { source, filter, count, stream, progressFn })
@@ -40,9 +40,9 @@ async function importFrom (source, dest, { trashOld = true, batch = 1, progressF
40
40
  if (decompress) pipes.push(createGunzip())
41
41
  if (ext === '.json') pipes.push(json.parse(opts))
42
42
  else if (['.ndjson', '.jsonl'].includes(ext)) pipes.push(ndjson.parse(opts))
43
- else if (ext === '.csv') pipes.push(csv.parse(merge({ headers: useHeader }, opts)))
44
- else if (ext === '.tsv') pipes.push(csv.parse(merge({ headers: useHeader }, merge(opts, { delimiter: '\t' }))))
45
- else if (ext === '.xlsx') pipes.push(xlsx.parse(merge({ header: useHeader }, opts)))
43
+ else if (ext === '.csv') pipes.push(csv.parse(merge({}, { headers: useHeader }, opts)))
44
+ else if (ext === '.tsv') pipes.push(csv.parse(merge({}, { headers: useHeader }, merge({}, opts, { delimiter: '\t' }))))
45
+ else if (ext === '.xlsx') pipes.push(xlsx.parse(merge({}, { header: useHeader }, opts)))
46
46
 
47
47
  const stream = DataStream.pipeline(...pipes)
48
48
  let batchNo = 1
@@ -50,12 +50,13 @@ async function importFrom (source, dest, { trashOld = true, batch = 1, progressF
50
50
  .batch(batch)
51
51
  .map(async items => {
52
52
  if (items.length === 0) return null
53
+ const batchStart = new Date()
53
54
  for (let item of items) {
54
55
  count++
55
56
  item = converterFn ? await converterFn.call(this, item) : item
56
57
  await recordCreate(dest, item, createOpts)
57
58
  }
58
- if (progressFn) await progressFn.call(this, { batchNo, data: items })
59
+ if (progressFn) await progressFn.call(this, { batchNo, data: items, batchStart, batchEnd: new Date() })
59
60
  batchNo++
60
61
  })
61
62
  .run()
@@ -1,9 +1,12 @@
1
1
  import Path from 'path'
2
2
 
3
+ const batch = 100
4
+
3
5
  function makeProgress (spin) {
4
- return async function ({ batchNo, batchTotal, data } = {}) {
5
- if (batchTotal === 0) return
6
- spin.setText('Batch %d of %d (%d records)', batchNo, batchTotal, data.length)
6
+ return async function ({ batchNo, data, batchStart, batchEnd } = {}) {
7
+ const { secToHms } = this.bajo.helper
8
+ if (data.length === 0) return
9
+ spin.setText('Batch #%d (%s)', batchNo, secToHms(batchEnd.toTime() - batchStart.toTime(), true))
7
10
  }
8
11
  }
9
12
 
@@ -38,7 +41,6 @@ async function exportTo ({ path, args }) {
38
41
  const spin = spinner().start('Exporting...')
39
42
  const progressFn = makeProgress.call(this, spin)
40
43
  const cfg = getConfig('bajoDb', { full: true })
41
- const { batch } = getConfig()
42
44
  const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
43
45
  const { connection } = await this.bajoDb.helper.getInfo(coll)
44
46
  await start.call(this, connection.name)
@@ -1,8 +1,11 @@
1
1
  import Path from 'path'
2
2
 
3
+ const batch = 100
4
+
3
5
  function makeProgress (spin) {
4
- return async function ({ batchNo, data } = {}) {
5
- spin.setText('Batch %d (%d records)', batchNo, data.length)
6
+ const { secToHms } = this.bajo.helper
7
+ return async function ({ batchNo, data, batchStart, batchEnd } = {}) {
8
+ spin.setText('Batch #%d (%s)', batchNo, secToHms(batchEnd.toTime() - batchStart.toTime(), true))
6
9
  }
7
10
  }
8
11
 
@@ -36,7 +39,6 @@ async function importFrom ({ path, args }) {
36
39
  const spin = spinner({ showCounter: true }).start('Importing...')
37
40
  const progressFn = makeProgress.call(this, spin)
38
41
  const cfg = getConfig('bajoDb', { full: true })
39
- const { batch } = getConfig()
40
42
  const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
41
43
  const { connection } = await this.bajoDb.helper.getInfo(coll)
42
44
  await start.call(this, connection.name)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "0.2.14",
3
+ "version": "0.2.15",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {