Drizzle | 查询中包含或排除列
PostgreSQL
MySQL
SQLite
本指南假定您熟悉
- 开始使用 PostgreSQL、 MySQL 和 SQLite
- Select 语句
- 获取类型化的表列
- 连接
- 关系型查询
- 关系型查询中的部分选择
Drizzle 提供了灵活的 API,用于在查询中包含或排除列。要包含所有列,您可以使用 .select()
方法,如下所示:
index.ts
schema.ts
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;
}[];
要包含所有列并附加额外列,您可以使用 getTableColumns()
工具函数,如下所示:
import { getTableColumns, sql } from 'drizzle-orm';
await db
.select({
...getTableColumns(posts),
titleLength: sql<number>`length(${posts.title})`,
})
.from(posts);
// result type
type Result = {
id: number;
title: string;
content: string;
views: number;
titleLength: number;
}[];
要排除列,您可以使用 getTableColumns()
工具函数,如下所示:
import { getTableColumns } from 'drizzle-orm';
const { content, ...rest } = getTableColumns(posts); // exclude "content" column
await db.select({ ...rest }).from(posts); // select all other columns
// result type
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';
// exclude "userId" and "postId" columns from "comments"
const { userId, postId, ...rest } = getTableColumns(comments);
await db
.select({
postId: posts.id, // include "id" column from "posts"
comment: { ...rest }, // include all other columns
user: users, // equivalent to getTableColumns(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,可让您轻松地在查询中包含或排除列。以下是如何包含所有列:
index.ts
schema.ts
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;
}[]
以下是如何使用关系型查询,在关系中包含或排除列:
index.ts
schema.ts
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;
}[];
}[]
以下是如何为条件选择创建自定义解决方案:
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);
// result type
type Result = {
id: number;
title?: string | undefined;
}[];