bajo-extra 0.1.0 → 0.2.1
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/helper/export-to.js
CHANGED
|
@@ -9,13 +9,12 @@ const { DataStream } = scramjet
|
|
|
9
9
|
const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx']
|
|
10
10
|
|
|
11
11
|
async function getFile (dest, ensureDir) {
|
|
12
|
-
const { importPkg,
|
|
12
|
+
const { importPkg, error, getPluginDataDir } = this.bajo.helper
|
|
13
13
|
const [fs, increment] = await importPkg('fs-extra', 'add-filename-increment')
|
|
14
|
-
const config = getConfig()
|
|
15
14
|
let file
|
|
16
15
|
if (path.isAbsolute(dest)) file = dest
|
|
17
16
|
else {
|
|
18
|
-
file = `${
|
|
17
|
+
file = `${getPluginDataDir('bajoDb')}/export/${dest}`
|
|
19
18
|
fs.ensureDirSync(path.dirname(file))
|
|
20
19
|
}
|
|
21
20
|
file = increment(file, { fs: true })
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import bcrypt from 'bcrypt'
|
|
2
|
+
import crypto from 'crypto'
|
|
3
|
+
|
|
4
|
+
async function hash (text, type = 'md5', { digest = 'hex', salt = 10 } = {}) {
|
|
5
|
+
if (typeof text !== 'string') text = JSON.stringify(text)
|
|
6
|
+
if (type === 'bcrypt') return await bcrypt.hash(text, salt)
|
|
7
|
+
return crypto.createHash(type).update(text).digest(digest)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default hash
|
|
@@ -8,18 +8,17 @@ const { DataStream } = scramjet
|
|
|
8
8
|
const supportedExt = ['.json', '.jsonl', '.ndjson', '.csv', '.xlsx']
|
|
9
9
|
|
|
10
10
|
async function importFrom (source, dest, { trashOld = true, batch, progressFn, useHeader = true } = {}) {
|
|
11
|
-
const { error, importPkg, getConfig } = this.bajo.helper
|
|
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
15
|
const fs = await importPkg('fs-extra')
|
|
16
|
-
const config = getConfig()
|
|
17
16
|
const cfg = getConfig('bajoExtra')
|
|
18
17
|
|
|
19
18
|
let file
|
|
20
19
|
if (path.isAbsolute(source)) file = source
|
|
21
20
|
else {
|
|
22
|
-
file = `${
|
|
21
|
+
file = `${getPluginDataDir('bajoDb')}/import/${source}`
|
|
23
22
|
fs.ensureDirSync(path.dirname(file))
|
|
24
23
|
}
|
|
25
24
|
if (!fs.existsSync(file)) throw error('Source file \'%s\' doesn\'t exist', file)
|
|
@@ -15,17 +15,17 @@ async function exportTo (path, args) {
|
|
|
15
15
|
if (!this.bajoDb) print.fatal('Bajo DB isn\'t loaded')
|
|
16
16
|
const schemas = map(this.bajoDb.schemas, 'name')
|
|
17
17
|
if (isEmpty(schemas)) print.fatal('No schema found!')
|
|
18
|
-
let [
|
|
19
|
-
if (isEmpty(
|
|
20
|
-
|
|
21
|
-
message: print.__('Please choose
|
|
18
|
+
let [coll, dest, query] = args
|
|
19
|
+
if (isEmpty(coll)) {
|
|
20
|
+
coll = await select({
|
|
21
|
+
message: print.__('Please choose collection:'),
|
|
22
22
|
choices: map(schemas, s => ({ value: s }))
|
|
23
23
|
})
|
|
24
24
|
}
|
|
25
25
|
if (isEmpty(dest)) {
|
|
26
26
|
dest = await input({
|
|
27
27
|
message: print.__('Please enter destination file:'),
|
|
28
|
-
default: `${
|
|
28
|
+
default: `${coll}-${dayjs().format('YYYYMMDD')}.ndjson`,
|
|
29
29
|
validate: (item) => !isEmpty(item)
|
|
30
30
|
})
|
|
31
31
|
}
|
|
@@ -38,12 +38,12 @@ async function exportTo (path, args) {
|
|
|
38
38
|
const progressFn = makeProgress.call(this, spinner)
|
|
39
39
|
const cfg = getConfig('bajoDb', { full: true })
|
|
40
40
|
const { batch } = getConfig()
|
|
41
|
-
const start = await importModule(`${cfg.dir}/bajo/start.js`)
|
|
42
|
-
const { connection } = await this.bajoDb.helper.getInfo(
|
|
41
|
+
const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
|
|
42
|
+
const { connection } = await this.bajoDb.helper.getInfo(coll)
|
|
43
43
|
await start.call(this, connection.name)
|
|
44
44
|
try {
|
|
45
45
|
const filter = { query }
|
|
46
|
-
const result = await this.bajoExtra.helper.exportTo(
|
|
46
|
+
const result = await this.bajoExtra.helper.exportTo(coll, dest, { filter, batch, progressFn })
|
|
47
47
|
spinner.succeed('%d records successfully exported to \'%s\'', result.count, Path.resolve(result.file))
|
|
48
48
|
} catch (err) {
|
|
49
49
|
console.log(err)
|
|
@@ -14,16 +14,16 @@ async function importFrom (path, args) {
|
|
|
14
14
|
if (!this.bajoDb) print.fatal('Bajo DB isn\'t loaded')
|
|
15
15
|
const schemas = map(this.bajoDb.schemas, 'name')
|
|
16
16
|
if (isEmpty(schemas)) print.fatal('No schema found!')
|
|
17
|
-
let [dest,
|
|
17
|
+
let [dest, coll] = args
|
|
18
18
|
if (isEmpty(dest)) {
|
|
19
19
|
dest = await input({
|
|
20
20
|
message: print.__('Please enter source file:'),
|
|
21
21
|
validate: (item) => !isEmpty(item)
|
|
22
22
|
})
|
|
23
23
|
}
|
|
24
|
-
if (isEmpty(
|
|
25
|
-
|
|
26
|
-
message: print.__('Please choose
|
|
24
|
+
if (isEmpty(coll)) {
|
|
25
|
+
coll = await select({
|
|
26
|
+
message: print.__('Please choose collection:'),
|
|
27
27
|
choices: map(schemas, s => ({ value: s }))
|
|
28
28
|
})
|
|
29
29
|
}
|
|
@@ -36,11 +36,11 @@ async function importFrom (path, args) {
|
|
|
36
36
|
const progressFn = makeProgress.call(this, spinner)
|
|
37
37
|
const cfg = getConfig('bajoDb', { full: true })
|
|
38
38
|
const { batch } = getConfig()
|
|
39
|
-
const start = await importModule(`${cfg.dir}/bajo/start.js`)
|
|
40
|
-
const { connection } = await this.bajoDb.helper.getInfo(
|
|
39
|
+
const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
|
|
40
|
+
const { connection } = await this.bajoDb.helper.getInfo(coll)
|
|
41
41
|
await start.call(this, connection.name)
|
|
42
42
|
try {
|
|
43
|
-
const result = await this.bajoExtra.helper.importFrom(dest,
|
|
43
|
+
const result = await this.bajoExtra.helper.importFrom(dest, coll, { batch, progressFn })
|
|
44
44
|
spinner.succeed('%d records successfully imported from \'%s\'', result.count, Path.resolve(result.file))
|
|
45
45
|
} catch (err) {
|
|
46
46
|
spinner.fatal('Error: %s', err.message)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bajo-extra",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Extra package for Bajo Framework",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -28,7 +28,12 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"async": "^3.2.4",
|
|
30
30
|
"axios": "^1.4.0",
|
|
31
|
+
"bcrypt": "^5.1.1",
|
|
32
|
+
"email-addresses": "^5.0.0",
|
|
33
|
+
"fast-jwt": "^3.2.0",
|
|
34
|
+
"fast-xml-parser": "^4.3.2",
|
|
31
35
|
"ndjson-csv-xlsx": "^1.1.1",
|
|
36
|
+
"performant-array-to-tree": "^1.11.0",
|
|
32
37
|
"query-string": "^8.1.0",
|
|
33
38
|
"scramjet": "^4.36.9"
|
|
34
39
|
}
|