-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstored.js
More file actions
114 lines (103 loc) · 3.16 KB
/
stored.js
File metadata and controls
114 lines (103 loc) · 3.16 KB
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
const { httpBody } = require('../response-transforms');
const CONTENT_TYPE_JSON = 'application/json';
const getBody = (conn, storedQuery, options) => {
if (!options.contentType || options.contentType === CONTENT_TYPE_JSON) {
const body = { ...storedQuery };
body.creator = conn.username;
body.shared = typeof body.shared === 'boolean' ? body.shared : false;
return body;
}
// pass storedQuery through
return storedQuery;
};
/*
body
- name string
- database string or "*"
- query
- shared boolean (defaults to false)
*/
const create = (conn, storedQuery, options = {}) => {
const headers = conn.headers();
headers.set('Content-Type', options.contentType || CONTENT_TYPE_JSON);
headers.set('Accept', options.accept || CONTENT_TYPE_JSON);
return fetch(conn.request('admin', 'queries', 'stored'), {
headers,
method: 'POST',
body: JSON.stringify(getBody(conn, storedQuery, options)),
}).then(httpBody);
};
const list = (conn, options = {}) => {
const headers = conn.headers();
headers.set('Accept', options.accept || CONTENT_TYPE_JSON);
return fetch(conn.request('admin', 'queries', 'stored'), {
headers,
}).then(httpBody);
};
const deleteStoredQuery = (conn, storedQuery) => {
const headers = conn.headers();
headers.set('Accept', CONTENT_TYPE_JSON);
return fetch(conn.request('admin', 'queries', 'stored', storedQuery), {
headers,
method: 'DELETE',
}).then(httpBody);
};
const renameStoredQuery = (conn, name, newName) => {
const headers = conn.headers();
headers.set('Content-Type', CONTENT_TYPE_JSON);
headers.set('Accept', CONTENT_TYPE_JSON);
const body = { name: newName };
return fetch(conn.request('admin', 'queries', 'stored', name), {
headers,
method: 'POST',
body: JSON.stringify(body),
}).then(httpBody);
};
const getStoredQuery = (conn, name, options = {}) => {
const headers = conn.headers();
headers.set('Accept', options.accept || CONTENT_TYPE_JSON);
return fetch(conn.request('admin', 'queries', 'stored', name), {
headers,
}).then(httpBody);
};
const replace = (conn, storedQuery) =>
deleteStoredQuery(conn, storedQuery.name).then(deleteResponse => {
// Update creates when the query did not already exist
if (deleteResponse.status === 404 || deleteResponse.ok) {
return create(conn, storedQuery);
}
return deleteResponse;
});
const updateStoredQuery = (
conn,
storedQuery,
options = {},
useUpdateMethod = true
) => {
if (!useUpdateMethod) {
return replace(conn, storedQuery);
}
const headers = conn.headers();
headers.set('Content-Type', options.contentType || CONTENT_TYPE_JSON);
headers.set('Accept', options.accept || CONTENT_TYPE_JSON);
return fetch(conn.request('admin', 'queries', 'stored'), {
headers,
method: 'PUT',
body: JSON.stringify(getBody(conn, storedQuery, options)),
})
.then(httpBody)
.then(updateResponse => {
if (updateResponse.status === 405) {
return replace(conn, storedQuery);
}
return updateResponse;
});
};
module.exports = {
create,
get: getStoredQuery,
list,
remove: deleteStoredQuery,
rename: renameStoredQuery,
update: updateStoredQuery,
};