Skip to content

Commit

Permalink
add support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-kadhe authored Mar 23, 2024
1 parent 1c85dd9 commit 9137949
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
22 changes: 11 additions & 11 deletions core/clickhouse_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ 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<T extends ChSchemaDefinition> {
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<T extends ChSchemaDefinition> {
GetOptions: () => ChSchemaOptions<T>
GetCreateTableQuery: () => string
GetCreateTableQueryAsList: () => string[]
}

export type ChSchemaDefinition = Record<string, SchemaValue>

/* This class is used to represent a clickhouse table schema */
export class ClickhouseSchema<SchemaDefinition extends ChSchemaDefinition> implements IClickhouseSchema {
export class ClickhouseSchema<SchemaDefinition extends ChSchemaDefinition> implements IClickhouseSchema<SchemaDefinition> {
readonly schema: SchemaDefinition
private readonly options: ChSchemaOptions
private readonly options: ChSchemaOptions<SchemaDefinition>

constructor (schema: SchemaDefinition, options: ChSchemaOptions) {
constructor (schema: SchemaDefinition, options: ChSchemaOptions<SchemaDefinition>) {
this.schema = schema
this.options = options
}

GetOptions (): ChSchemaOptions {
GetOptions (): ChSchemaOptions<SchemaDefinition> {
return this.options
}

Expand Down Expand Up @@ -68,8 +68,8 @@ export class ClickhouseSchema<SchemaDefinition extends ChSchemaDefinition> 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')

Expand Down
29 changes: 14 additions & 15 deletions tests/unit/clickhouse_schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('ClickhouseSchema Tests', () => {
email: { type: ClickhouseTypes.CHString },
age: { type: ClickhouseTypes.CHUInt8 }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
primary_key: 'id',
table_name: 'users_table'
}
Expand All @@ -26,7 +26,7 @@ describe('ClickhouseSchema Tests', () => {
email: { type: ClickhouseTypes.CHString },
age: { type: ClickhouseTypes.CHUInt8 }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
table_name: 'users_table'
}

Expand All @@ -42,7 +42,7 @@ describe('ClickhouseSchema Tests', () => {
email: { type: ClickhouseTypes.CHString },
age: { type: ClickhouseTypes.CHUInt8 }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
primary_key: 'id',
table_name: 'users_table'
}
Expand All @@ -60,7 +60,7 @@ describe('ClickhouseSchema Tests', () => {
email: { type: ClickhouseTypes.CHString, default: '[email protected]' },
age: { type: ClickhouseTypes.CHUInt8 }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
primary_key: 'id',
table_name: 'users_table'
}
Expand All @@ -78,7 +78,7 @@ describe('ClickhouseSchema Tests', () => {
email: { type: ClickhouseTypes.CHString, default: '[email protected]' },
age: { type: ClickhouseTypes.CHUInt8, default: 18 }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
primary_key: 'id',
table_name: 'users_table',
additional_options: ['COMMENT \'This table provides user details\'']
Expand All @@ -95,7 +95,7 @@ describe('ClickhouseSchema Tests', () => {
name: { type: ClickhouseTypes.CHString, default: 'John Doe' },
email: { type: ClickhouseTypes.CHString }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
table_name: 'users_table',
order_by: 'id'
}
Expand All @@ -111,7 +111,7 @@ describe('ClickhouseSchema Tests', () => {
name: { type: ClickhouseTypes.CHString, default: 'John Doe' },
email: { type: ClickhouseTypes.CHString }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
table_name: 'users_table',
primary_key: 'id',
on_cluster: 'users_cluster'
Expand All @@ -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)
})

Expand All @@ -145,7 +144,7 @@ describe('ClickhouseSchema Tests', () => {
name: { type: ClickhouseTypes.CHString, default: 'John Doe' },
email: { type: ClickhouseTypes.CHString }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
database: 'users_db',
table_name: 'users_table',
primary_key: 'id'
Expand All @@ -162,7 +161,7 @@ describe('ClickhouseSchema Tests', () => {
name: { type: ClickhouseTypes.CHString, default: 'John Doe' },
email: { type: ClickhouseTypes.CHString }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
table_name: 'users_table',
primary_key: 'id',
on_cluster: 'users_cluster',
Expand Down Expand Up @@ -192,7 +191,7 @@ describe('ClickhouseSchema Tests', () => {
name: { type: ClickhouseTypes.CHString, default: 'John Doe' },
email: { type: ClickhouseTypes.CHString }
}
const options: ChSchemaOptions = {
const options: ChSchemaOptions<typeof schemaDefinition> = {
table_name: 'users_table',
primary_key: 'id',
on_cluster: 'users_cluster',
Expand Down

1 comment on commit 9137949

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 100% 187/187
🟢 Branches 100% 25/25
🟢 Functions 100% 67/67
🟢 Lines 100% 170/170

Test suite run success

21 tests passing in 2 suites.

Report generated by 🧪jest coverage report action from 9137949

Please sign in to comment.