clickhouse-ts
Advanced tools
Comparing version 1.4.1 to 1.4.2
@@ -8,2 +8,3 @@ import { ClickhouseNamespace } from './interface'; | ||
private formatInsertRows; | ||
private formatInsertValue; | ||
/** | ||
@@ -10,0 +11,0 @@ * |
@@ -35,5 +35,17 @@ "use strict"; | ||
const keysArr = Object.keys(rows[0]); | ||
const valuesArr = rows.map(row => keysArr.map(key => row[key])); | ||
return { keysArr, valuesArr }; | ||
const valuesSqlArr = rows.map(row => `(${keysArr.map(key => this.formatInsertValue(row[key])).join(',')})`); | ||
return { keysArr, valuesSqlFormat: valuesSqlArr.join(',') }; | ||
}; | ||
this.formatInsertValue = (value) => { | ||
if (Array.isArray(value)) { | ||
return `[${value.map(this.formatInsertValue).join(',')}]`; | ||
} | ||
if (typeof value === 'object') { | ||
const mapValues = Object.entries(value).map((v) => { | ||
return [sqlstring_1.default.escape(v[0]), this.formatInsertValue(v[1])]; | ||
}); | ||
return `map(${mapValues.join(',')})`; | ||
} | ||
return sqlstring_1.default.escape(value); | ||
}; | ||
__classPrivateFieldSet(this, _Clickhouse_isFirstInsert, true, "f"); | ||
@@ -50,37 +62,18 @@ __classPrivateFieldSet(this, _Clickhouse_onChunkCb, [], "f"); | ||
getCacheManager(provider) { | ||
const options = { | ||
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 }); | ||
this.insert(table, rows); | ||
} | ||
}; | ||
if (provider === 'redis') { | ||
return new RedisCacheManager_1.RedisCacheManager({ | ||
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 | ||
}); | ||
} | ||
}); | ||
return new RedisCacheManager_1.RedisCacheManager(options); | ||
} | ||
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 | ||
}); | ||
} | ||
}); | ||
return new NodeJSCacheManager_1.NodeJSCacheManager(options); | ||
} | ||
@@ -98,8 +91,9 @@ } | ||
} | ||
const { keysArr, valuesArr } = this.formatInsertRows(rows); | ||
const keys = `(${keysArr.join(',')})`; | ||
const values = valuesArr.map(values => `(${values.map(value => sqlstring_1.default.escape(value)).join(',')})`).join(','); | ||
const request = `INSERT INTO ${table} ${keys} VALUES ${values} ${options.responseFormat ? 'FORMAT ' + options.responseFormat : ''}`; | ||
Debug_1.debug.log('Clickhouse.insert', { request }); | ||
await __classPrivateFieldGet(this, _Clickhouse_httpClient, "f").request({ data: request }); | ||
const { keysArr, valuesSqlFormat } = this.formatInsertRows(rows); | ||
const keys = keysArr.join(','); | ||
await __classPrivateFieldGet(this, _Clickhouse_httpClient, "f").request({ | ||
params: { query: `INSERT INTO ${table} (${keys}) VALUES` }, | ||
data: valuesSqlFormat, | ||
requestOptions: options | ||
}); | ||
return { inserted: rows.length, data: rows }; | ||
@@ -106,0 +100,0 @@ } |
@@ -12,3 +12,3 @@ { | ||
], | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"license": "ISC", | ||
@@ -15,0 +15,0 @@ "description": "Clickhouse client on TypeScript using redis caching queries", |
@@ -38,40 +38,20 @@ import { ClickhouseHttpClient } from '../httpClient/ClickhouseHttpClient' | ||
private getCacheManager(provider: 'redis' | 'nodejs' | 'none') { | ||
const options = { | ||
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: string, rows: ClickhouseNamespace.InsertRows) => { | ||
debug.log('hooks.useInsert', { table, rows }) | ||
this.insert(table, rows) | ||
} | ||
} | ||
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) => { | ||
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 | ||
}) | ||
} | ||
}) | ||
return new RedisCacheManager(options) | ||
} | ||
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 | ||
}) | ||
} | ||
}) | ||
return new NodeJSCacheManager(options) | ||
} | ||
@@ -82,11 +62,26 @@ } | ||
const keysArr = Object.keys(rows[0]) | ||
const valuesArr = rows.map(row => keysArr.map(key => row[key])) | ||
return { keysArr, valuesArr } | ||
const valuesSqlArr = rows.map(row => `(${keysArr.map(key => this.formatInsertValue(row[key])).join(',')})`) | ||
return { keysArr, valuesSqlFormat: valuesSqlArr.join(',') } | ||
} | ||
private formatInsertValue = (value: any): string => { | ||
if (Array.isArray(value)) { | ||
return `[${value.map(this.formatInsertValue).join(',')}]` | ||
} | ||
if (typeof value === 'object') { | ||
const mapValues = Object.entries(value).map((v) => { | ||
return [sqlstring.escape(v[0]), this.formatInsertValue(v[1])] | ||
}) | ||
return `map(${mapValues.join(',')})` | ||
} | ||
return sqlstring.escape(value) | ||
} | ||
/** | ||
* | ||
* | ||
* @param query database | ||
* @param rows [{ value1: 'text' }, {value2: number}] | ||
* | ||
* | ||
*/ | ||
@@ -102,12 +97,10 @@ public async insert( | ||
const { keysArr, valuesArr } = this.formatInsertRows(rows) | ||
const keys = `(${keysArr.join(',')})` | ||
const values = valuesArr.map(values => `(${values.map(value => sqlstring.escape(value)).join(',')})`).join(',') | ||
const { keysArr, valuesSqlFormat } = this.formatInsertRows(rows) | ||
const keys = keysArr.join(',') | ||
await this.#httpClient.request({ | ||
params: { query: `INSERT INTO ${table} (${keys}) VALUES` }, | ||
data: valuesSqlFormat, | ||
requestOptions: options | ||
}) | ||
const request = `INSERT INTO ${table} ${keys} VALUES ${values} ${options.responseFormat ? 'FORMAT ' + options.responseFormat : ''}` | ||
debug.log('Clickhouse.insert', { request }) | ||
await this.#httpClient.request({ data: request }) | ||
return { inserted: rows.length, data: rows } | ||
@@ -117,5 +110,5 @@ } | ||
/** | ||
* | ||
* | ||
* @param query WITH now() as time SELECT time; | ||
* | ||
* | ||
*/ | ||
@@ -141,3 +134,3 @@ public async query( | ||
throw new Error('Redis client is required') | ||
} | ||
} | ||
this.#redisClient = client | ||
@@ -144,0 +137,0 @@ } |
87873
1698