@quillsql/node 0.3.6 → 0.3.8

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/src/index.ts CHANGED
@@ -5,7 +5,7 @@ import {
5
5
  QuillClientResponse,
6
6
  QuillQueryParams,
7
7
  } from "./models/Quill";
8
- import { CachedPool } from "./db/CachedPools";
8
+ import { CachedConnection } from "./db/CachedConnection";
9
9
  import axios from "axios";
10
10
  import "dotenv/config";
11
11
  import {
@@ -13,6 +13,18 @@ import {
13
13
  mapQueries,
14
14
  removeFields,
15
15
  } from "./utils/RunQueryProcesses";
16
+ import { PG_TYPES } from "./assets/pgtypes";
17
+ import {
18
+ DatabaseType,
19
+ connectToDatabase,
20
+ getColumnInfoBySchemaByDatabase,
21
+ getColumnsByTableByDatabase,
22
+ getDatabaseCredentials,
23
+ getForiegnKeysByDatabase,
24
+ getSchemasByDatabase,
25
+ getTablesBySchemaByDatabase,
26
+ runQueryByDatabase,
27
+ } from "./db/DatabaseHelper";
16
28
 
17
29
  const HOST =
18
30
  process.env.ENV === "development"
@@ -25,9 +37,7 @@ const HOST =
25
37
 
26
38
  export default class QuillClass {
27
39
  // Configure cached connection pools with the given config.
28
- private connectionString;
29
- private ssl = { rejectUnauthorized: false };
30
- public targetPool;
40
+ public targetConnection;
31
41
  private baseUrl: string;
32
42
  private config: {
33
43
  headers: {
@@ -38,28 +48,49 @@ export default class QuillClass {
38
48
  constructor(
39
49
  privateKey: string,
40
50
  databaseConnectionString: string,
41
- cache: Partial<CacheCredentials> = {}
51
+ cache: Partial<CacheCredentials> = {},
52
+ databaseType: DatabaseType,
53
+ metadataServerURL?: string
42
54
  ) {
43
- this.baseUrl = HOST;
55
+ this.baseUrl = metadataServerURL ? metadataServerURL : HOST;
44
56
  this.config = { headers: { Authorization: `Bearer ${privateKey}` } };
45
- this.connectionString = databaseConnectionString;
46
- this.ssl = { rejectUnauthorized: false };
47
- this.targetPool = new CachedPool(
48
- { connectionString: this.connectionString, ssl: this.ssl },
57
+ this.targetConnection = new CachedConnection(
58
+ databaseType,
59
+ getDatabaseCredentials(databaseType, databaseConnectionString),
49
60
  cache
50
61
  );
51
62
  }
52
63
 
53
64
  public async query({ orgId, metadata }: QuillQueryParams): Promise<any> {
54
- this.targetPool.orgId = orgId;
65
+ this.targetConnection.orgId = orgId;
55
66
 
56
67
  try {
57
68
  // Initial Query Request
58
- const preQueryResults = await this.runQueries(metadata.preQueries);
69
+ const limitedQueries = metadata.preQueries
70
+ ? metadata.preQueries?.map((query) => query + " limit 1")
71
+ : [];
72
+ const preQueryResults = metadata.preQueries
73
+ ? await this.runQueries(limitedQueries)
74
+ : [];
75
+ const columns = metadata.preQueries
76
+ ? preQueryResults.queryResults[0].fields.map((field: any) => {
77
+ return {
78
+ fieldType: PG_TYPES.find((type) => field.dataTypeID === type.oid)
79
+ ? PG_TYPES.find((type) => field.dataTypeID === type.oid)
80
+ ?.typname
81
+ : field.dataTypeID,
82
+ name: field.name,
83
+ displayName: field.name,
84
+ isVisible: true,
85
+ field: field.name,
86
+ };
87
+ })
88
+ : metadata.columns;
59
89
  const response = await this.postQuill(metadata.task, {
60
90
  ...metadata,
61
91
  orgId,
62
- preQueryResults,
92
+ columns,
93
+ viewQuery: metadata.preQueries ? metadata.preQueries[0] : undefined,
63
94
  });
64
95
  // if there is no metadata object in the response, create one
65
96
  if (!response.metadata) {
@@ -107,24 +138,25 @@ export default class QuillClass {
107
138
  ) {
108
139
  let results: any;
109
140
  if (!queries) return { ...results, queryResults: [] };
141
+ if (!queries) return { ...results, queryResults: [] };
110
142
  if (runQueryConfig?.arrayToMap) {
111
143
  const mappedArray = await mapQueries(
112
144
  queries,
113
145
  runQueryConfig.arrayToMap,
114
- this.targetPool
146
+ this.targetConnection
115
147
  );
116
148
  return { ...results, queryResults: [], mappedArray };
117
149
  } else {
118
150
  const queryResults = await Promise.all(
119
151
  queries.map(async (query) => {
120
- return await this.targetPool.query(query);
152
+ return await this.targetConnection.query(query);
121
153
  })
122
154
  );
123
155
  results = { ...results, queryResults };
124
156
  if (runQueryConfig?.getSchema) {
125
157
  results = {
126
158
  ...results,
127
- columns: await getTableSchema(queryResults[0], this.targetPool),
159
+ columns: await getTableSchema(queryResults[0], this.targetConnection),
128
160
  };
129
161
  }
130
162
  if (runQueryConfig?.removeFields) {
@@ -150,7 +182,7 @@ export default class QuillClass {
150
182
  }
151
183
 
152
184
  public async close() {
153
- await this.targetPool.close();
185
+ await this.targetConnection.close();
154
186
  }
155
187
  }
156
188
 
@@ -158,14 +190,33 @@ const Quill = ({
158
190
  privateKey,
159
191
  databaseConnectionString,
160
192
  cache,
193
+ databaseType,
194
+ metadataServerURL,
161
195
  }: {
162
196
  privateKey: string;
163
197
  databaseConnectionString: string;
164
198
  cache?: Partial<CacheCredentials>;
199
+ databaseType: DatabaseType;
200
+ metadataServerURL?: string;
165
201
  }) => {
166
- return new QuillClass(privateKey, databaseConnectionString, cache);
202
+ return new QuillClass(
203
+ privateKey,
204
+ databaseConnectionString,
205
+ cache,
206
+ databaseType,
207
+ metadataServerURL
208
+ );
167
209
  };
168
210
 
169
211
  module.exports = Quill;
170
212
  module.exports.Quill = Quill;
171
213
  module.exports.default = Quill;
214
+ module.exports.getTablesBySchemaByDatabase = getTablesBySchemaByDatabase;
215
+ module.exports.getDatabaseCredentials = getDatabaseCredentials;
216
+ module.exports.getColumnsByTableByDatabase = getColumnsByTableByDatabase;
217
+ module.exports.getForiegnKeysByDatabase = getForiegnKeysByDatabase;
218
+ module.exports.getSchemasByDatabase = getSchemasByDatabase;
219
+ module.exports.getColumnInfoBySchemaByDatabase =
220
+ getColumnInfoBySchemaByDatabase;
221
+ module.exports.connectToDatabase = connectToDatabase;
222
+ module.exports.runQueryByDatabase = runQueryByDatabase;
@@ -1,4 +1,5 @@
1
1
  import Quill from ".";
2
+ import { DatabaseType } from "./db/DatabaseHelper";
2
3
 
3
4
  jest.mock(".");
4
5
 
@@ -6,8 +7,14 @@ describe("Quill", () => {
6
7
  let quill: Quill;
7
8
 
8
9
  beforeEach(() => {
9
- quill = new Quill("dummy_private_key", "dummy_db_url");
10
- quill.targetPool.query = jest.fn().mockResolvedValue([]);
10
+ quill = new Quill(
11
+ "dummy_private_key",
12
+ "dummy_db_url",
13
+ {},
14
+ DatabaseType.postgres,
15
+ undefined
16
+ );
17
+ quill.targetConnection.query = jest.fn().mockResolvedValue([]);
11
18
  });
12
19
 
13
20
  describe("query", () => {
@@ -0,0 +1,29 @@
1
+ export interface Client {
2
+ name: string;
3
+ databaseConnectionString: string;
4
+ etlDatabaseConnectionString: string;
5
+ stagingDatabaseConnectionString: string;
6
+ customerTableTitleFieldName: string;
7
+ databaseType: string;
8
+ // foreign key
9
+ customerFieldName: string;
10
+ // native key, ex: uuid
11
+ customerTableFieldName: string;
12
+ // table with all customers, ex: company_company
13
+ customerTableName: string;
14
+ customerView: string;
15
+ // sql type. ex: INT
16
+ customerFieldType: string;
17
+ // does db use ssl
18
+ useSsl: boolean;
19
+ serverCa: string;
20
+ clientCert: string;
21
+ clientKey: string;
22
+ defaultQuery: string;
23
+ ignoreDarkMode: boolean;
24
+ domainName: string;
25
+ hideSqlEditor: boolean;
26
+ adminCustomerId: string;
27
+ stagingAdminCustomerId: string;
28
+ cacheCloudConfig: { type: { cacheQueries: boolean }; default: null };
29
+ }
@@ -1,4 +1,3 @@
1
- import { CachedPool } from "../db/CachedPools";
2
1
  import { CacheCredentials } from "./Cache";
3
2
  import { DatabaseCredentials } from "./Database";
4
3
  import { FieldFormat, FormattedColumn } from "./Formats";
@@ -34,11 +33,6 @@ export interface QuillQueryParams {
34
33
  environment?: string;
35
34
  }
36
35
 
37
- export interface QuillTaskHandlerParams extends QuillQueryParams {
38
- privateKey: string;
39
- targetPool: CachedPool;
40
- }
41
-
42
36
  export interface QuillConfig {
43
37
  privateKey: string;
44
38
  db: Partial<DatabaseCredentials>;
@@ -1,4 +1,4 @@
1
- import { CachedPool } from "../db/CachedPools";
1
+ import { CachedConnection } from "../db/CachedConnection";
2
2
 
3
3
  interface TableSchemaInfo {
4
4
  fieldType: string;
@@ -9,9 +9,9 @@ interface TableSchemaInfo {
9
9
 
10
10
  export async function getTableSchema(
11
11
  queryResults: any,
12
- targetPool: CachedPool
12
+ targetConnection: CachedConnection
13
13
  ) {
14
- const typesQuery = await targetPool.query(
14
+ const typesQuery = await targetConnection.query(
15
15
  "select typname, oid, typarray from pg_type order by oid;"
16
16
  );
17
17
  const schema: TableSchemaInfo[] = queryResults[0].fields.map(
@@ -44,11 +44,11 @@ export function removeFields(queryResults: any, fieldsToRemove: string[]): any {
44
44
  export async function mapQueries(
45
45
  queries: string[],
46
46
  arrayToMap: { arrayName: string; field: string },
47
- targetPool: CachedPool
47
+ targetConnection: CachedConnection
48
48
  ): Promise<any[]> {
49
49
  const mappedArray = [];
50
50
  for (let i = 0; i < queries.length; i++) {
51
- const queryResult = await targetPool.query(queries[i]);
51
+ const queryResult = await targetConnection.query(queries[i]);
52
52
  mappedArray.push(queryResult.rows);
53
53
  }
54
54
  return mappedArray;
@@ -0,0 +1,13 @@
1
+ export function capitalize(text: string): string {
2
+ return text.charAt(0).toUpperCase() + text.slice(1);
3
+ }
4
+
5
+ export function depluralize(text: string): string {
6
+ if (text.endsWith("ies")) {
7
+ return text.slice(0, -3) + "y";
8
+ }
9
+ if (text.endsWith("s")) {
10
+ return text.slice(0, -1);
11
+ }
12
+ return text;
13
+ }