forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.ts
77 lines (65 loc) · 2.16 KB
/
export.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { flags } from '@oclif/command'
import fs from 'fs'
import cli from 'cli-ux'
import path from 'path'
import { Kommand } from '../../common'
import { kuzzleFlags } from '../../support/kuzzle'
import { dumpCollectionData, dumpCollectionMappings } from '../../support/dump-collection'
export default class IndexExport extends Kommand {
static description = 'Exports an index (JSONL or Kuzzle format)'
static flags = {
help: flags.help({}),
path: flags.string({
description: 'Dump root directory',
}),
'batch-size': flags.string({
description: 'Maximum batch size (see limits.documentsFetchCount config)',
default: '2000'
}),
format: flags.string({
description: '"jsonl or kuzzle - kuzzle will export in Kuzzle format usable for internal fixtures and jsonl allows to import that data back with kourou',
default: 'jsonl'
}),
...kuzzleFlags,
protocol: flags.string({
description: 'Kuzzle protocol (http or websocket)',
default: 'ws',
}),
}
static args = [
{ name: 'index', description: 'Index name', required: true },
]
async runSafe() {
const exportPath = this.flags.path
? path.join(this.flags.path, this.args.index)
: this.args.index
this.logInfo(`Dumping index "${this.args.index}" in ${exportPath}${path.sep} ...`)
fs.mkdirSync(exportPath, { recursive: true })
const { collections } = await this.sdk.collection.list(this.args.index)
for (const collection of collections) {
try {
if (collection.type !== 'realtime') {
await dumpCollectionMappings(
this.sdk,
this.args.index,
collection.name,
exportPath,
this.flags.format)
await dumpCollectionData(
this.sdk,
this.args.index,
collection.name,
Number(this.flags['batch-size']),
exportPath,
{},
this.flags.format)
cli.action.stop()
}
}
catch (error) {
this.logKo(`Error when exporting collection "${collection.name}": ${error}`)
}
}
this.logOk(`Index ${this.args.index} dumped`)
}
}