@quillsql/node 0.2.2 → 0.2.4

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 ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
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,18 +130,31 @@ module.exports = ({
127
130
  }
128
131
  );
129
132
  let dashConfig = response.data;
130
- if (dashConfig.filters.length) {
133
+ let newFilters = [];
134
+
135
+ if (dashConfig.filters && 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 = { options: rows };
141
+ newFilters.push({ ...dashConfig.filters[i], options: rows });
142
+ dashConfig.filters[i].options = rows;
139
143
  }
140
144
  }
141
- return dashConfig;
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
+ };
142
158
  }
143
159
 
144
160
  if (task === "create") {
@@ -156,9 +172,10 @@ module.exports = ({
156
172
  );
157
173
  return response.data;
158
174
  }
159
-
160
175
  if (task === "item") {
161
176
  try {
177
+ const { filters } = metadata;
178
+
162
179
  const resp = await axios.get(
163
180
  "https://quill-344421.uc.r.appspot.com/selfhostitem",
164
181
  {
@@ -171,20 +188,35 @@ module.exports = ({
171
188
  },
172
189
  }
173
190
  );
174
- const response = await axios.post(
175
- "https://quill-344421.uc.r.appspot.com/validate",
176
- { query: resp.data.queryString },
177
- {
178
- params: {
179
- orgId,
180
- },
181
- headers: {
182
- Authorization: `Bearer ${privateKey}`,
183
- },
184
- }
185
- );
186
- const { fieldToRemove } = response.data;
187
- const queryResult = await targetPool.query(response.data.query);
191
+ let fieldToRemove: any, query;
192
+
193
+ if (getCache(`config:${orgId}:${id}`) && !filters) {
194
+ getCache(`config:${orgId}:${id}`);
195
+ }
196
+
197
+ if (!filters && getCache(`config:${orgId}:${id}`)) {
198
+ ({ fieldToRemove, query } = getCache(`config:${orgId}:${id}`));
199
+ } else {
200
+ const response = await axios.post(
201
+ "https://quill-344421.uc.r.appspot.com/validate",
202
+ { query: resp.data.queryString },
203
+ {
204
+ params: {
205
+ orgId,
206
+ filters,
207
+ },
208
+ headers: {
209
+ Authorization: `Bearer ${privateKey}`,
210
+ },
211
+ }
212
+ );
213
+ ({ fieldToRemove, query } = response.data);
214
+
215
+ const cacheKey = `config:${orgId}:${id}`;
216
+ setCache(cacheKey, { fieldToRemove, query });
217
+ }
218
+ const queryResult = await targetPool.query(query);
219
+
188
220
  return {
189
221
  ...resp.data,
190
222
  fields: queryResult.fields.filter(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillsql/node",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Quill SDK for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {