@quillsql/node 0.3.4 → 0.3.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.
@@ -12,8 +12,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
+ const axios_1 = __importDefault(require("axios"));
15
16
  /** This client is currently not used but is a good design pratice */
16
- const node_fetch_1 = __importDefault(require("node-fetch"));
17
17
  class QuillServerClient {
18
18
  constructor(privateKey) {
19
19
  this.baseUrl = "";
@@ -21,12 +21,7 @@ class QuillServerClient {
21
21
  }
22
22
  postQuill(route, payload) {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
- const response = yield (0, node_fetch_1.default)(`${this.baseUrl}/${route}`, {
25
- method: "POST",
26
- headers: Object.assign(Object.assign({}, this.config.headers), { "Content-Type": "application/json" }),
27
- body: JSON.stringify(payload),
28
- });
29
- return response.json();
24
+ return axios_1.default.post(`${this.baseUrl}/${route}`, payload, this.config);
30
25
  });
31
26
  }
32
27
  }
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const CachedPools_1 = require("./db/CachedPools");
16
- const node_fetch_1 = __importDefault(require("node-fetch"));
16
+ const axios_1 = __importDefault(require("axios"));
17
17
  require("dotenv/config");
18
18
  const RunQueryProcesses_1 = require("./utils/RunQueryProcesses");
19
19
  const HOST = process.env.ENV === "development"
@@ -32,6 +32,7 @@ class QuillClass {
32
32
  this.targetPool = new CachedPools_1.CachedPool({ connectionString: this.connectionString, ssl: this.ssl }, cache);
33
33
  }
34
34
  query({ orgId, metadata }) {
35
+ var _a, _b;
35
36
  return __awaiter(this, void 0, void 0, function* () {
36
37
  this.targetPool.orgId = orgId;
37
38
  try {
@@ -39,11 +40,21 @@ class QuillClass {
39
40
  const preQueryResults = yield this.runQueries(metadata.preQueries);
40
41
  const response = yield this.postQuill(metadata.task, Object.assign(Object.assign({}, metadata), { orgId,
41
42
  preQueryResults }));
42
- const results = yield this.runQueries(response.queries, response.runQueryConfig);
43
43
  // if there is no metadata object in the response, create one
44
44
  if (!response.metadata) {
45
45
  response.metadata = {};
46
46
  }
47
+ const results = yield this.runQueries(response.queries, response.metadata.runQueryConfig);
48
+ // QUICK JANKY FIX TO UPDATE METADATA AFTER GETTING MAPPED ARRAYS
49
+ if (results.mappedArray &&
50
+ ((_b = (_a = response.metadata) === null || _a === void 0 ? void 0 : _a.runQueryConfig) === null || _b === void 0 ? void 0 : _b.arrayToMap)) {
51
+ const arrayToMap = response.metadata.runQueryConfig.arrayToMap;
52
+ results.mappedArray.forEach((array, index) => {
53
+ response.metadata[arrayToMap.arrayName][index][arrayToMap.field] =
54
+ array;
55
+ });
56
+ delete results.mappedArray;
57
+ }
47
58
  // if there is a single query array in queryResults and the rows and field objects to metadata
48
59
  if ((results === null || results === void 0 ? void 0 : results.queryResults.length) === 1) {
49
60
  const queryResults = results.queryResults[0];
@@ -71,7 +82,8 @@ class QuillClass {
71
82
  if (!queries)
72
83
  return Object.assign(Object.assign({}, results), { queryResults: [] });
73
84
  if (runQueryConfig === null || runQueryConfig === void 0 ? void 0 : runQueryConfig.arrayToMap) {
74
- return Object.assign(Object.assign({}, results), { mappedQueries: yield (0, RunQueryProcesses_1.mapQueries)(queries, runQueryConfig.arrayToMap, this.targetPool) });
85
+ const mappedArray = yield (0, RunQueryProcesses_1.mapQueries)(queries, runQueryConfig.arrayToMap, this.targetPool);
86
+ return Object.assign(Object.assign({}, results), { queryResults: [], mappedArray });
75
87
  }
76
88
  else {
77
89
  const queryResults = yield Promise.all(queries.map((query) => __awaiter(this, void 0, void 0, function* () {
@@ -90,13 +102,8 @@ class QuillClass {
90
102
  }
91
103
  postQuill(path, payload) {
92
104
  return __awaiter(this, void 0, void 0, function* () {
93
- const response = yield (0, node_fetch_1.default)(`${this.baseUrl}/sdk/${path}`, {
94
- method: "POST",
95
- headers: this.config.headers,
96
- body: JSON.stringify(payload),
97
- });
98
- const data = yield response.json();
99
- return data;
105
+ const response = yield axios_1.default.post(`${this.baseUrl}/sdk/${path}`, payload, this.config);
106
+ return response.data;
100
107
  });
101
108
  }
102
109
  close() {
@@ -37,12 +37,12 @@ function removeFields(queryResults, fieldsToRemove) {
37
37
  exports.removeFields = removeFields;
38
38
  function mapQueries(queries, arrayToMap, targetPool) {
39
39
  return __awaiter(this, void 0, void 0, function* () {
40
- let arrayToMapResult = [];
40
+ const mappedArray = [];
41
41
  for (let i = 0; i < queries.length; i++) {
42
42
  const queryResult = yield targetPool.query(queries[i]);
43
- arrayToMapResult.push(Object.assign(Object.assign({}, arrayToMap.arr[i]), { [arrayToMap.field]: queryResult.rows }));
43
+ mappedArray.push(queryResult.rows);
44
44
  }
45
- return arrayToMapResult;
45
+ return mappedArray;
46
46
  });
47
47
  }
48
48
  exports.mapQueries = mapQueries;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quillsql/node",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Quill's client SDK for node backends.",
5
5
  "main": "dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -26,8 +26,8 @@
26
26
  "typescript": "^5.3.3"
27
27
  },
28
28
  "dependencies": {
29
+ "axios": "^1.6.5",
29
30
  "dotenv": "^16.3.1",
30
- "node-fetch": "^3.3.2",
31
31
  "pg": "^8.11.3",
32
32
  "redis": "^4.6.12",
33
33
  "ts-jest": "^29.1.1"
@@ -1,5 +1,6 @@
1
+ import axios from "axios";
2
+
1
3
  /** This client is currently not used but is a good design pratice */
2
- import fetch from "node-fetch";
3
4
 
4
5
  class QuillServerClient {
5
6
  private baseUrl: string;
@@ -15,15 +16,7 @@ class QuillServerClient {
15
16
  }
16
17
 
17
18
  public async postQuill(route: string, payload: any): Promise<any> {
18
- const response = await fetch(`${this.baseUrl}/${route}`, {
19
- method: "POST",
20
- headers: {
21
- ...this.config.headers,
22
- "Content-Type": "application/json",
23
- },
24
- body: JSON.stringify(payload),
25
- });
26
- return response.json();
19
+ return axios.post(`${this.baseUrl}/${route}`, payload, this.config);
27
20
  }
28
21
  }
29
22
 
package/src/index.ts CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  QuillQueryParams,
7
7
  } from "./models/Quill";
8
8
  import { CachedPool } from "./db/CachedPools";
9
- import fetch from "node-fetch";
9
+ import axios from "axios";
10
10
  import "dotenv/config";
11
11
  import {
12
12
  getTableSchema,
@@ -61,14 +61,26 @@ export default class QuillClass {
61
61
  orgId,
62
62
  preQueryResults,
63
63
  });
64
- const results = await this.runQueries(
65
- response.queries,
66
- response.runQueryConfig
67
- );
68
64
  // if there is no metadata object in the response, create one
69
65
  if (!response.metadata) {
70
66
  response.metadata = {};
71
67
  }
68
+ const results = await this.runQueries(
69
+ response.queries,
70
+ response.metadata.runQueryConfig
71
+ );
72
+ // QUICK JANKY FIX TO UPDATE METADATA AFTER GETTING MAPPED ARRAYS
73
+ if (
74
+ results.mappedArray &&
75
+ response.metadata?.runQueryConfig?.arrayToMap
76
+ ) {
77
+ const arrayToMap = response.metadata.runQueryConfig.arrayToMap;
78
+ results.mappedArray.forEach((array: any, index: number) => {
79
+ response.metadata[arrayToMap.arrayName][index][arrayToMap.field] =
80
+ array;
81
+ });
82
+ delete results.mappedArray;
83
+ }
72
84
  // if there is a single query array in queryResults and the rows and field objects to metadata
73
85
  if (results?.queryResults.length === 1) {
74
86
  const queryResults = results.queryResults[0];
@@ -96,14 +108,12 @@ export default class QuillClass {
96
108
  let results: any;
97
109
  if (!queries) return { ...results, queryResults: [] };
98
110
  if (runQueryConfig?.arrayToMap) {
99
- return {
100
- ...results,
101
- mappedQueries: await mapQueries(
102
- queries,
103
- runQueryConfig.arrayToMap,
104
- this.targetPool
105
- ),
106
- };
111
+ const mappedArray = await mapQueries(
112
+ queries,
113
+ runQueryConfig.arrayToMap,
114
+ this.targetPool
115
+ );
116
+ return { ...results, queryResults: [], mappedArray };
107
117
  } else {
108
118
  const queryResults = await Promise.all(
109
119
  queries.map(async (query) => {
@@ -131,13 +141,12 @@ export default class QuillClass {
131
141
  path: string,
132
142
  payload: any
133
143
  ): Promise<QuillClientResponse> {
134
- const response = await fetch(`${this.baseUrl}/sdk/${path}`, {
135
- method: "POST",
136
- headers: this.config.headers,
137
- body: JSON.stringify(payload),
138
- });
139
- const data = await response.json();
140
- return data as QuillClientResponse;
144
+ const response = await axios.post(
145
+ `${this.baseUrl}/sdk/${path}`,
146
+ payload,
147
+ this.config
148
+ );
149
+ return response.data;
141
150
  }
142
151
 
143
152
  public async close() {
@@ -58,7 +58,7 @@ export interface QuillConfig {
58
58
  export interface AdditionalProcessing {
59
59
  getSchema?: boolean;
60
60
  removeFields?: string[];
61
- arrayToMap?: { arr: any; field: string };
61
+ arrayToMap?: { arrayName: string; field: string };
62
62
  }
63
63
 
64
64
  export interface QuillClientResponse {
@@ -43,16 +43,13 @@ export function removeFields(queryResults: any, fieldsToRemove: string[]): any {
43
43
 
44
44
  export async function mapQueries(
45
45
  queries: string[],
46
- arrayToMap: { arr: any; field: string },
46
+ arrayToMap: { arrayName: string; field: string },
47
47
  targetPool: CachedPool
48
- ) {
49
- let arrayToMapResult = [];
48
+ ): Promise<any[]> {
49
+ const mappedArray = [];
50
50
  for (let i = 0; i < queries.length; i++) {
51
51
  const queryResult = await targetPool.query(queries[i]);
52
- arrayToMapResult.push({
53
- ...arrayToMap.arr[i],
54
- [arrayToMap.field]: queryResult.rows,
55
- });
52
+ mappedArray.push(queryResult.rows);
56
53
  }
57
- return arrayToMapResult;
54
+ return mappedArray;
58
55
  }