连接 [SQL]

SQL 中的连接子句用于基于表之间相关的列,将 2 个或更多表组合起来。 Drizzle ORM 的连接语法在接近 SQL 风格和类型安全之间取得了平衡。

联接类型

Drizzle ORM 提供了 INNER JOINLEFT JOINRIGHT JOINFULL JOIN 的 API。 让我们快速看一下基于以下表结构的示例:

export const users = mssqlTable('users', {
  id: int().primaryKey().identity(),
  name: varchar({ length: 255 }).notNull(),
});

export const pets = mssqlTable('pets', {
  id: int().primaryKey().identity(),
  name: varchar({ length: 255 }).notNull(),
  ownerId: int('owner_id').notNull().references(() => users.id),
})

左联接

const result = await db.select().from(users).leftJoin(pets, eq(users.id, pets.ownerId))
select ... from [users] left join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
    user: {
        id: number;
        name: string;
    };
    pets: {
        id: number;
        name: string;
        ownerId: number;
    } | null;
}[];

右联接

const result = await db.select().from(users).rightJoin(pets, eq(users.id, pets.ownerId))
select ... from [users] right join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
    user: {
        id: number;
        name: string;
    } | null;
    pets: {
        id: number;
        name: string;
        ownerId: number;
    };
}[];

内联接

const result = await db.select().from(users).innerJoin(pets, eq(users.id, pets.ownerId))
select ... from [users] inner join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
    user: {
        id: number;
        name: string;
    };
    pets: {
        id: number;
        name: string;
        ownerId: number;
    };
}[];

全联接

const res = await db.select().from(users).fullJoin(pets, eq(users.id, pets.ownerId));
select ... from [users] full join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
    users: {
        id: number;
        name: string;
    } | null;
    pets: {
        id: number;
        name: string;
        ownerId: number;
    } | null;
}[];

部分选择

如果你需要选择特定的字段子集,或者希望返回一个扁平的响应类型,Drizzle ORM 支持使用部分选择进行连接,并且会根据 .select({ ... }) 结构自动推断返回类型。

await db.select({
  userId: users.id,
  petId: pets.id,
}).from(users).leftJoin(pets, eq(users.id, pets.ownerId))
select [users].[id], [pets].[id] from [users] left join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
  userId: number;
  petId: number | null;
}[];

你可能已经注意到,petId 现在可以为 null,这是因为我们使用了左连接,并且可能存在没有宠物的用户。

在需要使用 sql 运算符进行部分选择字段和聚合时,务必要牢记这一点, 为了获得正确的结果类型推断,你应该使用 sql<type | null>,这需要你自己来处理!

const result = await db.select({
  userId: users.id,
  petId: pets.id,
  petName1: sql`upper(${pets.name})`,
  petName2: sql<string | null>`upper(${pets.name})`,
  //˄我们应该在类型中明确写出 'string | null',因为我们这里对该字段使用了左连接
}).from(users).leftJoin(pets, eq(users.id, pets.ownerId))
select [users].[id], [pets].[id], upper([pets].[name])... from [users] left join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
  userId: number;
  petId: number | null;
  petName1: unknown;
  petName2: string | null;
}[];

为了避免在连接包含大量列的表时出现过多可为空字段,我们可以利用 嵌套选择对象语法, 我们的智能类型推断会让整个对象变为可空,而不是让表中的所有字段都变为可空!

await db.select({
  userId: users.id,
  userName: users.name,
  pet: {
    id: pets.id,
    name: pets.name,
    upperName: sql<string>`upper(${pets.name})`
  }
}).from(users).leftJoin(pets, eq(users.id, pets.ownerId))
select ... from [users] left join [pets] on [users].[id] = [pets].[owner_id]
// 结果类型
const result: {
    userId: number;
    userName: string;
    pet: {
        id: number;
        name: string;
        upperName: string;
    } | null;
}[];

别名与自连接

Drizzle ORM 支持表别名,当你需要进行自连接时,这会非常方便。

假设你需要获取带有父级信息的用户:

index.ts
schema.ts
import { user } from "./schema";

const parent = alias(user, "parent");
const result = await db
  .select()
  .from(user)
  .leftJoin(parent, eq(parent.id, user.parentId));
select ... from [user] left join [user] [parent] on [parent].[id] = [user].[parent_id]
// 结果类型
const result: {
    user: {
        id: number;
        name: string;
        parentId: number;
    };
    parent: {
        id: number;
        name: string;
        parentId: number;
    } | null;
}[];

聚合结果

Drizzle ORM 会直接从驱动返回按名称映射的结果,而不会改变其结构。

你可以按照自己想要的方式处理这些结果,下面是一个将多对一关系数据进行映射的示例:

type User = typeof users.$inferSelect;
type Pet = typeof pets.$inferSelect;

const rows = await db.select({
    user: users,
    pet: pets,
  }).from(users).leftJoin(pets, eq(users.id, pets.ownerId));

const result = rows.reduce<Record<number, { user: User; pets: Pet[] }>>(
  (acc, row) => {
    const user = row.user;
    const pet = row.pet;

    if (!acc[user.id]) {
      acc[user.id] = { user, pets: [] };
    }

    if (pet) {
      acc[user.id]!.pets.push(pet);
    }

    return acc;
  },
  {}
);

// result type
const result: Record<number, {
    user: User;
    pets: Pet[];
}>;

多对一示例

import { mssqlTable, varchar, int } from 'drizzle-orm/mssql-core';
import { drizzle } from 'drizzle-orm/node-mssql';

const cities = mssqlTable('cities', {
  id: int().primaryKey().identity(),
  name: varchar({ length: 255 }),
});

const users = mssqlTable('users', {
  id: int().primaryKey().identity(),
  name: varchar({ length: 255 }),
  cityId: int('city_id').references(() => cities.id)
});

const db = drizzle();

const result = await db.select().from(cities).leftJoin(users, eq(cities.id, users.cityId));

多对多示例

const users = mssqlTable('users', {
  id: int().primaryKey().identity(),
  name: varchar({ length: 255 }),
});

const chatGroups = mssqlTable('chat_groups', {
  id: int().primaryKey().identity(),
  name: varchar({ length: 255 }),
});

const usersToChatGroups = mssqlTable('usersToChatGroups', {
  userId: int('user_id').notNull().references(() => users.id),
  groupId: int('group_id').notNull().references(() => chatGroups.id),
});


// 查询 id 为 1 的用户组以及所有参与者(用户)
await db.select()
  .from(usersToChatGroups)
  .leftJoin(users, eq(usersToChatGroups.userId, users.id))
  .leftJoin(chatGroups, eq(usersToChatGroups.groupId, chatGroups.id))
  .where(eq(chatGroups.id, 1))