@quillsql/node 0.6.0 → 0.6.2
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 +4 -1
- package/dist/db/DatabaseHelper.js +32 -2
- package/dist/index.d.ts +4 -3
- package/dist/index.ispec.js +3 -0
- package/dist/index.js +9 -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 +41 -3
- package/src/index.ispec.ts +4 -0
- package/src/index.ts +13 -4
- 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,12 @@ 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>;
|
|
22
|
+
export declare function withPool<T>(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", pool: DatabaseConnection, callback: (connection: DatabaseConnection) => T): Promise<T>;
|
|
21
23
|
export declare function runQueryByDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connection: DatabaseConnection, sql: string): Promise<QuillQueryResults> | undefined;
|
|
24
|
+
export declare function connectAndRunQuery(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connectionString: string, sql: string): Promise<QuillQueryResults | undefined>;
|
|
22
25
|
export declare function disconnectFromDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", database: DatabaseConnection): void | Promise<void>;
|
|
23
26
|
export declare function getSchemasByDatabase(databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql", connection: DatabaseConnection): Promise<string[] | undefined>;
|
|
24
27
|
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.withPool = 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,30 @@ 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;
|
|
64
|
+
function withPool(databaseType, pool, callback) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
try {
|
|
67
|
+
return yield callback(pool);
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
yield disconnectFromDatabase(databaseType, pool);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
exports.withPool = withPool;
|
|
51
75
|
function runQueryByDatabase(databaseType, connection, sql) {
|
|
52
76
|
switch (databaseType.toLowerCase()) {
|
|
53
77
|
case "postgres":
|
|
@@ -65,6 +89,12 @@ function runQueryByDatabase(databaseType, connection, sql) {
|
|
|
65
89
|
}
|
|
66
90
|
}
|
|
67
91
|
exports.runQueryByDatabase = runQueryByDatabase;
|
|
92
|
+
function connectAndRunQuery(databaseType, connectionString, sql) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
return withConnection(databaseType, connectionString, (connection) => __awaiter(this, void 0, void 0, function* () { return yield runQueryByDatabase(databaseType, connection, sql); }));
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
exports.connectAndRunQuery = connectAndRunQuery;
|
|
68
98
|
function disconnectFromDatabase(databaseType, database) {
|
|
69
99
|
switch (databaseType.toLowerCase()) {
|
|
70
100
|
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,10 @@ 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.withPool = DatabaseHelper_1.withPool;
|
|
249
|
+
module.exports.connectToDatabase = DatabaseHelper_1.connectToDatabase;
|
|
250
|
+
module.exports.connectAndRunQuery = DatabaseHelper_1.connectAndRunQuery;
|
|
243
251
|
module.exports.Quill = Quill;
|
|
244
252
|
module.exports.getTablesBySchemaByDatabase = DatabaseHelper_1.getTablesBySchemaByDatabase;
|
|
245
253
|
module.exports.getDatabaseCredentials = DatabaseHelper_1.getDatabaseCredentials;
|
|
@@ -248,6 +256,5 @@ module.exports.getForiegnKeysByDatabase = DatabaseHelper_1.getForiegnKeysByDatab
|
|
|
248
256
|
module.exports.getSchemasByDatabase = DatabaseHelper_1.getSchemasByDatabase;
|
|
249
257
|
module.exports.getColumnInfoBySchemaByDatabase =
|
|
250
258
|
DatabaseHelper_1.getColumnInfoBySchemaByDatabase;
|
|
251
|
-
module.exports.connectToDatabase = DatabaseHelper_1.connectToDatabase;
|
|
252
259
|
module.exports.runQueryByDatabase = DatabaseHelper_1.runQueryByDatabase;
|
|
253
260
|
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,32 @@ 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
|
+
|
|
127
|
+
export async function withPool<T>(
|
|
128
|
+
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
129
|
+
pool: DatabaseConnection,
|
|
130
|
+
callback: (connection: DatabaseConnection) => T,
|
|
131
|
+
): Promise<T> {
|
|
132
|
+
try {
|
|
133
|
+
return await callback(pool);
|
|
134
|
+
} finally {
|
|
135
|
+
await disconnectFromDatabase(databaseType, pool);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
114
139
|
export function runQueryByDatabase(
|
|
115
140
|
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
116
141
|
connection: DatabaseConnection,
|
|
@@ -132,6 +157,19 @@ export function runQueryByDatabase(
|
|
|
132
157
|
}
|
|
133
158
|
}
|
|
134
159
|
|
|
160
|
+
export async function connectAndRunQuery(
|
|
161
|
+
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
162
|
+
connectionString: string,
|
|
163
|
+
sql: string,
|
|
164
|
+
): Promise<QuillQueryResults | undefined> {
|
|
165
|
+
return withConnection(
|
|
166
|
+
databaseType,
|
|
167
|
+
connectionString,
|
|
168
|
+
async (connection) =>
|
|
169
|
+
await runQueryByDatabase(databaseType, connection, sql),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
135
173
|
export function disconnectFromDatabase(
|
|
136
174
|
databaseType: "postgresql" | "snowflake" | "bigquery" | "mysql",
|
|
137
175
|
database: DatabaseConnection,
|
package/src/index.ispec.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ import "dotenv/config";
|
|
|
10
10
|
import { mapQueries, removeFields } from "./utils/RunQueryProcesses";
|
|
11
11
|
import {
|
|
12
12
|
connectToDatabase,
|
|
13
|
+
connectAndRunQuery,
|
|
13
14
|
getColumnInfoBySchemaByDatabase,
|
|
14
15
|
getColumnsByTableByDatabase,
|
|
15
16
|
getDatabaseCredentials,
|
|
@@ -17,6 +18,8 @@ import {
|
|
|
17
18
|
getSchemasByDatabase,
|
|
18
19
|
getTablesBySchemaByDatabase,
|
|
19
20
|
runQueryByDatabase,
|
|
21
|
+
withConnection,
|
|
22
|
+
withPool,
|
|
20
23
|
} from "./db/DatabaseHelper";
|
|
21
24
|
import { convertTypeToPostgres } from "./utils/schemaConversion";
|
|
22
25
|
|
|
@@ -43,9 +46,9 @@ export enum DatabaseType {
|
|
|
43
46
|
mysql = "mysql",
|
|
44
47
|
}
|
|
45
48
|
|
|
46
|
-
export class Quill {
|
|
49
|
+
export class Quill implements AsyncDisposable {
|
|
47
50
|
// Configure cached connection pools with the given config.
|
|
48
|
-
|
|
51
|
+
private targetConnection;
|
|
49
52
|
private baseUrl: string;
|
|
50
53
|
private config: {
|
|
51
54
|
headers: {
|
|
@@ -264,7 +267,6 @@ export class Quill {
|
|
|
264
267
|
runQueryConfig.schema!,
|
|
265
268
|
queryResult!,
|
|
266
269
|
);
|
|
267
|
-
this.targetConnection.close();
|
|
268
270
|
return schemaInfo;
|
|
269
271
|
} else {
|
|
270
272
|
if (runQueryConfig?.limitThousand) {
|
|
@@ -323,6 +325,10 @@ export class Quill {
|
|
|
323
325
|
return response.data;
|
|
324
326
|
}
|
|
325
327
|
|
|
328
|
+
public async [Symbol.asyncDispose](): Promise<void> {
|
|
329
|
+
await this.close();
|
|
330
|
+
}
|
|
331
|
+
|
|
326
332
|
public async close() {
|
|
327
333
|
await this.targetConnection.close();
|
|
328
334
|
}
|
|
@@ -355,6 +361,10 @@ const requireQuill = ({
|
|
|
355
361
|
|
|
356
362
|
module.exports = requireQuill;
|
|
357
363
|
module.exports.default = requireQuill;
|
|
364
|
+
module.exports.withConnection = withConnection;
|
|
365
|
+
module.exports.withPool = withPool;
|
|
366
|
+
module.exports.connectToDatabase = connectToDatabase;
|
|
367
|
+
module.exports.connectAndRunQuery = connectAndRunQuery;
|
|
358
368
|
module.exports.Quill = Quill;
|
|
359
369
|
module.exports.getTablesBySchemaByDatabase = getTablesBySchemaByDatabase;
|
|
360
370
|
module.exports.getDatabaseCredentials = getDatabaseCredentials;
|
|
@@ -363,7 +373,6 @@ module.exports.getForiegnKeysByDatabase = getForiegnKeysByDatabase;
|
|
|
363
373
|
module.exports.getSchemasByDatabase = getSchemasByDatabase;
|
|
364
374
|
module.exports.getColumnInfoBySchemaByDatabase =
|
|
365
375
|
getColumnInfoBySchemaByDatabase;
|
|
366
|
-
module.exports.connectToDatabase = connectToDatabase;
|
|
367
376
|
module.exports.runQueryByDatabase = runQueryByDatabase;
|
|
368
377
|
module.exports.DatabaseType = DatabaseType;
|
|
369
378
|
|
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