From 913794952ffb6d6d9ae737dd0cdb5212a5280dbb Mon Sep 17 00:00:00 2001 From: Rohit Kadhe <113367036+rohit-kadhe@users.noreply.github.com> Date: Sat, 23 Mar 2024 11:49:48 -0600 Subject: [PATCH] add support (#3) --- core/clickhouse_schema.ts | 22 ++++++++++----------- tests/unit/clickhouse_schema.test.ts | 29 ++++++++++++++-------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/core/clickhouse_schema.ts b/core/clickhouse_schema.ts index 9ea1c97..0167cd7 100644 --- a/core/clickhouse_schema.ts +++ b/core/clickhouse_schema.ts @@ -12,18 +12,18 @@ export interface SchemaValue { type: ChDataType, default?: unknown } // Note tha * @param engine is the engine to use for the table, default is MergeTree() * @param additional_options is an string array of options that are appended to the end of the create table query */ -export interface ChSchemaOptions { +export interface ChSchemaOptions { database?: string table_name: string on_cluster?: string - primary_key?: string - order_by?: string + primary_key?: keyof T + order_by?: keyof T engine?: string additional_options?: string[] } -interface IClickhouseSchema { - GetOptions: () => ChSchemaOptions +interface IClickhouseSchema { + GetOptions: () => ChSchemaOptions GetCreateTableQuery: () => string GetCreateTableQueryAsList: () => string[] } @@ -31,16 +31,16 @@ interface IClickhouseSchema { export type ChSchemaDefinition = Record /* This class is used to represent a clickhouse table schema */ -export class ClickhouseSchema implements IClickhouseSchema { +export class ClickhouseSchema implements IClickhouseSchema { readonly schema: SchemaDefinition - private readonly options: ChSchemaOptions + private readonly options: ChSchemaOptions - constructor (schema: SchemaDefinition, options: ChSchemaOptions) { + constructor (schema: SchemaDefinition, options: ChSchemaOptions) { this.schema = schema this.options = options } - GetOptions (): ChSchemaOptions { + GetOptions (): ChSchemaOptions { return this.options } @@ -68,8 +68,8 @@ export class ClickhouseSchema imple `CREATE TABLE IF NOT EXISTS ${this.options.database !== undefined ? `${this.options.database}.` : ''}${this.options.table_name}${this.options.on_cluster !== undefined ? ` ON CLUSTER ${this.options.on_cluster}` : ''}`, `(\n${columns}\n)`, `ENGINE = ${this.options.engine ?? 'MergeTree()'}`, - this.options.order_by !== undefined ? `ORDER BY ${this.options.order_by}` : '', - this.options.primary_key !== undefined ? `PRIMARY KEY ${this.options.primary_key}` : '', + this.options.order_by !== undefined ? `ORDER BY ${String(this.options.order_by)}` : '', + this.options.primary_key !== undefined ? `PRIMARY KEY ${String(this.options.primary_key)}` : '', additionalOptions ].filter(part => part.trim().length > 0).join('\n') diff --git a/tests/unit/clickhouse_schema.test.ts b/tests/unit/clickhouse_schema.test.ts index e049d0c..143ae24 100644 --- a/tests/unit/clickhouse_schema.test.ts +++ b/tests/unit/clickhouse_schema.test.ts @@ -9,7 +9,7 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString }, age: { type: ClickhouseTypes.CHUInt8 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { primary_key: 'id', table_name: 'users_table' } @@ -26,7 +26,7 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString }, age: { type: ClickhouseTypes.CHUInt8 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { table_name: 'users_table' } @@ -42,7 +42,7 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString }, age: { type: ClickhouseTypes.CHUInt8 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { primary_key: 'id', table_name: 'users_table' } @@ -60,7 +60,7 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString, default: 'john@gmail.com' }, age: { type: ClickhouseTypes.CHUInt8 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { primary_key: 'id', table_name: 'users_table' } @@ -78,7 +78,7 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString, default: 'john@gmail.com' }, age: { type: ClickhouseTypes.CHUInt8, default: 18 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { primary_key: 'id', table_name: 'users_table', additional_options: ['COMMENT \'This table provides user details\''] @@ -95,7 +95,7 @@ describe('ClickhouseSchema Tests', () => { name: { type: ClickhouseTypes.CHString, default: 'John Doe' }, email: { type: ClickhouseTypes.CHString } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { table_name: 'users_table', order_by: 'id' } @@ -111,7 +111,7 @@ describe('ClickhouseSchema Tests', () => { name: { type: ClickhouseTypes.CHString, default: 'John Doe' }, email: { type: ClickhouseTypes.CHString } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { table_name: 'users_table', primary_key: 'id', on_cluster: 'users_cluster' @@ -123,19 +123,18 @@ describe('ClickhouseSchema Tests', () => { }) it('should correctly generate a create table query with a specified engine', () => { - const schemaDefinition = { + const schema = new ClickhouseSchema({ id: { type: ClickhouseTypes.CHUUID }, name: { type: ClickhouseTypes.CHString, default: 'John Doe' }, email: { type: ClickhouseTypes.CHString } - } - const options: ChSchemaOptions = { + }, { table_name: 'users_table', primary_key: 'id', engine: 'ReplicatedMergeTree()' - } - const schema = new ClickhouseSchema(schemaDefinition, options) + }) const expectedQuery = 'CREATE TABLE IF NOT EXISTS users_table\n(\nid UUID,\nname String DEFAULT \'John Doe\',\nemail String\n)\nENGINE = ReplicatedMergeTree()\nPRIMARY KEY id' const query = schema.GetCreateTableQuery() + console.log(query) expect(query).toEqual(expectedQuery) }) @@ -145,7 +144,7 @@ describe('ClickhouseSchema Tests', () => { name: { type: ClickhouseTypes.CHString, default: 'John Doe' }, email: { type: ClickhouseTypes.CHString } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { database: 'users_db', table_name: 'users_table', primary_key: 'id' @@ -162,7 +161,7 @@ describe('ClickhouseSchema Tests', () => { name: { type: ClickhouseTypes.CHString, default: 'John Doe' }, email: { type: ClickhouseTypes.CHString } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { table_name: 'users_table', primary_key: 'id', on_cluster: 'users_cluster', @@ -192,7 +191,7 @@ describe('ClickhouseSchema Tests', () => { name: { type: ClickhouseTypes.CHString, default: 'John Doe' }, email: { type: ClickhouseTypes.CHString } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { table_name: 'users_table', primary_key: 'id', on_cluster: 'users_cluster',