Communitygithub.com

YepAPI/技能

租戶隔離(行級 vs. 模式)、子網域路由、每個租戶的配置以及資料分割。

技能 是什麼?

這項技能提供了一個全面的多租戶架構實作指南,適用於 SaaS 應用程式。它涵蓋了從行級隔離(預設)到模式分隔等多種隔離策略,以滿足不同租戶對客製化模式和嚴格資料隔離的需求。技能詳細說明了如何透過子網域(例如 {tenant}.app.com)或自訂域名進行租戶路由,並利用中介軟體將租戶資訊注入請求上下文。此外,它也指導如何為每個租戶儲存主題、功能標誌和限制等配置,並使用記憶體快取來最佳化效能。透過提供具體的程式碼範例(如 Prisma 中介軟體和 Next.js 中介軟體),這項技能幫助開發者實作安全的租戶資料隔離,同時避免常見的陷阱,例如遺漏租戶範圍查詢或缺乏適當的索引。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/multi-tenancy

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

多租戶

規則

  • 行級隔離(預設):為每個租戶範圍的資料表添加 tenantId 欄位,並透過中介軟體或 RLS(資料列層級安全性)強制實施。
  • 模式分隔租戶:當租戶需要自訂模式或嚴格的資料隔離時使用——此方法操作較複雜,但隔離性更佳。
  • 子網域路由:從 {tenant}.app.com 解析租戶——在中介軟體中解析,並在請求上下文中設定 tenantId。
  • 自訂域名:將 CNAME 指向您的應用程式,透過 Host 標頭查找租戶——使用 domains 資料表將自訂域名對應到租戶 ID。
  • 每個租戶的配置:將主題(標誌、顏色)、功能標誌和限制儲存在 tenant_settings 資料表中——使用帶 TTL 的記憶體進行快取。
  • RLS 模式(Postgres):ALTER TABLE posts ENABLE ROW LEVEL SECURITY; CREATE POLICY tenant_isolation ON posts USING (tenant_id = current_setting('app.tenant_id'))
  • 中介軟體:從子網域/標頭提取租戶,設定到上下文,並應用於所有資料庫查詢——絕不讓任何查詢在沒有租戶範圍的情況下執行。
  • 資料分割:對於大型資料表,按 tenantId 進行分割——可改善查詢效能,並支援每個租戶的備份/還原。

行級隔離模式

// 中介軟體——從子網域提取租戶
function getTenantFromHost(host: string): string {
  const subdomain = host.split(".")[0];
  return subdomain; // 或在 tenants 資料表中查找
}

// Prisma 中介軟體——自動按 tenantId 篩選
prisma.$use(async (params, next) => {
  if (TENANT_MODELS.includes(params.model)) {
    params.args.where = { ...params.args.where, tenantId: context.tenantId };
  }
  return next(params);
});

子網域路由(Next.js 中介軟體)

export function middleware(request: NextRequest) {
  const host = request.headers.get("host") ?? "";
  const tenant = host.split(".")[0];
  const response = NextResponse.next();
  response.headers.set("x-tenant-id", tenant);
  return response;
}

避免事項

  • 遺漏租戶範圍的查詢——只要少了一個 WHERE tenantId = ? 就會跨租戶洩漏資料
  • 在客戶端硬編碼租戶——應從伺服器端透過子網域/域名解析
  • 未對 tenantId 建立索引——每個租戶範圍的資料表都需要一個以 tenantId 開頭的複合索引
  • 在共用快取中混用租戶資料——快取鍵應以 {tenantId}:{key} 命名空間區隔

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相關技能