forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed type-level enum overwrite of arrays (fixes drizzle-team#1110), …
…added related type tests, removed unnecessary or repeating checks
- Loading branch information
1 parent
e0aaeb2
commit 3461c4e
Showing
6 changed files
with
369 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { | ||
bigint, | ||
binary, | ||
boolean, | ||
char, | ||
customType, | ||
date, | ||
datetime, | ||
decimal, | ||
double, | ||
float, | ||
int, | ||
json, | ||
longtext, | ||
mediumint, | ||
mediumtext, | ||
mysqlEnum, | ||
mysqlTable, | ||
real, | ||
serial, | ||
smallint, | ||
text, | ||
time, | ||
timestamp, | ||
tinyint, | ||
tinytext, | ||
varbinary, | ||
varchar, | ||
year, | ||
} from 'drizzle-orm/mysql-core'; | ||
import type { Output } from 'valibot'; | ||
import { createInsertSchema, createSelectSchema } from '../src'; | ||
import { type Equal, Expect } from './utils'; | ||
|
||
const customInt = customType<{ data: number }>({ | ||
dataType() { | ||
return 'int'; | ||
}, | ||
}); | ||
|
||
const testTable = mysqlTable('test', { | ||
bigint: bigint('bigint', { mode: 'bigint' }).notNull(), | ||
bigintNumber: bigint('bigintNumber', { mode: 'number' }).notNull(), | ||
binary: binary('binary').notNull(), | ||
boolean: boolean('boolean').notNull(), | ||
char: char('char', { length: 4 }).notNull(), | ||
charEnum: char('char', { enum: ['a', 'b', 'c'] }).notNull(), | ||
customInt: customInt('customInt').notNull(), | ||
date: date('date').notNull(), | ||
dateString: date('dateString', { mode: 'string' }).notNull(), | ||
datetime: datetime('datetime').notNull(), | ||
datetimeString: datetime('datetimeString', { mode: 'string' }).notNull(), | ||
decimal: decimal('decimal').notNull(), | ||
double: double('double').notNull(), | ||
enum: mysqlEnum('enum', ['a', 'b', 'c']).notNull(), | ||
float: float('float').notNull(), | ||
int: int('int').notNull(), | ||
json: json('json').notNull(), | ||
mediumint: mediumint('mediumint').notNull(), | ||
real: real('real').notNull(), | ||
serial: serial('serial').notNull(), | ||
smallint: smallint('smallint').notNull(), | ||
text: text('text').notNull(), | ||
textEnum: text('textEnum', { enum: ['a', 'b', 'c'] }).notNull(), | ||
tinytext: tinytext('tinytext').notNull(), | ||
tinytextEnum: tinytext('tinytextEnum', { enum: ['a', 'b', 'c'] }).notNull(), | ||
mediumtext: mediumtext('mediumtext').notNull(), | ||
mediumtextEnum: mediumtext('mediumtextEnum', { | ||
enum: ['a', 'b', 'c'], | ||
}).notNull(), | ||
longtext: longtext('longtext').notNull(), | ||
longtextEnum: longtext('longtextEnum', { enum: ['a', 'b', 'c'] }).notNull(), | ||
time: time('time').notNull(), | ||
timestamp: timestamp('timestamp').notNull(), | ||
timestampString: timestamp('timestampString', { mode: 'string' }).notNull(), | ||
tinyint: tinyint('tinyint').notNull(), | ||
varbinary: varbinary('varbinary', { length: 200 }).notNull(), | ||
varchar: varchar('varchar', { length: 200 }).notNull(), | ||
varcharEnum: varchar('varcharEnum', { | ||
length: 1, | ||
enum: ['a', 'b', 'c'], | ||
}).notNull(), | ||
year: year('year').notNull(), | ||
autoIncrement: int('autoIncrement').notNull().autoincrement(), | ||
}); | ||
|
||
const insertSchema = createInsertSchema(testTable); | ||
const selectSchema = createSelectSchema(testTable); | ||
|
||
type InsertType = Output<typeof insertSchema>; | ||
type SelectType = Output<typeof selectSchema>; | ||
|
||
Expect< | ||
Equal<InsertType, { | ||
bigint: bigint; | ||
boolean: boolean; | ||
json: string | number | boolean | any[] | { | ||
[x: string]: any; | ||
} | null; | ||
date: Date; | ||
bigintNumber: number; | ||
binary: string; | ||
char: string; | ||
charEnum: 'a' | 'b' | 'c'; | ||
int: number; | ||
customInt: any; | ||
dateString: string; | ||
datetime: Date; | ||
datetimeString: string; | ||
decimal: string; | ||
double: number; | ||
enum: 'a' | 'b' | 'c'; | ||
float: number; | ||
mediumint: number; | ||
real: number; | ||
smallint: number; | ||
text: string; | ||
tinytext: string; | ||
mediumtext: string; | ||
longtext: string; | ||
textEnum: 'a' | 'b' | 'c'; | ||
tinytextEnum: 'a' | 'b' | 'c'; | ||
mediumtextEnum: 'a' | 'b' | 'c'; | ||
longtextEnum: 'a' | 'b' | 'c'; | ||
time: string; | ||
timestamp: Date; | ||
timestampString: string; | ||
tinyint: number; | ||
varbinary: string; | ||
varchar: string; | ||
varcharEnum: 'a' | 'b' | 'c'; | ||
year: number; | ||
serial?: number; | ||
autoIncrement?: number; | ||
}> | ||
>; | ||
|
||
Expect< | ||
Equal<SelectType, { | ||
bigint: bigint; | ||
boolean: boolean; | ||
enum: 'a' | 'b' | 'c'; | ||
json: string | number | boolean | any[] | { | ||
[x: string]: any; | ||
} | null; | ||
date: Date; | ||
bigintNumber: number; | ||
binary: string; | ||
char: string; | ||
charEnum: 'a' | 'b' | 'c'; | ||
int: number; | ||
customInt: any; | ||
dateString: string; | ||
datetime: Date; | ||
datetimeString: string; | ||
decimal: string; | ||
double: number; | ||
float: number; | ||
mediumint: number; | ||
real: number; | ||
serial: number; | ||
smallint: number; | ||
text: string; | ||
tinytext: string; | ||
mediumtext: string; | ||
longtext: string; | ||
textEnum: 'a' | 'b' | 'c'; | ||
tinytextEnum: 'a' | 'b' | 'c'; | ||
mediumtextEnum: 'a' | 'b' | 'c'; | ||
longtextEnum: 'a' | 'b' | 'c'; | ||
time: string; | ||
timestamp: Date; | ||
timestampString: string; | ||
tinyint: number; | ||
varbinary: string; | ||
varchar: string; | ||
varcharEnum: 'a' | 'b' | 'c'; | ||
year: number; | ||
autoIncrement: number; | ||
}> | ||
>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { char, date, integer, pgEnum, pgTable, serial, text, timestamp, varchar } from 'drizzle-orm/pg-core'; | ||
import type { Output } from 'valibot'; | ||
import { createInsertSchema, createSelectSchema } from '../src'; | ||
import { type Equal, Expect } from './utils'; | ||
|
||
export const roleEnum = pgEnum('role', ['admin', 'user']); | ||
|
||
const testTable = pgTable('users', { | ||
intArr: integer('int_arr').array(), | ||
strArr: text('str_arr').array(), | ||
id: serial('id').primaryKey(), | ||
name: text('name'), | ||
email: text('email').notNull(), | ||
birthdayString: date('birthday_string').notNull(), | ||
birthdayDate: date('birthday_date', { mode: 'date' }).notNull(), | ||
createdAt: timestamp('created_at').notNull().defaultNow(), | ||
role: roleEnum('role').notNull(), | ||
roleText: text('role1', { enum: ['admin', 'user'] }).notNull(), | ||
roleText2: text('role2', { enum: ['admin', 'user'] }) | ||
.notNull() | ||
.default('user'), | ||
profession: varchar('profession', { length: 20 }).notNull(), | ||
initials: char('initials', { length: 2 }).notNull(), | ||
}); | ||
|
||
const insertSchema = createInsertSchema(testTable); | ||
const selectSchema = createSelectSchema(testTable); | ||
|
||
type InsertType = Output<typeof insertSchema>; | ||
type SelectType = Output<typeof selectSchema>; | ||
|
||
Expect< | ||
Equal<InsertType, { | ||
role: 'admin' | 'user'; | ||
email: string; | ||
birthdayString: string; | ||
birthdayDate: Date; | ||
roleText: 'admin' | 'user'; | ||
profession: string; | ||
initials: string; | ||
intArr?: number[] | null; | ||
strArr?: string[] | null; | ||
id?: number; | ||
name?: string | null; | ||
createdAt?: Date; | ||
roleText2?: 'admin' | 'user'; | ||
}> | ||
>; | ||
|
||
Expect< | ||
Equal<SelectType, { | ||
role: 'admin' | 'user'; | ||
intArr: number[] | null; | ||
strArr: string[] | null; | ||
id: number; | ||
name: string | null; | ||
email: string; | ||
birthdayString: string; | ||
birthdayDate: Date; | ||
createdAt: Date; | ||
roleText: 'admin' | 'user'; | ||
roleText2: 'admin' | 'user'; | ||
profession: string; | ||
initials: string; | ||
}> | ||
>; |
Oops, something went wrong.