이 문서의 모든 예제는 데이터베이스 컬럼명 별칭을 사용하지 않으며, 컬럼명은 TypeScript 키로부터 생성됩니다.
원한다면 컬럼명에 데이터베이스 별칭을 사용할 수 있으며, casing 파라미터를 사용하여 Drizzle의 매핑 전략을 정의할 수도 있습니다.
자세한 내용은 여기에서 확인할 수 있습니다.
모든 타입을 기본적으로 지원하지만, 충분하지 않다면 **커스텀 타입**을 자유롭게 생성할 수 있습니다.
이 문서의 모든 예제는 데이터베이스 컬럼명 별칭을 사용하지 않으며, 컬럼명은 TypeScript 키로부터 생성됩니다.
원한다면 컬럼명에 데이터베이스 별칭을 사용할 수 있으며, casing 파라미터를 사용하여 Drizzle의 매핑 전략을 정의할 수도 있습니다.
자세한 내용은 여기에서 확인할 수 있습니다.
부호 있는 정수로, 값의 크기에 따라 0, 1, 2, 3, 4, 6, 또는 8 바이트에 저장됩니다.
import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int()
});CREATE TABLE `table` (
`int` int
);import { tinyint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
tinyint: tinyint()
});CREATE TABLE `table` (
`tinyint` tinyint
);import { smallint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
smallint: smallint()
});CREATE TABLE `table` (
`smallint` smallint
);import { mediumint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
mediumint: mediumint()
});CREATE TABLE `table` (
`mediumint` mediumint
);import { bigint, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
bigint: bigint({ mode: 'number' })
bigintUnsigned: bigint({ mode: 'number', unsigned: true })
});
bigint('...', { mode: 'number' | 'bigint' });
// bigint에 unsigned 옵션을 지정할 수도 있습니다
bigint('...', { mode: 'number' | 'bigint', unsigned: true })CREATE TABLE `table` (
`bigint` bigint,
`bigintUnsigned` bigint unsigned
);bigint(M)의 M 설정은 생략했습니다. 이는 숫자 타입의 표시 너비를 나타내기 때문입니다.
import { real, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
real: real()
});CREATE TABLE `table` (
`real` real
);import { real, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
realPrecision: real({ precision: 1,}),
realPrecisionScale: real({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`realPrecision` real(1),
`realPrecisionScale` real(1, 1)
);import { decimal, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('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, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
decimalPrecision: decimal({ precision: 1,}),
decimalPrecisionScale: decimal({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`decimalPrecision` decimal(1),
`decimalPrecisionScale` decimal(1, 1)
);import { double, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
double: double('double')
});CREATE TABLE `table` (
`double` double
);import { double, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
doublePrecision: double({ precision: 1,}),
doublePrecisionScale: double({ precision: 1, scale: 1,}),
});CREATE TABLE `table` (
`doublePrecision` double(1),
`doublePrecisionScale` double(1, 1)
);import { float, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
float: float()
});CREATE TABLE `table` (
`float` float
);SERIAL은 BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE의 별칭입니다.
import { serial, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
serial: serial()
});CREATE TABLE `table` (
`serial` serial AUTO_INCREMENT
);import { binary, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
binary: binary()
});CREATE TABLE `table` (
`binary` binary
);import { varbinary, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
varbinary: varbinary({ length: 2}),
});CREATE TABLE `table` (
`varbinary` varbinary(2)
);import { char, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
char: char(),
});CREATE TABLE `table` (
`char` char
);{ enum: ["value1", "value2"] } 설정을 정의하여 insert와 select 타입을 추론할 수 있습니다. 런타임 값은 검증하지 않습니다.
import { varchar, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
varchar: varchar({ length: 2 }),
});
// text: "value1" | "value2" | null 로 추론됩니다
varchar: varchar({ length: 6, enum: ["value1", "value2"] })CREATE TABLE `table` (
`varchar` varchar(2)
);{ enum: ["value1", "value2"] } 설정을 정의하여 insert와 select 타입을 추론할 수 있습니다. 런타임 값은 검증하지 않습니다.
import { text, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
text: text(),
});
// text: "value1" | "value2" | null 로 추론됩니다
text: text({ enum: ["value1", "value2"] });CREATE TABLE `table` (
`text` text
);import { boolean, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
boolean: boolean(),
});CREATE TABLE `table` (
`boolean` boolean
);import { boolean, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
date: date(),
});CREATE TABLE `table` (
`date` date
);import { datetime, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
datetime: datetime(),
});
datetime('...', { mode: 'date' | "string"}),CREATE TABLE `table` (
`datetime` datetime
);import { time, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
time: time(),
});CREATE TABLE `table` (
`time` time
);import { year, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
year: year(),
});CREATE TABLE `table` (
`year` year
);import { timestamp, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
timestamp: timestamp(),
});
timestamp('...', { mode: 'date' | "string"}),CREATE TABLE `table` (
`timestamp` timestamp
);import { timestamp, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
timestamp: timestamp().defaultNow(),
});CREATE TABLE `table` (
`timestamp` timestamp DEFAULT (now())
);import { json, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
json: json(),
});
CREATE TABLE `table` (
`json` json
);json 객체 추론을 위해 .$type<..>()을 지정할 수 있습니다. 런타임 값은 검증하지 않습니다.
기본값, insert 및 select 스키마에 대한 컴파일 타임 보호를 제공합니다.
// { foo: string } 으로 추론됩니다
json: json().$type<{ foo: string }>();
// string[] 으로 추론됩니다
json: json().$type<string[]>();
// 컴파일되지 않습니다
json: json().$type<string[]>().default({});import { singlestoreEnum, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
popularity: singlestoreEnum(['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 = singlestoreTable('users', {
id: int().$type<UserId>().primaryKey(),
jsonField: json().$type<Data>(),
});NOT NULL 제약조건은 해당 컬럼이 NULL 값을 포함할 수 없도록 지정합니다.
import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().notNull(),
});CREATE TABLE `table` (
`int` int NOT NULL
);DEFAULT 절은 INSERT 시 사용자가 명시적으로 값을 제공하지 않을 때 컬럼에 사용할 기본값을 지정합니다.
컬럼 정의에 명시적인 DEFAULT 절이 없으면 컬럼의 기본값은 NULL입니다.
명시적인 DEFAULT 절은 기본값을 NULL, 문자열 상수, blob 상수, 부호 있는 숫자, 또는 괄호로 묶인 상수 표현식으로 지정할 수 있습니다.
import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().default(3),
});CREATE TABLE `table` (
`int` int DEFAULT 3
);동일한 함수의 별칭인 $default() 또는 $defaultFn()을 사용하면 런타임에 기본값을 생성하고 모든 insert 쿼리에서 이 값을 사용할 수 있습니다.
이 함수들은 uuid, cuid, cuid2 등 다양한 구현을 활용하는 데 도움이 됩니다.
참고: 이 값은 drizzle-kit 동작에 영향을 주지 않으며, drizzle-orm에서 런타임에만 사용됩니다.
import { varchar, singlestoreTable } from "drizzle-orm/singlestore-core";
import { createId } from '@paralleldrive/cuid2';
const table = singlestoreTable('table', {
id: varchar({ length: 128 }).$defaultFn(() => createId()),
});동일한 함수의 별칭인 $onUpdate() 또는 $onUpdateFn()을 사용하면 런타임에 기본값을 생성하고 모든 update 쿼리에서 이 값을 사용할 수 있습니다.
컬럼에 동적 업데이트 값을 추가합니다. 이 함수는 행이 업데이트될 때 호출되며, 값이 제공되지 않으면 반환된 값이 컬럼 값으로 사용됩니다. 기본값(또는 $defaultFn)이 제공되지 않으면 행이 삽입될 때도 함수가 호출되며, 반환된 값이 컬럼 값으로 사용됩니다.
참고: 이 값은 drizzle-kit 동작에 영향을 주지 않으며, drizzle-orm에서 런타임에만 사용됩니다.
import { text, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
alwaysNull: text().$type<string | null>().$onUpdate(() => null),
});import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().primaryKey(),
});CREATE TABLE `table` (
`int` int PRIMARY KEY NOT NULL
);import { int, singlestoreTable } from "drizzle-orm/singlestore-core";
const table = singlestoreTable('table', {
int: int().autoincrement(),
});CREATE TABLE `table` (
`int` int AUTO_INCREMENT
);