bajo-extra 0.2.12 → 0.2.14

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/config.json CHANGED
@@ -10,7 +10,7 @@
10
10
  }
11
11
  },
12
12
  "import": {
13
- "maxBatch": 100
13
+ "maxBatch": 1000
14
14
  }
15
15
  }
16
16
  }
@@ -1,6 +1,6 @@
1
1
  import path from 'path'
2
2
  import scramjet from 'scramjet'
3
- import format from 'ndjson-csv-xlsx'
3
+ import format from '../../lib/ndjson-csv-xlsx.js'
4
4
  import { createGzip } from 'zlib'
5
5
 
6
6
  const { json, ndjson, csv, xlsx } = format
@@ -14,7 +14,7 @@ async function getFile (dest, ensureDir) {
14
14
  let file
15
15
  if (path.isAbsolute(dest)) file = dest
16
16
  else {
17
- file = `${getPluginDataDir('bajoDb')}/export/${dest}`
17
+ file = `${getPluginDataDir('bajoExtra')}/export/${dest}`
18
18
  fs.ensureDirSync(path.dirname(file))
19
19
  }
20
20
  file = increment(file, { fs: true })
@@ -10,7 +10,7 @@ async function handler (rec, bulk) {
10
10
  save.checkUnique = save.checkUnique ?? 'id'
11
11
  if (['unique', 'upsert'].includes(save.mode)) {
12
12
  const query = isFunction(save.checkUnique) ? await save.checkUnique.call(this, rec, save) : set({}, save.checkUnique, rec[save.checkUnique])
13
- const resp = await recordFind(save.coll, { query, limit: 1 }, { skipCache: true })
13
+ const resp = await recordFind(save.coll, { query, limit: 1 }, { noCache: true })
14
14
  if (resp.length > 0) existing = resp[0]
15
15
  }
16
16
  if (existing) {
@@ -37,7 +37,7 @@ async function handler (rec, bulk) {
37
37
  }
38
38
  if (record && current.coll && current.query) {
39
39
  const query = await current.query.call(this, { body: rec, record, opts: save })
40
- const recs = await recordFind(current.coll, { query }, { skipCache: true })
40
+ const recs = await recordFind(current.coll, { query }, { noCache: true })
41
41
  const rc = current.converter ? await current.converter.call(this, { body: rec, record, opts: save }) : rec
42
42
  if (rc) {
43
43
  if (recs.length > 0) {
@@ -1,28 +1,29 @@
1
1
  import path from 'path'
2
2
  import scramjet from 'scramjet'
3
- import format from 'ndjson-csv-xlsx'
3
+ import format from '../../lib/ndjson-csv-xlsx.js'
4
4
  import { createGunzip } from 'zlib'
5
5
 
6
6
  const { json, ndjson, csv, xlsx } = format
7
7
  const { DataStream } = scramjet
8
- const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx']
8
+ const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx', '.tsv']
9
9
 
10
- async function importFrom (source, dest, { trashOld = true, batch, progressFn, useHeader = true } = {}) {
10
+ async function importFrom (source, dest, { trashOld = true, batch = 1, progressFn, converterFn, useHeader = true, fileType, createOpts = {} } = {}, opts = {}) {
11
11
  const { error, importPkg, getConfig, getPluginDataDir } = this.bajo.helper
12
12
  if (!this.bajoDb) throw error('Bajo DB isn\'t loaded')
13
13
  const { getInfo, recordClear, recordCreate } = this.bajoDb.helper
14
14
  await getInfo(dest)
15
+ const { merge } = await importPkg('lodash-es')
15
16
  const fs = await importPkg('fs-extra')
16
17
  const cfg = getConfig('bajoExtra')
17
18
 
18
19
  let file
19
20
  if (path.isAbsolute(source)) file = source
20
21
  else {
21
- file = `${getPluginDataDir('bajoDb')}/import/${source}`
22
+ file = `${getPluginDataDir('bajoExtra')}/import/${source}`
22
23
  fs.ensureDirSync(path.dirname(file))
23
24
  }
24
25
  if (!fs.existsSync(file)) throw error('Source file \'%s\' doesn\'t exist', file)
25
- let ext = path.extname(file)
26
+ let ext = fileType ? `.${fileType}` : path.extname(file)
26
27
  let decompress = false
27
28
  if (ext === '.gz') {
28
29
  ext = path.extname(path.basename(file, '.gz'))
@@ -31,16 +32,17 @@ async function importFrom (source, dest, { trashOld = true, batch, progressFn, u
31
32
  if (!supportedExt.includes(ext)) throw error('Unsupported format \'%s\'', ext.slice(1))
32
33
  if (trashOld) await recordClear(dest)
33
34
  const reader = fs.createReadStream(file)
34
- batch = parseInt(batch) ?? 100
35
+ batch = parseInt(batch) || 100
35
36
  if (batch > cfg.stream.import.maxBatch) batch = cfg.stream.import.maxBatch
36
37
  if (batch < 0) batch = 1
37
38
  let count = 0
38
39
  const pipes = [reader]
39
40
  if (decompress) pipes.push(createGunzip())
40
- if (ext === '.json') pipes.push(json.parse())
41
- else if (['.ndjson', '.jsonl'].includes(ext)) pipes.push(ndjson.parse())
42
- else if (ext === '.csv') pipes.push(csv.parse({ headers: useHeader }))
43
- else if (ext === '.xlsx') pipes.push(xlsx.parse({ header: useHeader }))
41
+ if (ext === '.json') pipes.push(json.parse(opts))
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)))
44
46
 
45
47
  const stream = DataStream.pipeline(...pipes)
46
48
  let batchNo = 1
@@ -48,13 +50,13 @@ async function importFrom (source, dest, { trashOld = true, batch, progressFn, u
48
50
  .batch(batch)
49
51
  .map(async items => {
50
52
  if (items.length === 0) return null
51
- if (progressFn) await progressFn.call(this, { batchNo, data: items })
52
- for (let i = 0; i < items.length; i++) {
53
+ for (let item of items) {
53
54
  count++
54
- await recordCreate(dest, items[i])
55
+ item = converterFn ? await converterFn.call(this, item) : item
56
+ await recordCreate(dest, item, createOpts)
55
57
  }
58
+ if (progressFn) await progressFn.call(this, { batchNo, data: items })
56
59
  batchNo++
57
- return null
58
60
  })
59
61
  .run()
60
62
 
@@ -33,7 +33,7 @@ async function importFrom ({ path, args }) {
33
33
  default: false
34
34
  })
35
35
  if (!answer) return print.fail('Aborted!', { exit: config.tool })
36
- const spin = spinner().start('Importing...')
36
+ const spin = spinner({ showCounter: true }).start('Importing...')
37
37
  const progressFn = makeProgress.call(this, spin)
38
38
  const cfg = getConfig('bajoDb', { full: true })
39
39
  const { batch } = getConfig()
@@ -0,0 +1,35 @@
1
+ // Borrowed from: https://github.com/fanlia/ndjson-csv-xlsx/blob/main/index.js
2
+
3
+ import ndjson from 'ndjson'
4
+ import csv from 'fast-csv'
5
+ import xlsxparse from 'xlsx-parse-stream'
6
+ import XLSXWriteStream from '@atomictech/xlsx-write-stream'
7
+ import StreamArray from 'stream-json/streamers/StreamArray.js'
8
+ import stringer from 'stream-json/Stringer.js'
9
+ import disassembler from 'stream-json/Disassembler.js'
10
+ import chain from 'stream-chain'
11
+
12
+ export default {
13
+ ndjson: {
14
+ parse: (...args) => ndjson.parse(...args),
15
+ stringify: (...args) => ndjson.stringify(...args)
16
+ },
17
+ csv: {
18
+ parse: (...args) => csv.parse(...args),
19
+ stringify: (...args) => csv.format(...args)
20
+ },
21
+ xlsx: {
22
+ parse: (...args) => xlsxparse(...args),
23
+ stringify: (...args) => new XLSXWriteStream(...args)
24
+ },
25
+ json: {
26
+ parse: (...args) => chain([
27
+ StreamArray.withParser(...args),
28
+ data => data.value
29
+ ]),
30
+ stringify: (options, ...args) => chain([
31
+ disassembler(),
32
+ stringer({ ...options, makeArray: true })
33
+ ])
34
+ }
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bajo-extra",
3
- "version": "0.2.12",
3
+ "version": "0.2.14",
4
4
  "description": "Extra package for Bajo Framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,16 +26,21 @@
26
26
  },
27
27
  "homepage": "https://github.com/ardhi/bajo-extra#readme",
28
28
  "dependencies": {
29
+ "@atomictech/xlsx-write-stream": "^2.0.2",
29
30
  "async": "^3.2.4",
30
31
  "axios": "^1.4.0",
31
32
  "bcrypt": "^5.1.1",
32
33
  "email-addresses": "^5.0.0",
34
+ "fast-csv": "^5.0.1",
33
35
  "fast-jwt": "^3.2.0",
34
36
  "fast-xml-parser": "^4.3.2",
35
37
  "littlehash": "^1.0.1",
36
- "ndjson-csv-xlsx": "^1.1.1",
38
+ "ndjson": "^2.0.0",
37
39
  "performant-array-to-tree": "^1.11.0",
38
40
  "query-string": "^8.1.0",
39
- "scramjet": "^4.36.9"
41
+ "scramjet": "^4.36.9",
42
+ "stream-chain": "^2.2.5",
43
+ "stream-json": "^1.8.0",
44
+ "xlsx-parse-stream": "^1.1.0"
40
45
  }
41
46
  }