@quillsql/node 0.2.1 → 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 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.js CHANGED
@@ -13,7 +13,7 @@ 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
- module.exports = ({ publicKey, privateKey, databaseConnectionString, stagingDatabaseConnectionString, }) => {
16
+ module.exports = ({ privateKey, databaseConnectionString, stagingDatabaseConnectionString, }) => {
17
17
  const pool = new Pool({
18
18
  connectionString: databaseConnectionString,
19
19
  });
@@ -27,16 +27,23 @@ module.exports = ({ publicKey, privateKey, databaseConnectionString, stagingData
27
27
  if (task === "query") {
28
28
  const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", { query: query }, {
29
29
  params: {
30
- publicKey,
31
30
  orgId,
32
31
  },
33
32
  headers: {
34
33
  Authorization: `Bearer ${privateKey}`,
35
34
  },
36
35
  });
36
+ const { fieldToRemove } = response.data;
37
37
  try {
38
38
  const queryResult = yield targetPool.query(response.data.query);
39
- return queryResult;
39
+ return Object.assign(Object.assign({}, queryResult), { fields: queryResult.fields.filter(
40
+ // @ts-ignore
41
+ (field) => field.name !== fieldToRemove),
42
+ // @ts-ignore
43
+ rows: queryResult.rows.map((row) => {
44
+ delete row[fieldToRemove];
45
+ return row;
46
+ }) });
40
47
  }
41
48
  catch (err) {
42
49
  return Object.assign(Object.assign({}, err), {
@@ -47,7 +54,6 @@ module.exports = ({ publicKey, privateKey, databaseConnectionString, stagingData
47
54
  if (task === "config") {
48
55
  const response = yield axios.get("https://quill-344421.uc.r.appspot.com/config", {
49
56
  params: {
50
- publicKey,
51
57
  orgId,
52
58
  // @ts-ignore
53
59
  name: metadata === null || metadata === void 0 ? void 0 : metadata.name,
@@ -71,7 +77,6 @@ module.exports = ({ publicKey, privateKey, databaseConnectionString, stagingData
71
77
  if (task === "create") {
72
78
  const response = yield axios.post("https://quill-344421.uc.r.appspot.com/item", Object.assign({}, metadata), {
73
79
  params: {
74
- publicKey,
75
80
  orgId,
76
81
  },
77
82
  headers: {
@@ -86,7 +91,6 @@ module.exports = ({ publicKey, privateKey, databaseConnectionString, stagingData
86
91
  params: {
87
92
  id,
88
93
  orgId,
89
- publicKey,
90
94
  },
91
95
  headers: {
92
96
  Authorization: `Bearer ${privateKey}`,
@@ -94,15 +98,22 @@ module.exports = ({ publicKey, privateKey, databaseConnectionString, stagingData
94
98
  });
95
99
  const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", { query: resp.data.queryString }, {
96
100
  params: {
97
- publicKey,
98
101
  orgId,
99
102
  },
100
103
  headers: {
101
104
  Authorization: `Bearer ${privateKey}`,
102
105
  },
103
106
  });
107
+ const { fieldToRemove } = response.data;
104
108
  const queryResult = yield targetPool.query(response.data.query);
105
- return Object.assign(Object.assign({}, resp.data), { rows: queryResult.rows, fields: queryResult.fields });
109
+ return Object.assign(Object.assign({}, resp.data), { fields: queryResult.fields.filter(
110
+ // @ts-ignore
111
+ (field) => field.name !== fieldToRemove),
112
+ // @ts-ignore
113
+ rows: queryResult.rows.map((row) => {
114
+ delete row[fieldToRemove];
115
+ return row;
116
+ }) });
106
117
  }
107
118
  catch (err) {
108
119
  return Object.assign(Object.assign({}, err), {
package/index.ts CHANGED
@@ -4,13 +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
- publicKey: string;
9
18
  privateKey: string;
10
19
  databaseConnectionString: string;
11
20
  stagingDatabaseConnectionString?: string;
12
21
  }
13
-
14
22
  type FieldFormat =
15
23
  | "whole_number"
16
24
  | "one_decimal_place"
@@ -23,14 +31,12 @@ type FieldFormat =
23
31
  | "hh_ap_pm"
24
32
  | "percent"
25
33
  | "string";
26
-
27
34
  interface FormattedColumn {
28
35
  label: string;
29
36
  field: string;
30
37
  chartType: string;
31
38
  format: FieldFormat;
32
39
  }
33
-
34
40
  interface QuillRequestMetadata {
35
41
  task: string;
36
42
  // a query to be run
@@ -49,16 +55,14 @@ interface QuillRequestMetadata {
49
55
  dashboardName?: string;
50
56
  columns?: FormattedColumn[];
51
57
  dateField?: { table: string; field: string };
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
- publicKey,
62
66
  privateKey,
63
67
  databaseConnectionString,
64
68
  stagingDatabaseConnectionString,
@@ -69,7 +73,6 @@ module.exports = ({
69
73
  const stagingPool = new Pool({
70
74
  connectionString: stagingDatabaseConnectionString,
71
75
  });
72
-
73
76
  return {
74
77
  query: async ({ orgId, metadata, environment }: QuillQueryParams) => {
75
78
  const targetPool = environment === "STAGING" ? stagingPool : pool;
@@ -81,7 +84,6 @@ module.exports = ({
81
84
  { query: query },
82
85
  {
83
86
  params: {
84
- publicKey,
85
87
  orgId,
86
88
  },
87
89
  headers: {
@@ -89,9 +91,21 @@ module.exports = ({
89
91
  },
90
92
  }
91
93
  );
94
+ const { fieldToRemove } = response.data;
92
95
  try {
93
96
  const queryResult = await targetPool.query(response.data.query);
94
- return queryResult;
97
+ return {
98
+ ...queryResult,
99
+ fields: queryResult.fields.filter(
100
+ // @ts-ignore
101
+ (field) => field.name !== fieldToRemove
102
+ ),
103
+ // @ts-ignore
104
+ rows: queryResult.rows.map((row) => {
105
+ delete row[fieldToRemove];
106
+ return row;
107
+ }),
108
+ };
95
109
  } catch (err) {
96
110
  return {
97
111
  // @ts-ignore
@@ -101,13 +115,11 @@ module.exports = ({
101
115
  };
102
116
  }
103
117
  }
104
-
105
118
  if (task === "config") {
106
119
  const response = await axios.get(
107
120
  "https://quill-344421.uc.r.appspot.com/config",
108
121
  {
109
122
  params: {
110
- publicKey,
111
123
  orgId,
112
124
  // @ts-ignore
113
125
  name: metadata?.name,
@@ -118,19 +130,32 @@ module.exports = ({
118
130
  }
119
131
  );
120
132
  let dashConfig = response.data;
133
+ let newFilters = [];
134
+
121
135
  if (dashConfig.filters.length) {
122
136
  for (let i = 0; i < dashConfig.filters.length; i++) {
123
- // parse query
124
- // run query
125
137
  const queryResult = await targetPool.query(
126
138
  dashConfig.filters[i].query
127
139
  );
128
140
  const { rows } = queryResult;
129
- dashConfig = { options: rows };
141
+ newFilters.push({ ...dashConfig.filters[i], options: rows });
142
+ dashConfig.filters[i].options = rows
130
143
  }
131
144
  }
132
- return dashConfig;
133
- }
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
+ }
134
159
 
135
160
  if (task === "create") {
136
161
  const response = await axios.post(
@@ -138,7 +163,6 @@ module.exports = ({
138
163
  { ...metadata },
139
164
  {
140
165
  params: {
141
- publicKey,
142
166
  orgId,
143
167
  },
144
168
  headers: {
@@ -148,41 +172,68 @@ module.exports = ({
148
172
  );
149
173
  return response.data;
150
174
  }
151
-
152
175
  if (task === "item") {
153
176
  try {
177
+
178
+ const {filters } = metadata;
179
+
154
180
  const resp = await axios.get(
155
181
  "https://quill-344421.uc.r.appspot.com/selfhostitem",
156
182
  {
157
183
  params: {
158
184
  id,
159
185
  orgId,
160
- publicKey,
161
186
  },
162
187
  headers: {
163
188
  Authorization: `Bearer ${privateKey}`,
164
189
  },
165
190
  }
166
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 {
167
202
  const response = await axios.post(
168
203
  "https://quill-344421.uc.r.appspot.com/validate",
169
204
  { query: resp.data.queryString },
170
205
  {
171
206
  params: {
172
- publicKey,
173
207
  orgId,
208
+ filters
174
209
  },
175
210
  headers: {
176
211
  Authorization: `Bearer ${privateKey}`,
177
212
  },
178
213
  }
179
214
  );
180
- const queryResult = await targetPool.query(response.data.query);
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
+
181
224
  return {
182
225
  ...resp.data,
183
- rows: queryResult.rows,
184
- fields: queryResult.fields,
226
+ fields: queryResult.fields.filter(
227
+ // @ts-ignore
228
+ (field) => field.name !== fieldToRemove
229
+ ),
230
+ // @ts-ignore
231
+ rows: queryResult.rows.map((row) => {
232
+ delete row[fieldToRemove];
233
+ return row;
234
+ }),
185
235
  };
236
+
186
237
  } catch (err) {
187
238
  return {
188
239
  // @ts-ignore
@@ -194,4 +245,4 @@ module.exports = ({
194
245
  }
195
246
  },
196
247
  };
197
- };
248
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillsql/node",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Quill SDK for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {