forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.ts
85 lines (71 loc) · 2.63 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
78
79
80
81
82
83
84
85
import { flags } from '@oclif/command'
import fs from 'fs'
import path from 'path'
import { Kommand } from '../../common'
import { kuzzleFlags } from '../../support/kuzzle'
import { dumpCollectionData, dumpCollectionMappings } from '../../support/dump-collection'
export default class CollectionExport extends Kommand {
static description = 'Exports a collection (JSONL 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'
}),
query: flags.string({
description: 'Only dump documents matching the query (JS or JSON format)',
default: '{}'
}),
editor: flags.boolean({
description: 'Open an editor (EDITOR env variable) to edit the query before sending'
}),
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 },
{ name: 'collection', description: 'Collection name', required: true },
]
static examples = [
'kourou collection:export nyc-open-data yellow-taxi',
'kourou collection:export nyc-open-data yellow-taxi --query \'{ term: { city: "Saigon" } }\'',
]
async runSafe() {
const exportPath = this.flags.path
? path.join(this.flags.path, this.args.index)
: this.args.index
let query = this.parseJs(this.flags.query)
if (this.flags.editor) {
query = this.fromEditor(query, { json: true })
}
const countAll = await this.sdk.document.count(this.args.index, this.args.collection)
const count = await this.sdk.document.count(this.args.index, this.args.collection, { query })
this.logInfo(`Dumping ${count} of ${countAll} documents from collection "${this.args.index}:${this.args.collection}" in ${path} ...`)
fs.mkdirSync(exportPath, { recursive: true })
await dumpCollectionMappings(
this.sdk,
this.args.index,
this.args.collection,
exportPath,
this.flags.format)
await dumpCollectionData(
this.sdk,
this.args.index,
this.args.collection,
Number(this.flags['batch-size']),
exportPath,
query,
this.flags.format)
this.logOk(`Collection ${this.args.index}:${this.args.collection} dumped`)
}
}