Skip to content

Commit

Permalink
Add SS support in drizzle-zod
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Mario564 committed Dec 17, 2024
1 parent 2e49ebc commit 42d7de9
Show file tree
Hide file tree
Showing 6 changed files with 649 additions and 75 deletions.
5 changes: 4 additions & 1 deletion drizzle-orm/src/singlestore-core/columns/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { mediumint } from './mediumint.ts';
import { real } from './real.ts';
import { serial } from './serial.ts';
import { smallint } from './smallint.ts';
import { text } from './text.ts';
import { longtext, mediumtext, text, tinytext } from './text.ts';
import { time } from './time.ts';
import { timestamp } from './timestamp.ts';
import { tinyint } from './tinyint.ts';
Expand All @@ -42,7 +42,10 @@ export function getSingleStoreColumnBuilders() {
real,
serial,
smallint,
longtext,
mediumtext,
text,
tinytext,
time,
timestamp,
tinyint,
Expand Down
71 changes: 41 additions & 30 deletions drizzle-orm/src/singlestore-core/columns/char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@ import type { AnySingleStoreTable } from '~/singlestore-core/table.ts';
import { getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';

export type SingleStoreCharBuilderInitial<TName extends string, TEnum extends [string, ...string[]]> =
SingleStoreCharBuilder<{
name: TName;
dataType: 'string';
columnType: 'SingleStoreChar';
data: TEnum[number];
driverParam: number | string;
enumValues: TEnum;
generated: undefined;
}>;
export type SingleStoreCharBuilderInitial<
TName extends string,
TEnum extends [string, ...string[]],
TLength extends number | undefined,
> = SingleStoreCharBuilder<{
name: TName;
dataType: 'string';
columnType: 'SingleStoreChar';
data: TEnum[number];
driverParam: number | string;
enumValues: TEnum;
generated: undefined;
length: TLength;
}>;

export class SingleStoreCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'SingleStoreChar'>>
extends SingleStoreColumnBuilder<
T,
SingleStoreCharConfig<T['enumValues']>
>
{
export class SingleStoreCharBuilder<
T extends ColumnBuilderBaseConfig<'string', 'SingleStoreChar'> & { length?: number | undefined },
> extends SingleStoreColumnBuilder<
T,
SingleStoreCharConfig<T['enumValues'], T['length']>,
{ length: T['length'] }
> {
static override readonly [entityKind]: string = 'SingleStoreCharBuilder';

constructor(name: T['name'], config: SingleStoreCharConfig<T['enumValues']>) {
constructor(name: T['name'], config: SingleStoreCharConfig<T['enumValues'], T['length']>) {
super(name, 'string', 'SingleStoreChar');
this.config.length = config.length;
this.config.enum = config.enum;
Expand All @@ -33,20 +38,20 @@ export class SingleStoreCharBuilder<T extends ColumnBuilderBaseConfig<'string',
/** @internal */
override build<TTableName extends string>(
table: AnySingleStoreTable<{ name: TTableName }>,
): SingleStoreChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }> {
return new SingleStoreChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }>(
): SingleStoreChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }> {
return new SingleStoreChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }>(
table,
this.config as ColumnBuilderRuntimeConfig<any, any>,
);
}
}

export class SingleStoreChar<T extends ColumnBaseConfig<'string', 'SingleStoreChar'>>
extends SingleStoreColumn<T, SingleStoreCharConfig<T['enumValues']>>
export class SingleStoreChar<T extends ColumnBaseConfig<'string', 'SingleStoreChar'> & { length?: number | undefined }>
extends SingleStoreColumn<T, SingleStoreCharConfig<T['enumValues'], T['length']>, { length: T['length'] }>
{
static override readonly [entityKind]: string = 'SingleStoreChar';

readonly length: number | undefined = this.config.length;
readonly length: T['length'] = this.config.length;
override readonly enumValues = this.config.enum;

getSQLType(): string {
Expand All @@ -56,19 +61,25 @@ export class SingleStoreChar<T extends ColumnBaseConfig<'string', 'SingleStoreCh

export interface SingleStoreCharConfig<
TEnum extends readonly string[] | string[] | undefined = readonly string[] | string[] | undefined,
TLength extends number | undefined = number | undefined,
> {
length?: number;
enum?: TEnum;
length?: TLength;
}

export function char(): SingleStoreCharBuilderInitial<'', [string, ...string[]]>;
export function char<U extends string, T extends Readonly<[U, ...U[]]>>(
config?: SingleStoreCharConfig<T | Writable<T>>,
): SingleStoreCharBuilderInitial<'', Writable<T>>;
export function char<TName extends string, U extends string, T extends Readonly<[U, ...U[]]>>(
export function char(): SingleStoreCharBuilderInitial<'', [string, ...string[]], undefined>;
export function char<U extends string, T extends Readonly<[U, ...U[]]>, L extends number | undefined>(
config?: SingleStoreCharConfig<T | Writable<T>, L>,
): SingleStoreCharBuilderInitial<'', Writable<T>, L>;
export function char<
TName extends string,
U extends string,
T extends Readonly<[U, ...U[]]>,
L extends number | undefined,
>(
name: TName,
config?: SingleStoreCharConfig<T | Writable<T>>,
): SingleStoreCharBuilderInitial<TName, Writable<T>>;
config?: SingleStoreCharConfig<T | Writable<T>, L>,
): SingleStoreCharBuilderInitial<TName, Writable<T>, L>;
export function char(a?: string | SingleStoreCharConfig, b: SingleStoreCharConfig = {}): any {
const { name, config } = getColumnNameAndConfig<SingleStoreCharConfig>(a, b);
return new SingleStoreCharBuilder(name, config as any);
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/singlestore-core/columns/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class SingleStoreText<T extends ColumnBaseConfig<'string', 'SingleStoreTe
{
static override readonly [entityKind]: string = 'SingleStoreText';

private textType: SingleStoreTextColumnType = this.config.textType;
readonly textType: SingleStoreTextColumnType = this.config.textType;

override readonly enumValues = this.config.enumValues;

Expand Down
71 changes: 41 additions & 30 deletions drizzle-orm/src/singlestore-core/columns/varchar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,30 @@ import type { AnySingleStoreTable } from '~/singlestore-core/table.ts';
import { getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { SingleStoreColumn, SingleStoreColumnBuilder } from './common.ts';

export type SingleStoreVarCharBuilderInitial<TName extends string, TEnum extends [string, ...string[]]> =
SingleStoreVarCharBuilder<
{
name: TName;
dataType: 'string';
columnType: 'SingleStoreVarChar';
data: TEnum[number];
driverParam: number | string;
enumValues: TEnum;
generated: undefined;
}
>;
export type SingleStoreVarCharBuilderInitial<
TName extends string,
TEnum extends [string, ...string[]],
TLength extends number | undefined,
> = SingleStoreVarCharBuilder<
{
name: TName;
dataType: 'string';
columnType: 'SingleStoreVarChar';
data: TEnum[number];
driverParam: number | string;
enumValues: TEnum;
generated: undefined;
length: TLength;
}
>;

export class SingleStoreVarCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'SingleStoreVarChar'>>
extends SingleStoreColumnBuilder<T, SingleStoreVarCharConfig<T['enumValues']>>
{
export class SingleStoreVarCharBuilder<
T extends ColumnBuilderBaseConfig<'string', 'SingleStoreVarChar'> & { length?: number | undefined },
> extends SingleStoreColumnBuilder<T, SingleStoreVarCharConfig<T['enumValues'], T['length']>, { length: T['length'] }> {
static override readonly [entityKind]: string = 'SingleStoreVarCharBuilder';

/** @internal */
constructor(name: T['name'], config: SingleStoreVarCharConfig<T['enumValues']>) {
constructor(name: T['name'], config: SingleStoreVarCharConfig<T['enumValues'], T['length']>) {
super(name, 'string', 'SingleStoreVarChar');
this.config.length = config.length;
this.config.enum = config.enum;
Expand All @@ -33,21 +37,22 @@ export class SingleStoreVarCharBuilder<T extends ColumnBuilderBaseConfig<'string
/** @internal */
override build<TTableName extends string>(
table: AnySingleStoreTable<{ name: TTableName }>,
): SingleStoreVarChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }> {
return new SingleStoreVarChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }>(
): SingleStoreVarChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }> {
return new SingleStoreVarChar<
MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }
>(
table,
this.config as ColumnBuilderRuntimeConfig<any, any>,
);
}
}

export class SingleStoreVarChar<T extends ColumnBaseConfig<'string', 'SingleStoreVarChar'>>
extends SingleStoreColumn<T, SingleStoreVarCharConfig<T['enumValues']>>
{
export class SingleStoreVarChar<
T extends ColumnBaseConfig<'string', 'SingleStoreVarChar'> & { length?: number | undefined },
> extends SingleStoreColumn<T, SingleStoreVarCharConfig<T['enumValues'], T['length']>, { length: T['length'] }> {
static override readonly [entityKind]: string = 'SingleStoreVarChar';

readonly length: number | undefined = this.config.length;

readonly length: T['length'] = this.config.length;
override readonly enumValues = this.config.enum;

getSQLType(): string {
Expand All @@ -57,18 +62,24 @@ export class SingleStoreVarChar<T extends ColumnBaseConfig<'string', 'SingleStor

export interface SingleStoreVarCharConfig<
TEnum extends string[] | readonly string[] | undefined = string[] | readonly string[] | undefined,
TLength extends number | undefined = number | undefined,
> {
length: number;
enum?: TEnum;
length?: TLength;
}

export function varchar<U extends string, T extends Readonly<[U, ...U[]]>>(
config: SingleStoreVarCharConfig<T | Writable<T>>,
): SingleStoreVarCharBuilderInitial<'', Writable<T>>;
export function varchar<TName extends string, U extends string, T extends Readonly<[U, ...U[]]>>(
export function varchar<U extends string, T extends Readonly<[U, ...U[]]>, L extends number | undefined>(
config: SingleStoreVarCharConfig<T | Writable<T>, L>,
): SingleStoreVarCharBuilderInitial<'', Writable<T>, L>;
export function varchar<
TName extends string,
U extends string,
T extends Readonly<[U, ...U[]]>,
L extends number | undefined,
>(
name: TName,
config: SingleStoreVarCharConfig<T | Writable<T>>,
): SingleStoreVarCharBuilderInitial<TName, Writable<T>>;
config: SingleStoreVarCharConfig<T | Writable<T>, L>,
): SingleStoreVarCharBuilderInitial<TName, Writable<T>, L>;
export function varchar(a?: string | SingleStoreVarCharConfig, b?: SingleStoreVarCharConfig): any {
const { name, config } = getColumnNameAndConfig<SingleStoreVarCharConfig>(a, b);
return new SingleStoreVarCharBuilder(name, config as any);
Expand Down
Loading

0 comments on commit 42d7de9

Please sign in to comment.