Drizzle | 在查询中包含或排除列
PostgreSQL
MySQL
SQLite

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;
}[];