diff --git a/core/clickhouse_schema.ts b/core/clickhouse_schema.ts index 0167cd7..9ea1c97 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?: keyof T - order_by?: keyof T + primary_key?: string + order_by?: string 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 ${String(this.options.order_by)}` : '', - this.options.primary_key !== undefined ? `PRIMARY KEY ${String(this.options.primary_key)}` : '', + this.options.order_by !== undefined ? `ORDER BY ${this.options.order_by}` : '', + this.options.primary_key !== undefined ? `PRIMARY KEY ${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 143ae24..e049d0c 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,18 +123,19 @@ describe('ClickhouseSchema Tests', () => { }) it('should correctly generate a create table query with a specified engine', () => { - const schema = new ClickhouseSchema({ + const schemaDefinition = { 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) }) @@ -144,7 +145,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' @@ -161,7 +162,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', @@ -191,7 +192,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',