Drizzle 与 Nile

本指南假定您熟悉

根据 官网 信息,Nile 是为多租户应用重新设计的 PostgreSQL。

查阅 Nile + Drizzle 官方 快速入门迁移 文档。

您可以将 Nile 与 Drizzle 的任何 Postgres 驱动程序配合使用,下面我们将展示 `node-postgres` 的用法。

步骤 1 - 安装包

npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit

步骤 2 - 初始化驱动程序并执行查询

index.ts
// Make sure to install the 'pg' package
import { drizzle } from 'drizzle-orm/node-postgres'

const db = drizzle(process.env.NILEDB_URL);

const response = await db.select().from(...);

如果您需要提供现有驱动程序

index.ts
// Make sure to install the 'pg' package
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";
const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});
const db = drizzle({ client: pool });

const response = await db.select().from(...);

连接到虚拟租户数据库

Nile 提供虚拟租户数据库。当您设置租户上下文时,Nile 会将您的查询定向到该特定租户的虚拟数据库,所有查询将仅适用于该租户(即 `select * from table` 将只返回该租户的记录)。

为了设置租户上下文,我们将每个查询包装在一个事务中,该事务在运行之前会设置相应的租户上下文。

租户 ID 可以作为参数直接传递给包装器

index.ts
import { drizzle } from 'drizzle-orm/node-postgres';
import { todosTable, tenants } from "./db/schema";
import { sql } from 'drizzle-orm';
import 'dotenv/config';

const db = drizzle(process.env.NILEDB_URL);

function tenantDB<T>(tenantId: string, cb: (tx: any) => T | Promise<T>): Promise<T> {
  return db.transaction(async (tx) => {
    if (tenantId) {
      await tx.execute(sql`set local nile.tenant_id = '${sql.raw(tenantId)}'`);
    }

    return cb(tx);
  }) as Promise<T>;
}

// In a webapp, you'll likely get it from the request path parameters or headers
const tenantId = '01943e56-16df-754f-a7b6-6234c368b400'

const response = await tenantDB(tenantId, async (tx) => {
    // No need for a "where" clause here
    return await tx.select().from(todosTable);
});

console.log(response);

如果您正在使用支持 AsyncLocalStorage 的 Web 框架,您可以设置 AsyncLocalStorage 并使用中间件填充租户 ID。在这种情况下,您的 Drizzle 客户端设置将是

import { drizzle } from 'drizzle-orm/node-postgres';
import dotenv from "dotenv/config";
import { sql } from "drizzle-orm";
import { AsyncLocalStorage } from "async_hooks";

export const db = drizzle(process.env.NILEDB_URL);
export const tenantContext = new AsyncLocalStorage<string | undefined>();

export function tenantDB<T>(cb: (tx: any) => T | Promise<T>): Promise<T> {
  return db.transaction(async (tx) => {
    const tenantId = tenantContext.getStore();
    console.log("executing query with tenant: " + tenantId);
    // if there's a tenant ID, set it in the transaction context
    if (tenantId) {
      await tx.execute(sql`set local nile.tenant_id = '${sql.raw(tenantId)}'`);
    }

    return cb(tx);
  }) as Promise<T>;
}

然后,配置一个中间件来填充 AsyncLocalStorage,并在处理请求时使用 `tenantDB` 方法。

app.ts
// Middleware to set tenant context
app.use("/api/tenants/:tenantId/*", async (c, next) => {
  const tenantId = c.req.param("tenantId");
  console.log("setting context to tenant: " + tenantId);
  return tenantContext.run(tenantId, () => next());
});

// Route handler
app.get("/api/tenants/:tenantId/todos", async (c) => {
    const todos = await tenantDB(c, async (tx) => {
      return await tx
        .select({
          id: todoSchema.id,
          tenant_id: todoSchema.tenantId,
          title: todoSchema.title,
          estimate: todoSchema.estimate,
        })
        .from(todoSchema);
    });
    return c.json(todos);
});

接下来是什么?