Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"**/_journal.json",
"**/tsup.config*.mjs",
"**/.sst",
"rollup.config-*.mjs",
"integration-tests/tests/prisma/*/client",
"integration-tests/tests/prisma/*/drizzle"
],
Expand Down
10 changes: 8 additions & 2 deletions drizzle-arktype/tests/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ test('all data types', (t) => {
bigint2: bigint({ mode: 'bigint' }).notNull(),
bigint3: bigint({ unsigned: true, mode: 'number' }).notNull(),
bigint4: bigint({ unsigned: true, mode: 'bigint' }).notNull(),
bigint5: bigint({ mode: 'string' }).notNull(),
bigint6: bigint({ unsigned: true, mode: 'string' }).notNull(),
binary: binary({ length: 10 }).notNull(),
boolean: boolean().notNull(),
char1: char({ length: 10 }).notNull(),
Expand Down Expand Up @@ -408,7 +410,11 @@ test('all data types', (t) => {
bigint2: type.bigint.narrow(bigintNarrow),
bigint3: type.keywords.number.integer.atLeast(0).atMost(Number.MAX_SAFE_INTEGER),
bigint4: type.bigint.narrow(unsignedBigintNarrow),
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`),
bigint5: type.string,
bigint6: type.string,
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`) as Type<
string
>,
boolean: type.boolean,
char1: type.string.exactlyLength(10),
char2: type.enumerated('a', 'b', 'c'),
Expand Down Expand Up @@ -443,7 +449,7 @@ test('all data types', (t) => {
varchar2: type.enumerated('a', 'b', 'c'),
varbinary: type(`/^[01]{0,10}$/`).describe(
`a string containing ones or zeros while being up to 10 characters long`,
),
) as Type<string>,
year: type.keywords.number.integer.atLeast(1901).atMost(2155),
longtext1: type.string.atMostLength(CONSTANTS.INT32_UNSIGNED_MAX),
longtext2: type.enumerated('a', 'b', 'c'),
Expand Down
2 changes: 2 additions & 0 deletions drizzle-arktype/tests/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ test('all data types', (t) => {
}) => ({
bigint1: bigint({ mode: 'number' }).notNull(),
bigint2: bigint({ mode: 'bigint' }).notNull(),
bigint3: bigint({ mode: 'string' }).notNull(),
bigserial1: bigserial({ mode: 'number' }).notNull(),
bigserial2: bigserial({ mode: 'bigint' }).notNull(),
bit: bit({ dimensions: 5 }).notNull(),
Expand Down Expand Up @@ -455,6 +456,7 @@ test('all data types', (t) => {
const expected = type({
bigint1: type.keywords.number.integer.atLeast(Number.MIN_SAFE_INTEGER).atMost(Number.MAX_SAFE_INTEGER),
bigint2: type.bigint.narrow(bigintNarrow),
bigint3: type.string,
bigserial1: type.keywords.number.integer.atLeast(Number.MIN_SAFE_INTEGER).atMost(Number.MAX_SAFE_INTEGER),
bigserial2: type.bigint.narrow(bigintNarrow),
bit: type(/^[01]{5}$/).describe('a string containing ones or zeros while being 5 characters long'),
Expand Down
10 changes: 8 additions & 2 deletions drizzle-arktype/tests/singlestore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ test('all data types', (t) => {
bigint2: bigint({ mode: 'bigint' }).notNull(),
bigint3: bigint({ unsigned: true, mode: 'number' }).notNull(),
bigint4: bigint({ unsigned: true, mode: 'bigint' }).notNull(),
bigint5: bigint({ mode: 'string' }).notNull(),
bigint6: bigint({ unsigned: true, mode: 'string' }).notNull(),
binary: binary({ length: 10 }).notNull(),
boolean: boolean().notNull(),
char1: char({ length: 10 }).notNull(),
Expand Down Expand Up @@ -410,7 +412,11 @@ test('all data types', (t) => {
bigint2: type.bigint.narrow(bigintNarrow),
bigint3: type.keywords.number.integer.atLeast(0).atMost(Number.MAX_SAFE_INTEGER),
bigint4: type.bigint.narrow(unsignedBigintNarrow),
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`),
bigint5: type.string,
bigint6: type.string,
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`) as Type<
string
>,
boolean: type.boolean,
char1: type.string.exactlyLength(10),
char2: type.enumerated('a', 'b', 'c'),
Expand Down Expand Up @@ -445,7 +451,7 @@ test('all data types', (t) => {
varchar2: type.enumerated('a', 'b', 'c'),
varbinary: type(`/^[01]{0,10}$/`).describe(
`a string containing ones or zeros while being up to 10 characters long`,
),
) as Type<string>,
year: type.keywords.number.integer.atLeast(1901).atMost(2155),
longtext1: type.string.atMostLength(CONSTANTS.INT32_UNSIGNED_MAX),
longtext2: type.enumerated('a', 'b', 'c'),
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/src/column-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ export type ColumnDataStringConstraint =
| 'datetime'
| 'enum'
| 'inet'
| 'int64'
| 'interval'
| 'macaddr'
| 'macaddr8'
| 'numeric'
| 'sparsevec'
| 'time'
| 'timestamp'
| 'uint64'
| 'unumeric'
| 'uuid';

Expand Down
56 changes: 52 additions & 4 deletions drizzle-orm/src/mysql-core/columns/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,49 @@ export class MySqlBigInt64<T extends ColumnBaseConfig<'bigint int64' | 'bigint u
}
}

export class MySqlBigIntStringBuilder<TUnsigned extends boolean | undefined>
extends MySqlColumnBuilderWithAutoIncrement<{
name: string;
dataType: Equal<TUnsigned, true> extends true ? 'string uint64' : 'string int64';
data: string;
driverParam: number | string;
}, { unsigned?: boolean }>
{
static override readonly [entityKind]: string = 'MySqlBigIntStringBuilder';

constructor(name: string, unsigned: boolean = false) {
super(name, unsigned ? 'string uint64' : 'string int64' as any, 'MySqlBigIntString');
this.config.unsigned = unsigned as TUnsigned;
}

/** @internal */
override build(table: MySqlTable) {
return new MySqlBigIntString(
table,
this.config as any,
);
}
}

export class MySqlBigIntString<T extends ColumnBaseConfig<'string int64' | 'string uint64'>>
extends MySqlColumnWithAutoIncrement<T, { unsigned: boolean }>
{
static override readonly [entityKind]: string = 'MySqlBigIntString';

getSQLType(): string {
return `bigint${this.config.unsigned ? ' unsigned' : ''}`;
}

override mapFromDriverValue(value: number | string): string {
if (typeof value === 'string') {
return value;
}
return String(value);
}
}

export interface MySqlBigIntConfig<
T extends 'number' | 'bigint' = 'number' | 'bigint',
T extends 'number' | 'bigint' | 'string' = 'number' | 'bigint' | 'string',
TUnsigned extends boolean | undefined = boolean | undefined,
> {
mode: T;
Expand All @@ -96,15 +137,22 @@ export interface MySqlBigIntConfig<

export function bigint<TMode extends MySqlBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
config: MySqlBigIntConfig<TMode, TUnsigned>,
): TMode extends 'number' ? MySqlBigInt53Builder<TUnsigned> : MySqlBigInt64Builder<TUnsigned>;
): TMode extends 'bigint' ? MySqlBigInt64Builder<TUnsigned>
: TMode extends 'string' ? MySqlBigIntStringBuilder<TUnsigned>
: MySqlBigInt53Builder<TUnsigned>;
export function bigint<TMode extends MySqlBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
name: string,
config: MySqlBigIntConfig<TMode, TUnsigned>,
): TMode extends 'number' ? MySqlBigInt53Builder<TUnsigned> : MySqlBigInt64Builder<TUnsigned>;
export function bigint(a?: string | MySqlBigIntConfig, b?: MySqlBigIntConfig) {
): TMode extends 'bigint' ? MySqlBigInt64Builder<TUnsigned>
: TMode extends 'string' ? MySqlBigIntStringBuilder<TUnsigned>
: MySqlBigInt53Builder<TUnsigned>;
export function bigint(a: string | MySqlBigIntConfig, b?: MySqlBigIntConfig) {
const { name, config } = getColumnNameAndConfig<MySqlBigIntConfig>(a, b);
if (config.mode === 'number') {
return new MySqlBigInt53Builder(name, config.unsigned);
}
if (config.mode === 'string') {
return new MySqlBigIntStringBuilder(name, config.unsigned);
}
return new MySqlBigInt64Builder(name, config.unsigned);
}
3 changes: 2 additions & 1 deletion drizzle-orm/src/mysql-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,7 +1220,8 @@ export class MySqlDialect {
case 'MySqlDecimal':
case 'MySqlDecimalNumber':
case 'MySqlDecimalBigInt':
case 'MySqlBigInt64': {
case 'MySqlBigInt64':
case 'MySqlBigIntString': {
return sql`cast(${name} as char) as ${sql.identifier(key)}`;
}

Expand Down
41 changes: 38 additions & 3 deletions drizzle-orm/src/pg-core/columns/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,56 @@ export class PgBigInt64<T extends ColumnBaseConfig<'bigint int64'>> extends PgCo
}
}

export interface PgBigIntConfig<T extends 'number' | 'bigint' = 'number' | 'bigint'> {
export class PgBigIntStringBuilder extends PgIntColumnBaseBuilder<{
name: string;
dataType: 'string int64';
data: string;
driverParam: string;
}> {
static override readonly [entityKind]: string = 'PgBigIntStringBuilder';

constructor(name: string) {
super(name, 'string int64', 'PgBigIntString');
}

/** @internal */
override build(table: PgTable<any>) {
return new PgBigIntString(table, this.config as any);
}
}

export class PgBigIntString<T extends ColumnBaseConfig<'string int64'>> extends PgColumn<T> {
static override readonly [entityKind]: string = 'PgBigIntString';

getSQLType(): string {
return 'bigint';
}

override mapFromDriverValue(value: string | number): string {
if (typeof value === 'string') return value;

return String(value);
}
}

export interface PgBigIntConfig<T extends 'number' | 'bigint' | 'string' = 'number' | 'bigint' | 'string'> {
mode: T;
}

export function bigint<TMode extends PgBigIntConfig['mode']>(
config: PgBigIntConfig<TMode>,
): TMode extends 'number' ? PgBigInt53Builder : PgBigInt64Builder;
): TMode extends 'string' ? PgBigIntStringBuilder : TMode extends 'bigint' ? PgBigInt64Builder : PgBigInt53Builder;
export function bigint<TMode extends PgBigIntConfig['mode']>(
name: string,
config: PgBigIntConfig<TMode>,
): TMode extends 'number' ? PgBigInt53Builder : PgBigInt64Builder;
): TMode extends 'string' ? PgBigIntStringBuilder : TMode extends 'bigint' ? PgBigInt64Builder : PgBigInt53Builder;
export function bigint(a: string | PgBigIntConfig, b?: PgBigIntConfig) {
const { name, config } = getColumnNameAndConfig<PgBigIntConfig>(a, b);
if (config.mode === 'number') {
return new PgBigInt53Builder(name);
}
if (config.mode === 'string') {
return new PgBigIntStringBuilder(name);
}
return new PgBigInt64Builder(name);
}
1 change: 1 addition & 0 deletions drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ export class PgDialect {
case 'PgNumericNumber':
case 'PgNumericBigInt':
case 'PgBigInt64':
case 'PgBigIntString':
case 'PgBigSerial64':
case 'PgTimestampString':
case 'PgGeometry':
Expand Down
47 changes: 44 additions & 3 deletions drizzle-orm/src/singlestore-core/columns/bigint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,42 @@ export class SingleStoreBigInt64<T extends ColumnBaseConfig<'bigint int64' | 'bi
}
}

export class SingleStoreBigIntStringBuilder<TUnsigned extends boolean | undefined>
extends SingleStoreColumnBuilderWithAutoIncrement<{
name: string;
dataType: Equal<TUnsigned, true> extends true ? 'string uint64' : 'string int64';
data: string;
driverParam: string;
}, { unsigned: boolean }>
{
static override readonly [entityKind]: string = 'SingleStoreBigIntStringBuilder';

constructor(name: string, unsigned: boolean = false) {
super(name, unsigned ? 'string uint64' : 'string int64' as any, 'SingleStoreBigIntString');
this.config.unsigned = unsigned;
}

/** @internal */
override build(table: SingleStoreTable) {
return new SingleStoreBigIntString(
table,
this.config as any,
);
}
}

export class SingleStoreBigIntString<T extends ColumnBaseConfig<'string int64' | 'string uint64'>>
extends SingleStoreColumnWithAutoIncrement<T, { unsigned: boolean }>
{
static override readonly [entityKind]: string = 'SingleStoreBigIntString';

getSQLType(): string {
return `bigint${this.config.unsigned ? ' unsigned' : ''}`;
}
}

export interface SingleStoreBigIntConfig<
T extends 'number' | 'bigint' = 'number' | 'bigint',
T extends 'number' | 'bigint' | 'string' = 'number' | 'bigint' | 'string',
TUnsigned extends boolean | undefined = boolean | undefined,
> {
mode: T;
Expand All @@ -94,15 +128,22 @@ export interface SingleStoreBigIntConfig<

export function bigint<TMode extends SingleStoreBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
config: SingleStoreBigIntConfig<TMode, TUnsigned>,
): TMode extends 'number' ? SingleStoreBigInt53Builder<TUnsigned> : SingleStoreBigInt64Builder<TUnsigned>;
): TMode extends 'string' ? SingleStoreBigIntStringBuilder<TUnsigned>
: TMode extends 'bigint' ? SingleStoreBigInt64Builder<TUnsigned>
: SingleStoreBigInt53Builder<TUnsigned>;
export function bigint<TMode extends SingleStoreBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
name: string,
config: SingleStoreBigIntConfig<TMode, TUnsigned>,
): TMode extends 'number' ? SingleStoreBigInt53Builder<TUnsigned> : SingleStoreBigInt64Builder<TUnsigned>;
): TMode extends 'string' ? SingleStoreBigIntStringBuilder<TUnsigned>
: TMode extends 'bigint' ? SingleStoreBigInt64Builder<TUnsigned>
: SingleStoreBigInt53Builder<TUnsigned>;
export function bigint(a?: string | SingleStoreBigIntConfig, b?: SingleStoreBigIntConfig) {
const { name, config } = getColumnNameAndConfig<SingleStoreBigIntConfig>(a, b);
if (config.mode === 'number') {
return new SingleStoreBigInt53Builder(name, config.unsigned);
}
if (config.mode === 'string') {
return new SingleStoreBigIntStringBuilder(name, config.unsigned);
}
return new SingleStoreBigInt64Builder(name, config.unsigned);
}
1 change: 1 addition & 0 deletions drizzle-orm/src/singlestore-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ export class SingleStoreDialect {
case 'SingleStoreDecimalNumber':
case 'SingleStoreDecimalBigInt':
case 'SingleStoreBigInt64':
case 'SingleStoreBigIntString':
case 'SingleStoreEnumColumn': {
return sql`cast(${name} as char) as ${sql.identifier(key)}`;
}
Expand Down
18 changes: 18 additions & 0 deletions drizzle-orm/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export type Writable<T> = {

export type NonArray<T> = T extends any[] ? never : T;

/**
* @deprecated
* Use `getColumns` instead
*/
export function getTableColumns<T extends Table>(table: T): T['_']['columns'] {
return table[Table.Symbol.Columns];
}
Expand All @@ -213,6 +217,20 @@ export function getViewSelectedFields<T extends View>(view: T): T['_']['selected
return view[ViewBaseConfig].selectedFields;
}

export function getColumns<T extends Table | View | Subquery>(
table: T,
): T extends Table ? T['_']['columns']
: T extends View ? T['_']['selectedFields']
: T extends Subquery ? T['_']['selectedFields']
: never
{
return (is(table, Table)
? table[Table.Symbol.Columns]
: is(table, View)
? table[ViewBaseConfig].selectedFields
: table._.selectedFields) as any;
}

/** @internal */
export function getTableLikeName(table: TableLike): string | undefined {
return is(table, Subquery)
Expand Down
5 changes: 3 additions & 2 deletions drizzle-orm/type-tests/mysql/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,9 @@ Expect<

{
mysqlTable('test', {
bigint: bigint('bigint', { mode: 'bigint' }),
number: bigint('number', { mode: 'number' }),
bigint: bigint('bigint', { mode: 'bigint' }).default(100n),
bigintNumber: bigint('bigint_number', { mode: 'number' }).default(100),
bigintString: bigint('bigint_string', { mode: 'string' }).default('100'),
date: date('date').default(new Date()),
date2: date('date2', { mode: 'date' }).default(new Date()),
date3: date('date3', { mode: 'string' }).default('2020-01-01'),
Expand Down
Loading