forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.ts
153 lines (121 loc) · 4.59 KB
/
query.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { flags } from '@oclif/command'
import _ from 'lodash'
import { Kommand } from '../../common'
import { kuzzleFlags } from '../../support/kuzzle'
class SdkQuery extends Kommand {
public static description = `
Executes an API query.
Query arguments
Arguments can be passed and repeated using the --arg or -a flag.
Index and collection names can be passed with --index (-i) and --collection (-c) flags
ID can be passed with the --id flag.
Examples:
- kourou sdk:query document:delete -i iot -c sensors -a refresh=wait_for
Query body
Body can be passed with the --body flag with either a JSON or JS string.
Body will be read from STDIN if available
Examples:
- kourou sdk:query document:create -i iot -c sensors --body '{creation: Date.now())}'
- kourou sdk:query admin:loadMappings < mappings.json
- echo '{dynamic: "strict"}' | kourou sdk:query collection:create -i iot -c sensors
Other
Use the --editor flag to modify the query before sending it to Kuzzle
Use the --display flag to display a specific property of the response
Examples:
- kourou sdk:query document:create -i iot -c sensors --editor
- kourou sdk:query server:now --display 'result.now'
Default fallback to API action
It's possible to use the "sdk:query" command by only specifying the corresponding controller
and action as first argument.
Kourou will try to infer the first arguments to one the following pattern:
- <command> <index>
- <command> <body>
- <command> <index> <collection>
- <command> <index> <collection> <id>
- <command> <index> <collection> <body>
- <command> <index> <collection> <id> <body>
If a flag is given (-i, -c, --body or --id), then the flag value has priority over
argument infering.
Examples:
- kourou collection:list iot
- kourou security:createUser '{ "content": { "profileIds": ["default"] } }' --id yagmur
- kourou collection:delete iot sensors
- kourou document:createOrReplace iot sensors sigfox-1 '{}'
- kourou bulk:import iot sensors '{ bulkData: [...] }'
- kourou admin:loadMappings < mappings.json
`;
public static flags = {
help: flags.help(),
arg: flags.string({
char: 'a',
description: 'Additional argument. Repeatable. (e.g. "-a refresh=wait_for")',
multiple: true
}),
body: flags.string({
description: 'Request body in JS or JSON format. Will be read from STDIN if available.',
default: '{}'
}),
editor: flags.boolean({
description: 'Open an editor (EDITOR env variable) to edit the request before sending.'
}),
'body-editor': flags.boolean({
description: 'Open an editor (EDITOR env variable) to edit the body before sending.'
}),
index: flags.string({
char: 'i',
description: 'Index argument'
}),
collection: flags.string({
char: 'c',
description: 'Collection argument'
}),
id: flags.string({
description: 'ID argument (_id)'
}),
display: flags.string({
description: 'Path of the property to display from the response (empty string to display the result)',
default: 'result'
}),
...kuzzleFlags,
};
static args = [
{ name: 'controller:action', description: 'Controller and action (eg: "server:now")', required: true },
]
static readStdin = true
async runSafe() {
const [controller, action] = this.args['controller:action'].split(':')
const requestArgs: any = {}
requestArgs.index = this.flags.index
requestArgs.collection = this.flags.collection
requestArgs._id = this.flags.id
for (const keyValue of this.flags.arg || []) {
const key = keyValue.substr(0, keyValue.indexOf('='))
const value = keyValue.substr(keyValue.indexOf('=') + 1)
requestArgs[key] = value
}
const body = this.stdin ? this.stdin : this.flags.body
let request = {
controller,
action,
...requestArgs,
body: this.parseJs(body),
}
// content from user editor
if (this.flags.editor && this.flags['body-editor']) {
throw new Error('You cannot specify --editor and --body-editor at the same time')
}
else if (this.flags.editor) {
request = this.fromEditor(request, { json: true })
}
else if (this.flags['body-editor']) {
request.body = this.fromEditor(request.body, { json: true })
}
const response = await this.sdk.query(request)
const display = this.flags.display === ''
? response
: _.get(response, this.flags.display)
this.log(JSON.stringify(display, null, 2))
this.logOk(`Successfully executed "${controller}:${action}"`)
}
}
export default SdkQuery