@hemia/db-connector 0.0.5 → 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,
@@ -756,4 +781,4 @@ class DBConnector {
756
781
  }
757
782
  DBConnector.instance = null;
758
783
 
759
- export { CoreSqlTypes, DBConnector, DBError, DBNoSQLType, DBSQLType, MySQLConnectionError, NoSQLConnector, SqlConnector };
784
+ export { ClickHouseConnector, CoreSqlTypes, DBConnector, DBError, DBNoSQLType, DBOLAPType, DBSQLType, MySQLConnectionError, NoSQLConnector, OLAPConnector, SqlConnector };
@@ -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,
@@ -674,12 +699,12 @@ class SQLConnectionManager {
674
699
  }
675
700
  }
676
701
 
677
- var DBOLAPType;
702
+ exports.DBOLAPType = void 0;
678
703
  (function (DBOLAPType) {
679
704
  DBOLAPType["ClickHouse"] = "ClickHouse";
680
705
  DBOLAPType["Druid"] = "Druid";
681
706
  DBOLAPType["Snowflake"] = "Snowflake";
682
- })(DBOLAPType || (DBOLAPType = {}));
707
+ })(exports.DBOLAPType || (exports.DBOLAPType = {}));
683
708
 
684
709
  class DBConnector {
685
710
  constructor() {
@@ -708,7 +733,7 @@ class DBConnector {
708
733
  else if (Object.values(exports.DBSQLType).includes(type)) {
709
734
  this.connection = SQLConnectionManager.getInstance(type, credentials);
710
735
  }
711
- else if (Object.values(DBOLAPType).includes(type)) {
736
+ else if (Object.values(exports.DBOLAPType).includes(type)) {
712
737
  this.connection = new ClickHouseConnector(credentials);
713
738
  }
714
739
  else {
@@ -741,7 +766,7 @@ class DBConnector {
741
766
  else if (Object.values(exports.DBSQLType).includes(type)) {
742
767
  this.connection = SQLConnectionManager.getInstance(type, credentials);
743
768
  }
744
- else if (Object.values(DBOLAPType).includes(type)) {
769
+ else if (Object.values(exports.DBOLAPType).includes(type)) {
745
770
  this.connection = new ClickHouseConnector(credentials);
746
771
  }
747
772
  else {
@@ -784,10 +809,12 @@ Object.defineProperty(exports, "fn", {
784
809
  enumerable: true,
785
810
  get: function () { return sequelize.fn; }
786
811
  });
812
+ exports.ClickHouseConnector = ClickHouseConnector;
787
813
  exports.DBConnector = DBConnector;
788
814
  exports.DBError = DBError;
789
815
  exports.MySQLConnectionError = MySQLConnectionError;
790
816
  exports.NoSQLConnector = NoSQLConnector;
817
+ exports.OLAPConnector = OLAPConnector;
791
818
  exports.SqlConnector = SqlConnector;
792
819
  Object.keys(mongoose).forEach(function (k) {
793
820
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
@@ -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[]>;
@@ -4,6 +4,7 @@ export { Recorset } from './types/RecordSet';
4
4
  export { DBConnector } from "./DBConnector";
5
5
  export { DBSQLType } from "./types/DBSQLTypes";
6
6
  export { DBNoSQLType } from "./types/DBNoSQLTypes";
7
+ export { DBOLAPType } from "./types/OLAPTypes";
7
8
  export { NoSQLConnector } from "./abstract/NoSQLConnector";
8
9
  export { SqlConnector, Filter } from "./abstract/SQLConnector";
9
10
  export { NoSQLOptions } from "./types/NoSQLOptions";
@@ -11,3 +12,5 @@ export * from 'mongoose';
11
12
  export { Model as ModelSequelize, Sequelize, DataTypes, CreateOptions, UpdateOptions, DestroyOptions, FindOptions, Attributes, WhereOptions, QueryOptions, InferAttributes, CreationAttributes, InferCreationAttributes, QueryTypes, QueryOptionsWithType, Transaction, Op, fn } from "sequelize";
12
13
  export { DBError } from "./errors/DBError";
13
14
  export { MySQLConnectionError } from "./errors/MySQLConnectionError";
15
+ export { OLAPConnector, InsertOptions, ClickHouseInsertFormat, ClickHouseInsertValues } from './abstract/OLAPConnector';
16
+ export { ClickHouseConnector } from './Core/ClickHouseConnector';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/db-connector",
3
- "version": "0.0.5",
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",