clickhouse-ts
Advanced tools
Comparing version 1.3.19 to 1.4.0
@@ -6,2 +6,3 @@ import { ClickhouseNamespace } from './interface'; | ||
constructor(context: ClickhouseNamespace.Constructor, options: ClickhouseNamespace.Options); | ||
private getCacheManager; | ||
/** | ||
@@ -28,3 +29,3 @@ * | ||
}>; | ||
useCaching(type: 'redis', client: Redis): void; | ||
useCaching(type: 'redis' | 'nodejs', client?: Redis): void; | ||
onChunk(onChunkCb: (chunkId: string, table: string, rows: ClickhouseNamespace.InsertRows) => void): void; | ||
@@ -31,0 +32,0 @@ cache(table: string, rows: ClickhouseNamespace.InsertRows): Promise<{ |
@@ -23,2 +23,3 @@ "use strict"; | ||
const Debug_1 = require("../debug/Debug"); | ||
const NodeJSCacheManager_1 = require("../caching/NodeJSCacheManager"); | ||
class Clickhouse { | ||
@@ -37,4 +38,11 @@ constructor(context, options) { | ||
__classPrivateFieldSet(this, _Clickhouse_httpClient, new ClickhouseHttpClient_1.ClickhouseHttpClient({ context, options: options.clickhouseOptions }), "f"); | ||
__classPrivateFieldSet(this, _Clickhouse_cacheManager, __classPrivateFieldGet(this, _Clickhouse_options, "f").cache ? | ||
new RedisCacheManager_1.RedisCacheManager({ | ||
__classPrivateFieldSet(this, _Clickhouse_cacheManager, this.getCacheManager(__classPrivateFieldGet(this, _Clickhouse_options, "f").cache?.provider ?? 'none'), "f"); | ||
if (__classPrivateFieldGet(this, _Clickhouse_options, "f").debug) { | ||
Debug_1.debug.setDebugMode(__classPrivateFieldGet(this, _Clickhouse_options, "f").debug.mode); | ||
Debug_1.debug.excludeDebugProviders(__classPrivateFieldGet(this, _Clickhouse_options, "f").debug.exclude ?? []); | ||
} | ||
} | ||
getCacheManager(provider) { | ||
if (provider === 'redis') { | ||
return new RedisCacheManager_1.RedisCacheManager({ | ||
chunkTTLSeconds: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkTTLSeconds, | ||
@@ -54,8 +62,22 @@ chunkExpireTimeSeconds: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkExpireTimeSeconds, | ||
} | ||
}) : | ||
undefined, "f"); | ||
if (__classPrivateFieldGet(this, _Clickhouse_options, "f").debug) { | ||
Debug_1.debug.setDebugMode(__classPrivateFieldGet(this, _Clickhouse_options, "f").debug.mode); | ||
Debug_1.debug.excludeDebugProviders(__classPrivateFieldGet(this, _Clickhouse_options, "f").debug.exclude ?? []); | ||
}); | ||
} | ||
if (provider === 'nodejs') { | ||
return new NodeJSCacheManager_1.NodeJSCacheManager({ | ||
chunkTTLSeconds: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkTTLSeconds, | ||
chunkExpireTimeSeconds: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkExpireTimeSeconds, | ||
chunkSizeLimit: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkSizeLimit, | ||
chunkResolverIntervalSeconds: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkResolverIntervalSeconds, | ||
chunkResolveType: __classPrivateFieldGet(this, _Clickhouse_options, "f").cache.chunkResolveType, | ||
useInsert: async (table, rows) => { | ||
Debug_1.debug.log('hooks.useInsert', { table, rows }); | ||
const keys = Object.keys(rows[0]).join(','); | ||
const values = rows.map(row => `(${Object.values(row).map(v => sqlstring_1.default.escape(v)).join(',')})`).join(','); | ||
await __classPrivateFieldGet(this, _Clickhouse_httpClient, "f").request({ | ||
params: { query: `INSERT INTO ${table} (${keys}) VALUES` }, | ||
data: values | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
@@ -90,3 +112,8 @@ /** | ||
} | ||
__classPrivateFieldSet(this, _Clickhouse_redisClient, client, "f"); | ||
if (type === 'redis') { | ||
if (!client) { | ||
throw new Error('Redis client is required'); | ||
} | ||
__classPrivateFieldSet(this, _Clickhouse_redisClient, client, "f"); | ||
} | ||
__classPrivateFieldGet(this, _Clickhouse_cacheManager, "f").on('chunk', (chunkId, table, rows) => { | ||
@@ -107,3 +134,5 @@ Debug_1.debug.log('Clickhouse.useCaching', 'received event \'chunk\'', { chunkId, table, rowsCount: rows.length, firstRow: rows[0] }); | ||
__classPrivateFieldSet(this, _Clickhouse_isFirstInsert, false, "f"); | ||
await __classPrivateFieldGet(this, _Clickhouse_cacheManager, "f").useRedisInstance(__classPrivateFieldGet(this, _Clickhouse_redisClient, "f")); | ||
if (__classPrivateFieldGet(this, _Clickhouse_cacheManager, "f") instanceof RedisCacheManager_1.RedisCacheManager) { | ||
await __classPrivateFieldGet(this, _Clickhouse_cacheManager, "f").useRedisInstance(__classPrivateFieldGet(this, _Clickhouse_redisClient, "f")); | ||
} | ||
} | ||
@@ -110,0 +139,0 @@ const result = await __classPrivateFieldGet(this, _Clickhouse_cacheManager, "f") |
@@ -18,2 +18,3 @@ /// <reference types="node" /> | ||
cache?: { | ||
provider: 'redis' | 'nodejs'; | ||
chunkTTLSeconds: number; | ||
@@ -20,0 +21,0 @@ chunkExpireTimeSeconds: number; |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "1.3.19", | ||
"version": "1.4.0", | ||
"license": "ISC", | ||
@@ -15,0 +15,0 @@ "description": "Clickhouse client on TypeScript using redis caching queries", |
@@ -7,2 +7,3 @@ import { ClickhouseHttpClient } from '../httpClient/ClickhouseHttpClient' | ||
import { debug } from '../debug/Debug' | ||
import { NodeJSCacheManager } from '../caching/NodeJSCacheManager' | ||
@@ -15,3 +16,3 @@ | ||
readonly #options: ClickhouseNamespace.Options | ||
readonly #cacheManager?: RedisCacheManager | ||
readonly #cacheManager?: RedisCacheManager | NodeJSCacheManager | ||
#onChunkCb: ((chunkId: string, table: string, rows: ClickhouseNamespace.InsertRows) => void)[] | ||
@@ -30,9 +31,18 @@ #redisClient?: Redis | ||
this.#cacheManager = this.#options.cache ? | ||
new RedisCacheManager({ | ||
chunkTTLSeconds: this.#options.cache.chunkTTLSeconds, | ||
chunkExpireTimeSeconds: this.#options.cache.chunkExpireTimeSeconds, | ||
chunkSizeLimit: this.#options.cache.chunkSizeLimit, | ||
chunkResolverIntervalSeconds: this.#options.cache.chunkResolverIntervalSeconds, | ||
chunkResolveType: this.#options.cache.chunkResolveType, | ||
this.#cacheManager = this.getCacheManager(this.#options.cache?.provider ?? 'none') | ||
if (this.#options.debug) { | ||
debug.setDebugMode(this.#options.debug.mode) | ||
debug.excludeDebugProviders(this.#options.debug.exclude ?? []) | ||
} | ||
} | ||
private getCacheManager(provider: 'redis' | 'nodejs' | 'none') { | ||
if (provider === 'redis') { | ||
return new RedisCacheManager({ | ||
chunkTTLSeconds: this.#options.cache!.chunkTTLSeconds, | ||
chunkExpireTimeSeconds: this.#options.cache!.chunkExpireTimeSeconds, | ||
chunkSizeLimit: this.#options.cache!.chunkSizeLimit, | ||
chunkResolverIntervalSeconds: this.#options.cache!.chunkResolverIntervalSeconds, | ||
chunkResolveType: this.#options.cache!.chunkResolveType, | ||
useInsert: async (table, rows) => { | ||
@@ -46,11 +56,26 @@ debug.log('hooks.useInsert', { table, rows }) | ||
data: values | ||
}) | ||
}) | ||
} | ||
}) : | ||
undefined | ||
}) | ||
} | ||
if (this.#options.debug) { | ||
debug.setDebugMode(this.#options.debug.mode) | ||
debug.excludeDebugProviders(this.#options.debug.exclude ?? []) | ||
} | ||
if (provider === 'nodejs') { | ||
return new NodeJSCacheManager({ | ||
chunkTTLSeconds: this.#options.cache!.chunkTTLSeconds, | ||
chunkExpireTimeSeconds: this.#options.cache!.chunkExpireTimeSeconds, | ||
chunkSizeLimit: this.#options.cache!.chunkSizeLimit, | ||
chunkResolverIntervalSeconds: this.#options.cache!.chunkResolverIntervalSeconds, | ||
chunkResolveType: this.#options.cache!.chunkResolveType, | ||
useInsert: async (table, rows) => { | ||
debug.log('hooks.useInsert', { table, rows }) | ||
const keys = Object.keys(rows[0]).join(',') | ||
const values = rows.map(row => `(${Object.values(row).map(v => sqlstring.escape(v)).join(',')})`).join(',') | ||
await this.#httpClient.request({ | ||
params: { query: `INSERT INTO ${table} (${keys}) VALUES` }, | ||
data: values | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
@@ -97,3 +122,3 @@ | ||
public useCaching(type: 'redis', client: Redis) { | ||
public useCaching(type: 'redis' | 'nodejs', client?: Redis) { | ||
if (!this.#cacheManager) { | ||
@@ -103,3 +128,8 @@ throw new Error('Cache manager is not initialized!') | ||
this.#redisClient = client | ||
if (type === 'redis') { | ||
if (!client) { | ||
throw new Error('Redis client is required') | ||
} | ||
this.#redisClient = client | ||
} | ||
@@ -138,3 +168,5 @@ this.#cacheManager.on('chunk', ( | ||
this.#isFirstInsert = false | ||
await this.#cacheManager.useRedisInstance(this.#redisClient!) | ||
if (this.#cacheManager instanceof RedisCacheManager) { | ||
await this.#cacheManager.useRedisInstance(this.#redisClient!) | ||
} | ||
} | ||
@@ -149,2 +181,2 @@ | ||
} | ||
} | ||
} |
@@ -32,2 +32,3 @@ import { Redis } from 'ioredis' | ||
cache?: { | ||
provider: 'redis' | 'nodejs', | ||
chunkTTLSeconds: number, | ||
@@ -50,2 +51,2 @@ chunkExpireTimeSeconds: number, | ||
} & Record<string, any> | ||
} | ||
} |
88966
34
1687