Database Rules
Defines mandatory rules for data access, migrations, and RLS to maintain data integrity in Supabase projects.
Install
mkdir -p .claude/skills/database-rules && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/15798" && unzip -o skill.zip -d .claude/skills/database-rules && rm skill.zipInstalls to .claude/skills/database-rules
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Supabase、RLS、マイグレーション、およびデータ操作に関する絶対ルールKey capabilities
- →Enforce `packages/shared/src/types/` as the single source of truth for schema and types.
- →Mandate the use of `packages/shared/src/dal` for all DB access logic.
- →Ensure date handling uses `YYYY-MM-DD` plain strings to avoid timezone issues.
- →Implement fractional indexing for list reordering.
- →Require Row Level Security (RLS) on all tables.
How it works
The skill enforces a set of absolute rules for Supabase, RLS, migrations, and data manipulation, ensuring consistency, security, and performance.
Inputs & outputs
When to use Database Rules
- →Implement Supabase DAL access
- →Handle timezone-safe dates
- →Apply fractional indexing for lists
- →Structure migrations for RLS
About this skill
Database Operations & Rules
※最優先遵守事項(DALの強制、Additive Changesのみ等の絶対ルール)については必ず .agent/rules/core-rules.md を参照すること。
1. SSOT (Single Source of Truth) とデータ型規約
- 型定義の参照: スキーマ構造やEnum値は、手動コードやドキュメントを推測せず、必ず
packages/shared/src/types/配下を参照すること。 - 日付SSOT: 時差ズレを防ぐため、日付(
date)は常にブラウザローカルタイムゾーンに基づくYYYY-MM-DD形式のプレーン文字列としてDALへ渡し、完全一致で保存・判定すること。 - Diaries アトミック追記仕様: 初回入力は
[HH:MM]\n本文、同一日の追記時は既存本文の末尾に改行2つ(\n\n)を挟んで[HH:MM]\n本文をアトミック結合しupsertすること。 - Slim Fetching: ノート一覧等は本文を除いた
NoteMetadata型で軽量フェッチし、詳細・編集時に完全なNote型へハイドレーションすること。
2. データ操作のベストプラクティス
- Fractional Indexing: 順序管理(
sort_order)は double precision を用いた Fractional Indexing を標準とし、単一レコードのupdateのみで完結させること(配列の一括更新禁止)。 - N+1 クエリ排除と RPC 活用: ダッシュボード等の複雑な階層データ取得は、PostgreSQL の
JSON_AGG等を活用した単一 RPC 関数を作成して一括取得すること。 - 楽観的UIの採番対称性: 新規作成時の仮
sort_orderは、DB側の採番(最上部挿入ならMath.min(...) - 1.0)と完全に一致させ、ワープ現象を物理排除すること。
3. マイグレーション & セキュリティ (RLS)
- マイグレーション手順: ローカルで変更テスト後、
supabase/migrationsにSQLを作成し、bunx supabase db dump --local > supabase/schema.sqlでスキーマを最新化すること。 - RLS (Row Level Security): 全テーブルで RLS を有効化し、
auth.uid() = user_idによるアクセスポリシーを必須とすること。 - PostgREST インジェクション対策:
.or('column.eq."${val}"')などの文字列連結クエリはパースエラーや脆弱性の原因となるため禁止。複雑な OR 条件は DB 側に RPC を作成してパラメータ渡しすること。 - PostgrestError の型判定:
catch (err: unknown)内ではtypeof err === 'object' && err !== null && 'message' in errのプロパティチェックでメッセージを取得すること(instanceof Error単体判定は不可)。
When not to use it
- →When directly modifying `packages/shared/src/types/supabase.ts` manually.
- →When building Supabase queries using string concatenation for conditions.
Prerequisites
Limitations
- →Do not hardcode specific values for database structure, table names, column names, or Enum values.
- →Direct writing to `packages/shared/src/types/supabase.ts` is absolutely prohibited.
- →String concatenation for query conditions like `.or('column.eq."${value}"')` is prohibited.
How it compares
This skill provides strict, opinionated rules for Supabase development, such as mandatory DAL usage and specific date handling, which prevents common pitfalls and ensures architectural consistency.
Compared to similar skills
Database Rules side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| Database Rules (this skill) | 0 | 1mo | No flags | Advanced |
| backend-dev | 1 | 6mo | No flags | Intermediate |
| drizzle-orm | 32 | 2mo | No flags | Intermediate |
| database-design | 6 | 6mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
backend-dev
marmelab
Coding practices for backend development in Atomic CRM. Use when deciding whether backend logic is needed, or when creating/modifying database migrations, views, triggers, RLS policies, edge functions, or custom dataProvider methods that call Supabase APIs.
drizzle-orm
EpicenterHQ
Drizzle ORM patterns for type branding and custom types. Use when working with Drizzle column definitions, branded types, or custom type conversions.
database-design
davila7
Database design principles and decision-making. Schema design, indexing strategy, ORM selection, serverless databases.
supabase-postgres-best-practices
davila7
Postgres performance optimization and best practices from Supabase. Use this skill when writing, reviewing, or optimizing Postgres queries, schema designs, or database configurations.
database-schema-designer
davila7
Design robust, scalable database schemas for SQL and NoSQL databases. Provides normalization guidelines, indexing strategies, migration patterns, constraint design, and performance optimization. Ensures data integrity, query performance, and maintainable data models.
database-migrations-sql-migrations
sickn33
SQL database migrations with zero-downtime strategies for PostgreSQL, MySQL, SQL Server