Communitygithub.com

YepAPI/skills

テナント分離(行レベル vs スキーマ)、サブドメインルーティング、テナントごとの設定、データパーティショニング。

skills とは?

このスキルは、マルチテナントアーキテクチャを実装するための包括的なガイドです。開発者は、行レベルの分離(デフォルト)とスキーマ単位のテナント分離の2つの主要な分離戦略を選択でき、それぞれのトレードオフ(運用の複雑さ vs 分離の厳格さ)を理解した上で実装できます。また、サブドメインやカスタムドメインを用いたテナントルーティング、Next.jsミドルウェアでのテナント解決、Prismaミドルウェアを用いた自動テナントフィルタリング、PostgreSQLのRow-Level Security(RLS)パターンなど、具体的な実装パターンがコード例とともに提供されています。このスキルは、Claude CodeやCursor、CodexなどのAIエージェントが、マルチテナントシステムを構築する際のベストプラクティスを一貫して適用できるように設計されています。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/multi-tenancy

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

マルチテナンシー

ルール

  • 行レベルの分離(デフォルト): テナントスコープのすべてのテーブルに tenantId カラムを追加し、ミドルウェアまたはRLS(行レベルセキュリティ)で適用します。
  • スキーマ単位のテナント分離: テナントがカスタムスキーマや厳格なデータ分離を必要とする場合に使用します。運用は複雑になりますが、分離性は向上します。
  • サブドメインルーティング: {tenant}.app.com からテナントを抽出し、ミドルウェアで解決してリクエストコンテキストに tenantId を設定します。
  • カスタムドメイン: アプリへのCNAMEレコードと、Host ヘッダーによるテナントの検索を行います。カスタムドメインとテナントIDをマッピングする domains テーブルを使用します。
  • テナントごとの設定: テーマ(ロゴ、色)、機能フラグ、制限値を 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 = ? が1つでも欠けると、テナント間でデータが漏洩します。
  • クライアント側でのテナントのハードコーディング — サーバーサイドでサブドメイン/ドメインから解決します。
  • 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.

関連スキル