@quillsql/node 0.2.2 → 0.2.3
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/.gitattributes +2 -0
- package/LICENSE +21 -0
- package/index.ts +53 -16
- package/package.json +1 -1
package/.gitattributes
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 stryvedev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/index.ts
CHANGED
|
@@ -4,12 +4,21 @@ 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
|
|
8
|
+
|
|
9
|
+
function setCache(key : any, value : any) {
|
|
10
|
+
cache[key] = value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function getCache(key : any) {
|
|
14
|
+
return cache[key];
|
|
15
|
+
}
|
|
16
|
+
|
|
7
17
|
interface QuillConfig {
|
|
8
18
|
privateKey: string;
|
|
9
19
|
databaseConnectionString: string;
|
|
10
20
|
stagingDatabaseConnectionString?: string;
|
|
11
21
|
}
|
|
12
|
-
|
|
13
22
|
type FieldFormat =
|
|
14
23
|
| "whole_number"
|
|
15
24
|
| "one_decimal_place"
|
|
@@ -22,14 +31,12 @@ type FieldFormat =
|
|
|
22
31
|
| "hh_ap_pm"
|
|
23
32
|
| "percent"
|
|
24
33
|
| "string";
|
|
25
|
-
|
|
26
34
|
interface FormattedColumn {
|
|
27
35
|
label: string;
|
|
28
36
|
field: string;
|
|
29
37
|
chartType: string;
|
|
30
38
|
format: FieldFormat;
|
|
31
39
|
}
|
|
32
|
-
|
|
33
40
|
interface QuillRequestMetadata {
|
|
34
41
|
task: string;
|
|
35
42
|
// a query to be run
|
|
@@ -50,13 +57,11 @@ interface QuillRequestMetadata {
|
|
|
50
57
|
dateField?: { table: string; field: string };
|
|
51
58
|
template?: boolean;
|
|
52
59
|
}
|
|
53
|
-
|
|
54
60
|
interface QuillQueryParams {
|
|
55
61
|
orgId: string;
|
|
56
62
|
metadata: QuillRequestMetadata;
|
|
57
63
|
environment?: string;
|
|
58
64
|
}
|
|
59
|
-
|
|
60
65
|
module.exports = ({
|
|
61
66
|
privateKey,
|
|
62
67
|
databaseConnectionString,
|
|
@@ -68,7 +73,6 @@ module.exports = ({
|
|
|
68
73
|
const stagingPool = new Pool({
|
|
69
74
|
connectionString: stagingDatabaseConnectionString,
|
|
70
75
|
});
|
|
71
|
-
|
|
72
76
|
return {
|
|
73
77
|
query: async ({ orgId, metadata, environment }: QuillQueryParams) => {
|
|
74
78
|
const targetPool = environment === "STAGING" ? stagingPool : pool;
|
|
@@ -111,7 +115,6 @@ module.exports = ({
|
|
|
111
115
|
};
|
|
112
116
|
}
|
|
113
117
|
}
|
|
114
|
-
|
|
115
118
|
if (task === "config") {
|
|
116
119
|
const response = await axios.get(
|
|
117
120
|
"https://quill-344421.uc.r.appspot.com/config",
|
|
@@ -127,19 +130,32 @@ module.exports = ({
|
|
|
127
130
|
}
|
|
128
131
|
);
|
|
129
132
|
let dashConfig = response.data;
|
|
133
|
+
let newFilters = [];
|
|
134
|
+
|
|
130
135
|
if (dashConfig.filters.length) {
|
|
131
136
|
for (let i = 0; i < dashConfig.filters.length; i++) {
|
|
132
|
-
// parse query
|
|
133
|
-
// run query
|
|
134
137
|
const queryResult = await targetPool.query(
|
|
135
138
|
dashConfig.filters[i].query
|
|
136
139
|
);
|
|
137
140
|
const { rows } = queryResult;
|
|
138
|
-
dashConfig
|
|
141
|
+
newFilters.push({ ...dashConfig.filters[i], options: rows });
|
|
142
|
+
dashConfig.filters[i].options = rows
|
|
139
143
|
}
|
|
140
144
|
}
|
|
141
|
-
|
|
142
|
-
|
|
145
|
+
|
|
146
|
+
dashConfig = { ...dashConfig, filters: newFilters };
|
|
147
|
+
|
|
148
|
+
const { fieldToRemove, newQueries } = response.data;
|
|
149
|
+
|
|
150
|
+
for (const newQuery of newQueries) {
|
|
151
|
+
const { query } = newQuery
|
|
152
|
+
const cacheKey = `config:${orgId}:${newQuery._id}`;
|
|
153
|
+
setCache(cacheKey, {fieldToRemove, query});
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
...dashConfig
|
|
157
|
+
};
|
|
158
|
+
}
|
|
143
159
|
|
|
144
160
|
if (task === "create") {
|
|
145
161
|
const response = await axios.post(
|
|
@@ -156,9 +172,11 @@ module.exports = ({
|
|
|
156
172
|
);
|
|
157
173
|
return response.data;
|
|
158
174
|
}
|
|
159
|
-
|
|
160
175
|
if (task === "item") {
|
|
161
176
|
try {
|
|
177
|
+
|
|
178
|
+
const {filters } = metadata;
|
|
179
|
+
|
|
162
180
|
const resp = await axios.get(
|
|
163
181
|
"https://quill-344421.uc.r.appspot.com/selfhostitem",
|
|
164
182
|
{
|
|
@@ -171,20 +189,38 @@ module.exports = ({
|
|
|
171
189
|
},
|
|
172
190
|
}
|
|
173
191
|
);
|
|
192
|
+
let fieldToRemove : any, query;
|
|
193
|
+
|
|
194
|
+
if (getCache(`config:${orgId}:${id}`) && !filters) {
|
|
195
|
+
getCache(`config:${orgId}:${id}`)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (!filters && getCache(`config:${orgId}:${id}`)) {
|
|
199
|
+
({fieldToRemove, query} = getCache(`config:${orgId}:${id}`));
|
|
200
|
+
}
|
|
201
|
+
else {
|
|
174
202
|
const response = await axios.post(
|
|
175
203
|
"https://quill-344421.uc.r.appspot.com/validate",
|
|
176
204
|
{ query: resp.data.queryString },
|
|
177
205
|
{
|
|
178
206
|
params: {
|
|
179
207
|
orgId,
|
|
208
|
+
filters
|
|
180
209
|
},
|
|
181
210
|
headers: {
|
|
182
211
|
Authorization: `Bearer ${privateKey}`,
|
|
183
212
|
},
|
|
184
213
|
}
|
|
185
214
|
);
|
|
186
|
-
|
|
187
|
-
|
|
215
|
+
({ fieldToRemove, query } = response.data);
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
const cacheKey = `config:${orgId}:${id}`;
|
|
220
|
+
setCache(cacheKey, {fieldToRemove, query});
|
|
221
|
+
};
|
|
222
|
+
const queryResult = await targetPool.query(query);
|
|
223
|
+
|
|
188
224
|
return {
|
|
189
225
|
...resp.data,
|
|
190
226
|
fields: queryResult.fields.filter(
|
|
@@ -197,6 +233,7 @@ module.exports = ({
|
|
|
197
233
|
return row;
|
|
198
234
|
}),
|
|
199
235
|
};
|
|
236
|
+
|
|
200
237
|
} catch (err) {
|
|
201
238
|
return {
|
|
202
239
|
// @ts-ignore
|
|
@@ -208,4 +245,4 @@ module.exports = ({
|
|
|
208
245
|
}
|
|
209
246
|
},
|
|
210
247
|
};
|
|
211
|
-
};
|
|
248
|
+
};
|