이 문서의 모든 예제는 데이터베이스 컬럼 이름 별칭을 사용하지 않으며, 컬럼 이름은 TypeScript 키에서 생성됩니다.
원한다면 컬럼 이름에 데이터베이스 별칭을 사용할 수 있으며, casing 파라미터를 사용하여 Drizzle의 매핑 전략을 정의할 수도 있습니다.
자세한 내용은 여기를 참조하세요.
모든 MySQL 컬럼 타입을 기본적으로 지원하며, 필요한 경우 **커스텀 타입**을 생성할 수 있습니다.
이 문서의 모든 예제는 데이터베이스 컬럼 이름 별칭을 사용하지 않으며, 컬럼 이름은 TypeScript 키에서 생성됩니다.
원한다면 컬럼 이름에 데이터베이스 별칭을 사용할 수 있으며, casing 파라미터를 사용하여 Drizzle의 매핑 전략을 정의할 수도 있습니다.
자세한 내용은 여기를 참조하세요.
부호 있는 정수형으로, 값의 크기에 따라 0, 1, 2, 3, 4, 6, 또는 8 바이트에 저장됩니다.
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int()
});CREATE TABLE `table` (
`int` int
);import { tinyint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
tinyint: tinyint()
});CREATE TABLE `table` (
`tinyint` tinyint
);import { smallint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
smallint: smallint()
});CREATE TABLE `table` (
`smallint` smallint
);import { mediumint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
mediumint: mediumint()
});CREATE TABLE `table` (
`mediumint` mediumint
);import { bigint, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
bigint: bigint({ mode: 'number' })
bigintUnsigned: bigint({ mode: 'number', unsigned: true })
});
bigint('...', { mode: 'number' | 'bigint' });
// You can also specify unsigned option for bigint
bigint('...', { mode: 'number' | 'bigint', unsigned: true })CREATE TABLE `table` (
`bigint` bigint,
`bigintUnsigned` bigint unsigned
);bigint(M)의 M 설정은 숫자 타입의 표시 너비를 나타내므로 생략했습니다.
import { real, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
real: real()
});CREATE TABLE `table` (
`real` real
);import { real, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
realPrecision: real({ precision: 1,}),
realPrecisionScale: real({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`realPrecision` real(1),
`realPrecisionScale` real(1, 1)
);import { decimal, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
decimal: decimal(),
decimalNum: decimal({ scale: 30, mode: 'number' }),
decimalBig: decimal({ scale: 30, mode: 'bigint' }),
});CREATE TABLE `table` (
`decimal` decimal,
`decimalNum` decimal(30),
`decimalBig` decimal(30)
);import { decimal, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
decimalPrecision: decimal({ precision: 1,}),
decimalPrecisionScale: decimal({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`decimalPrecision` decimal(1),
`decimalPrecisionScale` decimal(1, 1)
);import { double, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
double: double('double')
});CREATE TABLE `table` (
`double` double
);import { double, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
doublePrecision: double({ precision: 1,}),
doublePrecisionScale: double({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`doublePrecision` double(1),
`doublePrecisionScale` double(1, 1)
);import { float, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
float: float()
});CREATE TABLE `table` (
`float` float
);SERIAL은 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE의 별칭입니다.
import { serial, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
serial: serial()
});CREATE TABLE `table` (
`serial` serial AUTO_INCREMENT
);BINARY(M)은 정확히 M 바이트의 고정 길이 바이트 문자열을 저장합니다.
삽입 시 짧은 값은 M 바이트에 도달하도록 0x00 바이트로 오른쪽 패딩되며, 조회 시 패딩은 제거되지 않습니다.
후행 0x00을 포함한 모든 바이트는 비교, ORDER BY, DISTINCT에서 의미가 있습니다.
import { binary, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
binary: binary()
});CREATE TABLE `table` (
`binary` binary
);VARBINARY(M)은 최대 M 바이트의 가변 길이 바이트 문자열을 저장합니다.
삽입 시 짧은 값은 M 바이트에 도달하도록 0x00 바이트로 오른쪽 패딩되며, 조회 시 패딩은 제거되지 않습니다.
후행 0x00을 포함한 모든 바이트는 비교, ORDER BY, DISTINCT에서 의미가 있습니다.
import { varbinary, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
varbinary: varbinary({ length: 2}),
});CREATE TABLE `table` (
`varbinary` varbinary(2)
);drizzle-orm@1.0.0-beta.2부터 사용 가능
BLOB은 가변 크기의 데이터를 저장할 수 있는 이진 대형 객체입니다.
import { blob, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
blob: blob()
});CREATE TABLE `table` (
`blob` blob
);drizzle-orm@1.0.0-beta.2부터 사용 가능
TINYBLOB은 가변 크기의 데이터를 저장할 수 있는 이진 대형 객체입니다.
import { tinyblob, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
tinyblob: tinyblob()
});CREATE TABLE `table` (
`tinyblob` tinyblob
);drizzle-orm@1.0.0-beta.2부터 사용 가능
MEDIUMBLOB은 가변 크기의 데이터를 저장할 수 있는 이진 대형 객체입니다.
import { mediumblob, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
mediumblob: mediumblob()
});CREATE TABLE `table` (
`mediumblob` mediumblob
);drizzle-orm@1.0.0-beta.2부터 사용 가능
LONGBLOB은 가변 크기의 데이터를 저장할 수 있는 이진 대형 객체입니다.
import { longblob, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
longblob: longblob()
});CREATE TABLE `table` (
`longblob` longblob
);import { char, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
char: char(),
});CREATE TABLE `table` (
`char` char
);{ enum: ["value1", "value2"] } 설정을 정의하여 insert와 select 타입을 추론할 수 있으며, 런타임 값은 검증하지 않습니다.
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
varchar: varchar({ length: 2 }),
});
// will be inferred as text: "value1" | "value2" | null
varchar: varchar({ length: 6, enum: ["value1", "value2"] })CREATE TABLE `table` (
`varchar` varchar(2)
);{ enum: ["value1", "value2"] } 설정을 정의하여 insert와 select 타입을 추론할 수 있으며, 런타임 값은 검증하지 않습니다.
import { text, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
text: text(),
});
// will be inferred as text: "value1" | "value2" | null
text: text({ enum: ["value1", "value2"] });CREATE TABLE `table` (
`text` text
);import { boolean, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
boolean: boolean(),
});CREATE TABLE `table` (
`boolean` boolean
);import { boolean, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
date: date(),
});CREATE TABLE `table` (
`date` date
);import { datetime, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
datetime: datetime(),
});
datetime('...', { mode: 'date' | "string"}),
datetime('...', { fsp : 0..6}),CREATE TABLE `table` (
`datetime` datetime
);import { datetime, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
datetime: datetime({ mode: 'date', fsp: 6 }),
});CREATE TABLE `table` (
`datetime` datetime(6)
);import { time, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
time: time(),
timefsp: time({ fsp: 6 }),
});
time('...', { fsp: 0..6 }),CREATE TABLE `table` (
`time` time,
`timefsp` time(6)
);import { year, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
year: year(),
});CREATE TABLE `table` (
`year` year
);import { timestamp, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
timestamp: timestamp(),
});
timestamp('...', { mode: 'date' | "string"}),
timestamp('...', { fsp : 0..6}),CREATE TABLE `table` (
`timestamp` timestamp
);import { timestamp, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
timestamp: timestamp({ mode: 'date', fsp: 6 }),
});CREATE TABLE `table` (
`timestamp` timestamp(6)
);import { timestamp, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
timestamp: timestamp().defaultNow(),
});CREATE TABLE `table` (
`timestamp` timestamp DEFAULT (now())
);import { json, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
json: json(),
});
CREATE TABLE `table` (
`json` json
);JSON 객체 추론을 위해 .$type<..>()을 지정할 수 있으며, 런타임 값은 검증하지 않습니다.
기본값, 삽입 및 조회 스키마에 대한 컴파일 타임 보호를 제공합니다.
// will be inferred as { foo: string }
json: json().$type<{ foo: string }>();
// will be inferred as string[]
json: json().$type<string[]>();
// won't compile
json: json().$type<string[]>().default({});import { mysqlEnum, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
popularity: mysqlEnum(['unknown', 'known', 'popular']),
});CREATE TABLE `table` (
`popularity` enum('unknown','known','popular')
);모든 컬럼 빌더는 .$type() 메서드를 가지고 있으며, 이를 통해 컬럼의 데이터 타입을 커스터마이징할 수 있습니다. 예를 들어 unknown 타입이나 브랜드 타입을 사용할 때 유용합니다.
type UserId = number & { __brand: 'user_id' };
type Data = {
foo: string;
bar: number;
};
const users = mysqlTable('users', {
id: int().$type<UserId>().primaryKey(),
jsonField: json().$type<Data>(),
});NOT NULL 제약조건은 해당 컬럼이 NULL 값을 포함할 수 없음을 나타냅니다.
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().notNull(),
});CREATE TABLE `table` (
`int` int NOT NULL
);DEFAULT 절은 INSERT 시 사용자가 명시적으로 값을 제공하지 않을 때 컬럼에 사용할 기본값을 지정합니다.
컬럼 정의에 명시적인 DEFAULT 절이 없으면 컬럼의 기본값은 NULL입니다.
명시적인 DEFAULT 절은 기본값이 NULL, 문자열 상수, blob 상수, 부호 있는 숫자 또는 괄호로 묶인 상수 표현식일 수 있음을 지정할 수 있습니다.
import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().default(3),
});CREATE TABLE `table` (
`int` int DEFAULT 3
);동일한 함수의 다른 별칭인 $default() 또는 $defaultFn()을 사용하면 런타임에 기본값을 생성하고 모든 삽입 쿼리에서 이 값을 사용할 수 있습니다.
이 함수들은 uuid, cuid, cuid2 등 다양한 구현을 활용하는 데 도움이 됩니다.
참고: 이 값은 drizzle-kit 동작에 영향을 주지 않으며, drizzle-orm에서 런타임에만 사용됩니다
import { varchar, mysqlTable } from "drizzle-orm/mysql-core";
import { createId } from '@paralleldrive/cuid2';
const table = mysqlTable('table', {
id: varchar({ length: 128 }).$defaultFn(() => createId()),
});동일한 함수의 다른 별칭인 $onUpdate() 또는 $onUpdateFn()을 사용하면 런타임에 기본값을 생성하고 모든 업데이트 쿼리에서 이 값을 사용할 수 있습니다.
컬럼에 동적 업데이트 값을 추가합니다. 행이 업데이트될 때 함수가 호출되며, 값이 제공되지 않으면 반환된 값이 컬럼 값으로 사용됩니다. 기본값(또는 $defaultFn)이 제공되지 않으면 행이 삽입될 때도 함수가 호출되며, 반환된 값이 컬럼 값으로 사용됩니다.
참고: 이 값은 drizzle-kit 동작에 영향을 주지 않으며, drizzle-orm에서 런타임에만 사용됩니다
import { text, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
alwaysNull: text().$type<string | null>().$onUpdate(() => null),
});import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().primaryKey(),
});CREATE TABLE `table` (
`int` int PRIMARY KEY NOT NULL
);import { int, mysqlTable } from "drizzle-orm/mysql-core";
const table = mysqlTable('table', {
int: int().autoincrement(),
});CREATE TABLE `table` (
`int` int AUTO_INCREMENT
);