@cleverbrush/knex-clickhouse 1.1.6 → 1.1.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.
- package/dist/index.d.ts +6 -1
- package/dist/index.js +48 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Knex from 'knex';
|
|
2
|
-
import ClickhouseDriver from '@clickhouse/client';
|
|
2
|
+
import ClickhouseDriver, { type ClickHouseSettings } from '@clickhouse/client';
|
|
3
3
|
export declare class ClickhouseKnexClient extends Knex.Client {
|
|
4
4
|
#private;
|
|
5
5
|
_driver(): typeof ClickhouseDriver;
|
|
@@ -22,3 +22,8 @@ export declare class ClickhouseKnexClient extends Knex.Client {
|
|
|
22
22
|
acquireRawConnection(): Promise<any>;
|
|
23
23
|
destroyRawConnection(connection: any): Promise<void>;
|
|
24
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @returns {import('knex').Knex<any, unknown[]>}
|
|
28
|
+
*/
|
|
29
|
+
export declare const getClickhouseConnection: (config?: Knex.Knex.Config<any>, clickHouseSettings?: ClickHouseSettings) => Knex.Knex<any, unknown[]>;
|
package/dist/index.js
CHANGED
|
@@ -199,7 +199,9 @@ export class ClickhouseKnexClient extends Knex.Client {
|
|
|
199
199
|
password: this.config.connection.password,
|
|
200
200
|
// @ts-ignore
|
|
201
201
|
database: this.config.connection.database,
|
|
202
|
-
compression: { request: true, response: true }
|
|
202
|
+
compression: { request: true, response: true },
|
|
203
|
+
// @ts-ignore
|
|
204
|
+
clickhouse_settings: this.config.clickHouseSettings
|
|
203
205
|
});
|
|
204
206
|
// @ts-ignore
|
|
205
207
|
connection.connected = true;
|
|
@@ -216,3 +218,48 @@ Object.assign(ClickhouseKnexClient.prototype, {
|
|
|
216
218
|
driverName: 'clikhouse',
|
|
217
219
|
canCancelQuery: true
|
|
218
220
|
});
|
|
221
|
+
const registerKnexClickhouseExtensions = (knex) => {
|
|
222
|
+
Knex.QueryBuilder.extend('insertToClickhouse', function (rows) {
|
|
223
|
+
const getTuple = (r, p = ['(', ')']) => p[0] +
|
|
224
|
+
r
|
|
225
|
+
.map((c) => {
|
|
226
|
+
if (c === null) {
|
|
227
|
+
return 'NULL';
|
|
228
|
+
}
|
|
229
|
+
if (typeof c === 'string') {
|
|
230
|
+
return knex.raw('?', [c]).toQuery();
|
|
231
|
+
}
|
|
232
|
+
if (Array.isArray(c)) {
|
|
233
|
+
return getTuple(c, ['[', ']']);
|
|
234
|
+
}
|
|
235
|
+
if (typeof c === 'object') {
|
|
236
|
+
const keys = Object.keys(c);
|
|
237
|
+
return `[${keys
|
|
238
|
+
.map((k) => `(${knex.raw('?', [k]).toQuery()}, ${knex
|
|
239
|
+
.raw('?', [c[k]])
|
|
240
|
+
.toQuery()})`)
|
|
241
|
+
.join(', ')}]`;
|
|
242
|
+
}
|
|
243
|
+
return c;
|
|
244
|
+
})
|
|
245
|
+
.join(', ') +
|
|
246
|
+
p[1];
|
|
247
|
+
const values = rows.map((r) => getTuple(r));
|
|
248
|
+
// @ts-ignore
|
|
249
|
+
return knex.raw(`INSERT INTO ${this._single.table} VALUES ${values}`);
|
|
250
|
+
});
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
*
|
|
254
|
+
* @returns {import('knex').Knex<any, unknown[]>}
|
|
255
|
+
*/
|
|
256
|
+
export const getClickhouseConnection = (config = {}, clickHouseSettings) => {
|
|
257
|
+
const connection = Knex({
|
|
258
|
+
...config,
|
|
259
|
+
client: ClickhouseKnexClient,
|
|
260
|
+
// @ts-ignore
|
|
261
|
+
clickHouseSettings
|
|
262
|
+
});
|
|
263
|
+
registerKnexClickhouseExtensions(connection);
|
|
264
|
+
return connection;
|
|
265
|
+
};
|
package/package.json
CHANGED