@quillsql/node 0.2.5 → 0.2.7

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.
Files changed (3) hide show
  1. package/index.js +89 -44
  2. package/index.ts +106 -113
  3. 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,17 @@ 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", {
38
+ query: query,
39
+ orgId: orgId,
40
+ filters: [],
41
+ }, {
42
+ headers: {
43
+ Authorization: `Bearer ${privateKey}`,
44
+ },
45
+ });
46
+ const { fieldToRemove } = response.data;
38
47
  const queryResult = yield targetPool.query(response.data.query);
39
48
  return Object.assign(Object.assign({}, queryResult), { fields: queryResult.fields.filter(
40
49
  // @ts-ignore
@@ -52,60 +61,96 @@ module.exports = ({ privateKey, databaseConnectionString, stagingDatabaseConnect
52
61
  }
53
62
  }
54
63
  if (task === "config") {
55
- const response = yield axios.get("https://quill-344421.uc.r.appspot.com/config", {
56
- params: {
57
- orgId,
58
- // @ts-ignore
59
- name: metadata === null || metadata === void 0 ? void 0 : metadata.name,
60
- },
61
- headers: {
62
- Authorization: `Bearer ${privateKey}`,
63
- },
64
- });
65
- let dashConfig = response.data;
66
- if (dashConfig.filters.length) {
67
- for (let i = 0; i < dashConfig.filters.length; i++) {
68
- // parse query
69
- // run query
70
- const queryResult = yield targetPool.query(dashConfig.filters[i].query);
71
- const { rows } = queryResult;
72
- dashConfig = { options: rows };
64
+ try {
65
+ const response = yield axios.get("https://quill-344421.uc.r.appspot.com/config", {
66
+ params: {
67
+ orgId,
68
+ // @ts-ignore
69
+ name: metadata === null || metadata === void 0 ? void 0 : metadata.name,
70
+ },
71
+ headers: {
72
+ Authorization: `Bearer ${privateKey}`,
73
+ },
74
+ });
75
+ let dashConfig = response.data;
76
+ let newFilters = [];
77
+ if (dashConfig.filters && dashConfig.filters.length) {
78
+ for (let i = 0; i < dashConfig.filters.length; i++) {
79
+ const queryResult = yield targetPool.query(dashConfig.filters[i].query);
80
+ const { rows } = queryResult;
81
+ newFilters.push(Object.assign(Object.assign({}, dashConfig.filters[i]), { options: rows }));
82
+ dashConfig.filters[i].options = rows;
83
+ }
84
+ }
85
+ dashConfig = Object.assign(Object.assign({}, dashConfig), { filters: newFilters });
86
+ const { fieldToRemove, newQueries } = response.data;
87
+ if (newQueries) {
88
+ for (const newQuery of newQueries) {
89
+ const { query } = newQuery;
90
+ const cacheKey = `config:${orgId}:${newQuery._id}}`;
91
+ setCache(cacheKey, { fieldToRemove, query });
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 resp = yield axios.get("https://quill-344421.uc.r.appspot.com/selfhostitem", {
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
- const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", { query: resp.data.queryString }, {
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
- const { fieldToRemove } = response.data;
108
- const queryResult = yield targetPool.query(response.data.query);
133
+ let fieldToRemove, query;
134
+ if (false &&
135
+ getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`)) {
136
+ ({ fieldToRemove, query } = getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`));
137
+ }
138
+ else {
139
+ const response = yield axios.post("https://quill-344421.uc.r.appspot.com/validate", {
140
+ dashboardItemId: id,
141
+ query: resp.data.queryString,
142
+ filters: filters,
143
+ orgId: orgId,
144
+ }, {
145
+ headers: {
146
+ Authorization: `Bearer ${privateKey}`,
147
+ },
148
+ });
149
+ ({ fieldToRemove, query } = response.data);
150
+ const cacheKey = `config:${orgId}:${id}:${JSON.stringify(filters)}`;
151
+ setCache(cacheKey, { fieldToRemove, query });
152
+ }
153
+ const queryResult = yield targetPool.query(query);
109
154
  return Object.assign(Object.assign({}, resp.data), { fields: queryResult.fields.filter(
110
155
  // @ts-ignore
111
156
  (field) => field.name !== fieldToRemove),
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
 
@@ -70,6 +70,7 @@ module.exports = ({
70
70
  }: QuillConfig) => {
71
71
  const pool = new Pool({
72
72
  connectionString: databaseConnectionString,
73
+ ssl: { rejectUnauthorized: false },
73
74
  });
74
75
  const stagingPool = new Pool({
75
76
  connectionString: stagingDatabaseConnectionString,
@@ -81,19 +82,20 @@ module.exports = ({
81
82
 
82
83
  if (task === "query") {
83
84
  try {
84
- const response = await axios.post(
85
- `${process.env.SERVER_URL}/validate`,
86
- { query: query },
87
- {
88
- params: {
89
- orgId,
90
- },
91
- headers: {
92
- Authorization: `Bearer ${privateKey}`,
85
+ const response = await axios.post(
86
+ "https://quill-344421.uc.r.appspot.com/validate",
87
+ {
88
+ query: query,
89
+ orgId: orgId,
90
+ filters: [],
93
91
  },
94
- }
95
- );
96
- const { fieldToRemove } = response.data;
92
+ {
93
+ headers: {
94
+ Authorization: `Bearer ${privateKey}`,
95
+ },
96
+ }
97
+ );
98
+ const { fieldToRemove } = response.data;
97
99
  const queryResult = await targetPool.query(response.data.query);
98
100
  return {
99
101
  ...queryResult,
@@ -118,97 +120,89 @@ module.exports = ({
118
120
  }
119
121
  if (task === "config") {
120
122
  try {
121
- const response = await axios.get(
122
- `${process.env.SERVER_URL}/config`,
123
- {
124
- params: {
125
- orgId,
126
- // @ts-ignore
127
- name: metadata?.name,
128
- },
129
- headers: {
130
- Authorization: `Bearer ${privateKey}`,
131
- },
132
- }
133
- );
134
- let dashConfig = response.data;
135
- let newFilters = [];
136
-
137
-
138
- if (dashConfig.filters && dashConfig.filters.length) {
139
- for (let i = 0; i < dashConfig.filters.length; i++) {
140
- const queryResult = await targetPool.query(
141
- dashConfig.filters[i].query
142
- );
143
- const { rows } = queryResult;
144
- newFilters.push({ ...dashConfig.filters[i], options: rows });
145
- dashConfig.filters[i].options = rows
123
+ const response = await axios.get(
124
+ "https://quill-344421.uc.r.appspot.com/config",
125
+ {
126
+ params: {
127
+ orgId,
128
+ // @ts-ignore
129
+ name: metadata?.name,
130
+ },
131
+ headers: {
132
+ Authorization: `Bearer ${privateKey}`,
133
+ },
134
+ }
135
+ );
136
+ let dashConfig = response.data;
137
+ let newFilters = [];
138
+
139
+ if (dashConfig.filters && dashConfig.filters.length) {
140
+ for (let i = 0; i < dashConfig.filters.length; i++) {
141
+ const queryResult = await targetPool.query(
142
+ dashConfig.filters[i].query
143
+ );
144
+ const { rows } = queryResult;
145
+ newFilters.push({ ...dashConfig.filters[i], options: rows });
146
+ dashConfig.filters[i].options = rows;
147
+ }
146
148
  }
147
-
148
- }
149
149
 
150
- dashConfig = { ...dashConfig, filters: newFilters };
150
+ dashConfig = { ...dashConfig, filters: newFilters };
151
151
 
152
- const { fieldToRemove, newQueries } = response.data;
153
-
154
- if (newQueries) {
155
- for (const newQuery of newQueries) {
156
- const { query } = newQuery
157
- const cacheKey = `config:${orgId}:${newQuery._id}}`;
158
- setCache(cacheKey, {fieldToRemove, query});
159
- };
160
- }
161
-
162
- return {
163
- ...dashConfig
164
- };
152
+ const { fieldToRemove, newQueries } = response.data;
153
+
154
+ if (newQueries) {
155
+ for (const newQuery of newQueries) {
156
+ const { query } = newQuery;
157
+ const cacheKey = `config:${orgId}:${newQuery._id}}`;
158
+ setCache(cacheKey, { fieldToRemove, query });
159
+ }
160
+ }
165
161
 
162
+ return {
163
+ ...dashConfig,
164
+ };
165
+ } catch {
166
+ return {
167
+ // @ts-ignore
168
+ ...err,
169
+ // @ts-ignore
170
+ errorMessage: err && err.message ? err.message : "",
171
+ };
166
172
  }
167
- catch {
168
- return {
169
- // @ts-ignore
170
- ...err,
171
- // @ts-ignore
172
- errorMessage: err && err.message ? err.message : "",
173
- };
174
173
  }
175
- }
176
-
174
+
177
175
  if (task === "create") {
178
176
  try {
179
- const response = await axios.post(
180
- `${process.env.SERVER_URL}/item`,
181
- { ...metadata },
182
- {
183
- params: {
184
- orgId,
185
- query
186
- },
187
- headers: {
188
- Authorization: `Bearer ${privateKey}`,
189
- },
190
- }
191
- );
192
- return response.data;
193
- }
194
- catch {
195
- return {
196
- // @ts-ignore
197
- ...err,
198
- // @ts-ignore
199
- errorMessage: err && err.message ? err.message : "",
200
- };
177
+ const response = await axios.post(
178
+ "https://quill-344421.uc.r.appspot.com/item",
179
+ { ...metadata },
180
+ {
181
+ params: {
182
+ orgId,
183
+ query,
184
+ },
185
+ headers: {
186
+ Authorization: `Bearer ${privateKey}`,
187
+ },
188
+ }
189
+ );
190
+ return response.data;
191
+ } catch {
192
+ return {
193
+ // @ts-ignore
194
+ ...err,
195
+ // @ts-ignore
196
+ errorMessage: err && err.message ? err.message : "",
197
+ };
198
+ }
201
199
  }
202
- }
203
200
 
204
201
  if (task === "item") {
205
202
  try {
206
-
207
- const {filters } = metadata;
208
- console.log(filters);
209
-
203
+ const { filters } = metadata;
210
204
  const resp = await axios.get(
211
- `${process.env.SERVER_URL}/selfhostitem`,
205
+ "https://quill-344421.uc.r.appspot.com/selfhostitem",
212
206
  {
213
207
  params: {
214
208
  id,
@@ -219,39 +213,39 @@ module.exports = ({
219
213
  },
220
214
  }
221
215
  );
222
- let fieldToRemove : any, query;
216
+ let fieldToRemove: any, query;
223
217
 
224
- if (false && getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`)) {
225
- ({fieldToRemove, query} = getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`));
226
- }
227
- else {
218
+ if (
219
+ false &&
220
+ getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`)
221
+ ) {
222
+ ({ fieldToRemove, query } = getCache(
223
+ `config:${orgId}:${id}:${JSON.stringify(filters)}`
224
+ ));
225
+ } else {
228
226
  const response = await axios.post(
229
- `${process.env.SERVER_URL}/validate`,
230
- {
227
+ "https://quill-344421.uc.r.appspot.com/validate",
228
+ {
231
229
  dashboardItemId: id,
232
230
  query: resp.data.queryString,
233
231
  filters: filters,
234
- orgId: orgId
232
+ orgId: orgId,
235
233
  },
236
234
  {
237
235
  headers: {
238
- Authorization: `Bearer ${privateKey}`
239
- }
236
+ Authorization: `Bearer ${privateKey}`,
237
+ },
240
238
  }
241
239
  );
242
240
 
243
-
241
+ ({ fieldToRemove, query } = response.data);
244
242
 
245
- ({ fieldToRemove, query } = response.data);
246
-
247
- const cacheKey = `config:${orgId}:${id}:${JSON.stringify(filters)}`;
248
- setCache(cacheKey, {fieldToRemove, query});
249
- };
243
+ const cacheKey = `config:${orgId}:${id}:${JSON.stringify(filters)}`;
244
+ setCache(cacheKey, { fieldToRemove, query });
245
+ }
250
246
 
251
-
252
247
  const queryResult = await targetPool.query(query);
253
248
 
254
-
255
249
  return {
256
250
  ...resp.data,
257
251
  fields: queryResult.fields.filter(
@@ -264,7 +258,6 @@ module.exports = ({
264
258
  return row;
265
259
  }),
266
260
  };
267
-
268
261
  } catch (err) {
269
262
  return {
270
263
  // @ts-ignore
@@ -276,4 +269,4 @@ module.exports = ({
276
269
  }
277
270
  },
278
271
  };
279
- };
272
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillsql/node",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Quill SDK for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {