Skip to content

Commit

Permalink
options check
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-kadhe committed Mar 24, 2024
1 parent 4bd6489 commit 2de6e8e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
27 changes: 13 additions & 14 deletions core/clickhouse_schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type ChDataType } from '@clickhouse-schema-data-types/index'

export interface SchemaValue { type: ChDataType, default?: unknown } // Note that default is a string because we can't guarantee that it will be a valid value
export interface SchemaValue { type: ChDataType, default?: unknown }
export type ChSchemaDefinition = Record<string, SchemaValue>
/**
* ChSchemaOptions is used to define the options for a clickhouse table schema.
*
Expand All @@ -12,35 +13,33 @@ 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> {
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> {
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 +67,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 ${this.options.order_by.toString()}` : '',
this.options.primary_key !== undefined ? `PRIMARY KEY ${this.options.primary_key.toString()}` : '',
additionalOptions
].filter(part => part.trim().length > 0).join('\n')

Expand Down
6 changes: 3 additions & 3 deletions core/infer_schema_type.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { type ClickhouseSchema, type ChSchemaDefinition } from '@clickhouse-schema-core/clickhouse_schema'
import { type ClickhouseSchema } from '@clickhouse-schema-core/clickhouse_schema'
import { type ChDataType } from '@clickhouse-schema-data-types/index'
import { type InferArray, type InferEnum, type InferJSON, type InferNullable, type InferTypeFromMap } from '@clickhouse-schema-utils/util'

/** InferSchemaClickhouseSchemaType is a type that takes a ClickhouseSchema and returns the typescript that it represents */
export type InferClickhouseSchemaType<T extends ClickhouseSchema<ChSchemaDefinition>> = { [K in keyof T['schema']]: InferType<T['schema'][K]['type']> }
export type InferClickhouseSchemaType<T extends ClickhouseSchema<any>> = { [K in keyof T['schema']]: InferType<T['schema'][K]['type']> }

/** InferType is a type that takes a ChDataType and returns the typescript that it represents */
type InferType<T extends ChDataType> = T['dataTypeMarker'] extends 'Array' ? InferArray<T> : T['dataTypeMarker'] extends 'Enum' ? InferEnum<T> : T['dataTypeMarker'] extends 'JSON' ? InferJSON<T> : T['dataTypeMarker'] extends 'Nullable' ? InferNullable<T> : InferTypeFromMap<T['typeStr']>
export type InferType<T extends ChDataType> = T['dataTypeMarker'] extends 'Array' ? InferArray<T> : T['dataTypeMarker'] extends 'Enum' ? InferEnum<T> : T['dataTypeMarker'] extends 'JSON' ? InferJSON<T> : T['dataTypeMarker'] extends 'Nullable' ? InferNullable<T> : InferTypeFromMap<T['typeStr']>
33 changes: 17 additions & 16 deletions tests/unit/clickhouse_schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ClickhouseSchema, type ChSchemaOptions } from '@clickhouse-schema-core/clickhouse_schema'
import { type InferClickhouseSchemaType } from '@clickhouse-schema-core/infer_schema_type'
import { ClickhouseTypes } from '@clickhouse-schema-data-types/index'

describe('ClickhouseSchema Tests', () => {
Expand All @@ -9,13 +10,14 @@ 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'
}

const schema = new ClickhouseSchema(schemaDefinition, options)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ChSchemaDefinition = InferClickhouseSchemaType<typeof schema>
expect(schema.GetOptions()).toEqual(options)
})

Expand All @@ -26,7 +28,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 +44,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 +62,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 +80,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 +97,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 +113,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 +125,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 +146,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 +163,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 +193,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

0 comments on commit 2de6e8e

Please sign in to comment.