bajo-extra 0.2.8 → 0.2.9
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/fetch-and-save.js +10 -15
- package/bajoCli/tool/export-to.js +11 -16
- package/bajoCli/tool/import-from.js +12 -20
- package/bajoCli/tool.js +2 -2
- package/package.json +1 -1
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
async function fetchAndSave ({ source = {}, converter, coll, current = {}, options = {} } = {}) {
|
|
2
|
-
const {
|
|
2
|
+
const { setImmediate, importPkg, spinner, print } = 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
|
|
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)
|
|
6
|
+
const spin = spinner({ showCounter: true }).start('Fetching starts...')
|
|
11
7
|
const resp = await fetch(source.url, source.options ?? {})
|
|
12
|
-
if (isEmpty(resp))
|
|
8
|
+
if (isEmpty(resp)) spin.fatal('No result from server, aborted!')
|
|
13
9
|
if (source.abort) {
|
|
14
10
|
const aborted = await source.abort.call(this, resp)
|
|
15
|
-
if (aborted)
|
|
11
|
+
if (aborted) spin.fatal(aborted)
|
|
16
12
|
}
|
|
17
13
|
let count = 0
|
|
18
14
|
const iterator = isFunction(source.iterator) ? await source.iterator.call(this, resp) : resp[source.iterator]
|
|
19
|
-
|
|
15
|
+
spin.setText('Got %d records, processing...', iterator.length)
|
|
20
16
|
for (let r of iterator) {
|
|
21
17
|
await setImmediate()
|
|
22
18
|
if (converter) r = await converter.call(this, r, options)
|
|
@@ -36,17 +32,16 @@ async function fetchAndSave ({ source = {}, converter, coll, current = {}, optio
|
|
|
36
32
|
}
|
|
37
33
|
}
|
|
38
34
|
}
|
|
39
|
-
if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${
|
|
40
|
-
else
|
|
35
|
+
if (options.printCount && (count % options.printCount === 0)) print.succeed(`[${spin.getElapsed()}] Batch line %d/%d`, count, iterator.length)
|
|
36
|
+
else if (!spin.opts.isLog) spin.setText('Record %d/%d...', count, iterator.length)
|
|
41
37
|
count++
|
|
42
38
|
} catch (err) {
|
|
43
39
|
console.log(err)
|
|
44
|
-
|
|
40
|
+
spin.setText(validationErrorMessage(err) + ', continue')
|
|
45
41
|
}
|
|
46
42
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
else spinner.succeed('Done!')
|
|
43
|
+
spin.info(`${count}/${iterator.length} records processed`)
|
|
44
|
+
spin.succeed('Done!')
|
|
50
45
|
}
|
|
51
46
|
|
|
52
47
|
export default fetchAndSave
|
|
@@ -1,26 +1,21 @@
|
|
|
1
1
|
import Path from 'path'
|
|
2
2
|
|
|
3
|
-
function makeProgress (
|
|
3
|
+
function makeProgress (spin) {
|
|
4
4
|
return async function ({ batchNo, batchTotal, data } = {}) {
|
|
5
5
|
if (batchTotal === 0) return
|
|
6
|
-
|
|
6
|
+
spin.setText('Batch %d of %d (%d records)', batchNo, batchTotal, data.length)
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
async function exportTo ({ path, args
|
|
11
|
-
const { importPkg, print, dayjs, getConfig, importModule } = this.bajo.helper
|
|
10
|
+
async function exportTo ({ path, args }) {
|
|
11
|
+
const { importPkg, print, dayjs, getConfig, importModule, spinner } = this.bajo.helper
|
|
12
12
|
const { isEmpty, map } = await importPkg('lodash-es')
|
|
13
13
|
const [input, select] = await importPkg('bajo-cli:@inquirer/input',
|
|
14
14
|
'bajo-cli:@inquirer/select')
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (returnEarly) return
|
|
18
|
-
}
|
|
15
|
+
const config = getConfig()
|
|
16
|
+
if (!this.bajoDb) return print.fail('Bajo DB isn\'t loaded', { exit: config.tool })
|
|
19
17
|
const schemas = map(this.bajoDb.schemas, 'name')
|
|
20
|
-
if (isEmpty(schemas)) {
|
|
21
|
-
print.fail('No schema found!', { exit: !returnEarly })
|
|
22
|
-
if (returnEarly) return
|
|
23
|
-
}
|
|
18
|
+
if (isEmpty(schemas)) return print.fail('No schema found!', { exit: config.tool })
|
|
24
19
|
let [coll, dest, query] = args
|
|
25
20
|
if (isEmpty(coll)) {
|
|
26
21
|
coll = await select({
|
|
@@ -40,8 +35,8 @@ async function exportTo ({ path, args, returnEarly }) {
|
|
|
40
35
|
message: print.__('Please enter a query (if any):')
|
|
41
36
|
})
|
|
42
37
|
}
|
|
43
|
-
const
|
|
44
|
-
const progressFn = makeProgress.call(this,
|
|
38
|
+
const spin = spinner().start('Exporting...')
|
|
39
|
+
const progressFn = makeProgress.call(this, spin)
|
|
45
40
|
const cfg = getConfig('bajoDb', { full: true })
|
|
46
41
|
const { batch } = getConfig()
|
|
47
42
|
const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
|
|
@@ -50,10 +45,10 @@ async function exportTo ({ path, args, returnEarly }) {
|
|
|
50
45
|
try {
|
|
51
46
|
const filter = { query }
|
|
52
47
|
const result = await this.bajoExtra.helper.exportTo(coll, dest, { filter, batch, progressFn })
|
|
53
|
-
|
|
48
|
+
spin.succeed('%d records successfully exported to \'%s\'', result.count, Path.resolve(result.file))
|
|
54
49
|
} catch (err) {
|
|
55
50
|
console.log(err)
|
|
56
|
-
|
|
51
|
+
spin.fail('Error: %s', err.message, { exit: config.tool })
|
|
57
52
|
}
|
|
58
53
|
}
|
|
59
54
|
|
|
@@ -1,25 +1,20 @@
|
|
|
1
1
|
import Path from 'path'
|
|
2
2
|
|
|
3
|
-
function makeProgress (
|
|
3
|
+
function makeProgress (spin) {
|
|
4
4
|
return async function ({ batchNo, data } = {}) {
|
|
5
|
-
|
|
5
|
+
spin.setText('Batch %d (%d records)', batchNo, data.length)
|
|
6
6
|
}
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
async function importFrom ({ path, args
|
|
10
|
-
const { importPkg, print, importModule, getConfig } = this.bajo.helper
|
|
9
|
+
async function importFrom ({ path, args }) {
|
|
10
|
+
const { importPkg, print, importModule, getConfig, spinner } = this.bajo.helper
|
|
11
11
|
const { isEmpty, map } = await importPkg('lodash-es')
|
|
12
12
|
const [input, select, confirm] = await importPkg('bajo-cli:@inquirer/input',
|
|
13
13
|
'bajo-cli:@inquirer/select', 'bajo-cli:@inquirer/confirm')
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (returnEarly) return
|
|
17
|
-
}
|
|
14
|
+
const config = getConfig()
|
|
15
|
+
if (!this.bajoDb) return print.fail('Bajo DB isn\'t loaded', { exit: config.tool })
|
|
18
16
|
const schemas = map(this.bajoDb.schemas, 'name')
|
|
19
|
-
if (isEmpty(schemas)) {
|
|
20
|
-
print.fail('No schema found!', { exit: !returnEarly })
|
|
21
|
-
if (returnEarly) return
|
|
22
|
-
}
|
|
17
|
+
if (isEmpty(schemas)) return print.fail('No schema found!', { exit: config.tool })
|
|
23
18
|
let [dest, coll] = args
|
|
24
19
|
if (isEmpty(dest)) {
|
|
25
20
|
dest = await input({
|
|
@@ -37,12 +32,9 @@ async function importFrom ({ path, args, returnEarly }) {
|
|
|
37
32
|
message: print.__('You\'re about to replace ALL records with the new ones. Are you really sure?'),
|
|
38
33
|
default: false
|
|
39
34
|
})
|
|
40
|
-
if (!answer) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
const spinner = print.bora('Importing...').start()
|
|
45
|
-
const progressFn = makeProgress.call(this, spinner)
|
|
35
|
+
if (!answer) return print.fail('Aborted!', { exit: config.tool })
|
|
36
|
+
const spin = spinner().start('Importing...')
|
|
37
|
+
const progressFn = makeProgress.call(this, spin)
|
|
46
38
|
const cfg = getConfig('bajoDb', { full: true })
|
|
47
39
|
const { batch } = getConfig()
|
|
48
40
|
const start = await importModule(`${cfg.dir.pkg}/bajo/start.js`)
|
|
@@ -50,9 +42,9 @@ async function importFrom ({ path, args, returnEarly }) {
|
|
|
50
42
|
await start.call(this, connection.name)
|
|
51
43
|
try {
|
|
52
44
|
const result = await this.bajoExtra.helper.importFrom(dest, coll, { batch, progressFn })
|
|
53
|
-
|
|
45
|
+
spin.succeed('%d records successfully imported from \'%s\'', result.count, Path.resolve(result.file))
|
|
54
46
|
} catch (err) {
|
|
55
|
-
|
|
47
|
+
spin.fail('Error: %s', err.message, { exit: config.tool })
|
|
56
48
|
}
|
|
57
49
|
}
|
|
58
50
|
|
package/bajoCli/tool.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
async function tool ({ path, args = []
|
|
1
|
+
async function tool ({ path, args = [] }) {
|
|
2
2
|
const { currentLoc } = this.bajo.helper
|
|
3
3
|
const { runToolMethod } = this.bajoCli.helper
|
|
4
4
|
const options = { demonize: ['shell'] }
|
|
5
|
-
await runToolMethod({ path, args, dir: `${currentLoc(import.meta).dir}/tool`, options
|
|
5
|
+
await runToolMethod({ path, args, dir: `${currentLoc(import.meta).dir}/tool`, options })
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export default tool
|