DrizzleORM v0.16.2 릴리스
Jan 21, 2023
Drizzle ORM은 쿼리 빌더로 사용할 수 있으며, SQL 스키마의 단일 소스(source of truth)이자 자동 마이그레이션 생성을 위한 CLI를 제공하는 관용적 TypeScript ORM입니다.
지난 주요 업데이트 이후 많은 요청된 기능들을 추가했습니다 🚀
🎉 PostgreSQL 스키마
이제 PostgreSQL 스키마를 선언하고 해당 스키마 내에 생성할 테이블을 정의할 수 있습니다
// src/schema.ts
import { pgSchema } from "drizzle-orm-pg";
export const mySchema = pgSchema("my_schema");
export const users = mySchema("users", {
id: serial("id").primaryKey(),
name: text("name"),
email: text("email"),
});CREATE SCHEMA "my_schema";
CREATE TABLE IF NOT EXISTS "my_schema"."users" (
"id" serial PRIMARY KEY NOT NULL,
"name" text,
"email" text
);drizzle-kit이 필요한 모든 SQL 마이그레이션을 자동으로 생성합니다
drizzle-kit generate:pg --schema=src/schema.ts --out=migrations/🎉 MySQL 데이터베이스/스키마
이제 MySQL 데이터베이스/스키마를 선언하고 그 안에 생성할 테이블을 정의할 수 있습니다
// schema.ts
import { mysqlSchema } from "drizzle-orm-mysql";
const mySchema = mysqlSchema("my_schema");
const users = mySchema("users", {
id: serial("id").primaryKey(),
name: text("name"),
email: text("email"),
});drizzle-kit이 필요한 모든 SQL 마이그레이션을 자동으로 생성합니다 shell drizzle-kit generate:mysql --schema=src/schema.ts --out=migrations/
그러면 다음과 같은 SQL 마이그레이션이 자동으로 생성됩니다
CREATE DATABASE `my_schema`;
CREATE TABLE `my_schema`.`users` (
`id` serial PRIMARY KEY NOT NULL,
`name` text,
`email` text
);🎉 PostgreSQL 인트로스펙트
이제 drizzle-kit을 사용하여 기존 PostgreSQL 데이터베이스에서 몇 초 만에 데이터베이스 스키마를 가져올 수 있습니다. 이를 통해 기존 ORM이나 바닐라 SQL에서 전환하는 데 따르는 대부분의 어려움이 사라집니다. 지원 항목:
- enum
- 모든 네이티브 및 비네이티브 컬럼을 가진 테이블
- 인덱스
- 외래 키, 자기 참조 및 순환 외래 키
- 스키마
drizzle-kit introspect:pg --out=migrations/ --connectionString=postgresql://user:pass@host:port/db_name다음과 같은 schema.ts가 생성됩니다
export const myEnum = pgEnum("my_enum", ["one", "two", "three"]);
export const mySchema = pgSchema("my_schema");
export const users = mySchema("users", {
id: serial("id").primaryKey(),
name: text("name"),
email: text("email"),
});
export const users2 = pgTable("users2", {
id: serial("id").primaryKey(),
name: varchar("name2"),
enum: myEnum("enum"),
});
export const allColumns = pgTable("all_columns", {
sm: smallint("smallint"),
smdef: smallint("smallint_def").default(10),
int: integer("integer"),
intdef: integer("integer_def").default(10),
numeric: numeric("numeric"),
numeric2: numeric("numeric2", { precision: 7 }),
numeric3: numeric("numeric3", { scale: 7 }),
numeric4: numeric("numeric4", { precision: 7, scale: 7 }),
numericdef: numeric("numeridef").default("100"),
bigint: bigint("bigint", { mode: "number" }),
bigintdef: bigint("bigint", { mode: "number" }).default(100),
bool: boolean("boolean"),
booldef: boolean("boolean_def").default(true),
text: text("text"),
textdef: text("textdef").default("text"),
varchar: varchar("varchar"),
varchardef: varchar("varchardef").default("text"),
serial: serial("serial"),
bigserial: bigserial("bigserial", { mode: "bigint" }),
decimal: decimal("decimal", { precision: 100, scale: 2 }),
decimaldef: decimal("decimaldef", { precision: 100, scale: 2 }).default(
"100.0",
),
doublePrecision: doublePrecision("decimal"),
doublePrecisiondef: doublePrecision("decimaldef").default(100.0),
real: real("real"),
realdef: real("decimaldef").default(100.0),
json: json<{ attr: string }>("json"),
jsondef: json<{ attr: string }>("jsondef").default({ attr: "value" }),
jsonb: jsonb<{ attr: string }>("jsonb"),
jsonbdef: jsonb<{ attr: string }>("jsonbdef").default({ attr: "value" }),
time: time("time"),
time2: time("time2", { precision: 6, withTimezone: true }),
timedefnow: time("timedefnow").defaultNow(),
timestamp: timestamp("timestamp"),
timestamp2: timestamp("timestamp2", { precision: 6, withTimezone: true }),
timestamp3: timestamp("timestamp3", { withTimezone: true }),
timestamp4: timestamp("timestamp4", { precision: 4 }),
timestampdef: timestamp("timestampdef").defaultNow(),
date: date("date", { mode: "date" }),
datedef: date("datedef").defaultNow(),
interval: interval("interval"),
intervaldef: interval("intervaldef").default("10 days"),
});
export const cyclic1 = pgTable("cyclic1", {
id: serial("id").primaryKey(),
ext2: integer("ext2").references(() => cyclic2.id),
});
export const cyclic2 = pgTable("cyclic2", {
id: serial("id").primaryKey(),
ext1: integer("ext1").references((): AnyPgColumn => cyclic1.id),
});
export const example = pgTable(
"example",
{
id: serial("id").primaryKey(),
reportsTo: integer("reports_to"),
},
(table) => {
return {
reportsToFK: foreignKey({
columns: [table.reportsTo],
foreignColumns: [table.id],
}),
};
},
);🎉 Postgres.js 드라이버 지원
postgres.js에 대한 완전한 지원을 추가했습니다. 가볍고 빠릅니다 🚀
// schema.ts
import { pgTable, serial, text, varchar } from "drizzle-orm-pg";
export const users = pgTable("users", {
id: serial("id").primaryKey(),
fullName: text("full_name"),
phone: varchar("phone", { length: 256 }),
});
// index.ts
import { drizzle, PostgresJsDatabase } from "drizzle-orm-pg/postgres.js";
import postgres from "postgres";
import { users } from "./schema";
const client = postgres(connectionString);
const db: PostgresJsDatabase = drizzle(client);
const allUsers = await db.select(users);전체 PostgreSQL 문서는 여기를 참조하세요
🎉 PostgreSQL 및 MySQL 타입
필요한 비네이티브 PostgreSQL 또는 MySQL 타입을 생성할 수 있는 유용한 연산자를 추가했습니다
// PostgreSQL
const customText = customType<{ data: string }>({
dataType() {
return "text";
},
});
const usersTable = pgTable("users", {
name: customText("name").notNull(),
});
// MySQL
const customText = customType<{ data: string }>({
dataType() {
return "text";
},
});
const usersTable = mysqlTable("users", {
name: customText("name").notNull(),
});