getColumns는 drizzle-orm@1.0.0-beta.2부터 사용할 수 있습니다(자세한 내용은 여기를 참고하세요)
1.0 이전 버전(예: 0.45.1)을 사용하는 경우 getTableColumns를 사용하세요
Drizzle은 쿼리에서 컬럼을 포함하거나 제외할 수 있는 유연한 API를 제공합니다. 모든 컬럼을 포함하려면 다음과 같이 .select() 메서드를 사용할 수 있습니다:
import { posts } from './schema';
const db = drizzle(...);
await db.select().from(posts);// result type
type Result = {
id: number;
title: string;
content: string;
views: number;
}[];특정 컬럼을 포함하려면 다음과 같이 .select() 메서드를 사용할 수 있습니다:
await db.select({ title: posts.title }).from(posts);// result type
type Result = {
title: string;
}[];추가 컬럼과 함께 모든 컬럼을 포함하려면 다음과 같이 getColumns() 유틸리티 함수를 사용할 수 있습니다:
getColumns는 drizzle-orm@1.0.0-beta.2부터 사용할 수 있습니다(자세한 내용은 여기를 참고하세요)
1.0 이전 버전(예: 0.45.1)을 사용하는 경우 getTableColumns를 사용하세요
import { getColumns, sql } from 'drizzle-orm';
await db
.select({
...getColumns(posts),
titleLength: sql<number>`length(${posts.title})`,
})
.from(posts);// result type
type Result = {
id: number;
title: string;
content: string;
views: number;
titleLength: number;
}[];컬럼을 제외하려면 다음과 같이 getColumns() 유틸리티 함수를 사용할 수 있습니다:
getColumns는 drizzle-orm@1.0.0-beta.2부터 사용할 수 있습니다(자세한 내용은 여기를 참고하세요)
1.0 이전 버전(예: 0.45.1)을 사용하는 경우 getTableColumns를 사용하세요
import { getColumns } from 'drizzle-orm';
const { content, ...rest } = getColumns(posts); // exclude "content" column
await db.select({ ...rest }).from(posts); // select all other columns// result type
type Result = {
id: number;
title: string;
views: number;
}[];다음은 조인에서 컬럼을 포함하거나 제외하는 방법입니다:
import { eq, getColumns } from 'drizzle-orm';
import { comments, posts, users } from './db/schema';
// exclude "userId" and "postId" columns from "comments"
const { userId, postId, ...rest } = getColumns(comments);
await db
.select({
postId: posts.id, // include "id" column from "posts"
comment: { ...rest }, // include all other columns
user: users, // equivalent to getColumns(users)
})
.from(posts)
.leftJoin(comments, eq(posts.id, comments.postId))
.leftJoin(users, eq(users.id, posts.userId));// result type
type Result = {
postId: number;
comment: {
id: number;
content: string;
createdAt: Date;
} | null;
user: {
id: number;
name: string;
email: string;
} | null;
}[];Drizzle은 쿼리에서 컬럼을 쉽게 포함하거나 제외할 수 있는 유용한 관계형 쿼리 API를 제공합니다. 다음은 모든 컬럼을 포함하는 방법입니다:
import * as schema from './schema';
const db = drizzle(..., { schema });
await db.query.posts.findMany();// result type
type Result = {
id: number;
title: string;
content: string;
views: number;
}[]다음은 관계형 쿼리를 사용하여 특정 컬럼을 포함하는 방법입니다:
await db.query.posts.findMany({
columns: {
title: true,
},
});// result type
type Result = {
title: string;
}[]다음은 관계형 쿼리를 사용하여 추가 컬럼과 함께 모든 컬럼을 포함하는 방법입니다:
import { sql } from 'drizzle-orm';
await db.query.posts.findMany({
extras: {
titleLength: sql<number>`length(${posts.title})`.as('title_length'),
},
});// result type
type Result = {
id: number;
title: string;
content: string;
views: number;
titleLength: number;
}[];다음은 관계형 쿼리를 사용하여 컬럼을 제외하는 방법입니다:
await db.query.posts.findMany({
columns: {
content: false,
},
});// result type
type Result = {
id: number;
title: string;
views: number;
}[]다음은 관계형 쿼리를 사용하여 관계에서 컬럼을 포함하거나 제외하는 방법입니다:
import * as schema from './schema';
const db = drizzle(..., { schema });
await db.query.posts.findMany({
columns: {
id: true, // include "id" column
},
with: {
comments: {
columns: {
userId: false, // exclude "userId" column
postId: false, // exclude "postId" column
},
},
user: true, // include all columns from "users" table
},
});// result type
type Result = {
id: number;
user: {
id: number;
name: string;
email: string;
};
comments: {
id: number;
content: string;
createdAt: Date;
}[];
}[]다음은 조건부 조회를 위한 커스텀 솔루션을 만드는 방법입니다:
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);// result type
type Result = {
id: number;
title?: string | undefined;
}[];