Drizzle | 在查询中包含或排除列
PostgreSQL
MySQL
SQLite
This guide assumes familiarity with:
- 开始使用 PostgreSQL, MySQL 和 SQLite
- 选择语句
- 获取类型化表列
- 连接
- 关系查询
- 使用关系查询的部分选择
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<number>`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<number>`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;
}[];