forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.ts
62 lines (53 loc) · 1.84 KB
/
import.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
import path from 'path'
import fs from 'fs'
import { flags } from '@oclif/command'
import { Kommand } from '../../common'
import { kuzzleFlags } from '../../support/kuzzle'
import { restoreCollectionData, restoreCollectionMappings } from '../../support/restore-collection'
export default class CollectionImport extends Kommand {
static description = 'Imports a collection'
static flags = {
help: flags.help({}),
'batch-size': flags.string({
description: 'Maximum batch size (see limits.documentsWriteCount config)',
default: '200'
}),
index: flags.string({
description: 'If set, override the index destination name',
}),
collection: flags.string({
description: 'If set, override the collection destination name',
}),
'no-mappings': flags.boolean({
description: 'Skip collection mappings'
}),
...kuzzleFlags,
protocol: flags.string({
description: 'Kuzzle protocol (http or websocket)',
default: 'ws',
}),
}
static args = [
{ name: 'path', description: 'Dump directory path', required: true },
]
async runSafe() {
this.logInfo(`Start importing dump from ${this.args.path}`)
if (!this.flags['no-mappings']) {
const mappingsPath = path.join(this.args.path, 'mappings.json')
const dump = JSON.parse(fs.readFileSync(mappingsPath, 'utf8'))
await restoreCollectionMappings(
this.sdk,
dump,
this.flags.index,
this.flags.collection)
}
const { index, collection, total } = await restoreCollectionData(
this.sdk,
this.log.bind(this),
Number(this.flags['batch-size']),
path.join(this.args.path, 'documents.jsonl'),
this.flags.index,
this.flags.collection)
this.logOk(`Successfully imported ${total} documents from "${this.args.path}" in "${index}:${collection}"`)
}
}