Skip to content

Commit 7722e6a

Browse files
Sukairo-02RomanNabukhotnyiAndriiShermandankochetovAngelelz
authored
Updates for beta branch (#5000)
- Added string modes for SingleStore, PostgreSQL, MySQL bigint columns - Added getColumns util function to get columns\selected fields from tables, views and subqueries --------- Co-authored-by: Roman Nabukhotnyi <[email protected]> Co-authored-by: Andrii Sherman <[email protected]> Co-authored-by: Dan Kochetov <[email protected]> Co-authored-by: Angelelz <[email protected]>
1 parent 7275360 commit 7722e6a

37 files changed

+4451
-3671
lines changed

dprint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"**/_journal.json",
2323
"**/tsup.config*.mjs",
2424
"**/.sst",
25+
"rollup.config-*.mjs",
2526
"integration-tests/tests/prisma/*/client",
2627
"integration-tests/tests/prisma/*/drizzle"
2728
],

drizzle-arktype/tests/mysql.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ test('all data types', (t) => {
359359
bigint2: bigint({ mode: 'bigint' }).notNull(),
360360
bigint3: bigint({ unsigned: true, mode: 'number' }).notNull(),
361361
bigint4: bigint({ unsigned: true, mode: 'bigint' }).notNull(),
362+
bigint5: bigint({ mode: 'string' }).notNull(),
363+
bigint6: bigint({ unsigned: true, mode: 'string' }).notNull(),
362364
binary: binary({ length: 10 }).notNull(),
363365
boolean: boolean().notNull(),
364366
char1: char({ length: 10 }).notNull(),
@@ -408,7 +410,11 @@ test('all data types', (t) => {
408410
bigint2: type.bigint.narrow(bigintNarrow),
409411
bigint3: type.keywords.number.integer.atLeast(0).atMost(Number.MAX_SAFE_INTEGER),
410412
bigint4: type.bigint.narrow(unsignedBigintNarrow),
411-
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`),
413+
bigint5: type.string,
414+
bigint6: type.string,
415+
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`) as Type<
416+
string
417+
>,
412418
boolean: type.boolean,
413419
char1: type.string.exactlyLength(10),
414420
char2: type.enumerated('a', 'b', 'c'),
@@ -443,7 +449,7 @@ test('all data types', (t) => {
443449
varchar2: type.enumerated('a', 'b', 'c'),
444450
varbinary: type(`/^[01]{0,10}$/`).describe(
445451
`a string containing ones or zeros while being up to 10 characters long`,
446-
),
452+
) as Type<string>,
447453
year: type.keywords.number.integer.atLeast(1901).atMost(2155),
448454
longtext1: type.string.atMostLength(CONSTANTS.INT32_UNSIGNED_MAX),
449455
longtext2: type.enumerated('a', 'b', 'c'),

drizzle-arktype/tests/pg.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ test('all data types', (t) => {
407407
}) => ({
408408
bigint1: bigint({ mode: 'number' }).notNull(),
409409
bigint2: bigint({ mode: 'bigint' }).notNull(),
410+
bigint3: bigint({ mode: 'string' }).notNull(),
410411
bigserial1: bigserial({ mode: 'number' }).notNull(),
411412
bigserial2: bigserial({ mode: 'bigint' }).notNull(),
412413
bit: bit({ dimensions: 5 }).notNull(),
@@ -455,6 +456,7 @@ test('all data types', (t) => {
455456
const expected = type({
456457
bigint1: type.keywords.number.integer.atLeast(Number.MIN_SAFE_INTEGER).atMost(Number.MAX_SAFE_INTEGER),
457458
bigint2: type.bigint.narrow(bigintNarrow),
459+
bigint3: type.string,
458460
bigserial1: type.keywords.number.integer.atLeast(Number.MIN_SAFE_INTEGER).atMost(Number.MAX_SAFE_INTEGER),
459461
bigserial2: type.bigint.narrow(bigintNarrow),
460462
bit: type(/^[01]{5}$/).describe('a string containing ones or zeros while being 5 characters long'),

drizzle-arktype/tests/singlestore.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,8 @@ test('all data types', (t) => {
361361
bigint2: bigint({ mode: 'bigint' }).notNull(),
362362
bigint3: bigint({ unsigned: true, mode: 'number' }).notNull(),
363363
bigint4: bigint({ unsigned: true, mode: 'bigint' }).notNull(),
364+
bigint5: bigint({ mode: 'string' }).notNull(),
365+
bigint6: bigint({ unsigned: true, mode: 'string' }).notNull(),
364366
binary: binary({ length: 10 }).notNull(),
365367
boolean: boolean().notNull(),
366368
char1: char({ length: 10 }).notNull(),
@@ -410,7 +412,11 @@ test('all data types', (t) => {
410412
bigint2: type.bigint.narrow(bigintNarrow),
411413
bigint3: type.keywords.number.integer.atLeast(0).atMost(Number.MAX_SAFE_INTEGER),
412414
bigint4: type.bigint.narrow(unsignedBigintNarrow),
413-
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`),
415+
bigint5: type.string,
416+
bigint6: type.string,
417+
binary: type(`/^[01]{10}$/`).describe(`a string containing ones or zeros while being 10 characters long`) as Type<
418+
string
419+
>,
414420
boolean: type.boolean,
415421
char1: type.string.exactlyLength(10),
416422
char2: type.enumerated('a', 'b', 'c'),
@@ -445,7 +451,7 @@ test('all data types', (t) => {
445451
varchar2: type.enumerated('a', 'b', 'c'),
446452
varbinary: type(`/^[01]{0,10}$/`).describe(
447453
`a string containing ones or zeros while being up to 10 characters long`,
448-
),
454+
) as Type<string>,
449455
year: type.keywords.number.integer.atLeast(1901).atMost(2155),
450456
longtext1: type.string.atMostLength(CONSTANTS.INT32_UNSIGNED_MAX),
451457
longtext2: type.enumerated('a', 'b', 'c'),

drizzle-orm/src/column-builder.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ export type ColumnDataStringConstraint =
6969
| 'datetime'
7070
| 'enum'
7171
| 'inet'
72+
| 'int64'
7273
| 'interval'
7374
| 'macaddr'
7475
| 'macaddr8'
7576
| 'numeric'
7677
| 'sparsevec'
7778
| 'time'
7879
| 'timestamp'
80+
| 'uint64'
7981
| 'unumeric'
8082
| 'uuid';
8183

drizzle-orm/src/mysql-core/columns/bigint.ts

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,49 @@ export class MySqlBigInt64<T extends ColumnBaseConfig<'bigint int64' | 'bigint u
8686
}
8787
}
8888

89+
export class MySqlBigIntStringBuilder<TUnsigned extends boolean | undefined>
90+
extends MySqlColumnBuilderWithAutoIncrement<{
91+
name: string;
92+
dataType: Equal<TUnsigned, true> extends true ? 'string uint64' : 'string int64';
93+
data: string;
94+
driverParam: number | string;
95+
}, { unsigned?: boolean }>
96+
{
97+
static override readonly [entityKind]: string = 'MySqlBigIntStringBuilder';
98+
99+
constructor(name: string, unsigned: boolean = false) {
100+
super(name, unsigned ? 'string uint64' : 'string int64' as any, 'MySqlBigIntString');
101+
this.config.unsigned = unsigned as TUnsigned;
102+
}
103+
104+
/** @internal */
105+
override build(table: MySqlTable) {
106+
return new MySqlBigIntString(
107+
table,
108+
this.config as any,
109+
);
110+
}
111+
}
112+
113+
export class MySqlBigIntString<T extends ColumnBaseConfig<'string int64' | 'string uint64'>>
114+
extends MySqlColumnWithAutoIncrement<T, { unsigned: boolean }>
115+
{
116+
static override readonly [entityKind]: string = 'MySqlBigIntString';
117+
118+
getSQLType(): string {
119+
return `bigint${this.config.unsigned ? ' unsigned' : ''}`;
120+
}
121+
122+
override mapFromDriverValue(value: number | string): string {
123+
if (typeof value === 'string') {
124+
return value;
125+
}
126+
return String(value);
127+
}
128+
}
129+
89130
export interface MySqlBigIntConfig<
90-
T extends 'number' | 'bigint' = 'number' | 'bigint',
131+
T extends 'number' | 'bigint' | 'string' = 'number' | 'bigint' | 'string',
91132
TUnsigned extends boolean | undefined = boolean | undefined,
92133
> {
93134
mode: T;
@@ -96,15 +137,22 @@ export interface MySqlBigIntConfig<
96137

97138
export function bigint<TMode extends MySqlBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
98139
config: MySqlBigIntConfig<TMode, TUnsigned>,
99-
): TMode extends 'number' ? MySqlBigInt53Builder<TUnsigned> : MySqlBigInt64Builder<TUnsigned>;
140+
): TMode extends 'bigint' ? MySqlBigInt64Builder<TUnsigned>
141+
: TMode extends 'string' ? MySqlBigIntStringBuilder<TUnsigned>
142+
: MySqlBigInt53Builder<TUnsigned>;
100143
export function bigint<TMode extends MySqlBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
101144
name: string,
102145
config: MySqlBigIntConfig<TMode, TUnsigned>,
103-
): TMode extends 'number' ? MySqlBigInt53Builder<TUnsigned> : MySqlBigInt64Builder<TUnsigned>;
104-
export function bigint(a?: string | MySqlBigIntConfig, b?: MySqlBigIntConfig) {
146+
): TMode extends 'bigint' ? MySqlBigInt64Builder<TUnsigned>
147+
: TMode extends 'string' ? MySqlBigIntStringBuilder<TUnsigned>
148+
: MySqlBigInt53Builder<TUnsigned>;
149+
export function bigint(a: string | MySqlBigIntConfig, b?: MySqlBigIntConfig) {
105150
const { name, config } = getColumnNameAndConfig<MySqlBigIntConfig>(a, b);
106151
if (config.mode === 'number') {
107152
return new MySqlBigInt53Builder(name, config.unsigned);
108153
}
154+
if (config.mode === 'string') {
155+
return new MySqlBigIntStringBuilder(name, config.unsigned);
156+
}
109157
return new MySqlBigInt64Builder(name, config.unsigned);
110158
}

drizzle-orm/src/mysql-core/dialect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1220,7 +1220,8 @@ export class MySqlDialect {
12201220
case 'MySqlDecimal':
12211221
case 'MySqlDecimalNumber':
12221222
case 'MySqlDecimalBigInt':
1223-
case 'MySqlBigInt64': {
1223+
case 'MySqlBigInt64':
1224+
case 'MySqlBigIntString': {
12241225
return sql`cast(${name} as char) as ${sql.identifier(key)}`;
12251226
}
12261227

drizzle-orm/src/pg-core/columns/bigint.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,56 @@ export class PgBigInt64<T extends ColumnBaseConfig<'bigint int64'>> extends PgCo
6969
}
7070
}
7171

72-
export interface PgBigIntConfig<T extends 'number' | 'bigint' = 'number' | 'bigint'> {
72+
export class PgBigIntStringBuilder extends PgIntColumnBaseBuilder<{
73+
name: string;
74+
dataType: 'string int64';
75+
data: string;
76+
driverParam: string;
77+
}> {
78+
static override readonly [entityKind]: string = 'PgBigIntStringBuilder';
79+
80+
constructor(name: string) {
81+
super(name, 'string int64', 'PgBigIntString');
82+
}
83+
84+
/** @internal */
85+
override build(table: PgTable<any>) {
86+
return new PgBigIntString(table, this.config as any);
87+
}
88+
}
89+
90+
export class PgBigIntString<T extends ColumnBaseConfig<'string int64'>> extends PgColumn<T> {
91+
static override readonly [entityKind]: string = 'PgBigIntString';
92+
93+
getSQLType(): string {
94+
return 'bigint';
95+
}
96+
97+
override mapFromDriverValue(value: string | number): string {
98+
if (typeof value === 'string') return value;
99+
100+
return String(value);
101+
}
102+
}
103+
104+
export interface PgBigIntConfig<T extends 'number' | 'bigint' | 'string' = 'number' | 'bigint' | 'string'> {
73105
mode: T;
74106
}
75107

76108
export function bigint<TMode extends PgBigIntConfig['mode']>(
77109
config: PgBigIntConfig<TMode>,
78-
): TMode extends 'number' ? PgBigInt53Builder : PgBigInt64Builder;
110+
): TMode extends 'string' ? PgBigIntStringBuilder : TMode extends 'bigint' ? PgBigInt64Builder : PgBigInt53Builder;
79111
export function bigint<TMode extends PgBigIntConfig['mode']>(
80112
name: string,
81113
config: PgBigIntConfig<TMode>,
82-
): TMode extends 'number' ? PgBigInt53Builder : PgBigInt64Builder;
114+
): TMode extends 'string' ? PgBigIntStringBuilder : TMode extends 'bigint' ? PgBigInt64Builder : PgBigInt53Builder;
83115
export function bigint(a: string | PgBigIntConfig, b?: PgBigIntConfig) {
84116
const { name, config } = getColumnNameAndConfig<PgBigIntConfig>(a, b);
85117
if (config.mode === 'number') {
86118
return new PgBigInt53Builder(name);
87119
}
120+
if (config.mode === 'string') {
121+
return new PgBigIntStringBuilder(name);
122+
}
88123
return new PgBigInt64Builder(name);
89124
}

drizzle-orm/src/pg-core/dialect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,7 @@ export class PgDialect {
970970
case 'PgNumericNumber':
971971
case 'PgNumericBigInt':
972972
case 'PgBigInt64':
973+
case 'PgBigIntString':
973974
case 'PgBigSerial64':
974975
case 'PgTimestampString':
975976
case 'PgGeometry':

drizzle-orm/src/singlestore-core/columns/bigint.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,42 @@ export class SingleStoreBigInt64<T extends ColumnBaseConfig<'bigint int64' | 'bi
8484
}
8585
}
8686

87+
export class SingleStoreBigIntStringBuilder<TUnsigned extends boolean | undefined>
88+
extends SingleStoreColumnBuilderWithAutoIncrement<{
89+
name: string;
90+
dataType: Equal<TUnsigned, true> extends true ? 'string uint64' : 'string int64';
91+
data: string;
92+
driverParam: string;
93+
}, { unsigned: boolean }>
94+
{
95+
static override readonly [entityKind]: string = 'SingleStoreBigIntStringBuilder';
96+
97+
constructor(name: string, unsigned: boolean = false) {
98+
super(name, unsigned ? 'string uint64' : 'string int64' as any, 'SingleStoreBigIntString');
99+
this.config.unsigned = unsigned;
100+
}
101+
102+
/** @internal */
103+
override build(table: SingleStoreTable) {
104+
return new SingleStoreBigIntString(
105+
table,
106+
this.config as any,
107+
);
108+
}
109+
}
110+
111+
export class SingleStoreBigIntString<T extends ColumnBaseConfig<'string int64' | 'string uint64'>>
112+
extends SingleStoreColumnWithAutoIncrement<T, { unsigned: boolean }>
113+
{
114+
static override readonly [entityKind]: string = 'SingleStoreBigIntString';
115+
116+
getSQLType(): string {
117+
return `bigint${this.config.unsigned ? ' unsigned' : ''}`;
118+
}
119+
}
120+
87121
export interface SingleStoreBigIntConfig<
88-
T extends 'number' | 'bigint' = 'number' | 'bigint',
122+
T extends 'number' | 'bigint' | 'string' = 'number' | 'bigint' | 'string',
89123
TUnsigned extends boolean | undefined = boolean | undefined,
90124
> {
91125
mode: T;
@@ -94,15 +128,22 @@ export interface SingleStoreBigIntConfig<
94128

95129
export function bigint<TMode extends SingleStoreBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
96130
config: SingleStoreBigIntConfig<TMode, TUnsigned>,
97-
): TMode extends 'number' ? SingleStoreBigInt53Builder<TUnsigned> : SingleStoreBigInt64Builder<TUnsigned>;
131+
): TMode extends 'string' ? SingleStoreBigIntStringBuilder<TUnsigned>
132+
: TMode extends 'bigint' ? SingleStoreBigInt64Builder<TUnsigned>
133+
: SingleStoreBigInt53Builder<TUnsigned>;
98134
export function bigint<TMode extends SingleStoreBigIntConfig['mode'], TUnsigned extends boolean | undefined>(
99135
name: string,
100136
config: SingleStoreBigIntConfig<TMode, TUnsigned>,
101-
): TMode extends 'number' ? SingleStoreBigInt53Builder<TUnsigned> : SingleStoreBigInt64Builder<TUnsigned>;
137+
): TMode extends 'string' ? SingleStoreBigIntStringBuilder<TUnsigned>
138+
: TMode extends 'bigint' ? SingleStoreBigInt64Builder<TUnsigned>
139+
: SingleStoreBigInt53Builder<TUnsigned>;
102140
export function bigint(a?: string | SingleStoreBigIntConfig, b?: SingleStoreBigIntConfig) {
103141
const { name, config } = getColumnNameAndConfig<SingleStoreBigIntConfig>(a, b);
104142
if (config.mode === 'number') {
105143
return new SingleStoreBigInt53Builder(name, config.unsigned);
106144
}
145+
if (config.mode === 'string') {
146+
return new SingleStoreBigIntStringBuilder(name, config.unsigned);
147+
}
107148
return new SingleStoreBigInt64Builder(name, config.unsigned);
108149
}

0 commit comments

Comments
 (0)