From 2de6e8e767ed039df1c5941f936df65924909323 Mon Sep 17 00:00:00 2001 From: Rohit Kadhe Date: Sun, 24 Mar 2024 10:39:34 -0600 Subject: [PATCH] options check --- core/clickhouse_schema.ts | 27 +++++++++++------------ core/infer_schema_type.ts | 6 ++--- tests/unit/clickhouse_schema.test.ts | 33 ++++++++++++++-------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/core/clickhouse_schema.ts b/core/clickhouse_schema.ts index 9ea1c97..2b605bd 100644 --- a/core/clickhouse_schema.ts +++ b/core/clickhouse_schema.ts @@ -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 /** * ChSchemaOptions is used to define the options for a clickhouse table schema. * @@ -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 { 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[] } -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 +67,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 ${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') diff --git a/core/infer_schema_type.ts b/core/infer_schema_type.ts index cd7abcc..ed0bf7d 100644 --- a/core/infer_schema_type.ts +++ b/core/infer_schema_type.ts @@ -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> = { [K in keyof T['schema']]: InferType } +export type InferClickhouseSchemaType> = { [K in keyof T['schema']]: InferType } /** InferType is a type that takes a ChDataType and returns the typescript that it represents */ -type InferType = T['dataTypeMarker'] extends 'Array' ? InferArray : T['dataTypeMarker'] extends 'Enum' ? InferEnum : T['dataTypeMarker'] extends 'JSON' ? InferJSON : T['dataTypeMarker'] extends 'Nullable' ? InferNullable : InferTypeFromMap +export type InferType = T['dataTypeMarker'] extends 'Array' ? InferArray : T['dataTypeMarker'] extends 'Enum' ? InferEnum : T['dataTypeMarker'] extends 'JSON' ? InferJSON : T['dataTypeMarker'] extends 'Nullable' ? InferNullable : InferTypeFromMap diff --git a/tests/unit/clickhouse_schema.test.ts b/tests/unit/clickhouse_schema.test.ts index e049d0c..05579b6 100644 --- a/tests/unit/clickhouse_schema.test.ts +++ b/tests/unit/clickhouse_schema.test.ts @@ -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', () => { @@ -9,13 +10,14 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString }, age: { type: ClickhouseTypes.CHUInt8 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { 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 expect(schema.GetOptions()).toEqual(options) }) @@ -26,7 +28,7 @@ describe('ClickhouseSchema Tests', () => { email: { type: ClickhouseTypes.CHString }, age: { type: ClickhouseTypes.CHUInt8 } } - const options: ChSchemaOptions = { + const options: ChSchemaOptions = { table_name: 'users_table' } @@ -42,7 +44,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 +62,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 +80,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 +97,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 +113,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 +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) }) @@ -145,7 +146,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 +163,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 +193,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',