dobo-extra 1.2.1 → 1.2.3
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/lib/ndjson-csv-xlsx.js +7 -5
- package/package.json +1 -1
- package/plugin-method/export-to.js +29 -10
package/lib/ndjson-csv-xlsx.js
CHANGED
|
@@ -5,10 +5,12 @@ import csv from 'fast-csv'
|
|
|
5
5
|
import xlsxparse from 'xlsx-parse-stream'
|
|
6
6
|
import XLSXWriteStream from '@atomictech/xlsx-write-stream'
|
|
7
7
|
import StreamArray from 'stream-json/streamers/StreamArray.js'
|
|
8
|
-
import
|
|
9
|
-
import
|
|
8
|
+
import Stringer from 'stream-json/Stringer.js'
|
|
9
|
+
import Disassembler from 'stream-json/Disassembler.js'
|
|
10
10
|
import chain from 'stream-chain'
|
|
11
11
|
|
|
12
|
+
const XLSXStreamer = XLSXWriteStream.default
|
|
13
|
+
|
|
12
14
|
export default {
|
|
13
15
|
ndjson: {
|
|
14
16
|
parse: (...args) => ndjson.parse(...args),
|
|
@@ -20,7 +22,7 @@ export default {
|
|
|
20
22
|
},
|
|
21
23
|
xlsx: {
|
|
22
24
|
parse: (...args) => xlsxparse(...args),
|
|
23
|
-
stringify: (...args) => new
|
|
25
|
+
stringify: (...args) => new XLSXStreamer(...args)
|
|
24
26
|
},
|
|
25
27
|
json: {
|
|
26
28
|
parse: (...args) => chain([
|
|
@@ -28,8 +30,8 @@ export default {
|
|
|
28
30
|
data => data.value
|
|
29
31
|
]),
|
|
30
32
|
stringify: (options, ...args) => chain([
|
|
31
|
-
|
|
32
|
-
|
|
33
|
+
new Disassembler(),
|
|
34
|
+
new Stringer({ ...options, makeArray: true })
|
|
33
35
|
])
|
|
34
36
|
}
|
|
35
37
|
}
|
package/package.json
CHANGED
|
@@ -27,20 +27,41 @@ async function getFile (dest, ensureDir) {
|
|
|
27
27
|
let ext = path.extname(file)
|
|
28
28
|
if (ext === '.gz') {
|
|
29
29
|
compress = true
|
|
30
|
-
ext = path.extname(
|
|
30
|
+
ext = path.extname(file.slice(0, -3))
|
|
31
31
|
// file = file.slice(0, file.length - 3)
|
|
32
32
|
}
|
|
33
33
|
if (!supportedExt.includes(ext)) throw this.error('unsupportedFormat%s', ext.slice(1))
|
|
34
34
|
return { file, ext, compress }
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
-
async function getData ({ source, filter, count, stream, progressFn }) {
|
|
37
|
+
async function getData ({ source, filter, count, stream, progressFn, fields }) {
|
|
38
38
|
let cnt = count ?? 0
|
|
39
|
-
const {
|
|
39
|
+
const { find } = this.lib._
|
|
40
|
+
const { recordFind, getSchema } = this.app.dobo
|
|
41
|
+
const { maxLimit, hardLimit } = this.app.dobo.config.default.filter
|
|
42
|
+
filter.limit = maxLimit
|
|
43
|
+
let sort
|
|
44
|
+
const schema = getSchema(source)
|
|
45
|
+
const idField = find(schema.properties, { name: 'id' })
|
|
46
|
+
for (const name of ['createdAt', 'updatedAt', 'ts', 'dt']) {
|
|
47
|
+
const field = find(schema.properties, { name })
|
|
48
|
+
if (field) {
|
|
49
|
+
sort = field.name
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
filter.sort = `${sort ?? idField}:1`
|
|
40
54
|
for (;;) {
|
|
41
55
|
const batchStart = new Date()
|
|
42
|
-
const { data, page } = await recordFind(source, filter, { dataOnly: false })
|
|
56
|
+
const { data, page } = await recordFind(source, filter, { dataOnly: false, fields })
|
|
43
57
|
if (data.length === 0) break
|
|
58
|
+
if (cnt + data.length > hardLimit) {
|
|
59
|
+
const sliced = data.slice(0, hardLimit - cnt)
|
|
60
|
+
await stream.pull(sliced)
|
|
61
|
+
cnt += sliced.length
|
|
62
|
+
if (progressFn) await progressFn.call(this, { batchNo: page, data: sliced, batchStart, batchEnd: new Date() })
|
|
63
|
+
break
|
|
64
|
+
}
|
|
44
65
|
cnt += data.length
|
|
45
66
|
await stream.pull(data)
|
|
46
67
|
if (progressFn) await progressFn.call(this, { batchNo: page, data, batchStart, batchEnd: new Date() })
|
|
@@ -50,9 +71,10 @@ async function getData ({ source, filter, count, stream, progressFn }) {
|
|
|
50
71
|
return cnt
|
|
51
72
|
}
|
|
52
73
|
|
|
53
|
-
function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, batch = 500, progressFn } = {}, opts = {}) {
|
|
74
|
+
function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, batch = 500, progressFn, fields } = {}, opts = {}) {
|
|
54
75
|
const { fs } = this.lib
|
|
55
76
|
const { merge } = this.lib._
|
|
77
|
+
const { getInfo } = this.app.dobo
|
|
56
78
|
|
|
57
79
|
filter.page = 1
|
|
58
80
|
batch = parseInt(batch) ?? 500
|
|
@@ -61,7 +83,6 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
|
|
|
61
83
|
filter.limit = batch
|
|
62
84
|
|
|
63
85
|
return new Promise((resolve, reject) => {
|
|
64
|
-
const { getInfo } = this.app.dobo
|
|
65
86
|
let count = 0
|
|
66
87
|
let file
|
|
67
88
|
let ext
|
|
@@ -69,9 +90,7 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
|
|
|
69
90
|
let compress
|
|
70
91
|
let writer
|
|
71
92
|
getInfo(source)
|
|
72
|
-
|
|
73
|
-
return getFile.call(this, dest, ensureDir)
|
|
74
|
-
})
|
|
93
|
+
getFile.call(this, dest, ensureDir)
|
|
75
94
|
.then(res => {
|
|
76
95
|
file = res.file
|
|
77
96
|
ext = res.ext
|
|
@@ -93,7 +112,7 @@ function exportTo (source, dest, { filter = {}, ensureDir, useHeader = true, bat
|
|
|
93
112
|
else if (ext === '.xlsx') pipes.push(xlsx.stringify(merge({}, { header: useHeader }, opts)))
|
|
94
113
|
if (compress) pipes.push(createGzip())
|
|
95
114
|
DataStream.pipeline(stream, ...pipes).pipe(writer)
|
|
96
|
-
return getData.call(this, { source, filter, count, stream, progressFn })
|
|
115
|
+
return getData.call(this, { source, filter, count, stream, fields, progressFn })
|
|
97
116
|
})
|
|
98
117
|
.then(cnt => {
|
|
99
118
|
count = cnt
|