Skip to content

Commit

Permalink
Overhaul validator packages (#3508)
Browse files Browse the repository at this point in the history
* Unminify validator build output

* Reimplement `createSelectSchema` in `drizzle-zod`

* Add `getViewSelectedFields` util function

* Support views in `drizzle-zod`

* Reimplement refinement in zod select schema

* Create schema creation factory for zod

* Support enums in `createSelectSchema` in `drizzle-zod`

* Update peer deps' versions in `drizzle-zod`

* Add `getViewSelectedFields` util function

* Fix view types

* Add `$inferSelect` to views

* Format

* Fix lint errors

* Reimplement `createInsertSchema` in `drizzle-zod`

* Add `isView` util function

* Handle additional properties

* Add `createUpdateSchema` in `drizzle-zod` + Additional tests

* Update type

* Disallow unknown keys in `drizzle-zod` refinement

* Add checks comparison

* Test all PG data types

* Restructure `drizzle-zod` files

* Support PG arrays in `drizzle-zod`

* Add MySql tests to drizzle-zod

* Add SQLite tests + Fix MySql tests

* Format

* Update drizzle-valibot peer deps

* Fix redundant checks in drizzle-zod

* Wipe drizzzle-valibot and drizzle-typebox

* Adapt drizzle-zod code for drizzle-valibot

* Update string schema generation in drizzle-zod

* Add type tests to drizzle-zod

* Undo additional validation to strings in drizzle-zod

* Add missing text types in `getMySqlColumnBuilders` return value

* Handle additional MySQL text types in drizzle-zod

* Make necessary type changes to ORM

* Add MySQL tests to drizzle-valibot

* Some ORM type-level changes

* Improve and simplify array handling in drizzle-zod

* Add drizzle-valibot PG tests

* Add SQLite tests to drizzle-valibot

* Fix drizzle-valibot not identifying length in MySQL char

* Handle false-positive type error + Format drizzle-valibot

* Optimize drizzle-zod

* Optimize drizzle-valibot

* Update peer deps in drizzle-typebox

* Reimplement everything in drizzle-typebox

* Add MySQL tests for drizzle-typebox

* Add PG tests to drizzle-typebox

* Write SQLite tests for drizzle-typebox

* Format

* Update validator packages README

* Format

* Add tests for tables within schemas

* Properly implement `Buffer` schema in drizzle-typebox

* Fix types

* Fix type tests in ORM

* Fix type tests

* Lint

* Fix builds for validator packages

* Format

* Fix types ans lint

* fix

* Fix

* Update ORM types

* Fix MySQL char types

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>
  • Loading branch information
L-Mario564 and AndriiSherman authored Dec 6, 2024
1 parent a44af76 commit 7525e49
Show file tree
Hide file tree
Showing 75 changed files with 6,904 additions and 3,218 deletions.
48 changes: 43 additions & 5 deletions drizzle-orm/src/column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,49 @@ export type BuildColumn<
TTableName extends string,
TBuilder extends ColumnBuilderBase,
TDialect extends Dialect,
> = TDialect extends 'pg' ? PgColumn<MakeColumnConfig<TBuilder['_'], TTableName>>
: TDialect extends 'mysql' ? MySqlColumn<MakeColumnConfig<TBuilder['_'], TTableName>>
: TDialect extends 'singlestore' ? SingleStoreColumn<MakeColumnConfig<TBuilder['_'], TTableName>>
: TDialect extends 'sqlite' ? SQLiteColumn<MakeColumnConfig<TBuilder['_'], TTableName>>
: TDialect extends 'common' ? Column<MakeColumnConfig<TBuilder['_'], TTableName>>
> = TDialect extends 'pg' ? PgColumn<
MakeColumnConfig<TBuilder['_'], TTableName>,
{},
Simplify<Omit<TBuilder['_'], keyof MakeColumnConfig<TBuilder['_'], TTableName> | 'brand' | 'dialect'>>
>
: TDialect extends 'mysql' ? MySqlColumn<
MakeColumnConfig<TBuilder['_'], TTableName>,
{},
Simplify<
Omit<
TBuilder['_'],
| keyof MakeColumnConfig<TBuilder['_'], TTableName>
| 'brand'
| 'dialect'
| 'primaryKeyHasDefault'
| 'mysqlColumnBuilderBrand'
>
>
>
: TDialect extends 'sqlite' ? SQLiteColumn<
MakeColumnConfig<TBuilder['_'], TTableName>,
{},
Simplify<Omit<TBuilder['_'], keyof MakeColumnConfig<TBuilder['_'], TTableName> | 'brand' | 'dialect'>>
>
: TDialect extends 'common' ? Column<
MakeColumnConfig<TBuilder['_'], TTableName>,
{},
Simplify<Omit<TBuilder['_'], keyof MakeColumnConfig<TBuilder['_'], TTableName> | 'brand' | 'dialect'>>
>
: TDialect extends 'singlestore' ? SingleStoreColumn<
MakeColumnConfig<TBuilder['_'], TTableName>,
{},
Simplify<
Omit<
TBuilder['_'],
| keyof MakeColumnConfig<TBuilder['_'], TTableName>
| 'brand'
| 'dialect'
| 'primaryKeyHasDefault'
| 'singlestoreColumnBuilderBrand'
>
>
>
: never;

export type BuildIndexColumn<
Expand Down
5 changes: 4 additions & 1 deletion drizzle-orm/src/mysql-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 Down Expand Up @@ -49,6 +49,9 @@ export function getMySqlColumnBuilders() {
varbinary,
varchar,
year,
longtext,
mediumtext,
tinytext,
};
}

Expand Down
48 changes: 31 additions & 17 deletions drizzle-orm/src/mysql-core/columns/char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ import type { AnyMySqlTable } from '~/mysql-core/table.ts';
import { getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { MySqlColumn, MySqlColumnBuilder } from './common.ts';

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

export class MySqlCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'MySqlChar'>> extends MySqlColumnBuilder<
export class MySqlCharBuilder<
T extends ColumnBuilderBaseConfig<'string', 'MySqlChar'> & { length?: number | undefined },
> extends MySqlColumnBuilder<
T,
MySqlCharConfig<T['enumValues']>
MySqlCharConfig<T['enumValues'], T['length']>,
{ length: T['length'] }
> {
static override readonly [entityKind]: string = 'MySqlCharBuilder';

constructor(name: T['name'], config: MySqlCharConfig<T['enumValues']>) {
constructor(name: T['name'], config: MySqlCharConfig<T['enumValues'], T['length']>) {
super(name, 'string', 'MySqlChar');
this.config.length = config.length;
this.config.enum = config.enum;
Expand All @@ -29,20 +37,20 @@ export class MySqlCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'MySql
/** @internal */
override build<TTableName extends string>(
table: AnyMySqlTable<{ name: TTableName }>,
): MySqlChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }> {
return new MySqlChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }>(
): MySqlChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }> {
return new MySqlChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }>(
table,
this.config as ColumnBuilderRuntimeConfig<any, any>,
);
}
}

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

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

getSQLType(): string {
Expand All @@ -52,19 +60,25 @@ export class MySqlChar<T extends ColumnBaseConfig<'string', 'MySqlChar'>>

export interface MySqlCharConfig<
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(): MySqlCharBuilderInitial<'', [string, ...string[]]>;
export function char<U extends string, T extends Readonly<[U, ...U[]]>>(
config?: MySqlCharConfig<T | Writable<T>>,
): MySqlCharBuilderInitial<'', Writable<T>>;
export function char<TName extends string, U extends string, T extends Readonly<[U, ...U[]]>>(
export function char(): MySqlCharBuilderInitial<'', [string, ...string[]], undefined>;
export function char<U extends string, T extends Readonly<[U, ...U[]]>, L extends number | undefined>(
config?: MySqlCharConfig<T | Writable<T>, L>,
): MySqlCharBuilderInitial<'', Writable<T>, L>;
export function char<
TName extends string,
U extends string,
T extends Readonly<[U, ...U[]]>,
L extends number | undefined,
>(
name: TName,
config?: MySqlCharConfig<T | Writable<T>>,
): MySqlCharBuilderInitial<TName, Writable<T>>;
config?: MySqlCharConfig<T | Writable<T>, L>,
): MySqlCharBuilderInitial<TName, Writable<T>, L>;
export function char(a?: string | MySqlCharConfig, b: MySqlCharConfig = {}): any {
const { name, config } = getColumnNameAndConfig<MySqlCharConfig>(a, b);
return new MySqlCharBuilder(name, config as any);
Expand Down
5 changes: 3 additions & 2 deletions drizzle-orm/src/mysql-core/columns/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export abstract class MySqlColumnBuilder<
// To understand how to use `MySqlColumn` and `AnyMySqlColumn`, see `Column` and `AnyColumn` documentation.
export abstract class MySqlColumn<
T extends ColumnBaseConfig<ColumnDataType, string> = ColumnBaseConfig<ColumnDataType, string>,
TRuntimeConfig extends object = object,
> extends Column<T, TRuntimeConfig, { dialect: 'mysql' }> {
TRuntimeConfig extends object = {},
TTypeConfig extends object = {},
> extends Column<T, TRuntimeConfig, TTypeConfig & { dialect: 'mysql' }> {
static override readonly [entityKind]: string = 'MySqlColumn';

constructor(
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/mysql-core/columns/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class MySqlText<T extends ColumnBaseConfig<'string', 'MySqlText'>>
{
static override readonly [entityKind]: string = 'MySqlText';

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

override readonly enumValues = this.config.enumValues;

Expand Down
43 changes: 27 additions & 16 deletions drizzle-orm/src/mysql-core/columns/varchar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@ import type { AnyMySqlTable } from '~/mysql-core/table.ts';
import { getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { MySqlColumn, MySqlColumnBuilder } from './common.ts';

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

export class MySqlVarCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'MySqlVarChar'>>
extends MySqlColumnBuilder<T, MySqlVarCharConfig<T['enumValues']>>
{
export class MySqlVarCharBuilder<
T extends ColumnBuilderBaseConfig<'string', 'MySqlVarChar'> & { length?: number | undefined },
> extends MySqlColumnBuilder<T, MySqlVarCharConfig<T['enumValues'], T['length']>> {
static override readonly [entityKind]: string = 'MySqlVarCharBuilder';

/** @internal */
constructor(name: T['name'], config: MySqlVarCharConfig<T['enumValues']>) {
constructor(name: T['name'], config: MySqlVarCharConfig<T['enumValues'], T['length']>) {
super(name, 'string', 'MySqlVarChar');
this.config.length = config.length;
this.config.enum = config.enum;
Expand All @@ -31,16 +36,16 @@ export class MySqlVarCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'My
/** @internal */
override build<TTableName extends string>(
table: AnyMySqlTable<{ name: TTableName }>,
): MySqlVarChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }> {
return new MySqlVarChar<MakeColumnConfig<T, TTableName> & { enumValues: T['enumValues'] }>(
): MySqlVarChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }> {
return new MySqlVarChar<MakeColumnConfig<T, TTableName> & { length: T['length']; enumValues: T['enumValues'] }>(
table,
this.config as ColumnBuilderRuntimeConfig<any, any>,
);
}
}

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

Expand All @@ -55,18 +60,24 @@ export class MySqlVarChar<T extends ColumnBaseConfig<'string', 'MySqlVarChar'>>

export interface MySqlVarCharConfig<
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: MySqlVarCharConfig<T | Writable<T>>,
): MySqlVarCharBuilderInitial<'', 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: MySqlVarCharConfig<T | Writable<T>, L>,
): MySqlVarCharBuilderInitial<'', Writable<T>, L>;
export function varchar<
TName extends string,
U extends string,
T extends Readonly<[U, ...U[]]>,
L extends number | undefined,
>(
name: TName,
config: MySqlVarCharConfig<T | Writable<T>>,
): MySqlVarCharBuilderInitial<TName, Writable<T>>;
config: MySqlVarCharConfig<T | Writable<T>, L>,
): MySqlVarCharBuilderInitial<TName, Writable<T>, L>;
export function varchar(a?: string | MySqlVarCharConfig, b?: MySqlVarCharConfig): any {
const { name, config } = getColumnNameAndConfig<MySqlVarCharConfig>(a, b);
return new MySqlVarCharBuilder(name, config as any);
Expand Down
53 changes: 35 additions & 18 deletions drizzle-orm/src/pg-core/columns/char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ import type { AnyPgTable } from '~/pg-core/table.ts';
import { getColumnNameAndConfig, type Writable } from '~/utils.ts';
import { PgColumn, PgColumnBuilder } from './common.ts';

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

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

constructor(name: T['name'], config: PgCharConfig<T['enumValues']>) {
constructor(name: T['name'], config: PgCharConfig<T['enumValues'], T['length']>) {
super(name, 'string', 'PgChar');
this.config.length = config.length;
this.config.enumValues = config.enum;
Expand All @@ -29,13 +37,16 @@ export class PgCharBuilder<T extends ColumnBuilderBaseConfig<'string', 'PgChar'>
/** @internal */
override build<TTableName extends string>(
table: AnyPgTable<{ name: TTableName }>,
): PgChar<MakeColumnConfig<T, TTableName>> {
return new PgChar<MakeColumnConfig<T, TTableName>>(table, this.config as ColumnBuilderRuntimeConfig<any, any>);
): PgChar<MakeColumnConfig<T, TTableName> & { length: T['length'] }> {
return new PgChar<MakeColumnConfig<T, TTableName> & { length: T['length'] }>(
table,
this.config as ColumnBuilderRuntimeConfig<any, any>,
);
}
}

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

Expand All @@ -49,19 +60,25 @@ export class PgChar<T extends ColumnBaseConfig<'string', 'PgChar'>>

export interface PgCharConfig<
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(): PgCharBuilderInitial<'', [string, ...string[]]>;
export function char<U extends string, T extends Readonly<[U, ...U[]]>>(
config?: PgCharConfig<T | Writable<T>>,
): PgCharBuilderInitial<'', Writable<T>>;
export function char<TName extends string, U extends string, T extends Readonly<[U, ...U[]]>>(
export function char(): PgCharBuilderInitial<'', [string, ...string[]], undefined>;
export function char<U extends string, T extends Readonly<[U, ...U[]]>, L extends number | undefined>(
config?: PgCharConfig<T | Writable<T>, L>,
): PgCharBuilderInitial<'', Writable<T>, L>;
export function char<
TName extends string,
U extends string,
T extends Readonly<[U, ...U[]]>,
L extends number | undefined,
>(
name: TName,
config?: PgCharConfig<T | Writable<T>>,
): PgCharBuilderInitial<TName, Writable<T>>;
config?: PgCharConfig<T | Writable<T>, L>,
): PgCharBuilderInitial<TName, Writable<T>, L>;
export function char(a?: string | PgCharConfig, b: PgCharConfig = {}): any {
const { name, config } = getColumnNameAndConfig<PgCharConfig>(a, b);
return new PgCharBuilder(name, config as any);
Expand Down
Loading

0 comments on commit 7525e49

Please sign in to comment.