@hemia/db-connector 0.0.6 → 0.0.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.
|
@@ -52,7 +52,10 @@ class OLAPConnector {
|
|
|
52
52
|
class ClickHouseConnector extends OLAPConnector {
|
|
53
53
|
constructor(credentials) {
|
|
54
54
|
super();
|
|
55
|
+
this.client = null;
|
|
55
56
|
this.credentials = credentials;
|
|
57
|
+
}
|
|
58
|
+
buildConnectionUrl() {
|
|
56
59
|
let url;
|
|
57
60
|
if (this.credentials.host.match(/^https?:\/\/.+:\d+$/)) {
|
|
58
61
|
url = this.credentials.host;
|
|
@@ -65,16 +68,24 @@ class ClickHouseConnector extends OLAPConnector {
|
|
|
65
68
|
const port = this.credentials.port || 8123;
|
|
66
69
|
url = `http://${this.credentials.host}:${port}`;
|
|
67
70
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
return url;
|
|
72
|
+
}
|
|
73
|
+
ensureClient() {
|
|
74
|
+
if (!this.client) {
|
|
75
|
+
const url = this.buildConnectionUrl();
|
|
76
|
+
this.client = createClient({
|
|
77
|
+
url,
|
|
78
|
+
username: this.credentials.user,
|
|
79
|
+
password: this.credentials.password,
|
|
80
|
+
database: this.credentials.database,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return this.client;
|
|
74
84
|
}
|
|
75
85
|
connect() {
|
|
76
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
-
const
|
|
87
|
+
const client = this.ensureClient();
|
|
88
|
+
const isConnected = yield client.ping();
|
|
78
89
|
if (!isConnected) {
|
|
79
90
|
throw new Error('ClickHouse connection failed.');
|
|
80
91
|
}
|
|
@@ -82,12 +93,25 @@ class ClickHouseConnector extends OLAPConnector {
|
|
|
82
93
|
}
|
|
83
94
|
disconnect() {
|
|
84
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
|
|
96
|
+
if (this.client) {
|
|
97
|
+
yield this.client.close();
|
|
98
|
+
this.client = null;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
isConnected() {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
if (!this.client) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const isConnected = yield this.client.ping();
|
|
108
|
+
return isConnected ? true : false;
|
|
86
109
|
});
|
|
87
110
|
}
|
|
88
111
|
query(sql) {
|
|
89
112
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
const
|
|
113
|
+
const client = this.ensureClient();
|
|
114
|
+
const resultSet = yield client.query({
|
|
91
115
|
query: sql,
|
|
92
116
|
format: 'JSONEachRow',
|
|
93
117
|
});
|
|
@@ -96,12 +120,18 @@ class ClickHouseConnector extends OLAPConnector {
|
|
|
96
120
|
}
|
|
97
121
|
insert(options) {
|
|
98
122
|
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
table,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
123
|
+
try {
|
|
124
|
+
const client = this.ensureClient();
|
|
125
|
+
const { table, values, format = 'JSONEachRow' } = options;
|
|
126
|
+
return yield client.insert({
|
|
127
|
+
table,
|
|
128
|
+
values: values,
|
|
129
|
+
format: format,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
105
135
|
});
|
|
106
136
|
}
|
|
107
137
|
find(table_1) {
|
|
@@ -52,7 +52,10 @@ class OLAPConnector {
|
|
|
52
52
|
class ClickHouseConnector extends OLAPConnector {
|
|
53
53
|
constructor(credentials) {
|
|
54
54
|
super();
|
|
55
|
+
this.client = null;
|
|
55
56
|
this.credentials = credentials;
|
|
57
|
+
}
|
|
58
|
+
buildConnectionUrl() {
|
|
56
59
|
let url;
|
|
57
60
|
if (this.credentials.host.match(/^https?:\/\/.+:\d+$/)) {
|
|
58
61
|
url = this.credentials.host;
|
|
@@ -65,16 +68,24 @@ class ClickHouseConnector extends OLAPConnector {
|
|
|
65
68
|
const port = this.credentials.port || 8123;
|
|
66
69
|
url = `http://${this.credentials.host}:${port}`;
|
|
67
70
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
return url;
|
|
72
|
+
}
|
|
73
|
+
ensureClient() {
|
|
74
|
+
if (!this.client) {
|
|
75
|
+
const url = this.buildConnectionUrl();
|
|
76
|
+
this.client = client.createClient({
|
|
77
|
+
url,
|
|
78
|
+
username: this.credentials.user,
|
|
79
|
+
password: this.credentials.password,
|
|
80
|
+
database: this.credentials.database,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return this.client;
|
|
74
84
|
}
|
|
75
85
|
connect() {
|
|
76
86
|
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
-
const
|
|
87
|
+
const client = this.ensureClient();
|
|
88
|
+
const isConnected = yield client.ping();
|
|
78
89
|
if (!isConnected) {
|
|
79
90
|
throw new Error('ClickHouse connection failed.');
|
|
80
91
|
}
|
|
@@ -82,12 +93,25 @@ class ClickHouseConnector extends OLAPConnector {
|
|
|
82
93
|
}
|
|
83
94
|
disconnect() {
|
|
84
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
|
|
96
|
+
if (this.client) {
|
|
97
|
+
yield this.client.close();
|
|
98
|
+
this.client = null;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
isConnected() {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
if (!this.client) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const isConnected = yield this.client.ping();
|
|
108
|
+
return isConnected ? true : false;
|
|
86
109
|
});
|
|
87
110
|
}
|
|
88
111
|
query(sql) {
|
|
89
112
|
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
-
const
|
|
113
|
+
const client = this.ensureClient();
|
|
114
|
+
const resultSet = yield client.query({
|
|
91
115
|
query: sql,
|
|
92
116
|
format: 'JSONEachRow',
|
|
93
117
|
});
|
|
@@ -96,12 +120,18 @@ class ClickHouseConnector extends OLAPConnector {
|
|
|
96
120
|
}
|
|
97
121
|
insert(options) {
|
|
98
122
|
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
table,
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
123
|
+
try {
|
|
124
|
+
const client = this.ensureClient();
|
|
125
|
+
const { table, values, format = 'JSONEachRow' } = options;
|
|
126
|
+
return yield client.insert({
|
|
127
|
+
table,
|
|
128
|
+
values: values,
|
|
129
|
+
format: format,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
105
135
|
});
|
|
106
136
|
}
|
|
107
137
|
find(table_1) {
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
+
import { InsertResult } from '@clickhouse/client';
|
|
1
2
|
import { CredentialsConnection } from '../types/CredentialsConnection';
|
|
2
3
|
import { OLAPConnector, InsertOptions, ClickHouseInsertFormat } from '../abstract/OLAPConnector';
|
|
3
4
|
export declare class ClickHouseConnector extends OLAPConnector {
|
|
4
5
|
private client;
|
|
5
6
|
private credentials;
|
|
6
7
|
constructor(credentials: CredentialsConnection);
|
|
8
|
+
private buildConnectionUrl;
|
|
9
|
+
private ensureClient;
|
|
7
10
|
connect(): Promise<void>;
|
|
8
11
|
disconnect(): Promise<void>;
|
|
12
|
+
isConnected(): Promise<boolean>;
|
|
9
13
|
query<T = unknown>(sql: string): Promise<T[]>;
|
|
10
|
-
insert<F extends ClickHouseInsertFormat = 'JSONEachRow'>(options: InsertOptions<F>): Promise<
|
|
14
|
+
insert<F extends ClickHouseInsertFormat = 'JSONEachRow'>(options: InsertOptions<F>): Promise<InsertResult>;
|
|
11
15
|
find<T = unknown>(table: string, filters?: Record<string, any>): Promise<T[]>;
|
|
12
16
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { InsertResult } from "@clickhouse/client";
|
|
1
2
|
export type ClickHouseInsertFormat = 'JSONEachRow' | 'JSON' | 'JSONCompactEachRow' | 'CSV' | 'TabSeparated' | 'TabSeparatedWithNames' | 'Values';
|
|
2
3
|
export type ClickHouseInsertValues<T> = T extends 'JSONEachRow' | 'JSONCompactEachRow' | 'JSON' ? Record<string, any>[] : string;
|
|
3
4
|
export interface InsertOptions<F extends ClickHouseInsertFormat = 'JSONEachRow'> {
|
|
@@ -8,7 +9,8 @@ export interface InsertOptions<F extends ClickHouseInsertFormat = 'JSONEachRow'>
|
|
|
8
9
|
export declare abstract class OLAPConnector {
|
|
9
10
|
abstract connect(): Promise<void>;
|
|
10
11
|
abstract disconnect(): Promise<void>;
|
|
12
|
+
abstract isConnected(): Promise<boolean>;
|
|
11
13
|
abstract query<T = unknown>(sql: string): Promise<T[]>;
|
|
12
|
-
abstract insert<F extends ClickHouseInsertFormat = 'JSONEachRow'>(options: InsertOptions<F>): Promise<
|
|
14
|
+
abstract insert<F extends ClickHouseInsertFormat = 'JSONEachRow'>(options: InsertOptions<F>): Promise<InsertResult>;
|
|
13
15
|
abstract find<T = unknown>(table: string, filters: Record<string, any>): Promise<T[]>;
|
|
14
16
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hemia/db-connector",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Hemia Database Conector",
|
|
5
5
|
"main": "dist/hemia-db-connector.js",
|
|
6
6
|
"module": "dist/hemia-db-connector.esm.js",
|
|
@@ -74,4 +74,4 @@
|
|
|
74
74
|
"files": [
|
|
75
75
|
"dist"
|
|
76
76
|
]
|
|
77
|
-
}
|
|
77
|
+
}
|