@quillsql/node 0.6.0 → 0.6.1
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/dist/db/CachedConnection.d.ts +10 -7
- package/dist/db/CachedConnection.js +9 -20
- package/dist/db/DatabaseHelper.d.ts +3 -1
- package/dist/db/DatabaseHelper.js +21 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.ispec.js +3 -0
- package/dist/index.js +7 -2
- package/dist/index.uspec.js +3 -0
- package/dist/models/Cache.d.ts +1 -1
- package/package.json +1 -1
- package/src/db/CachedConnection.ts +24 -26
- package/src/db/DatabaseHelper.ts +29 -3
- package/src/index.ispec.ts +4 -0
- package/src/index.ts +10 -5
- package/src/index.uspec.ts +4 -0
- package/src/models/Cache.ts +1 -1
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Mappable, CacheCredentials } from "../models/Cache";
|
|
2
2
|
import { DatabaseConnection } from "./DatabaseHelper";
|
|
3
|
+
import { PostgresConnectionConfig } from "./Postgres";
|
|
4
|
+
import { SnowflakeConnectionConfig } from "./Snowflake";
|
|
5
|
+
import { BigQueryConfig } from "./BigQuery";
|
|
6
|
+
import { MysqlConnectionConfig } from "./Mysql";
|
|
3
7
|
export declare class CachedConnection {
|
|
4
8
|
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql";
|
|
5
|
-
pool: DatabaseConnection
|
|
9
|
+
readonly pool: DatabaseConnection;
|
|
6
10
|
orgId: any;
|
|
7
11
|
ttl: number;
|
|
8
|
-
cache:
|
|
9
|
-
private
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
constructor(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", config: any, cacheConfig?: Partial<CacheCredentials>);
|
|
12
|
+
cache: Mappable | null;
|
|
13
|
+
private _isClosed;
|
|
14
|
+
get isClosed(): boolean;
|
|
15
|
+
constructor(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", config: PostgresConnectionConfig | SnowflakeConnectionConfig | BigQueryConfig | MysqlConnectionConfig, cacheConfig?: Partial<CacheCredentials>);
|
|
13
16
|
query(text: string): Promise<any>;
|
|
14
17
|
/**
|
|
15
18
|
* Configures and returns a cache instance or null if none could be created.
|
|
@@ -16,21 +16,23 @@ const DatabaseHelper_1 = require("./DatabaseHelper");
|
|
|
16
16
|
/** The TTL for new cache entries (default: 1h) */
|
|
17
17
|
const DEFAULT_CACHE_TTL = 24 * 60 * 60;
|
|
18
18
|
class CachedConnection {
|
|
19
|
+
get isClosed() {
|
|
20
|
+
return this._isClosed;
|
|
21
|
+
}
|
|
19
22
|
constructor(databaseType, config, cacheConfig = {}) {
|
|
20
23
|
var _a;
|
|
21
|
-
this.
|
|
22
|
-
this.MAX_ACTIVE_QUERIES = 0;
|
|
24
|
+
this._isClosed = false;
|
|
23
25
|
this.databaseType = databaseType;
|
|
24
26
|
this.pool = (0, DatabaseHelper_1.connectToDatabase)(databaseType, config);
|
|
25
|
-
this.config = config;
|
|
26
27
|
this.ttl = (_a = cacheConfig === null || cacheConfig === void 0 ? void 0 : cacheConfig.ttl) !== null && _a !== void 0 ? _a : DEFAULT_CACHE_TTL;
|
|
27
28
|
this.cache = this.getCache(cacheConfig);
|
|
28
29
|
}
|
|
29
30
|
query(text) {
|
|
30
31
|
return __awaiter(this, void 0, void 0, function* () {
|
|
31
32
|
try {
|
|
32
|
-
this.
|
|
33
|
-
|
|
33
|
+
if (this.isClosed) {
|
|
34
|
+
throw new Error("Connection is closed");
|
|
35
|
+
}
|
|
34
36
|
if (!this.cache) {
|
|
35
37
|
return yield (0, DatabaseHelper_1.runQueryByDatabase)(this.databaseType, this.pool, text);
|
|
36
38
|
}
|
|
@@ -54,14 +56,6 @@ class CachedConnection {
|
|
|
54
56
|
throw new Error(err.message);
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
|
-
finally {
|
|
58
|
-
this.activeQueries--;
|
|
59
|
-
if (this.activeQueries <= this.MAX_ACTIVE_QUERIES) {
|
|
60
|
-
if (this.databaseType.toLowerCase() === "mysql") {
|
|
61
|
-
this.close();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
59
|
});
|
|
66
60
|
}
|
|
67
61
|
/**
|
|
@@ -77,17 +71,12 @@ class CachedConnection {
|
|
|
77
71
|
return null;
|
|
78
72
|
}
|
|
79
73
|
getPool() {
|
|
80
|
-
if (!this.pool) {
|
|
81
|
-
this.pool = (0, DatabaseHelper_1.connectToDatabase)(this.databaseType, this.config);
|
|
82
|
-
}
|
|
83
74
|
return this.pool;
|
|
84
75
|
}
|
|
85
76
|
close() {
|
|
86
77
|
return __awaiter(this, void 0, void 0, function* () {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
this.pool = null;
|
|
90
|
-
}
|
|
78
|
+
(0, DatabaseHelper_1.disconnectFromDatabase)(this.databaseType, this.pool);
|
|
79
|
+
this._isClosed = true;
|
|
91
80
|
});
|
|
92
81
|
}
|
|
93
82
|
}
|
|
@@ -16,9 +16,11 @@ export interface QuillQueryResults {
|
|
|
16
16
|
[fieldName: string]: any;
|
|
17
17
|
}[];
|
|
18
18
|
}
|
|
19
|
-
export declare function getDatabaseCredentials(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connectionString: string): PostgresConnectionConfig | SnowflakeConnectionConfig | BigQueryConfig | MysqlConnectionConfig
|
|
19
|
+
export declare function getDatabaseCredentials(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connectionString: string): PostgresConnectionConfig | SnowflakeConnectionConfig | BigQueryConfig | MysqlConnectionConfig;
|
|
20
20
|
export declare function connectToDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", config: PostgresConnectionConfig | SnowflakeConnectionConfig | BigQueryConfig | MysqlConnectionConfig): DatabaseConnection;
|
|
21
|
+
export declare function withConnection<T>(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connectionString: string, callback: (connection: DatabaseConnection) => T): Promise<T>;
|
|
21
22
|
export declare function runQueryByDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connection: DatabaseConnection, sql: string): Promise<QuillQueryResults> | undefined;
|
|
23
|
+
export declare function connectAndRunQuery(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connectionString: string, sql: string): Promise<QuillQueryResults | undefined>;
|
|
22
24
|
export declare function disconnectFromDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", database: DatabaseConnection): void | Promise<void>;
|
|
23
25
|
export declare function getSchemasByDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connection: DatabaseConnection): Promise<string[] | undefined>;
|
|
24
26
|
export declare function getTablesBySchemaByDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connection: DatabaseConnection, schemaName: string | string[]): Promise<string[] | {
|
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.getColumnInfoBySchemaByDatabase = exports.getForiegnKeysByDatabase = exports.getColumnsByTableByDatabase = exports.getTablesBySchemaByDatabase = exports.getSchemasByDatabase = exports.disconnectFromDatabase = exports.runQueryByDatabase = exports.connectToDatabase = exports.getDatabaseCredentials = void 0;
|
|
12
|
+
exports.getColumnInfoBySchemaByDatabase = exports.getForiegnKeysByDatabase = exports.getColumnsByTableByDatabase = exports.getTablesBySchemaByDatabase = exports.getSchemasByDatabase = exports.disconnectFromDatabase = exports.connectAndRunQuery = exports.runQueryByDatabase = exports.withConnection = exports.connectToDatabase = exports.getDatabaseCredentials = void 0;
|
|
13
13
|
const Postgres_1 = require("./Postgres");
|
|
14
14
|
const Snowflake_1 = require("./Snowflake");
|
|
15
15
|
const BigQuery_1 = require("./BigQuery");
|
|
@@ -27,7 +27,7 @@ function getDatabaseCredentials(databaseType, connectionString) {
|
|
|
27
27
|
case "mysql":
|
|
28
28
|
return (0, Mysql_1.formatMysqlConfig)(connectionString);
|
|
29
29
|
default:
|
|
30
|
-
|
|
30
|
+
throw new Error("Invalid database type");
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
exports.getDatabaseCredentials = getDatabaseCredentials;
|
|
@@ -48,6 +48,19 @@ function connectToDatabase(databaseType, config) {
|
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
50
|
exports.connectToDatabase = connectToDatabase;
|
|
51
|
+
function withConnection(databaseType, connectionString, callback) {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
const config = getDatabaseCredentials(databaseType, connectionString);
|
|
54
|
+
const connection = connectToDatabase(databaseType, config);
|
|
55
|
+
try {
|
|
56
|
+
return yield callback(connection);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
yield disconnectFromDatabase(databaseType, connection);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
exports.withConnection = withConnection;
|
|
51
64
|
function runQueryByDatabase(databaseType, connection, sql) {
|
|
52
65
|
switch (databaseType.toLowerCase()) {
|
|
53
66
|
case "postgres":
|
|
@@ -65,6 +78,12 @@ function runQueryByDatabase(databaseType, connection, sql) {
|
|
|
65
78
|
}
|
|
66
79
|
}
|
|
67
80
|
exports.runQueryByDatabase = runQueryByDatabase;
|
|
81
|
+
function connectAndRunQuery(databaseType, connectionString, sql) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
return withConnection(databaseType, connectionString, (connection) => __awaiter(this, void 0, void 0, function* () { return yield runQueryByDatabase(databaseType, connection, sql); }));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
exports.connectAndRunQuery = connectAndRunQuery;
|
|
68
87
|
function disconnectFromDatabase(databaseType, database) {
|
|
69
88
|
switch (databaseType.toLowerCase()) {
|
|
70
89
|
case "postgres":
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { CacheCredentials } from "./models/Cache";
|
|
2
3
|
import { QuillQueryParams } from "./models/Quill";
|
|
3
|
-
import { CachedConnection } from "./db/CachedConnection";
|
|
4
4
|
import "dotenv/config";
|
|
5
5
|
/**
|
|
6
6
|
* Quill - Fullstack API Platform for Dashboards and Reporting.
|
|
@@ -20,8 +20,8 @@ export declare enum DatabaseType {
|
|
|
20
20
|
bigquery = "bigquery",
|
|
21
21
|
mysql = "mysql"
|
|
22
22
|
}
|
|
23
|
-
export declare class Quill {
|
|
24
|
-
targetConnection
|
|
23
|
+
export declare class Quill implements AsyncDisposable {
|
|
24
|
+
private targetConnection;
|
|
25
25
|
private baseUrl;
|
|
26
26
|
private config;
|
|
27
27
|
constructor(data: {
|
|
@@ -35,6 +35,7 @@ export declare class Quill {
|
|
|
35
35
|
query({ orgId, metadata, }: QuillQueryParams): Promise<QuillQueryResult>;
|
|
36
36
|
private runQueries;
|
|
37
37
|
private postQuill;
|
|
38
|
+
[Symbol.asyncDispose](): Promise<void>;
|
|
38
39
|
close(): Promise<void>;
|
|
39
40
|
}
|
|
40
41
|
export { QuillQueryParams as QuillRequest } from "./models/Quill";
|
package/dist/index.ispec.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -178,7 +178,6 @@ class Quill {
|
|
|
178
178
|
else if (runQueryConfig === null || runQueryConfig === void 0 ? void 0 : runQueryConfig.getTables) {
|
|
179
179
|
const queryResult = yield (0, DatabaseHelper_1.getTablesBySchemaByDatabase)(this.targetConnection.databaseType, this.targetConnection.getPool(), runQueryConfig.schemaNames || runQueryConfig.schema);
|
|
180
180
|
const schemaInfo = yield (0, DatabaseHelper_1.getColumnInfoBySchemaByDatabase)(this.targetConnection.databaseType, this.targetConnection.getPool(), runQueryConfig.schema, queryResult);
|
|
181
|
-
this.targetConnection.close();
|
|
182
181
|
return schemaInfo;
|
|
183
182
|
}
|
|
184
183
|
else {
|
|
@@ -221,6 +220,11 @@ class Quill {
|
|
|
221
220
|
return response.data;
|
|
222
221
|
});
|
|
223
222
|
}
|
|
223
|
+
[Symbol.asyncDispose]() {
|
|
224
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
225
|
+
yield this.close();
|
|
226
|
+
});
|
|
227
|
+
}
|
|
224
228
|
close() {
|
|
225
229
|
return __awaiter(this, void 0, void 0, function* () {
|
|
226
230
|
yield this.targetConnection.close();
|
|
@@ -240,6 +244,8 @@ const requireQuill = ({ privateKey, databaseConnectionString, databaseConfig, ca
|
|
|
240
244
|
};
|
|
241
245
|
module.exports = requireQuill;
|
|
242
246
|
module.exports.default = requireQuill;
|
|
247
|
+
module.exports.withConnection = DatabaseHelper_1.withConnection;
|
|
248
|
+
module.exports.connectAndRunQuery = DatabaseHelper_1.connectAndRunQuery;
|
|
243
249
|
module.exports.Quill = Quill;
|
|
244
250
|
module.exports.getTablesBySchemaByDatabase = DatabaseHelper_1.getTablesBySchemaByDatabase;
|
|
245
251
|
module.exports.getDatabaseCredentials = DatabaseHelper_1.getDatabaseCredentials;
|
|
@@ -248,6 +254,5 @@ module.exports.getForiegnKeysByDatabase = DatabaseHelper_1.getForiegnKeysByDatab
|
|
|
248
254
|
module.exports.getSchemasByDatabase = DatabaseHelper_1.getSchemasByDatabase;
|
|
249
255
|
module.exports.getColumnInfoBySchemaByDatabase =
|
|
250
256
|
DatabaseHelper_1.getColumnInfoBySchemaByDatabase;
|
|
251
|
-
module.exports.connectToDatabase = DatabaseHelper_1.connectToDatabase;
|
|
252
257
|
module.exports.runQueryByDatabase = DatabaseHelper_1.runQueryByDatabase;
|
|
253
258
|
module.exports.DatabaseType = DatabaseType;
|
package/dist/index.uspec.js
CHANGED
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
// );
|
|
15
15
|
// quill.targetConnection.query = jest.fn().mockResolvedValue([]);
|
|
16
16
|
// });
|
|
17
|
+
// afterEach(async () => {
|
|
18
|
+
// await quill.close();
|
|
19
|
+
// });
|
|
17
20
|
// describe("query", () => {
|
|
18
21
|
// it("return nothing when suppling no queries", () => {
|
|
19
22
|
// const metadata = {
|
package/dist/models/Cache.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Mappable, CacheCredentials } from "../models/Cache";
|
|
2
2
|
import { QuillConfig } from "../models/Quill";
|
|
3
3
|
import { createClient } from "redis";
|
|
4
4
|
import { isSuperset, PgError } from "../utils/Error";
|
|
@@ -8,36 +8,46 @@ import {
|
|
|
8
8
|
disconnectFromDatabase,
|
|
9
9
|
runQueryByDatabase,
|
|
10
10
|
} from "./DatabaseHelper";
|
|
11
|
+
import { PostgresConnectionConfig } from "./Postgres";
|
|
12
|
+
import { SnowflakeConnectionConfig } from "./Snowflake";
|
|
13
|
+
import { BigQueryConfig } from "./BigQuery";
|
|
14
|
+
import { MysqlConnectionConfig } from "./Mysql";
|
|
11
15
|
|
|
12
16
|
/** The TTL for new cache entries (default: 1h) */
|
|
13
17
|
const DEFAULT_CACHE_TTL = 24 * 60 * 60;
|
|
14
18
|
|
|
15
19
|
export class CachedConnection {
|
|
16
20
|
public databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql";
|
|
17
|
-
public pool: DatabaseConnection
|
|
21
|
+
public readonly pool: DatabaseConnection;
|
|
18
22
|
public orgId: any;
|
|
19
23
|
public ttl: number;
|
|
20
|
-
public cache:
|
|
21
|
-
|
|
22
|
-
private
|
|
23
|
-
|
|
24
|
+
public cache: Mappable | null;
|
|
25
|
+
|
|
26
|
+
private _isClosed: boolean = false;
|
|
27
|
+
public get isClosed(): boolean {
|
|
28
|
+
return this._isClosed;
|
|
29
|
+
}
|
|
24
30
|
|
|
25
31
|
constructor(
|
|
26
32
|
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
27
|
-
config:
|
|
33
|
+
config:
|
|
34
|
+
| PostgresConnectionConfig
|
|
35
|
+
| SnowflakeConnectionConfig
|
|
36
|
+
| BigQueryConfig
|
|
37
|
+
| MysqlConnectionConfig,
|
|
28
38
|
cacheConfig: Partial<CacheCredentials> = {},
|
|
29
39
|
) {
|
|
30
40
|
this.databaseType = databaseType;
|
|
31
41
|
this.pool = connectToDatabase(databaseType, config);
|
|
32
|
-
this.config = config;
|
|
33
42
|
this.ttl = cacheConfig?.ttl ?? DEFAULT_CACHE_TTL;
|
|
34
43
|
this.cache = this.getCache(cacheConfig);
|
|
35
44
|
}
|
|
36
45
|
|
|
37
46
|
public async query(text: string): Promise<any> {
|
|
38
47
|
try {
|
|
39
|
-
this.
|
|
40
|
-
|
|
48
|
+
if (this.isClosed) {
|
|
49
|
+
throw new Error("Connection is closed");
|
|
50
|
+
}
|
|
41
51
|
if (!this.cache) {
|
|
42
52
|
return await runQueryByDatabase(this.databaseType, this.pool, text);
|
|
43
53
|
}
|
|
@@ -66,13 +76,6 @@ export class CachedConnection {
|
|
|
66
76
|
} else if (err instanceof Error) {
|
|
67
77
|
throw new Error(err.message);
|
|
68
78
|
}
|
|
69
|
-
} finally {
|
|
70
|
-
this.activeQueries--;
|
|
71
|
-
if (this.activeQueries <= this.MAX_ACTIVE_QUERIES) {
|
|
72
|
-
if (this.databaseType.toLowerCase() === "mysql") {
|
|
73
|
-
this.close();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
79
|
}
|
|
77
80
|
}
|
|
78
81
|
|
|
@@ -85,27 +88,22 @@ export class CachedConnection {
|
|
|
85
88
|
host,
|
|
86
89
|
port,
|
|
87
90
|
cacheType,
|
|
88
|
-
}: QuillConfig["cache"]):
|
|
91
|
+
}: QuillConfig["cache"]): Mappable | null {
|
|
89
92
|
if (cacheType === "redis" || cacheType === "rediss") {
|
|
90
93
|
const redisURL = `${cacheType}://${username}:${password}@${host}:${port}`;
|
|
91
94
|
const client = createClient({ url: redisURL });
|
|
92
95
|
client.connect();
|
|
93
|
-
return client as
|
|
96
|
+
return client as Mappable;
|
|
94
97
|
}
|
|
95
98
|
return null;
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
public getPool() {
|
|
99
|
-
if (!this.pool) {
|
|
100
|
-
this.pool = connectToDatabase(this.databaseType, this.config);
|
|
101
|
-
}
|
|
102
102
|
return this.pool;
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
async close() {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
this.pool = null;
|
|
109
|
-
}
|
|
106
|
+
disconnectFromDatabase(this.databaseType, this.pool);
|
|
107
|
+
this._isClosed = true;
|
|
110
108
|
}
|
|
111
109
|
}
|
package/src/db/DatabaseHelper.ts
CHANGED
|
@@ -69,8 +69,7 @@ export function getDatabaseCredentials(
|
|
|
69
69
|
| PostgresConnectionConfig
|
|
70
70
|
| SnowflakeConnectionConfig
|
|
71
71
|
| BigQueryConfig
|
|
72
|
-
| MysqlConnectionConfig
|
|
73
|
-
| undefined {
|
|
72
|
+
| MysqlConnectionConfig {
|
|
74
73
|
switch (databaseType.toLowerCase()) {
|
|
75
74
|
case "postgres":
|
|
76
75
|
return formatPostgresConfig(connectionString);
|
|
@@ -83,7 +82,7 @@ export function getDatabaseCredentials(
|
|
|
83
82
|
case "mysql":
|
|
84
83
|
return formatMysqlConfig(connectionString);
|
|
85
84
|
default:
|
|
86
|
-
|
|
85
|
+
throw new Error("Invalid database type");
|
|
87
86
|
}
|
|
88
87
|
}
|
|
89
88
|
|
|
@@ -111,6 +110,20 @@ export function connectToDatabase(
|
|
|
111
110
|
}
|
|
112
111
|
}
|
|
113
112
|
|
|
113
|
+
export async function withConnection<T>(
|
|
114
|
+
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
115
|
+
connectionString: string,
|
|
116
|
+
callback: (connection: DatabaseConnection) => T,
|
|
117
|
+
): Promise<T> {
|
|
118
|
+
const config = getDatabaseCredentials(databaseType, connectionString);
|
|
119
|
+
const connection = connectToDatabase(databaseType, config);
|
|
120
|
+
try {
|
|
121
|
+
return await callback(connection);
|
|
122
|
+
} finally {
|
|
123
|
+
await disconnectFromDatabase(databaseType, connection);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
114
127
|
export function runQueryByDatabase(
|
|
115
128
|
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
116
129
|
connection: DatabaseConnection,
|
|
@@ -132,6 +145,19 @@ export function runQueryByDatabase(
|
|
|
132
145
|
}
|
|
133
146
|
}
|
|
134
147
|
|
|
148
|
+
export async function connectAndRunQuery(
|
|
149
|
+
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
150
|
+
connectionString: string,
|
|
151
|
+
sql: string,
|
|
152
|
+
): Promise<QuillQueryResults | undefined> {
|
|
153
|
+
return withConnection(
|
|
154
|
+
databaseType,
|
|
155
|
+
connectionString,
|
|
156
|
+
async (connection) =>
|
|
157
|
+
await runQueryByDatabase(databaseType, connection, sql),
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
135
161
|
export function disconnectFromDatabase(
|
|
136
162
|
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
137
163
|
database: DatabaseConnection,
|
package/src/index.ispec.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ import axios from "axios";
|
|
|
9
9
|
import "dotenv/config";
|
|
10
10
|
import { mapQueries, removeFields } from "./utils/RunQueryProcesses";
|
|
11
11
|
import {
|
|
12
|
-
|
|
12
|
+
connectAndRunQuery,
|
|
13
13
|
getColumnInfoBySchemaByDatabase,
|
|
14
14
|
getColumnsByTableByDatabase,
|
|
15
15
|
getDatabaseCredentials,
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
getSchemasByDatabase,
|
|
18
18
|
getTablesBySchemaByDatabase,
|
|
19
19
|
runQueryByDatabase,
|
|
20
|
+
withConnection,
|
|
20
21
|
} from "./db/DatabaseHelper";
|
|
21
22
|
import { convertTypeToPostgres } from "./utils/schemaConversion";
|
|
22
23
|
|
|
@@ -43,9 +44,9 @@ export enum DatabaseType {
|
|
|
43
44
|
mysql = "mysql",
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
export class Quill {
|
|
47
|
+
export class Quill implements AsyncDisposable {
|
|
47
48
|
// Configure cached connection pools with the given config.
|
|
48
|
-
|
|
49
|
+
private targetConnection;
|
|
49
50
|
private baseUrl: string;
|
|
50
51
|
private config: {
|
|
51
52
|
headers: {
|
|
@@ -264,7 +265,6 @@ export class Quill {
|
|
|
264
265
|
runQueryConfig.schema!,
|
|
265
266
|
queryResult!,
|
|
266
267
|
);
|
|
267
|
-
this.targetConnection.close();
|
|
268
268
|
return schemaInfo;
|
|
269
269
|
} else {
|
|
270
270
|
if (runQueryConfig?.limitThousand) {
|
|
@@ -323,6 +323,10 @@ export class Quill {
|
|
|
323
323
|
return response.data;
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
+
public async [Symbol.asyncDispose](): Promise<void> {
|
|
327
|
+
await this.close();
|
|
328
|
+
}
|
|
329
|
+
|
|
326
330
|
public async close() {
|
|
327
331
|
await this.targetConnection.close();
|
|
328
332
|
}
|
|
@@ -355,6 +359,8 @@ const requireQuill = ({
|
|
|
355
359
|
|
|
356
360
|
module.exports = requireQuill;
|
|
357
361
|
module.exports.default = requireQuill;
|
|
362
|
+
module.exports.withConnection = withConnection;
|
|
363
|
+
module.exports.connectAndRunQuery = connectAndRunQuery;
|
|
358
364
|
module.exports.Quill = Quill;
|
|
359
365
|
module.exports.getTablesBySchemaByDatabase = getTablesBySchemaByDatabase;
|
|
360
366
|
module.exports.getDatabaseCredentials = getDatabaseCredentials;
|
|
@@ -363,7 +369,6 @@ module.exports.getForiegnKeysByDatabase = getForiegnKeysByDatabase;
|
|
|
363
369
|
module.exports.getSchemasByDatabase = getSchemasByDatabase;
|
|
364
370
|
module.exports.getColumnInfoBySchemaByDatabase =
|
|
365
371
|
getColumnInfoBySchemaByDatabase;
|
|
366
|
-
module.exports.connectToDatabase = connectToDatabase;
|
|
367
372
|
module.exports.runQueryByDatabase = runQueryByDatabase;
|
|
368
373
|
module.exports.DatabaseType = DatabaseType;
|
|
369
374
|
|
package/src/index.uspec.ts
CHANGED
|
@@ -17,6 +17,10 @@
|
|
|
17
17
|
// quill.targetConnection.query = jest.fn().mockResolvedValue([]);
|
|
18
18
|
// });
|
|
19
19
|
|
|
20
|
+
// afterEach(async () => {
|
|
21
|
+
// await quill.close();
|
|
22
|
+
// });
|
|
23
|
+
|
|
20
24
|
// describe("query", () => {
|
|
21
25
|
// it("return nothing when suppling no queries", () => {
|
|
22
26
|
// const metadata = {
|
package/src/models/Cache.ts
CHANGED