@quillsql/node 0.2.5 → 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.
Files changed (3) hide show
  1. package/index.js +91 -45
  2. package/index.ts +9 -10
  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,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
- 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 };
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 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);
109
- return Object.assign(Object.assign({}, resp.data), { fields: queryResult.fields.filter(
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
@@ -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,
@@ -82,7 +83,7 @@ module.exports = ({
82
83
  if (task === "query") {
83
84
  try {
84
85
  const response = await axios.post(
85
- `${process.env.SERVER_URL}/validate`,
86
+ "https://quill-344421.uc.r.appspot.com/validate",
86
87
  { query: query },
87
88
  {
88
89
  params: {
@@ -119,7 +120,7 @@ module.exports = ({
119
120
  if (task === "config") {
120
121
  try {
121
122
  const response = await axios.get(
122
- `${process.env.SERVER_URL}/config`,
123
+ "https://quill-344421.uc.r.appspot.com/config",
123
124
  {
124
125
  params: {
125
126
  orgId,
@@ -177,7 +178,7 @@ module.exports = ({
177
178
  if (task === "create") {
178
179
  try {
179
180
  const response = await axios.post(
180
- `${process.env.SERVER_URL}/item`,
181
+ "https://quill-344421.uc.r.appspot.com/item",
181
182
  { ...metadata },
182
183
  {
183
184
  params: {
@@ -205,10 +206,8 @@ module.exports = ({
205
206
  try {
206
207
 
207
208
  const {filters } = metadata;
208
- console.log(filters);
209
-
210
209
  const resp = await axios.get(
211
- `${process.env.SERVER_URL}/selfhostitem`,
210
+ "https://quill-344421.uc.r.appspot.com/selfhostitem",
212
211
  {
213
212
  params: {
214
213
  id,
@@ -221,12 +220,13 @@ module.exports = ({
221
220
  );
222
221
  let fieldToRemove : any, query;
223
222
 
223
+
224
224
  if (false && getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`)) {
225
225
  ({fieldToRemove, query} = getCache(`config:${orgId}:${id}:${JSON.stringify(filters)}`));
226
226
  }
227
227
  else {
228
228
  const response = await axios.post(
229
- `${process.env.SERVER_URL}/validate`,
229
+ "https://quill-344421.uc.r.appspot.com/validate",
230
230
  {
231
231
  dashboardItemId: id,
232
232
  query: resp.data.queryString,
@@ -247,14 +247,13 @@ module.exports = ({
247
247
  const cacheKey = `config:${orgId}:${id}:${JSON.stringify(filters)}`;
248
248
  setCache(cacheKey, {fieldToRemove, query});
249
249
  };
250
-
251
250
 
252
251
  const queryResult = await targetPool.query(query);
253
-
254
252
 
255
253
  return {
256
254
  ...resp.data,
257
- fields: queryResult.fields.filter(
255
+ fields: queryResult.fields
256
+ .filter(
258
257
  // @ts-ignore
259
258
  (field) => field.name !== fieldToRemove
260
259
  ),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillsql/node",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "Quill SDK for Node.js",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {