@quillsql/node 0.2.4 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +91 -45
- package/index.ts +70 -35
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -13,9 +13,17 @@ const axios = require("axios");
|
|
|
13
13
|
var PgError = require("pg-error");
|
|
14
14
|
Connection.prototype.parseE = PgError.parse;
|
|
15
15
|
Connection.prototype.parseN = PgError.parse;
|
|
16
|
+
const cache = {}; //set the cache
|
|
17
|
+
function setCache(key, value) {
|
|
18
|
+
cache[key] = value;
|
|
19
|
+
}
|
|
20
|
+
function getCache(key) {
|
|
21
|
+
return cache[key];
|
|
22
|
+
}
|
|
16
23
|
module.exports = ({ privateKey, databaseConnectionString, stagingDatabaseConnectionString, }) => {
|
|
17
24
|
const pool = new Pool({
|
|
18
25
|
connectionString: databaseConnectionString,
|
|
26
|
+
ssl: { rejectUnauthorized: false }
|
|
19
27
|
});
|
|
20
28
|
const stagingPool = new Pool({
|
|
21
29
|
connectionString: stagingDatabaseConnectionString,
|
|
@@ -25,16 +33,16 @@ module.exports = ({ privateKey, databaseConnectionString, stagingDatabaseConnect
|
|
|
25
33
|
const targetPool = environment === "STAGING" ? stagingPool : pool;
|
|
26
34
|
const { task, query, id } = metadata;
|
|
27
35
|
if (task === "query") {
|
|
28
|
-
const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", { query: query }, {
|
|
29
|
-
params: {
|
|
30
|
-
orgId,
|
|
31
|
-
},
|
|
32
|
-
headers: {
|
|
33
|
-
Authorization: `Bearer ${privateKey}`,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
const { fieldToRemove } = response.data;
|
|
37
36
|
try {
|
|
37
|
+
const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", { query: query }, {
|
|
38
|
+
params: {
|
|
39
|
+
orgId,
|
|
40
|
+
},
|
|
41
|
+
headers: {
|
|
42
|
+
Authorization: `Bearer ${privateKey}`,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const { fieldToRemove } = response.data;
|
|
38
46
|
const queryResult = yield targetPool.query(response.data.query);
|
|
39
47
|
return Object.assign(Object.assign({}, queryResult), { fields: queryResult.fields.filter(
|
|
40
48
|
// @ts-ignore
|
|
@@ -52,61 +60,99 @@ module.exports = ({ privateKey, databaseConnectionString, stagingDatabaseConnect
|
|
|
52
60
|
}
|
|
53
61
|
}
|
|
54
62
|
if (task === "config") {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
63
|
+
try {
|
|
64
|
+
const response = yield axios.get("https://quill-344421.uc.r.appspot.com/config", {
|
|
65
|
+
params: {
|
|
66
|
+
orgId,
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
name: metadata === null || metadata === void 0 ? void 0 : metadata.name,
|
|
69
|
+
},
|
|
70
|
+
headers: {
|
|
71
|
+
Authorization: `Bearer ${privateKey}`,
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
let dashConfig = response.data;
|
|
75
|
+
let newFilters = [];
|
|
76
|
+
if (dashConfig.filters && dashConfig.filters.length) {
|
|
77
|
+
for (let i = 0; i < dashConfig.filters.length; i++) {
|
|
78
|
+
const queryResult = yield targetPool.query(dashConfig.filters[i].query);
|
|
79
|
+
const { rows } = queryResult;
|
|
80
|
+
newFilters.push(Object.assign(Object.assign({}, dashConfig.filters[i]), { options: rows }));
|
|
81
|
+
dashConfig.filters[i].options = rows;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
dashConfig = Object.assign(Object.assign({}, dashConfig), { filters: newFilters });
|
|
85
|
+
const { fieldToRemove, newQueries } = response.data;
|
|
86
|
+
if (newQueries) {
|
|
87
|
+
for (const newQuery of newQueries) {
|
|
88
|
+
const { query } = newQuery;
|
|
89
|
+
const cacheKey = `config:${orgId}:${newQuery._id}}`;
|
|
90
|
+
setCache(cacheKey, { fieldToRemove, query });
|
|
91
|
+
}
|
|
92
|
+
;
|
|
73
93
|
}
|
|
94
|
+
return Object.assign({}, dashConfig);
|
|
95
|
+
}
|
|
96
|
+
catch (_a) {
|
|
97
|
+
return Object.assign(Object.assign({}, err), {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
errorMessage: err && err.message ? err.message : "" });
|
|
74
100
|
}
|
|
75
|
-
return dashConfig;
|
|
76
101
|
}
|
|
77
102
|
if (task === "create") {
|
|
78
|
-
const response = yield axios.post("https://quill-344421.uc.r.appspot.com/item", Object.assign({}, metadata), {
|
|
79
|
-
params: {
|
|
80
|
-
orgId,
|
|
81
|
-
},
|
|
82
|
-
headers: {
|
|
83
|
-
Authorization: `Bearer ${privateKey}`,
|
|
84
|
-
},
|
|
85
|
-
});
|
|
86
|
-
return response.data;
|
|
87
|
-
}
|
|
88
|
-
if (task === "item") {
|
|
89
103
|
try {
|
|
90
|
-
const
|
|
104
|
+
const response = yield axios.post("https://quill-344421.uc.r.appspot.com/item", Object.assign({}, metadata), {
|
|
91
105
|
params: {
|
|
92
|
-
id,
|
|
93
106
|
orgId,
|
|
107
|
+
query
|
|
94
108
|
},
|
|
95
109
|
headers: {
|
|
96
110
|
Authorization: `Bearer ${privateKey}`,
|
|
97
111
|
},
|
|
98
112
|
});
|
|
99
|
-
|
|
113
|
+
return response.data;
|
|
114
|
+
}
|
|
115
|
+
catch (_b) {
|
|
116
|
+
return Object.assign(Object.assign({}, err), {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
errorMessage: err && err.message ? err.message : "" });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (task === "item") {
|
|
122
|
+
try {
|
|
123
|
+
const { filters } = metadata;
|
|
124
|
+
const resp = yield axios.get("https://quill-344421.uc.r.appspot.com/selfhostitem", {
|
|
100
125
|
params: {
|
|
126
|
+
id,
|
|
101
127
|
orgId,
|
|
102
128
|
},
|
|
103
129
|
headers: {
|
|
104
130
|
Authorization: `Bearer ${privateKey}`,
|
|
105
131
|
},
|
|
106
132
|
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
133
|
+
let fieldToRemove, query;
|
|
134
|
+
if (false && getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`)) {
|
|
135
|
+
({ fieldToRemove, query } = getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`));
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", {
|
|
139
|
+
dashboardItemId: id,
|
|
140
|
+
query: resp.data.queryString,
|
|
141
|
+
filters: filters,
|
|
142
|
+
orgId: orgId
|
|
143
|
+
}, {
|
|
144
|
+
headers: {
|
|
145
|
+
Authorization: `Bearer ${privateKey}`
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
({ fieldToRemove, query } = response.data);
|
|
149
|
+
const cacheKey = `config:${orgId}:${id}:${JSON.stringify(filters)}`;
|
|
150
|
+
setCache(cacheKey, { fieldToRemove, query });
|
|
151
|
+
}
|
|
152
|
+
;
|
|
153
|
+
const queryResult = yield targetPool.query(query);
|
|
154
|
+
return Object.assign(Object.assign({}, resp.data), { fields: queryResult.fields
|
|
155
|
+
.filter(
|
|
110
156
|
// @ts-ignore
|
|
111
157
|
(field) => field.name !== fieldToRemove),
|
|
112
158
|
// @ts-ignore
|
package/index.ts
CHANGED
|
@@ -4,13 +4,13 @@ var PgError = require("pg-error");
|
|
|
4
4
|
Connection.prototype.parseE = PgError.parse;
|
|
5
5
|
Connection.prototype.parseN = PgError.parse;
|
|
6
6
|
|
|
7
|
-
const cache: any = {}; //set the cache
|
|
7
|
+
const cache : any = {}; //set the cache
|
|
8
8
|
|
|
9
|
-
function setCache(key: any, value: any) {
|
|
9
|
+
function setCache(key : any, value : any) {
|
|
10
10
|
cache[key] = value;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
function getCache(key: any) {
|
|
13
|
+
function getCache(key : any) {
|
|
14
14
|
return cache[key];
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -19,6 +19,7 @@ interface QuillConfig {
|
|
|
19
19
|
databaseConnectionString: string;
|
|
20
20
|
stagingDatabaseConnectionString?: string;
|
|
21
21
|
}
|
|
22
|
+
|
|
22
23
|
type FieldFormat =
|
|
23
24
|
| "whole_number"
|
|
24
25
|
| "one_decimal_place"
|
|
@@ -69,6 +70,7 @@ module.exports = ({
|
|
|
69
70
|
}: QuillConfig) => {
|
|
70
71
|
const pool = new Pool({
|
|
71
72
|
connectionString: databaseConnectionString,
|
|
73
|
+
ssl: {rejectUnauthorized: false}
|
|
72
74
|
});
|
|
73
75
|
const stagingPool = new Pool({
|
|
74
76
|
connectionString: stagingDatabaseConnectionString,
|
|
@@ -79,6 +81,7 @@ module.exports = ({
|
|
|
79
81
|
const { task, query, id } = metadata;
|
|
80
82
|
|
|
81
83
|
if (task === "query") {
|
|
84
|
+
try {
|
|
82
85
|
const response = await axios.post(
|
|
83
86
|
"https://quill-344421.uc.r.appspot.com/validate",
|
|
84
87
|
{ query: query },
|
|
@@ -89,10 +92,9 @@ module.exports = ({
|
|
|
89
92
|
headers: {
|
|
90
93
|
Authorization: `Bearer ${privateKey}`,
|
|
91
94
|
},
|
|
92
|
-
}
|
|
95
|
+
}
|
|
93
96
|
);
|
|
94
97
|
const { fieldToRemove } = response.data;
|
|
95
|
-
try {
|
|
96
98
|
const queryResult = await targetPool.query(response.data.query);
|
|
97
99
|
return {
|
|
98
100
|
...queryResult,
|
|
@@ -116,6 +118,7 @@ module.exports = ({
|
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
120
|
if (task === "config") {
|
|
121
|
+
try {
|
|
119
122
|
const response = await axios.get(
|
|
120
123
|
"https://quill-344421.uc.r.appspot.com/config",
|
|
121
124
|
{
|
|
@@ -131,7 +134,8 @@ module.exports = ({
|
|
|
131
134
|
);
|
|
132
135
|
let dashConfig = response.data;
|
|
133
136
|
let newFilters = [];
|
|
134
|
-
|
|
137
|
+
|
|
138
|
+
|
|
135
139
|
if (dashConfig.filters && dashConfig.filters.length) {
|
|
136
140
|
for (let i = 0; i < dashConfig.filters.length; i++) {
|
|
137
141
|
const queryResult = await targetPool.query(
|
|
@@ -139,31 +143,47 @@ module.exports = ({
|
|
|
139
143
|
);
|
|
140
144
|
const { rows } = queryResult;
|
|
141
145
|
newFilters.push({ ...dashConfig.filters[i], options: rows });
|
|
142
|
-
dashConfig.filters[i].options = rows
|
|
146
|
+
dashConfig.filters[i].options = rows
|
|
143
147
|
}
|
|
148
|
+
|
|
144
149
|
}
|
|
145
150
|
|
|
146
151
|
dashConfig = { ...dashConfig, filters: newFilters };
|
|
147
152
|
|
|
148
153
|
const { fieldToRemove, newQueries } = response.data;
|
|
154
|
+
|
|
155
|
+
if (newQueries) {
|
|
156
|
+
for (const newQuery of newQueries) {
|
|
157
|
+
const { query } = newQuery
|
|
158
|
+
const cacheKey = `config:${orgId}:${newQuery._id}}`;
|
|
159
|
+
setCache(cacheKey, {fieldToRemove, query});
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return {
|
|
164
|
+
...dashConfig
|
|
165
|
+
};
|
|
149
166
|
|
|
150
|
-
for (const newQuery of newQueries) {
|
|
151
|
-
const { query } = newQuery;
|
|
152
|
-
const cacheKey = `config:${orgId}:${newQuery._id}`;
|
|
153
|
-
setCache(cacheKey, { fieldToRemove, query });
|
|
154
167
|
}
|
|
168
|
+
catch {
|
|
155
169
|
return {
|
|
156
|
-
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
...err,
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
errorMessage: err && err.message ? err.message : "",
|
|
157
174
|
};
|
|
158
175
|
}
|
|
159
|
-
|
|
176
|
+
}
|
|
177
|
+
|
|
160
178
|
if (task === "create") {
|
|
179
|
+
try {
|
|
161
180
|
const response = await axios.post(
|
|
162
181
|
"https://quill-344421.uc.r.appspot.com/item",
|
|
163
182
|
{ ...metadata },
|
|
164
183
|
{
|
|
165
184
|
params: {
|
|
166
185
|
orgId,
|
|
186
|
+
query
|
|
167
187
|
},
|
|
168
188
|
headers: {
|
|
169
189
|
Authorization: `Bearer ${privateKey}`,
|
|
@@ -172,10 +192,20 @@ module.exports = ({
|
|
|
172
192
|
);
|
|
173
193
|
return response.data;
|
|
174
194
|
}
|
|
195
|
+
catch {
|
|
196
|
+
return {
|
|
197
|
+
// @ts-ignore
|
|
198
|
+
...err,
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
errorMessage: err && err.message ? err.message : "",
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
175
205
|
if (task === "item") {
|
|
176
206
|
try {
|
|
177
|
-
const { filters } = metadata;
|
|
178
207
|
|
|
208
|
+
const {filters } = metadata;
|
|
179
209
|
const resp = await axios.get(
|
|
180
210
|
"https://quill-344421.uc.r.appspot.com/selfhostitem",
|
|
181
211
|
{
|
|
@@ -188,38 +218,42 @@ module.exports = ({
|
|
|
188
218
|
},
|
|
189
219
|
}
|
|
190
220
|
);
|
|
191
|
-
let fieldToRemove: any, query;
|
|
221
|
+
let fieldToRemove : any, query;
|
|
192
222
|
|
|
193
|
-
if (getCache(`config:${orgId}:${id}`) && !filters) {
|
|
194
|
-
getCache(`config:${orgId}:${id}`);
|
|
195
|
-
}
|
|
196
223
|
|
|
197
|
-
if (
|
|
198
|
-
({
|
|
199
|
-
}
|
|
224
|
+
if (false && getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`)) {
|
|
225
|
+
({fieldToRemove, query} = getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`));
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
200
228
|
const response = await axios.post(
|
|
201
229
|
"https://quill-344421.uc.r.appspot.com/validate",
|
|
202
|
-
{
|
|
230
|
+
{
|
|
231
|
+
dashboardItemId: id,
|
|
232
|
+
query: resp.data.queryString,
|
|
233
|
+
filters: filters,
|
|
234
|
+
orgId: orgId
|
|
235
|
+
},
|
|
203
236
|
{
|
|
204
|
-
params: {
|
|
205
|
-
orgId,
|
|
206
|
-
filters,
|
|
207
|
-
},
|
|
208
237
|
headers: {
|
|
209
|
-
Authorization: `Bearer ${privateKey}
|
|
210
|
-
}
|
|
238
|
+
Authorization: `Bearer ${privateKey}`
|
|
239
|
+
}
|
|
211
240
|
}
|
|
212
241
|
);
|
|
213
|
-
({ fieldToRemove, query } = response.data);
|
|
214
242
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
218
|
-
const queryResult = await targetPool.query(query);
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
({ fieldToRemove, query } = response.data);
|
|
219
246
|
|
|
247
|
+
const cacheKey = `config:${orgId}:${id}:${JSON.stringify(filters)}`;
|
|
248
|
+
setCache(cacheKey, {fieldToRemove, query});
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const queryResult = await targetPool.query(query);
|
|
252
|
+
|
|
220
253
|
return {
|
|
221
254
|
...resp.data,
|
|
222
|
-
fields: queryResult.fields
|
|
255
|
+
fields: queryResult.fields
|
|
256
|
+
.filter(
|
|
223
257
|
// @ts-ignore
|
|
224
258
|
(field) => field.name !== fieldToRemove
|
|
225
259
|
),
|
|
@@ -229,6 +263,7 @@ module.exports = ({
|
|
|
229
263
|
return row;
|
|
230
264
|
}),
|
|
231
265
|
};
|
|
266
|
+
|
|
232
267
|
} catch (err) {
|
|
233
268
|
return {
|
|
234
269
|
// @ts-ignore
|
|
@@ -240,4 +275,4 @@ module.exports = ({
|
|
|
240
275
|
}
|
|
241
276
|
},
|
|
242
277
|
};
|
|
243
|
-
};
|
|
278
|
+
};
|