Skip to content

Commit

Permalink
Add default value type checks (#19)
Browse files Browse the repository at this point in the history
* add default value checks

* add comments for editor intelisense
  • Loading branch information
rohit-kadhe authored Apr 1, 2024
1 parent f4e94a1 commit 04e17c1
Show file tree
Hide file tree
Showing 19 changed files with 394 additions and 221 deletions.
8 changes: 4 additions & 4 deletions core/clickhouse_schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type ChDataType } from '@clickhouse-schema-data-types/index'

export interface SchemaValue { type: ChDataType, default?: any }
export interface SchemaValue { type: ChDataType }
export type ChSchemaDefinition = Record<string, SchemaValue>
/**
* ChSchemaOptions is used to define the options for a clickhouse table schema.
Expand Down Expand Up @@ -58,10 +58,10 @@ export class ClickhouseSchema<SchemaDefinition extends ChSchemaDefinition> imple
const columns = Object.entries(this.schema as ChSchemaDefinition)
.map(([name, field]) => {
// Check if default is defined and a string, add single quotes; otherwise, just use the value
const defaultValue = field.default !== undefined
? (typeof field.default === 'string' ? `'${field.default}'` : field.default)
const defaultValue = field.type.default !== undefined
? (typeof field.type.default === 'string' ? `'${field.type.default}'` : field.type.default)
: ''
return `${name} ${field.type}${field.default !== undefined ? ` DEFAULT ${defaultValue}` : ''}`
return `${name} ${field.type}${field.type.default !== undefined ? ` DEFAULT ${defaultValue}` : ''}`
}
)
.join(',\n')
Expand Down
24 changes: 4 additions & 20 deletions core/infer_schema_type.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import { type ClickhouseSchema } from '@clickhouse-schema-core/clickhouse_schema'
import { type ChArray } from '@clickhouse-schema-data-types/ch_array'
import { type ChJSON } from '@clickhouse-schema-data-types/ch_json'
import { type ChEnum, type ChLowCardinality } from '@clickhouse-schema-data-types/ch_low_ cardinality'
import { type ChNullable } from '@clickhouse-schema-data-types/ch_nullable'
import { type ChDataType } from '@clickhouse-schema-data-types/index'
import { 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<any>> = { [K in keyof T['schema']]: InferType<T['schema'][K]['type']> }
/** Infer is a type that takes a ChDataType and returns the typescript that it represents */
type Infer<T extends ChDataType> = T['typeScriptType']

/** InferType is a type that takes a ChDataType and returns the typescript that it represents */
export type InferType<T extends ChDataType> =
T extends ChNullable<infer NullableType>
? InferTypeFromMap<NullableType['typeStr']> | null
: T extends ChArray<infer ArrayType>
? Array<InferType<ArrayType>>
: T extends ChEnum<infer EnumType>
? keyof EnumType
: T extends ChJSON<infer Schema>
? { [K in keyof Schema]: InferType<Schema[K]['type']> }
: T extends ChLowCardinality<infer LowCardinalityType>
? InferTypeFromMap<LowCardinalityType['typeStr']>
: InferTypeFromMap<T['typeStr']>
/** InferSchemaClickhouseSchemaType is a type that takes a ClickhouseSchema and returns the typescript that it represents */
export type InferClickhouseSchemaType<T extends ClickhouseSchema<any>> = { [K in keyof T['schema']]: Infer<T['schema'][K]['type']> }
5 changes: 4 additions & 1 deletion data_types/ch_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
export class ChArray<T extends ChDataType | ChArray<ChDataType>> implements ChDataType {
readonly innerType: T
readonly typeStr: string
readonly typeScriptType!: Array<T['typeScriptType']>
readonly default?: Array<T['typeScriptType']>

constructor (t: T) {
constructor (t: T, defaultVal?: Array<T['typeScriptType']>) {
this.default = defaultVal
if (t instanceof ChArray) {
this.innerType = new ChArray(t.innerType) as T
} else {
Expand Down
6 changes: 6 additions & 0 deletions data_types/ch_boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
*/
export class ChBoolean implements ChDataType {
readonly typeStr: 'Boolean' = 'Boolean' as const
readonly typeScriptType!: boolean
readonly default?: boolean

constructor (defaultValue?: boolean) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand Down
21 changes: 19 additions & 2 deletions data_types/ch_date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
*/
export class ChDate implements ChDataType {
readonly typeStr: 'Date' = 'Date' as const
readonly typeScriptType!: Date
readonly default?: Date

constructor (defaultValue?: Date) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -16,6 +22,11 @@ export class ChDate implements ChDataType {
*/
export class ChDate32 implements ChDataType {
readonly typeStr: 'Date32' = 'Date32' as const
readonly typeScriptType!: Date
readonly default?: Date
constructor (defaultValue?: Date) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -27,9 +38,12 @@ export class ChDate32 implements ChDataType {
*/
export class ChDateTime<T extends string> implements ChDataType {
readonly typeStr: `DateTime('${T}')`
readonly typeScriptType!: Date
readonly defaultValue?: Date

constructor (readonly timezone: T) {
constructor (readonly timezone: T, defaultValue?: Date) {
this.typeStr = `DateTime('${timezone}')`
this.defaultValue = defaultValue
}

toString (): string {
Expand All @@ -44,9 +58,12 @@ export class ChDateTime<T extends string> implements ChDataType {
*/
export class ChDateTime64<T extends number, V extends string> implements ChDataType {
readonly typeStr: `DateTime64(${T}, '${V}')`
readonly typeScriptType!: Date
readonly defaultValue?: Date

constructor (readonly precision: T, readonly timezone: V) {
constructor (readonly precision: T, readonly timezone: V, defaultValue?: Date) {
this.typeStr = `DateTime64(${precision}, '${timezone}')`
this.defaultValue = defaultValue
}

toString (): string {
Expand Down
5 changes: 4 additions & 1 deletion data_types/ch_decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
*/
export class ChDecimal<P extends number, S extends number> implements ChDataType {
readonly typeStr: `Decimal(${P},${S})`
readonly typeScriptType!: number
readonly default?: number

constructor (readonly precision: P, readonly scale: S) {
constructor (readonly precision: P, readonly scale: S, defaultVal?: number) {
this.typeStr = `Decimal(${precision},${scale})`
this.default = defaultVal
}

toString (): string {
Expand Down
12 changes: 12 additions & 0 deletions data_types/ch_float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
*/
export class ChFloat32 implements ChDataType {
readonly typeStr: 'Float32' = 'Float32' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -15,6 +21,12 @@ export class ChFloat32 implements ChDataType {
*/
export class ChFloat64 implements ChDataType {
readonly typeStr: 'Float64' = 'Float64' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand Down
80 changes: 78 additions & 2 deletions data_types/ch_integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
*/
export class ChUInt8 implements ChDataType {
readonly typeStr: 'UInt8' = 'UInt8' as const
readonly typeScriptType!: number
readonly default?: number

constructor () {
constructor (defaultValue?: number) {
this.typeStr = 'UInt8'
this.default = defaultValue
}

toString (): string {
Expand All @@ -20,9 +23,12 @@ export class ChUInt8 implements ChDataType {
*/
export class ChUInt16 implements ChDataType {
readonly typeStr: 'UInt16' = 'UInt16' as const
readonly typeScriptType!: number
readonly default?: number

constructor () {
constructor (defaultValue?: number) {
this.typeStr = 'UInt16'
this.default = defaultValue
}

toString (): string {
Expand All @@ -35,6 +41,13 @@ export class ChUInt16 implements ChDataType {
*/
export class ChUInt32 implements ChDataType {
readonly typeStr: 'UInt32' = 'UInt32' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'UInt32'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -46,6 +59,13 @@ export class ChUInt32 implements ChDataType {
*/
export class ChUInt64 implements ChDataType {
readonly typeStr: 'UInt64' = 'UInt64' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'UInt64'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -57,6 +77,13 @@ export class ChUInt64 implements ChDataType {
*/
export class ChUInt128 implements ChDataType {
readonly typeStr: 'UInt128' = 'UInt128' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'UInt128'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -68,6 +95,13 @@ export class ChUInt128 implements ChDataType {
*/
export class ChUInt256 implements ChDataType {
readonly typeStr: 'UInt256' = 'UInt256' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'UInt256'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -79,6 +113,13 @@ export class ChUInt256 implements ChDataType {
*/
export class ChInt8 implements ChDataType {
readonly typeStr: 'Int8' = 'Int8' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'Int8'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -91,6 +132,13 @@ export class ChInt8 implements ChDataType {
*/
export class ChInt16 implements ChDataType {
readonly typeStr: 'Int16' = 'Int16' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'Int16'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -102,6 +150,13 @@ export class ChInt16 implements ChDataType {
*/
export class ChInt32 implements ChDataType {
readonly typeStr: 'Int32' = 'Int32' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'Int32'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -113,6 +168,13 @@ export class ChInt32 implements ChDataType {
*/
export class ChInt64 implements ChDataType {
readonly typeStr: 'Int64' = 'Int64' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'Int64'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -124,6 +186,13 @@ export class ChInt64 implements ChDataType {
*/
export class ChInt128 implements ChDataType {
readonly typeStr: 'Int128' = 'Int128' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'Int128'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -135,6 +204,13 @@ export class ChInt128 implements ChDataType {
*/
export class ChInt256 implements ChDataType {
readonly typeStr: 'Int256' = 'Int256' as const
readonly typeScriptType!: number
readonly default?: number

constructor (defaultValue?: number) {
this.typeStr = 'Int256'
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand Down
12 changes: 12 additions & 0 deletions data_types/ch_ip_address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
*/
export class ChIPv4 implements ChDataType {
readonly typeStr: 'IPv4' = 'IPv4' as const
readonly typeScriptType!: string
readonly default?: string

constructor (defaultValue?: string) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand All @@ -15,7 +21,13 @@ export class ChIPv4 implements ChDataType {
* ChIPv6 is a class that represents a Clickhouse IPv6 data type
*/
export class ChIPv6 implements ChDataType {
readonly typeScriptType!: string
readonly typeStr: 'IPv6' = 'IPv6' as const
readonly default?: string

constructor (defaultValue?: string) {
this.default = defaultValue
}

toString (): string {
return this.typeStr
Expand Down
5 changes: 4 additions & 1 deletion data_types/ch_json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import { type ChDataType } from '@clickhouse-schema-data-types/index'
export class ChJSON<T extends ChSchemaDefinition> implements ChDataType {
readonly typeStr: 'JSON'
readonly innerType: T
readonly typeScriptType!: { [K in keyof T]: T[K]['type']['typeScriptType'] }
readonly default?: { [K in keyof T]: T[K]['type']['typeScriptType'] }

constructor (innerType: T) {
constructor (innerType: T, defaultValue?: { [K in keyof T]: T[K]['type']['typeScriptType'] }) {
this.typeStr = 'JSON'
this.innerType = innerType
this.default = defaultValue
}

toString (): string {
Expand Down
Loading

1 comment on commit 04e17c1

@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% 268/268
🟢 Branches 100% 26/26
🟢 Functions 100% 98/98
🟢 Lines 100% 204/204

Test suite run success

25 tests passing in 2 suites.

Report generated by 🧪jest coverage report action from 04e17c1

Please sign in to comment.