Skip to content

Commit

Permalink
Revert "add support (#3)"
Browse files Browse the repository at this point in the history
This reverts commit 9137949.
  • Loading branch information
rohit-kadhe authored Mar 23, 2024
1 parent 9137949 commit 6532fea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 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<T extends ChSchemaDefinition> {
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<T extends ChSchemaDefinition> {
GetOptions: () => ChSchemaOptions<T>
interface IClickhouseSchema {
GetOptions: () => ChSchemaOptions
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<SchemaDefinition> {
export class ClickhouseSchema<SchemaDefinition extends ChSchemaDefinition> implements IClickhouseSchema {
readonly schema: SchemaDefinition
private readonly options: ChSchemaOptions<SchemaDefinition>
private readonly options: ChSchemaOptions

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

GetOptions (): ChSchemaOptions<SchemaDefinition> {
GetOptions (): ChSchemaOptions {
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 ${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')

Expand Down
29 changes: 15 additions & 14 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<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
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<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
table_name: 'users_table'
}

Expand All @@ -42,7 +42,7 @@ describe('ClickhouseSchema Tests', () => {
email: { type: ClickhouseTypes.CHString },
age: { type: ClickhouseTypes.CHUInt8 }
}
const options: ChSchemaOptions<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
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<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
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<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
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<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
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<typeof schemaDefinition> = {
const options: ChSchemaOptions = {
table_name: 'users_table',
primary_key: 'id',
on_cluster: 'users_cluster'
Expand All @@ -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)
})

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

0 comments on commit 6532fea

Please sign in to comment.