Skip to content
全部能力
平台packages/platform/db

db 能力包

类型化 Prisma client 包装 — extension 友好、RLS 兼容、读写分离时按租户绑定连接。

打开文档
稳定性
稳定
作用域
全局
边界
packages/platform/db
查询 · packages/platform/db
RLS已启用
apps/web/app/posts/page.tsx
// Tenant context resolved by @nebutra/tenant middleware
const ctx = getCurrentTenant();

const posts = await prisma.post.findMany({
  where: {
    tenantId: ctx.tenant.id,
    published: true,
  },
  orderBy: { publishedAt: "desc" },
  take: 5,
});
// → Prisma client wrapped with withRls(prisma, ctx.tenant.id)
// → PostgreSQL enforces RLS via SET LOCAL app.tenant_id

查询结果

通过行级安全按租户隔离

2 / 5
idtitletenant_idpublished_at
pst_01HF…a3kLaunching multi-tenant RLStenant_a2026-05-21
pst_01HF…b7mMigrating off shared schemastenant_a2026-05-18
pst_01HF…c2qInternal roadmap (draft)tenant_b2026-05-17
pst_01HF…d9sCustomer-only changelogtenant_c2026-05-15
pst_01HF…e4wRFC: schema-per-tenanttenant_b2026-05-12

RLS 在数据库层屏蔽其他租户的行,应用永远看不到它们。

可见行

847

tenant_a 内

RLS 隐藏

12,401

其他租户

使用方式db.ts
typescript
db.ts
1import { prisma } from "@nebutra/db";
2
3// Wrapped Prisma client — RLS enforced via withRls() per request.
4const posts = await prisma.post.findMany({
5  where: { tenantId: ctx.tenant.id, published: true },
6  include: { author: true },
7  orderBy: { publishedAt: "desc" },
8  take: 20,
9});
10
11await prisma.$transaction(async (tx) => {
12  await tx.post.update({ where: { id }, data: { published: true } });
13  await tx.auditLog.create({ data: { action: "post.publish", postId: id } });
14});