@intentsolutionsio/supabase-pack 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +73 -47
- package/package.json +4 -4
- package/skills/supabase-advanced-troubleshooting/SKILL.md +404 -200
- package/skills/supabase-advanced-troubleshooting/references/errors.md +11 -0
- package/skills/supabase-advanced-troubleshooting/references/evidence-collection-framework.md +34 -0
- package/skills/supabase-advanced-troubleshooting/references/examples.md +11 -0
- package/skills/supabase-advanced-troubleshooting/references/rls-edge-functions-realtime.md +363 -0
- package/skills/supabase-advanced-troubleshooting/references/systematic-isolation.md +56 -0
- package/skills/supabase-advanced-troubleshooting/references/timing-analysis.md +35 -0
- package/skills/supabase-architecture-variants/SKILL.md +395 -216
- package/skills/supabase-architecture-variants/references/errors.md +11 -0
- package/skills/supabase-architecture-variants/references/examples.md +12 -0
- package/skills/supabase-architecture-variants/references/serverless-and-multi-tenant.md +251 -0
- package/skills/supabase-architecture-variants/references/variant-a-monolith-(simple).md +44 -0
- package/skills/supabase-architecture-variants/references/variant-b-service-layer-(moderate).md +72 -0
- package/skills/supabase-architecture-variants/references/variant-c-microservice-(complex).md +81 -0
- package/skills/supabase-auth-storage-realtime-core/SKILL.md +471 -37
- package/skills/supabase-ci-integration/SKILL.md +315 -67
- package/skills/supabase-ci-integration/references/errors.md +10 -0
- package/skills/supabase-ci-integration/references/examples.md +36 -0
- package/skills/supabase-ci-integration/references/implementation.md +54 -0
- package/skills/supabase-common-errors/SKILL.md +320 -62
- package/skills/supabase-common-errors/references/errors.md +53 -0
- package/skills/supabase-common-errors/references/examples.md +23 -0
- package/skills/supabase-cost-tuning/SKILL.md +365 -131
- package/skills/supabase-cost-tuning/references/cost-estimation.md +34 -0
- package/skills/supabase-cost-tuning/references/cost-reduction-strategies.md +40 -0
- package/skills/supabase-cost-tuning/references/errors.md +11 -0
- package/skills/supabase-cost-tuning/references/examples.md +15 -0
- package/skills/supabase-data-handling/SKILL.md +378 -145
- package/skills/supabase-data-handling/references/errors.md +11 -0
- package/skills/supabase-data-handling/references/examples.md +27 -0
- package/skills/supabase-data-handling/references/implementation.md +223 -0
- package/skills/supabase-data-handling/references/retention-and-backup.md +221 -0
- package/skills/supabase-debug-bundle/SKILL.md +267 -73
- package/skills/supabase-debug-bundle/references/errors.md +12 -0
- package/skills/supabase-debug-bundle/references/examples.md +24 -0
- package/skills/supabase-debug-bundle/references/implementation.md +54 -0
- package/skills/supabase-deploy-integration/SKILL.md +258 -147
- package/skills/supabase-deploy-integration/references/errors.md +11 -0
- package/skills/supabase-deploy-integration/references/examples.md +21 -0
- package/skills/supabase-deploy-integration/references/google-cloud-run.md +36 -0
- package/skills/supabase-deploy-integration/references/vercel-deployment.md +35 -0
- package/skills/supabase-enterprise-rbac/SKILL.md +327 -160
- package/skills/supabase-enterprise-rbac/references/api-scoping-and-enforcement.md +255 -0
- package/skills/supabase-enterprise-rbac/references/errors.md +11 -0
- package/skills/supabase-enterprise-rbac/references/examples.md +12 -0
- package/skills/supabase-enterprise-rbac/references/role-implementation.md +33 -0
- package/skills/supabase-enterprise-rbac/references/sso-integration.md +35 -0
- package/skills/supabase-hello-world/SKILL.md +160 -54
- package/skills/supabase-incident-runbook/SKILL.md +453 -131
- package/skills/supabase-incident-runbook/references/errors.md +11 -0
- package/skills/supabase-incident-runbook/references/examples.md +10 -0
- package/skills/supabase-incident-runbook/references/immediate-actions-by-error-type.md +41 -0
- package/skills/supabase-install-auth/SKILL.md +186 -50
- package/skills/supabase-install-auth/references/examples.md +102 -0
- package/skills/supabase-known-pitfalls/SKILL.md +411 -241
- package/skills/supabase-known-pitfalls/references/errors.md +11 -0
- package/skills/supabase-known-pitfalls/references/examples.md +12 -0
- package/skills/supabase-load-scale/SKILL.md +346 -217
- package/skills/supabase-load-scale/references/capacity-planning.md +47 -0
- package/skills/supabase-load-scale/references/errors.md +11 -0
- package/skills/supabase-load-scale/references/examples.md +26 -0
- package/skills/supabase-load-scale/references/load-testing-with-k6.md +59 -0
- package/skills/supabase-load-scale/references/scaling-patterns.md +65 -0
- package/skills/supabase-load-scale/references/table-partitioning.md +263 -0
- package/skills/supabase-local-dev-loop/SKILL.md +272 -73
- package/skills/supabase-local-dev-loop/references/errors.md +11 -0
- package/skills/supabase-local-dev-loop/references/examples.md +21 -0
- package/skills/supabase-local-dev-loop/references/implementation.md +60 -0
- package/skills/supabase-migration-deep-dive/SKILL.md +338 -177
- package/skills/supabase-migration-deep-dive/references/backfill-versioning-rollback.md +258 -0
- package/skills/supabase-migration-deep-dive/references/errors.md +11 -0
- package/skills/supabase-migration-deep-dive/references/examples.md +12 -0
- package/skills/supabase-migration-deep-dive/references/implementation-plan.md +80 -0
- package/skills/supabase-migration-deep-dive/references/pre-migration-assessment.md +39 -0
- package/skills/supabase-multi-env-setup/SKILL.md +393 -152
- package/skills/supabase-multi-env-setup/references/configuration-structure.md +59 -0
- package/skills/supabase-multi-env-setup/references/errors.md +11 -0
- package/skills/supabase-multi-env-setup/references/examples.md +11 -0
- package/skills/supabase-observability/SKILL.md +318 -196
- package/skills/supabase-observability/references/alert-configuration.md +40 -0
- package/skills/supabase-observability/references/errors.md +11 -0
- package/skills/supabase-observability/references/examples.md +13 -0
- package/skills/supabase-observability/references/metrics-collection.md +65 -0
- package/skills/supabase-performance-tuning/SKILL.md +304 -160
- package/skills/supabase-performance-tuning/references/caching-strategy.md +49 -0
- package/skills/supabase-performance-tuning/references/errors.md +11 -0
- package/skills/supabase-performance-tuning/references/examples.md +13 -0
- package/skills/supabase-policy-guardrails/SKILL.md +248 -221
- package/skills/supabase-policy-guardrails/references/ci-cost-security.md +484 -0
- package/skills/supabase-policy-guardrails/references/errors.md +11 -0
- package/skills/supabase-policy-guardrails/references/eslint-rules.md +46 -0
- package/skills/supabase-policy-guardrails/references/examples.md +10 -0
- package/skills/supabase-prod-checklist/SKILL.md +474 -84
- package/skills/supabase-prod-checklist/references/errors.md +63 -0
- package/skills/supabase-prod-checklist/references/examples.md +153 -0
- package/skills/supabase-prod-checklist/references/implementation.md +113 -0
- package/skills/supabase-rate-limits/SKILL.md +311 -98
- package/skills/supabase-rate-limits/references/errors.md +11 -0
- package/skills/supabase-rate-limits/references/examples.md +46 -0
- package/skills/supabase-rate-limits/references/implementation.md +66 -0
- package/skills/supabase-reference-architecture/SKILL.md +249 -182
- package/skills/supabase-reference-architecture/references/errors.md +29 -0
- package/skills/supabase-reference-architecture/references/examples.md +116 -0
- package/skills/supabase-reference-architecture/references/key-components.md +244 -0
- package/skills/supabase-reference-architecture/references/project-structure.md +109 -0
- package/skills/supabase-reliability-patterns/SKILL.md +229 -234
- package/skills/supabase-reliability-patterns/references/circuit-breaker.md +36 -0
- package/skills/supabase-reliability-patterns/references/dead-letter-queue.md +48 -0
- package/skills/supabase-reliability-patterns/references/errors.md +11 -0
- package/skills/supabase-reliability-patterns/references/examples.md +11 -0
- package/skills/supabase-reliability-patterns/references/idempotency-keys.md +36 -0
- package/skills/supabase-reliability-patterns/references/offline-degradation-health-dualwrite.md +489 -0
- package/skills/supabase-schema-from-requirements/SKILL.md +373 -34
- package/skills/supabase-sdk-patterns/SKILL.md +388 -99
- package/skills/supabase-sdk-patterns/references/errors.md +11 -0
- package/skills/supabase-sdk-patterns/references/examples.md +45 -0
- package/skills/supabase-sdk-patterns/references/implementation.md +67 -0
- package/skills/supabase-security-basics/SKILL.md +282 -102
- package/skills/supabase-security-basics/references/errors.md +10 -0
- package/skills/supabase-security-basics/references/examples.md +70 -0
- package/skills/supabase-security-basics/references/implementation.md +39 -0
- package/skills/supabase-upgrade-migration/SKILL.md +248 -66
- package/skills/supabase-upgrade-migration/references/errors.md +10 -0
- package/skills/supabase-upgrade-migration/references/examples.md +51 -0
- package/skills/supabase-upgrade-migration/references/implementation.md +29 -0
- package/skills/supabase-webhooks-events/SKILL.md +412 -138
- package/skills/supabase-webhooks-events/references/errors.md +55 -0
- package/skills/supabase-webhooks-events/references/event-handler-pattern.md +106 -0
- package/skills/supabase-webhooks-events/references/examples.md +133 -0
- package/skills/supabase-webhooks-events/references/signature-verification.md +165 -0
|
@@ -1,71 +1,410 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: supabase-schema-from-requirements
|
|
3
|
-
description:
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
description: 'Design Supabase Postgres schema from business requirements with migrations,
|
|
4
|
+
RLS, and types.
|
|
5
|
+
|
|
6
|
+
Use when translating specifications into database tables, creating migration files,
|
|
7
|
+
|
|
8
|
+
adding Row Level Security policies, or generating TypeScript types from schema.
|
|
9
|
+
|
|
10
|
+
Trigger with phrases like "supabase schema", "design database supabase",
|
|
11
|
+
|
|
12
|
+
"schema from requirements", "supabase migration", "supabase tables from spec".
|
|
13
|
+
|
|
14
|
+
'
|
|
15
|
+
allowed-tools: Read, Write, Edit, Bash(supabase:*), Bash(npx:*), Grep
|
|
10
16
|
version: 1.0.0
|
|
11
17
|
license: MIT
|
|
12
18
|
author: Jeremy Longshore <jeremy@intentsolutions.io>
|
|
19
|
+
tags:
|
|
20
|
+
- saas
|
|
21
|
+
- supabase
|
|
22
|
+
- database
|
|
23
|
+
- migration
|
|
24
|
+
- schema-design
|
|
25
|
+
- postgres
|
|
26
|
+
- rls
|
|
27
|
+
compatibility: Designed for Claude Code, also compatible with Codex and OpenClaw
|
|
13
28
|
---
|
|
14
|
-
|
|
15
29
|
# Supabase Schema from Requirements
|
|
16
30
|
|
|
17
31
|
## Overview
|
|
18
|
-
Generate Supabase database schema from natural language requirements.
|
|
19
|
-
This is the primary workflow for starting new Supabase projects.
|
|
20
32
|
|
|
33
|
+
Translate business requirements into a production-ready Postgres schema inside Supabase. This skill covers the full path from specification document to applied migration: entity extraction, table creation with proper data types and constraints, Row Level Security policies, performance indexes, timestamp triggers, and TypeScript type generation. It is the highest-leverage activity in the early stages of any Supabase project because every downstream feature (auth, storage, realtime, edge functions) depends on well-designed tables.
|
|
21
34
|
|
|
22
35
|
## Prerequisites
|
|
23
|
-
|
|
24
|
-
-
|
|
25
|
-
-
|
|
36
|
+
|
|
37
|
+
- Supabase CLI installed (`npm install -g supabase`) and project linked (`supabase link`)
|
|
38
|
+
- `@supabase/supabase-js` v2+ installed in the project
|
|
39
|
+
- Business requirements, PRD, or specification document identifying entities and access rules
|
|
40
|
+
- Local Supabase running (`supabase start`) or a linked remote project
|
|
26
41
|
|
|
27
42
|
## Instructions
|
|
28
43
|
|
|
29
|
-
### Step 1:
|
|
30
|
-
|
|
31
|
-
|
|
44
|
+
### Step 1: Parse Requirements and Create Migration
|
|
45
|
+
|
|
46
|
+
Read the requirements document and extract entities, attributes, relationships, and access control rules. Map each entity to a Postgres table.
|
|
47
|
+
|
|
48
|
+
**Entity extraction example** (project management app):
|
|
49
|
+
|
|
50
|
+
| Entity | Key Columns | Relationships |
|
|
51
|
+
|--------|------------|---------------|
|
|
52
|
+
| Organization | name, slug, plan | has many Projects, has many Members |
|
|
53
|
+
| Project | name, description, status | belongs to Organization, has many Tasks |
|
|
54
|
+
| Task | title, priority, status, due_date | belongs to Project, assigned to User |
|
|
55
|
+
| Member | role (owner/admin/member) | junction linking User to Organization |
|
|
56
|
+
|
|
57
|
+
Create the migration file:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npx supabase migration new create_tables
|
|
61
|
+
# Creates: supabase/migrations/<timestamp>_create_tables.sql
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Write the migration SQL using standard Postgres data types (`uuid`, `text`, `integer`, `boolean`, `timestamptz`, `jsonb`):
|
|
65
|
+
|
|
66
|
+
```sql
|
|
67
|
+
-- supabase/migrations/<timestamp>_create_tables.sql
|
|
68
|
+
|
|
69
|
+
-- Enable required extensions
|
|
70
|
+
create extension if not exists "uuid-ossp";
|
|
71
|
+
create extension if not exists "moddatetime";
|
|
72
|
+
|
|
73
|
+
-- Organizations
|
|
74
|
+
create table public.organizations (
|
|
75
|
+
id uuid default uuid_generate_v4() primary key,
|
|
76
|
+
name text not null,
|
|
77
|
+
slug text unique not null,
|
|
78
|
+
plan text default 'free' check (plan in ('free', 'pro', 'enterprise')),
|
|
79
|
+
metadata jsonb default '{}'::jsonb,
|
|
80
|
+
created_at timestamptz default now() not null,
|
|
81
|
+
updated_at timestamptz default now() not null
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
-- Organization members (junction table)
|
|
85
|
+
create table public.members (
|
|
86
|
+
id uuid default uuid_generate_v4() primary key,
|
|
87
|
+
organization_id uuid references public.organizations(id) on delete cascade not null,
|
|
88
|
+
user_id uuid references auth.users(id) on delete cascade not null,
|
|
89
|
+
role text default 'member' check (role in ('owner', 'admin', 'member')),
|
|
90
|
+
created_at timestamptz default now() not null,
|
|
91
|
+
unique (organization_id, user_id)
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
-- Projects
|
|
95
|
+
create table public.projects (
|
|
96
|
+
id uuid default uuid_generate_v4() primary key,
|
|
97
|
+
organization_id uuid references public.organizations(id) on delete cascade not null,
|
|
98
|
+
name text not null,
|
|
99
|
+
description text,
|
|
100
|
+
status text default 'active' check (status in ('active', 'archived', 'deleted')),
|
|
101
|
+
settings jsonb default '{}'::jsonb,
|
|
102
|
+
created_at timestamptz default now() not null,
|
|
103
|
+
updated_at timestamptz default now() not null
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
-- Tasks
|
|
107
|
+
create table public.tasks (
|
|
108
|
+
id uuid default uuid_generate_v4() primary key,
|
|
109
|
+
project_id uuid references public.projects(id) on delete cascade not null,
|
|
110
|
+
assigned_to uuid references auth.users(id) on delete set null,
|
|
111
|
+
title text not null,
|
|
112
|
+
description text,
|
|
113
|
+
priority integer default 0 check (priority between 0 and 4),
|
|
114
|
+
status text default 'todo' check (status in ('todo', 'in_progress', 'done', 'cancelled')),
|
|
115
|
+
due_date date,
|
|
116
|
+
tags text[] default '{}',
|
|
117
|
+
created_at timestamptz default now() not null,
|
|
118
|
+
updated_at timestamptz default now() not null
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
-- Indexes for common query patterns
|
|
122
|
+
create index idx_members_user on public.members(user_id);
|
|
123
|
+
create index idx_members_org on public.members(organization_id);
|
|
124
|
+
create index idx_projects_org on public.projects(organization_id);
|
|
125
|
+
create index idx_tasks_project on public.tasks(project_id);
|
|
126
|
+
create index idx_tasks_assigned on public.tasks(assigned_to);
|
|
127
|
+
create index idx_tasks_status on public.tasks(status) where status not in ('done', 'cancelled');
|
|
128
|
+
create index idx_tasks_due on public.tasks(due_date) where due_date is not null;
|
|
129
|
+
create index idx_orgs_slug on public.organizations(slug);
|
|
130
|
+
|
|
131
|
+
-- Automatic updated_at triggers via moddatetime extension
|
|
132
|
+
create trigger handle_updated_at before update on public.organizations
|
|
133
|
+
for each row execute procedure moddatetime(updated_at);
|
|
134
|
+
create trigger handle_updated_at before update on public.projects
|
|
135
|
+
for each row execute procedure moddatetime(updated_at);
|
|
136
|
+
create trigger handle_updated_at before update on public.tasks
|
|
137
|
+
for each row execute procedure moddatetime(updated_at);
|
|
32
138
|
```
|
|
33
139
|
|
|
34
|
-
|
|
140
|
+
**Data type selection guide:**
|
|
141
|
+
|
|
142
|
+
| Use case | Type | Notes |
|
|
143
|
+
|----------|------|-------|
|
|
144
|
+
| Primary keys | `uuid` | Always with `uuid_generate_v4()` default |
|
|
145
|
+
| Names, titles | `text` | Prefer over `varchar` in Postgres |
|
|
146
|
+
| Counts, ranks | `integer` | Use `bigint` for sequences |
|
|
147
|
+
| Flags | `boolean` | Default explicitly to `true` or `false` |
|
|
148
|
+
| Timestamps | `timestamptz` | Never use `timestamp` without timezone |
|
|
149
|
+
| Flexible data | `jsonb` | Queryable JSON; use for settings, metadata |
|
|
150
|
+
| Lists | `text[]` | Postgres arrays for simple tags or labels |
|
|
151
|
+
|
|
152
|
+
### Step 2: Add Row Level Security Policies
|
|
153
|
+
|
|
154
|
+
Every table exposed to the client must have RLS enabled. Write helper functions first to avoid repeating authorization logic across policies.
|
|
155
|
+
|
|
156
|
+
```sql
|
|
157
|
+
-- Helper: check if user is a member of an organization
|
|
158
|
+
create or replace function public.is_org_member(org_id uuid)
|
|
159
|
+
returns boolean as $$
|
|
160
|
+
select exists (
|
|
161
|
+
select 1 from public.members
|
|
162
|
+
where organization_id = org_id
|
|
163
|
+
and user_id = auth.uid()
|
|
164
|
+
);
|
|
165
|
+
$$ language sql security definer stable;
|
|
166
|
+
|
|
167
|
+
-- Helper: check if user is org admin or owner
|
|
168
|
+
create or replace function public.is_org_admin(org_id uuid)
|
|
169
|
+
returns boolean as $$
|
|
170
|
+
select exists (
|
|
171
|
+
select 1 from public.members
|
|
172
|
+
where organization_id = org_id
|
|
173
|
+
and user_id = auth.uid()
|
|
174
|
+
and role in ('owner', 'admin')
|
|
175
|
+
);
|
|
176
|
+
$$ language sql security definer stable;
|
|
177
|
+
|
|
178
|
+
-- Organizations RLS
|
|
179
|
+
alter table public.organizations enable row level security;
|
|
180
|
+
|
|
181
|
+
create policy "Users read own orgs"
|
|
182
|
+
on public.organizations for select
|
|
183
|
+
using (public.is_org_member(id));
|
|
184
|
+
|
|
185
|
+
create policy "Authenticated users create orgs"
|
|
186
|
+
on public.organizations for insert
|
|
187
|
+
with check (auth.uid() is not null);
|
|
188
|
+
|
|
189
|
+
create policy "Admins update orgs"
|
|
190
|
+
on public.organizations for update
|
|
191
|
+
using (public.is_org_admin(id));
|
|
192
|
+
|
|
193
|
+
create policy "Owners delete orgs"
|
|
194
|
+
on public.organizations for delete
|
|
195
|
+
using (
|
|
196
|
+
exists (
|
|
197
|
+
select 1 from public.members
|
|
198
|
+
where organization_id = id
|
|
199
|
+
and user_id = auth.uid()
|
|
200
|
+
and role = 'owner'
|
|
201
|
+
)
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
-- Members RLS
|
|
205
|
+
alter table public.members enable row level security;
|
|
206
|
+
|
|
207
|
+
create policy "Members view org roster"
|
|
208
|
+
on public.members for select
|
|
209
|
+
using (public.is_org_member(organization_id));
|
|
210
|
+
|
|
211
|
+
create policy "Admins manage members"
|
|
212
|
+
on public.members for all
|
|
213
|
+
using (public.is_org_admin(organization_id));
|
|
214
|
+
|
|
215
|
+
-- Projects RLS
|
|
216
|
+
alter table public.projects enable row level security;
|
|
217
|
+
|
|
218
|
+
create policy "Members view projects"
|
|
219
|
+
on public.projects for select
|
|
220
|
+
using (public.is_org_member(organization_id));
|
|
221
|
+
|
|
222
|
+
create policy "Admins manage projects"
|
|
223
|
+
on public.projects for all
|
|
224
|
+
using (public.is_org_admin(organization_id));
|
|
225
|
+
|
|
226
|
+
-- Tasks RLS
|
|
227
|
+
alter table public.tasks enable row level security;
|
|
228
|
+
|
|
229
|
+
create policy "Members view tasks"
|
|
230
|
+
on public.tasks for select
|
|
231
|
+
using (
|
|
232
|
+
exists (
|
|
233
|
+
select 1 from public.projects p
|
|
234
|
+
where p.id = project_id
|
|
235
|
+
and public.is_org_member(p.organization_id)
|
|
236
|
+
)
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
create policy "Members create tasks"
|
|
240
|
+
on public.tasks for insert
|
|
241
|
+
with check (
|
|
242
|
+
exists (
|
|
243
|
+
select 1 from public.projects p
|
|
244
|
+
where p.id = project_id
|
|
245
|
+
and public.is_org_member(p.organization_id)
|
|
246
|
+
)
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
create policy "Assignee or admin updates tasks"
|
|
250
|
+
on public.tasks for update
|
|
251
|
+
using (
|
|
252
|
+
assigned_to = auth.uid()
|
|
253
|
+
or exists (
|
|
254
|
+
select 1 from public.projects p
|
|
255
|
+
where p.id = project_id
|
|
256
|
+
and public.is_org_admin(p.organization_id)
|
|
257
|
+
)
|
|
258
|
+
);
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
**RLS policy naming convention:** Use short, descriptive names that state who and what action: `"Users read own"`, `"Admins manage members"`, `"Assignee updates tasks"`.
|
|
262
|
+
|
|
263
|
+
### Step 3: Apply Migration and Generate Types
|
|
264
|
+
|
|
265
|
+
```bash
|
|
266
|
+
# Apply migration locally
|
|
267
|
+
npx supabase db reset
|
|
268
|
+
|
|
269
|
+
# Or push to a linked remote project
|
|
270
|
+
npx supabase db push
|
|
271
|
+
|
|
272
|
+
# Generate TypeScript types from the live schema
|
|
273
|
+
npx supabase gen types typescript --local > types/supabase.ts
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
Use the generated types with the Supabase client:
|
|
277
|
+
|
|
35
278
|
```typescript
|
|
36
|
-
|
|
279
|
+
import { createClient } from '@supabase/supabase-js'
|
|
280
|
+
import type { Database } from './types/supabase'
|
|
281
|
+
|
|
282
|
+
const supabase = createClient<Database>(
|
|
283
|
+
process.env.NEXT_PUBLIC_SUPABASE_URL!,
|
|
284
|
+
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
// Typed insert
|
|
288
|
+
const { data: org, error } = await supabase
|
|
289
|
+
.from('organizations')
|
|
290
|
+
.insert({ name: 'Acme Corp', slug: 'acme', plan: 'pro' })
|
|
291
|
+
.select()
|
|
292
|
+
.single()
|
|
293
|
+
|
|
294
|
+
// Typed select with foreign key join
|
|
295
|
+
const { data: tasks } = await supabase
|
|
296
|
+
.from('tasks')
|
|
297
|
+
.select('*, project:project_id(name, organization_id)')
|
|
298
|
+
.eq('status', 'todo')
|
|
299
|
+
.order('due_date', { ascending: true })
|
|
300
|
+
|
|
301
|
+
// Nested join across multiple tables
|
|
302
|
+
const { data: orgWithProjects } = await supabase
|
|
303
|
+
.from('organizations')
|
|
304
|
+
.select(`
|
|
305
|
+
id, name, slug,
|
|
306
|
+
projects:projects(
|
|
307
|
+
id, name, status,
|
|
308
|
+
tasks:tasks(id, title, status, assigned_to)
|
|
309
|
+
)
|
|
310
|
+
`)
|
|
311
|
+
.eq('slug', 'acme')
|
|
312
|
+
.single()
|
|
37
313
|
```
|
|
38
314
|
|
|
39
|
-
|
|
315
|
+
Verify RLS is working:
|
|
316
|
+
|
|
40
317
|
```typescript
|
|
41
|
-
//
|
|
318
|
+
// This should return only rows the authenticated user can see
|
|
319
|
+
const { data, error } = await supabase.from('organizations').select('*')
|
|
320
|
+
|
|
321
|
+
if (error) {
|
|
322
|
+
console.error('RLS check failed:', error.message)
|
|
323
|
+
}
|
|
324
|
+
console.log(`User can see ${data?.length ?? 0} organizations`)
|
|
42
325
|
```
|
|
43
326
|
|
|
44
327
|
## Output
|
|
45
|
-
|
|
46
|
-
-
|
|
47
|
-
-
|
|
328
|
+
|
|
329
|
+
- SQL migration file under `supabase/migrations/` with all tables, constraints, and indexes
|
|
330
|
+
- RLS policies matching the access control rules from requirements
|
|
331
|
+
- Helper functions (`is_org_member`, `is_org_admin`) for reusable authorization checks
|
|
332
|
+
- `moddatetime` triggers for automatic `updated_at` management
|
|
333
|
+
- Generated TypeScript types at `types/supabase.ts` for type-safe client queries
|
|
334
|
+
- Partial indexes on frequently filtered columns (status, due_date)
|
|
48
335
|
|
|
49
336
|
## Error Handling
|
|
337
|
+
|
|
50
338
|
| Error | Cause | Solution |
|
|
51
339
|
|-------|-------|----------|
|
|
52
|
-
|
|
|
53
|
-
|
|
|
340
|
+
| `42P07: relation already exists` | Table name collision in migration | Use `create table if not exists` or rename the table |
|
|
341
|
+
| `23503: foreign key violation` | Insert references a nonexistent parent row | Insert parent rows first, or check the UUID |
|
|
342
|
+
| `42501: insufficient privilege` | RLS helper function permissions | Add `security definer` to the function definition |
|
|
343
|
+
| `42883: function uuid_generate_v4() does not exist` | Extension not enabled | Add `create extension if not exists "uuid-ossp"` |
|
|
344
|
+
| `supabase db push` succeeds but tables missing | Migration was already recorded | Check `supabase_migrations.schema_migrations` and repair |
|
|
345
|
+
| `PGRST204: column not found` | TypeScript types out of sync | Re-run `npx supabase gen types typescript --local > types/supabase.ts` |
|
|
346
|
+
| `new row violates row-level security` | RLS policy blocks the operation | Verify `auth.uid()` matches expected value; check policy `using` clause |
|
|
347
|
+
| `moddatetime` trigger fails | Extension not enabled | Add `create extension if not exists "moddatetime"` at top of migration |
|
|
54
348
|
|
|
55
349
|
## Examples
|
|
56
350
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
351
|
+
**E-commerce schema** (different domain, same pattern):
|
|
352
|
+
|
|
353
|
+
```sql
|
|
354
|
+
create table public.products (
|
|
355
|
+
id uuid default uuid_generate_v4() primary key,
|
|
356
|
+
store_id uuid references public.stores(id) on delete cascade not null,
|
|
357
|
+
name text not null,
|
|
358
|
+
price integer not null check (price >= 0), -- cents
|
|
359
|
+
currency text default 'usd',
|
|
360
|
+
inventory integer default 0 check (inventory >= 0),
|
|
361
|
+
metadata jsonb default '{}'::jsonb,
|
|
362
|
+
is_active boolean default true,
|
|
363
|
+
created_at timestamptz default now() not null,
|
|
364
|
+
updated_at timestamptz default now() not null
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
alter table public.products enable row level security;
|
|
368
|
+
|
|
369
|
+
create policy "Anyone reads active products"
|
|
370
|
+
on public.products for select
|
|
371
|
+
using (is_active = true);
|
|
372
|
+
|
|
373
|
+
create policy "Store owners manage products"
|
|
374
|
+
on public.products for all
|
|
375
|
+
using (
|
|
376
|
+
exists (
|
|
377
|
+
select 1 from public.stores s
|
|
378
|
+
where s.id = store_id and s.owner_id = auth.uid()
|
|
379
|
+
)
|
|
380
|
+
);
|
|
381
|
+
|
|
382
|
+
create trigger handle_updated_at before update on public.products
|
|
383
|
+
for each row execute procedure moddatetime(updated_at);
|
|
60
384
|
```
|
|
61
385
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
386
|
+
**Querying the e-commerce schema from the client:**
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
// Products with store info
|
|
390
|
+
const { data: products } = await supabase
|
|
391
|
+
.from('products')
|
|
392
|
+
.select('*, store:store_id(name, slug)')
|
|
393
|
+
.eq('is_active', true)
|
|
394
|
+
.gte('inventory', 1)
|
|
395
|
+
.order('price', { ascending: true })
|
|
396
|
+
.limit(20)
|
|
397
|
+
```
|
|
65
398
|
|
|
66
399
|
## Resources
|
|
67
|
-
|
|
68
|
-
- [
|
|
400
|
+
|
|
401
|
+
- [Row Level Security Guide](https://supabase.com/docs/guides/database/postgres/row-level-security)
|
|
402
|
+
- [Database Migrations](https://supabase.com/docs/guides/deployment/database-migrations)
|
|
403
|
+
- [Supabase CLI Reference](https://supabase.com/docs/reference/cli)
|
|
404
|
+
- [JavaScript Client — Select](https://supabase.com/docs/reference/javascript/select)
|
|
405
|
+
- [TypeScript Support](https://supabase.com/docs/guides/api/rest/generating-types)
|
|
406
|
+
- [PostgreSQL Data Types](https://www.postgresql.org/docs/current/datatype.html)
|
|
69
407
|
|
|
70
408
|
## Next Steps
|
|
71
|
-
|
|
409
|
+
|
|
410
|
+
For auth integration, file storage, and realtime subscriptions on top of this schema, proceed to `supabase-auth-storage-realtime-core`.
|