Drizzle | 쿼리에서 컬럼 포함 또는 제외하기
PostgreSQL
MySQL
SQLite
This guide assumes familiarity with:
Drizzle은 쿼리에서 컬럼을 포함하거나 제외할 수 있는 유연한 API를 제공합니다. 모든 컬럼을 포함하려면 다음과 같이 .select()
메서드를 사용할 수 있습니다.
index.ts
schema.ts
import { posts } from './schema';
const db = drizzle(...);
await db.select().from(posts);
// 결과 타입
type Result = {
id: number;
title: string;
content: string;
views: number;
}[];
특정 컬럼만 포함하려면 다음과 같이 .select()
메서드를 사용합니다.
await db.select({ title: posts.title }).from(posts);
// 결과 타입
type Result = {
title: string;
}[];
모든 컬럼과 추가 컬럼을 포함하려면 getTableColumns()
유틸리티 함수를 사용합니다.
import { getTableColumns, sql } from 'drizzle-orm';
await db
.select({
...getTableColumns(posts),
titleLength: sql`length(${posts.title})`,
})
.from(posts);
// 결과 타입
type Result = {
id: number;
title: string;
content: string;
views: number;
titleLength: number;
}[];
컬럼을 제외하려면 getTableColumns()
유틸리티 함수를 사용합니다.
import { getTableColumns } from 'drizzle-orm';
const { content, ...rest } = getTableColumns(posts); // "content" 컬럼 제외
await db.select({ ...rest }).from(posts); // 나머지 컬럼 선택
// 결과 타입
type Result = {
id: number;
title: string;
views: number;
}[];
조인과 함께 컬럼을 포함하거나 제외하는 방법은 다음과 같습니다.
index.ts
schema.ts
import { eq, getTableColumns } from 'drizzle-orm';
import { comments, posts, users } from './db/schema';
// "comments"에서 "userId"와 "postId" 컬럼 제외
const { userId, postId, ...rest } = getTableColumns(comments);
await db
.select({
postId: posts.id, // "posts"에서 "id" 컬럼 포함
comment: { ...rest }, // 나머지 컬럼 포함
user: users, // getTableColumns(users)와 동일
})
.from(posts)
.leftJoin(comments, eq(posts.id, comments.postId))
.leftJoin(users, eq(users.id, posts.userId));
// 결과 타입
type Result = {
postId: number;
comment: {
id: number;
content: string;
createdAt: Date;
} | null;
user: {
id: number;
name: string;
email: string;
} | null;
}[];
Drizzle은 관계형 쿼리 API를 제공하여 쿼리에서 컬럼을 쉽게 포함하거나 제외할 수 있습니다. 모든 컬럼을 포함하는 방법은 다음과 같습니다.
index.ts
schema.ts
import * as schema from './schema';
const db = drizzle(..., { schema });
await db.query.posts.findMany();
// 결과 타입
type Result = {
id: number;
title: string;
content: string;
views: number;
}[]
관계형 쿼리를 사용하여 특정 컬럼만 포함하는 방법은 다음과 같습니다.
await db.query.posts.findMany({
columns: {
title: true,
},
});
// 결과 타입
type Result = {
title: string;
}[]
관계형 쿼리를 사용하여 모든 컬럼과 추가 컬럼을 포함하는 방법은 다음과 같습니다.
import { sql } from 'drizzle-orm';
await db.query.posts.findMany({
extras: {
titleLength: sql`length(${posts.title})`.as('title_length'),
},
});
// 결과 타입
type Result = {
id: number;
title: string;
content: string;
views: number;
titleLength: number;
}[];
관계형 쿼리를 사용하여 컬럼을 제외하는 방법은 다음과 같습니다.
await db.query.posts.findMany({
columns: {
content: false,
},
});
// 결과 타입
type Result = {
id: number;
title: string;
views: number;
}[]
관계형 쿼리를 사용하여 관계와 함께 컬럼을 포함하거나 제외하는 방법은 다음과 같습니다.
index.ts
schema.ts
import * as schema from './schema';
const db = drizzle(..., { schema });
await db.query.posts.findMany({
columns: {
id: true, // "id" 컬럼 포함
},
with: {
comments: {
columns: {
userId: false, // "userId" 컬럼 제외
postId: false, // "postId" 컬럼 제외
},
},
user: true, // "users" 테이블의 모든 컬럼 포함
},
});
// 결과 타입
type Result = {
id: number;
user: {
id: number;
name: string;
email: string;
};
comments: {
id: number;
content: string;
createdAt: Date;
}[];
}[]
조건부 선택을 위한 커스텀 솔루션을 만드는 방법은 다음과 같습니다.
index.ts
schema.ts
import { posts } from './schema';
const searchPosts = async (withTitle = false) => {
await db
.select({
id: posts.id,
...(withTitle && { title: posts.title }),
})
.from(posts);
};
await searchPosts();
await searchPosts(true);
// 결과 타입
type Result = {
id: number;
title?: string | undefined;
}[];