@hemia/db-connector 0.0.6 → 0.0.7

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
- this.client = createClient({
69
- url,
70
- username: this.credentials.user,
71
- password: this.credentials.password,
72
- database: this.credentials.database,
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 isConnected = yield this.client.ping();
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
- yield this.client.close();
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 resultSet = yield this.client.query({
113
+ const client = this.ensureClient();
114
+ const resultSet = yield client.query({
91
115
  query: sql,
92
116
  format: 'JSONEachRow',
93
117
  });
@@ -96,8 +120,9 @@ class ClickHouseConnector extends OLAPConnector {
96
120
  }
97
121
  insert(options) {
98
122
  return __awaiter(this, void 0, void 0, function* () {
123
+ const client = this.ensureClient();
99
124
  const { table, values, format = 'JSONEachRow' } = options;
100
- yield this.client.insert({
125
+ yield client.insert({
101
126
  table,
102
127
  values: values,
103
128
  format: format,
@@ -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
- this.client = client.createClient({
69
- url,
70
- username: this.credentials.user,
71
- password: this.credentials.password,
72
- database: this.credentials.database,
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 isConnected = yield this.client.ping();
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
- yield this.client.close();
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 resultSet = yield this.client.query({
113
+ const client = this.ensureClient();
114
+ const resultSet = yield client.query({
91
115
  query: sql,
92
116
  format: 'JSONEachRow',
93
117
  });
@@ -96,8 +120,9 @@ class ClickHouseConnector extends OLAPConnector {
96
120
  }
97
121
  insert(options) {
98
122
  return __awaiter(this, void 0, void 0, function* () {
123
+ const client = this.ensureClient();
99
124
  const { table, values, format = 'JSONEachRow' } = options;
100
- yield this.client.insert({
125
+ yield client.insert({
101
126
  table,
102
127
  values: values,
103
128
  format: format,
@@ -4,8 +4,11 @@ export declare class ClickHouseConnector extends OLAPConnector {
4
4
  private client;
5
5
  private credentials;
6
6
  constructor(credentials: CredentialsConnection);
7
+ private buildConnectionUrl;
8
+ private ensureClient;
7
9
  connect(): Promise<void>;
8
10
  disconnect(): Promise<void>;
11
+ isConnected(): Promise<boolean>;
9
12
  query<T = unknown>(sql: string): Promise<T[]>;
10
13
  insert<F extends ClickHouseInsertFormat = 'JSONEachRow'>(options: InsertOptions<F>): Promise<void>;
11
14
  find<T = unknown>(table: string, filters?: Record<string, any>): Promise<T[]>;
@@ -8,6 +8,7 @@ export interface InsertOptions<F extends ClickHouseInsertFormat = 'JSONEachRow'>
8
8
  export declare abstract class OLAPConnector {
9
9
  abstract connect(): Promise<void>;
10
10
  abstract disconnect(): Promise<void>;
11
+ abstract isConnected(): Promise<boolean>;
11
12
  abstract query<T = unknown>(sql: string): Promise<T[]>;
12
13
  abstract insert<F extends ClickHouseInsertFormat = 'JSONEachRow'>(options: InsertOptions<F>): Promise<void>;
13
14
  abstract find<T = unknown>(table: string, filters: Record<string, any>): Promise<T[]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/db-connector",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Hemia Database Conector",
5
5
  "main": "dist/hemia-db-connector.js",
6
6
  "module": "dist/hemia-db-connector.esm.js",