@polymorphism-tech/morph-spec 4.3.4 → 4.3.6

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.
Files changed (164) hide show
  1. package/.morph/.morphversion +5 -0
  2. package/.morph/config/agents.json +948 -0
  3. package/.morph/config/config.json +9 -9
  4. package/.morph/project/context/README.md +17 -0
  5. package/.morph/project/context/detection-log.md +16 -0
  6. package/.morph/project/standards/inferred.md +59 -0
  7. package/.morph/standards/ai-agents/blazor-ui.md +364 -0
  8. package/.morph/standards/ai-agents/production.md +415 -0
  9. package/.morph/standards/ai-agents/setup.md +418 -0
  10. package/.morph/standards/ai-agents/team-orchestration.md +479 -0
  11. package/.morph/standards/ai-agents/workflows.md +354 -0
  12. package/.morph/standards/architecture/ddd/aggregates.md +120 -0
  13. package/.morph/standards/architecture/ddd/entities.md +99 -0
  14. package/.morph/standards/architecture/ddd/value-objects.md +124 -0
  15. package/.morph/standards/backend/api/minimal-api.md +494 -0
  16. package/.morph/standards/backend/api/rest.md +492 -0
  17. package/.morph/standards/backend/api/validation.md +88 -0
  18. package/.morph/standards/backend/authentication/passkeys.md +428 -0
  19. package/.morph/standards/backend/database/ef-core.md +199 -0
  20. package/.morph/standards/backend/database/migrations.md +393 -0
  21. package/.morph/standards/backend/database/postgresql/database.md +352 -0
  22. package/.morph/standards/backend/database/repository-patterns.md +528 -0
  23. package/.morph/standards/backend/database/vector-search-rag.md +541 -0
  24. package/.morph/standards/backend/dotnet/async.md +366 -0
  25. package/.morph/standards/backend/dotnet/core.md +117 -0
  26. package/.morph/standards/backend/dotnet/di.md +439 -0
  27. package/.morph/standards/backend/dotnet/program-cs-checklist.md +92 -0
  28. package/.morph/standards/backend/integrations/asaas/asaas-api.md +216 -0
  29. package/.morph/standards/backend/integrations/clerk/clerk-auth.md +290 -0
  30. package/.morph/standards/backend/integrations/hangfire/hangfire-jobs.md +350 -0
  31. package/.morph/standards/backend/integrations/resend/resend-email.md +385 -0
  32. package/.morph/standards/context/analytics.md +96 -0
  33. package/.morph/standards/context/bundles.md +110 -0
  34. package/.morph/standards/context/priming.md +78 -0
  35. package/.morph/standards/core/architecture.md +185 -0
  36. package/.morph/standards/core/coding.md +214 -0
  37. package/.morph/standards/core/git-branching-strategy.md +403 -0
  38. package/.morph/standards/core/git.md +185 -0
  39. package/.morph/standards/core/testing.md +295 -0
  40. package/.morph/standards/data/nosql/blob-storage.md +102 -0
  41. package/.morph/standards/data/nosql/cache/redis.md +97 -0
  42. package/.morph/standards/data/nosql/cosmos-db.md +118 -0
  43. package/.morph/standards/data/vector-search/azure-ai-search.md +121 -0
  44. package/.morph/standards/data/vector-search/rag-chunking.md +104 -0
  45. package/.morph/standards/frontend/blazor/design-checklist.md +222 -0
  46. package/.morph/standards/frontend/blazor/fluent-ui-setup.md +595 -0
  47. package/.morph/standards/frontend/blazor/fluent-ui.md +137 -0
  48. package/.morph/standards/frontend/blazor/html-conversion.md +184 -0
  49. package/.morph/standards/frontend/blazor/lifecycle.md +195 -0
  50. package/.morph/standards/frontend/blazor/pitfalls.md +198 -0
  51. package/.morph/standards/frontend/blazor/state.md +191 -0
  52. package/.morph/standards/frontend/design-system/animations.md +151 -0
  53. package/.morph/standards/frontend/design-system/naming.md +64 -0
  54. package/.morph/standards/frontend/nextjs/nextjs-patterns.md +198 -0
  55. package/.morph/standards/infrastructure/azure/azure.md +624 -0
  56. package/.morph/standards/infrastructure/azure/bicep/bicep-patterns.md +422 -0
  57. package/.morph/standards/infrastructure/azure/devops/azure-devops-setup.md +516 -0
  58. package/.morph/standards/infrastructure/azure/devops/local-development.md +520 -0
  59. package/.morph/standards/infrastructure/azure/services/functions.md +486 -0
  60. package/.morph/standards/infrastructure/azure/services/service-bus.md +459 -0
  61. package/.morph/standards/infrastructure/azure/services/storage.md +407 -0
  62. package/.morph/standards/infrastructure/docker/easypanel-deploy.md +196 -0
  63. package/.morph/standards/infrastructure/supabase/mcp-setup.md +252 -0
  64. package/.morph/standards/infrastructure/supabase/supabase-auth.md +176 -0
  65. package/.morph/standards/infrastructure/supabase/supabase-pgvector.md +169 -0
  66. package/.morph/standards/infrastructure/supabase/supabase-rls.md +184 -0
  67. package/.morph/standards/infrastructure/supabase/supabase-storage.md +153 -0
  68. package/.morph/standards/integration/api/graphql.md +91 -0
  69. package/.morph/standards/integration/api/grpc.md +114 -0
  70. package/.morph/standards/integration/api/rest-design.md +95 -0
  71. package/.morph/standards/integration/event-driven/cqrs.md +101 -0
  72. package/.morph/standards/integration/event-driven/event-sourcing.md +124 -0
  73. package/.morph/standards/integration/event-driven/service-bus.md +95 -0
  74. package/.morph/standards/observability/logging.md +131 -0
  75. package/.morph/standards/observability/metrics.md +121 -0
  76. package/.morph/standards/observability/monitoring.md +114 -0
  77. package/.morph/standards/observability/tracing.md +132 -0
  78. package/.morph/standards/workflows/parallel-execution.md +112 -0
  79. package/.morph/standards/workflows/thread-management.md +113 -0
  80. package/.morph/templates/.idea/morph-templates.xml +92 -0
  81. package/.morph/templates/.vscode/morph-templates.code-snippets +186 -0
  82. package/.morph/templates/IDE-SNIPPETS.md +266 -0
  83. package/.morph/templates/README.md +814 -0
  84. package/.morph/templates/REGISTRY.json +1677 -0
  85. package/.morph/templates/code/dotnet/backend/repository.cs +141 -0
  86. package/.morph/templates/code/dotnet/backend/service.cs +139 -0
  87. package/.morph/templates/code/dotnet/contracts/Commands.cs +74 -0
  88. package/.morph/templates/code/dotnet/contracts/Entities.cs +25 -0
  89. package/.morph/templates/code/dotnet/contracts/Queries.cs +74 -0
  90. package/.morph/templates/code/dotnet/contracts/README.md +74 -0
  91. package/.morph/templates/code/dotnet/contracts/api-contracts.cs +173 -0
  92. package/.morph/templates/code/dotnet/contracts/contracts.cs +217 -0
  93. package/.morph/templates/code/dotnet/database/migration.cs +83 -0
  94. package/.morph/templates/code/dotnet/frontend/component.razor +239 -0
  95. package/.morph/templates/code/dotnet/jobs/agent.cs +163 -0
  96. package/.morph/templates/code/dotnet/jobs/job.cs +171 -0
  97. package/.morph/templates/code/dotnet/test.cs +239 -0
  98. package/.morph/templates/code/sql/rls-policy.sql +57 -0
  99. package/.morph/templates/code/sql/supabase-migration.sql +100 -0
  100. package/.morph/templates/code/sql/supabase-migration.template.sql +113 -0
  101. package/.morph/templates/code/typescript/contracts.ts +168 -0
  102. package/.morph/templates/context/CONTEXT-FEATURE.md +276 -0
  103. package/.morph/templates/context/CONTEXT.md +181 -0
  104. package/.morph/templates/docs/proposal.md +182 -0
  105. package/.morph/templates/docs/spec.md +149 -0
  106. package/.morph/templates/examples/design-system-examples.md +357 -0
  107. package/.morph/templates/examples/spec-examples.md +90 -0
  108. package/.morph/templates/feature/decisions.md +187 -0
  109. package/.morph/templates/feature/recap.md +146 -0
  110. package/.morph/templates/feature/tasks.md +199 -0
  111. package/.morph/templates/infrastructure/azure/Dockerfile.example +82 -0
  112. package/.morph/templates/infrastructure/azure/README.md +286 -0
  113. package/.morph/templates/infrastructure/azure/app-insights.bicep +63 -0
  114. package/.morph/templates/infrastructure/azure/app-service.bicep +164 -0
  115. package/.morph/templates/infrastructure/azure/container-app-env.bicep +49 -0
  116. package/.morph/templates/infrastructure/azure/container-app.bicep +156 -0
  117. package/.morph/templates/infrastructure/azure/deploy-checklist.md +426 -0
  118. package/.morph/templates/infrastructure/azure/deploy.ps1 +229 -0
  119. package/.morph/templates/infrastructure/azure/deploy.sh +208 -0
  120. package/.morph/templates/infrastructure/azure/key-vault.bicep +91 -0
  121. package/.morph/templates/infrastructure/azure/main.bicep +189 -0
  122. package/.morph/templates/infrastructure/azure/parameters.dev.json +29 -0
  123. package/.morph/templates/infrastructure/azure/parameters.prod.json +29 -0
  124. package/.morph/templates/infrastructure/azure/parameters.staging.json +29 -0
  125. package/.morph/templates/infrastructure/azure/sql-database.bicep +103 -0
  126. package/.morph/templates/infrastructure/azure/storage.bicep +106 -0
  127. package/.morph/templates/infrastructure/docker/Dockerfile.template +58 -0
  128. package/.morph/templates/infrastructure/docker/docker-compose.template.yml +67 -0
  129. package/.morph/templates/infrastructure/docker/dockerfile-api.dockerfile +38 -0
  130. package/.morph/templates/infrastructure/docker/dockerfile-web.dockerfile +48 -0
  131. package/.morph/templates/infrastructure/docker/easypanel.template.json +54 -0
  132. package/.morph/templates/infrastructure/github/README.md +593 -0
  133. package/.morph/templates/infrastructure/github/actions/azure-auth/action.yml.hbs +22 -0
  134. package/.morph/templates/infrastructure/github/actions/docker-build-push/action.yml.hbs +45 -0
  135. package/.morph/templates/infrastructure/github/actions/health-check/action.yml.hbs +27 -0
  136. package/.morph/templates/infrastructure/github/workflows/deploy-azure-app-service.yml.hbs +61 -0
  137. package/.morph/templates/infrastructure/github/workflows/deploy-easypanel.yml.hbs +31 -0
  138. package/.morph/templates/infrastructure/github/workflows/docker-build-push.yml.hbs +59 -0
  139. package/.morph/templates/infrastructure/github/workflows/dotnet-build.yml.hbs +39 -0
  140. package/.morph/templates/integrations/asaas-client.cs +387 -0
  141. package/.morph/templates/integrations/asaas-webhook.cs +351 -0
  142. package/.morph/templates/integrations/azure-identity-config.cs +288 -0
  143. package/.morph/templates/integrations/clerk-config.cs +258 -0
  144. package/.morph/templates/meta-prompts/fusion/fusion-agent.md +76 -0
  145. package/.morph/templates/meta-prompts/fusion/fusion-aggregator.md +100 -0
  146. package/.morph/templates/meta-prompts/hops/hop-retry.md +78 -0
  147. package/.morph/templates/meta-prompts/hops/hop-validation.md +97 -0
  148. package/.morph/templates/meta-prompts/hops/hop-wrapper.md +36 -0
  149. package/.morph/templates/meta-prompts/parallel-workers/parallel-coordinator.md +113 -0
  150. package/.morph/templates/meta-prompts/parallel-workers/parallel-worker.md +80 -0
  151. package/.morph/templates/meta-prompts/squad-leaders/backend-squad.md +90 -0
  152. package/.morph/templates/meta-prompts/squad-leaders/frontend-squad.md +126 -0
  153. package/.morph/templates/meta-prompts/squad-leaders/squad-leader.md +43 -0
  154. package/.morph/templates/meta-prompts/validators/checkpoint-validator.md +107 -0
  155. package/.morph/templates/meta-prompts/validators/pre-commit-validator.md +95 -0
  156. package/.morph/templates/saas/subscription.cs +347 -0
  157. package/.morph/templates/saas/tenant.cs +338 -0
  158. package/.morph/templates/state.template.json +17 -0
  159. package/.morph/templates/ui/FluentDesignTheme.cs +149 -0
  160. package/.morph/templates/ui/MudTheme.cs +281 -0
  161. package/.morph/templates/ui/design-system.css +226 -0
  162. package/bin/morph-spec.js +1 -1
  163. package/package.json +1 -1
  164. package/src/commands/project/update.js +185 -46
@@ -0,0 +1,252 @@
1
+ # Supabase MCP Server Setup Guide
2
+
3
+ > **Scope:** nextjs-supabase
4
+ > **Layer:** 2 (on keyword)
5
+ > **Keywords:** supabase, mcp, setup, model context protocol
6
+ > **Load When:** supabase mcp setup keywords detected
7
+
8
+ Direct database management from Claude Code via Model Context Protocol
9
+
10
+ ## What It Provides
11
+
12
+ The Supabase MCP Server lets Claude Code interact directly with your Supabase project's database. This enables:
13
+
14
+ - Running SQL queries and viewing results
15
+ - Creating and managing database migrations
16
+ - Inspecting table schemas, indexes, and RLS policies
17
+ - Managing Supabase Auth, Storage, and Edge Functions
18
+ - Schema diffing and migration generation
19
+
20
+ ## CRITICAL WARNING
21
+
22
+ ```
23
+ ==========================================================
24
+ NEVER connect to a PRODUCTION Supabase project via MCP.
25
+
26
+ MCP provides direct database access. A single bad query
27
+ can DROP tables, DELETE data, or DISABLE RLS.
28
+
29
+ ONLY connect to:
30
+ - Local Supabase (supabase start)
31
+ - Development/staging projects
32
+ - Disposable test projects
33
+ ==========================================================
34
+ ```
35
+
36
+ ## Setup
37
+
38
+ ### 1. Get Your Supabase Connection Details
39
+
40
+ From your Supabase Dashboard > Project Settings > API:
41
+ - **Project URL**: `https://xxxxxxxxxxxx.supabase.co`
42
+ - **Service Role Key**: `eyJhbGc...` (Settings > API > service_role key)
43
+
44
+ For local Supabase (`supabase start`):
45
+ - **Project URL**: `http://127.0.0.1:54321`
46
+ - **Service Role Key**: Output from `supabase status`
47
+
48
+ ### 2. Configure Claude Desktop / Claude Code
49
+
50
+ Add the Supabase MCP server to your configuration.
51
+
52
+ **Claude Desktop** (`claude_desktop_config.json`):
53
+
54
+ ```json
55
+ {
56
+ "mcpServers": {
57
+ "supabase": {
58
+ "command": "npx",
59
+ "args": [
60
+ "-y",
61
+ "@supabase/mcp-server-supabase@latest",
62
+ "--supabase-url",
63
+ "https://xxxxxxxxxxxx.supabase.co",
64
+ "--supabase-service-role-key",
65
+ "eyJhbGc...",
66
+ "--read-only"
67
+ ]
68
+ }
69
+ }
70
+ }
71
+ ```
72
+
73
+ **Claude Code** (`.claude/settings.local.json`):
74
+
75
+ ```json
76
+ {
77
+ "mcpServers": {
78
+ "supabase": {
79
+ "command": "npx",
80
+ "args": [
81
+ "-y",
82
+ "@supabase/mcp-server-supabase@latest",
83
+ "--supabase-url",
84
+ "https://xxxxxxxxxxxx.supabase.co",
85
+ "--supabase-service-role-key",
86
+ "eyJhbGc...",
87
+ "--read-only"
88
+ ]
89
+ }
90
+ }
91
+ }
92
+ ```
93
+
94
+ ### 3. Default: Read-Only Mode
95
+
96
+ The `--read-only` flag is included by default. In this mode:
97
+
98
+ | Operation | Allowed |
99
+ |-----------|---------|
100
+ | SELECT queries | Yes |
101
+ | DESCRIBE / schema inspection | Yes |
102
+ | LIST tables, policies, indexes | Yes |
103
+ | INSERT / UPDATE / DELETE | No |
104
+ | CREATE / ALTER / DROP | No |
105
+ | Migration creation | No |
106
+
107
+ To enable write access (development only), remove `--read-only`:
108
+
109
+ ```json
110
+ {
111
+ "mcpServers": {
112
+ "supabase": {
113
+ "command": "npx",
114
+ "args": [
115
+ "-y",
116
+ "@supabase/mcp-server-supabase@latest",
117
+ "--supabase-url",
118
+ "http://127.0.0.1:54321",
119
+ "--supabase-service-role-key",
120
+ "your-local-service-role-key"
121
+ ]
122
+ }
123
+ }
124
+ }
125
+ ```
126
+
127
+ Write mode should ONLY be used with local Supabase or disposable dev projects.
128
+
129
+ ## Available Tool Groups
130
+
131
+ The Supabase MCP Server exposes these tool groups:
132
+
133
+ ### Database Tools
134
+
135
+ | Tool | Description |
136
+ |------|-------------|
137
+ | `list_tables` | List all tables in the project |
138
+ | `list_extensions` | List installed PostgreSQL extensions |
139
+ | `list_migrations` | List applied migrations |
140
+ | `apply_migration` | Apply a new SQL migration |
141
+ | `execute_sql` | Run arbitrary SQL (read-only by default) |
142
+ | `get_migration_status` | Check pending vs applied migrations |
143
+
144
+ ### Auth Tools
145
+
146
+ | Tool | Description |
147
+ |------|-------------|
148
+ | `get_auth_config` | View auth provider configuration |
149
+ | `list_auth_users` | List authenticated users |
150
+
151
+ ### Storage Tools
152
+
153
+ | Tool | Description |
154
+ |------|-------------|
155
+ | `list_storage_buckets` | List storage buckets |
156
+ | `get_storage_config` | View storage configuration |
157
+
158
+ ### Project Tools
159
+
160
+ | Tool | Description |
161
+ |------|-------------|
162
+ | `get_project_config` | View project settings |
163
+ | `get_project_api_keys` | View API keys |
164
+
165
+ ## Usage for Migrations and Schema Management
166
+
167
+ ### Inspecting Current Schema
168
+
169
+ Ask Claude Code to use the MCP tools:
170
+
171
+ ```
172
+ "Show me all tables and their RLS policies"
173
+ "What indexes exist on the documents table?"
174
+ "List all migrations applied so far"
175
+ ```
176
+
177
+ ### Creating Migrations
178
+
179
+ With write mode enabled on a local/dev project:
180
+
181
+ ```
182
+ "Create a migration to add a 'tags' column to the documents table"
183
+ "Add an RLS policy so only tenant admins can delete documents"
184
+ "Enable the pgvector extension and create the embedding column"
185
+ ```
186
+
187
+ The MCP server will generate and apply SQL migrations through Supabase's migration system.
188
+
189
+ ### Schema Diffing
190
+
191
+ ```
192
+ "Compare the current schema against the migration files"
193
+ "Show me what would change if I apply the pending migration"
194
+ ```
195
+
196
+ ## Usage with MORPH-SPEC Workflow
197
+
198
+ During MORPH-SPEC phases, the MCP server is useful for:
199
+
200
+ | Phase | MCP Usage |
201
+ |-------|-----------|
202
+ | **Phase 2 (Design)** | Inspect existing schema to inform spec |
203
+ | **Phase 4 (Tasks)** | Verify migration dependencies |
204
+ | **Phase 5 (Implement)** | Apply migrations, verify RLS policies, test queries |
205
+
206
+ ### Example Workflow
207
+
208
+ ```
209
+ 1. Design phase: "List current tables and extensions"
210
+ 2. Write migration SQL in supabase/migrations/
211
+ 3. Apply via MCP: "Apply migration 003_add_search_logs.sql"
212
+ 4. Verify: "Show RLS policies on search_logs table"
213
+ 5. Test: "Run SELECT * FROM search_logs LIMIT 5"
214
+ ```
215
+
216
+ ## Security Best Practices
217
+
218
+ 1. **Always use `--read-only` unless actively running migrations**
219
+ 2. **Never store service_role keys in version control** -- use environment variables or a secrets manager
220
+ 3. **Use local Supabase for development** (`supabase start`) instead of cloud projects
221
+ 4. **Rotate keys if accidentally exposed** -- Supabase Dashboard > Settings > API > Regenerate
222
+ 5. **Restrict MCP to development machines only** -- never configure on CI/CD or shared servers
223
+ 6. **Review SQL before applying** -- the MCP server executes SQL with service_role privileges, bypassing all RLS
224
+
225
+ ### Environment Variable Alternative
226
+
227
+ Instead of hardcoding keys in the config file:
228
+
229
+ ```json
230
+ {
231
+ "mcpServers": {
232
+ "supabase": {
233
+ "command": "npx",
234
+ "args": [
235
+ "-y",
236
+ "@supabase/mcp-server-supabase@latest",
237
+ "--read-only"
238
+ ],
239
+ "env": {
240
+ "SUPABASE_URL": "${SUPABASE_URL}",
241
+ "SUPABASE_SERVICE_ROLE_KEY": "${SUPABASE_SERVICE_ROLE_KEY}"
242
+ }
243
+ }
244
+ }
245
+ }
246
+ ```
247
+
248
+ Set the environment variables in your shell profile or `.env` file (never committed).
249
+
250
+ ---
251
+
252
+ *MORPH-SPEC by Polymorphism Tech*
@@ -0,0 +1,176 @@
1
+ # Supabase Authentication Standard
2
+
3
+ > **Scope:** nextjs-supabase
4
+ > **Layer:** 2 (on keyword)
5
+ > **Keywords:** supabase, auth, rls, user, authentication
6
+ > **Load When:** supabase auth keywords detected
7
+
8
+ Stack: Next.js 15 + Supabase + .NET Backend
9
+
10
+ ## Core Rules
11
+
12
+ - NEVER use `supabase.auth.getSession()` on server -- reads from cookies without validation
13
+ - ALWAYS use `supabase.auth.getUser()` on server -- validates JWT with Supabase
14
+ - NEVER expose `service_role` key on frontend -- bypasses RLS
15
+ - ALWAYS use `@supabase/ssr` for Next.js -- not `@supabase/auth-helpers-nextjs` (deprecated)
16
+ - ALWAYS use PKCE flow for SSR auth
17
+
18
+ ## Client Setup
19
+
20
+ ### Browser Client
21
+
22
+ ```ts
23
+ // lib/supabase/client.ts
24
+ import { createBrowserClient } from "@supabase/ssr";
25
+
26
+ export function createClient() {
27
+ return createBrowserClient(
28
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
29
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
30
+ );
31
+ }
32
+ ```
33
+
34
+ ### Server Client
35
+
36
+ ```ts
37
+ // lib/supabase/server.ts
38
+ import { createServerClient } from "@supabase/ssr";
39
+ import { cookies } from "next/headers";
40
+
41
+ export async function createClient() {
42
+ const cookieStore = await cookies();
43
+ return createServerClient(
44
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
45
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
46
+ {
47
+ cookies: {
48
+ getAll() { return cookieStore.getAll(); },
49
+ setAll(cookiesToSet) {
50
+ cookiesToSet.forEach(({ name, value, options }) =>
51
+ cookieStore.set(name, value, options));
52
+ },
53
+ },
54
+ }
55
+ );
56
+ }
57
+ ```
58
+
59
+ ## Auth Flows
60
+
61
+ ```ts
62
+ // Email/Password sign up
63
+ await supabase.auth.signUp({ email, password,
64
+ options: { emailRedirectTo: `${origin}/auth/callback` } });
65
+
66
+ // Email/Password sign in
67
+ await supabase.auth.signInWithPassword({ email, password });
68
+
69
+ // OAuth (Google / GitHub)
70
+ await supabase.auth.signInWithOAuth({
71
+ provider: "google", // or "github"
72
+ options: { redirectTo: `${origin}/auth/callback`,
73
+ queryParams: { access_type: "offline", prompt: "consent" } } // Google-specific
74
+ });
75
+
76
+ // Magic Link
77
+ await supabase.auth.signInWithOtp({ email,
78
+ options: { emailRedirectTo: `${origin}/auth/callback` } });
79
+ ```
80
+
81
+ ## Auth Callback Route (PKCE)
82
+
83
+ ```ts
84
+ // app/auth/callback/route.ts
85
+ import { createClient } from "@/lib/supabase/server";
86
+ import { NextResponse } from "next/server";
87
+
88
+ export async function GET(request: Request) {
89
+ const { searchParams, origin } = new URL(request.url);
90
+ const code = searchParams.get("code");
91
+ const next = searchParams.get("next") ?? "/dashboard";
92
+ if (code) {
93
+ const supabase = await createClient();
94
+ const { error } = await supabase.auth.exchangeCodeForSession(code);
95
+ if (!error) return NextResponse.redirect(`${origin}${next}`);
96
+ }
97
+ return NextResponse.redirect(`${origin}/auth/error`);
98
+ }
99
+ ```
100
+
101
+ ## Middleware Pattern
102
+
103
+ ```ts
104
+ // middleware.ts
105
+ import { createServerClient } from "@supabase/ssr";
106
+ import { NextResponse, type NextRequest } from "next/server";
107
+
108
+ export async function middleware(request: NextRequest) {
109
+ let supabaseResponse = NextResponse.next({ request });
110
+ const supabase = createServerClient(
111
+ process.env.NEXT_PUBLIC_SUPABASE_URL!,
112
+ process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
113
+ {
114
+ cookies: {
115
+ getAll() { return request.cookies.getAll(); },
116
+ setAll(cookiesToSet) {
117
+ cookiesToSet.forEach(({ name, value, options }) => {
118
+ request.cookies.set(name, value);
119
+ supabaseResponse.cookies.set(name, value, options);
120
+ });
121
+ },
122
+ },
123
+ }
124
+ );
125
+ const { data: { user } } = await supabase.auth.getUser();
126
+ if (!user && request.nextUrl.pathname.startsWith("/dashboard"))
127
+ return NextResponse.redirect(new URL("/login", request.url));
128
+ return supabaseResponse;
129
+ }
130
+
131
+ export const config = {
132
+ matcher: ["/((?!_next/static|_next/image|favicon.ico|api/webhooks).*)"],
133
+ };
134
+ ```
135
+
136
+ ## .NET JWT Validation
137
+
138
+ ```csharp
139
+ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
140
+ .AddJwtBearer(options => {
141
+ options.TokenValidationParameters = new TokenValidationParameters {
142
+ ValidateIssuer = true,
143
+ ValidIssuer = $"https://{supabaseProjectRef}.supabase.co/auth/v1",
144
+ ValidateAudience = true,
145
+ ValidAudience = "authenticated",
146
+ ValidateIssuerSigningKey = true,
147
+ IssuerSigningKey = new SymmetricSecurityKey(
148
+ Encoding.UTF8.GetBytes(supabaseJwtSecret)),
149
+ ValidateLifetime = true,
150
+ ClockSkew = TimeSpan.FromSeconds(30)
151
+ };
152
+ });
153
+
154
+ // Extract user ID: maps to auth.uid()
155
+ var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
156
+ ```
157
+
158
+ ## Environment Variables
159
+
160
+ | Variable | Where | Purpose |
161
+ |----------|-------|---------|
162
+ | `NEXT_PUBLIC_SUPABASE_URL` | Frontend | Supabase project URL |
163
+ | `NEXT_PUBLIC_SUPABASE_ANON_KEY` | Frontend | Public anon key (respects RLS) |
164
+ | `SUPABASE_SERVICE_ROLE_KEY` | Backend ONLY | Bypasses RLS -- NEVER on frontend |
165
+ | `SUPABASE_JWT_SECRET` | Backend ONLY | JWT validation secret |
166
+
167
+ ## Common Mistakes
168
+
169
+ | Wrong | Right | Why |
170
+ |-------|-------|-----|
171
+ | `getSession()` on server | `getUser()` on server | getSession reads unvalidated cookie data |
172
+ | `@supabase/auth-helpers-nextjs` | `@supabase/ssr` | auth-helpers is deprecated |
173
+ | `service_role` in `NEXT_PUBLIC_*` | `anon` key in `NEXT_PUBLIC_*` | service_role bypasses all RLS |
174
+ | Implicit flow for SSR | PKCE flow with code exchange | Implicit exposes tokens in URL fragments |
175
+ | Auth only in page components | Auth check in middleware.ts | Middleware prevents flash of content |
176
+ | Missing `setAll` in cookie config | Both `getAll` and `setAll` | Session refresh silently fails without setAll |
@@ -0,0 +1,169 @@
1
+ # Supabase pgvector Standard
2
+
3
+ > **Scope:** nextjs-supabase
4
+ > **Layer:** 2 (on keyword)
5
+ > **Keywords:** supabase, pgvector, embedding, similarity, vector search
6
+ > **Load When:** supabase pgvector keywords detected
7
+
8
+ Stack: Next.js 15 + Supabase + .NET Backend
9
+
10
+ ## Core Rules
11
+
12
+ - ALWAYS use HNSW indexes for production (faster queries, no training required)
13
+ - ALWAYS match dimensions to embedding model (e.g., 1536 for text-embedding-3-small)
14
+ - NEVER store embeddings without an index -- full table scan at query time
15
+ - Use `halfvec` for large datasets to halve storage (16-bit vs 32-bit per dimension)
16
+ - ALWAYS use RLS on tables containing embeddings
17
+
18
+ ## Setup and Table Design
19
+
20
+ ```sql
21
+ CREATE EXTENSION IF NOT EXISTS vector;
22
+
23
+ CREATE TABLE documents (
24
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
25
+ user_id UUID NOT NULL REFERENCES auth.users(id),
26
+ title TEXT NOT NULL,
27
+ content TEXT NOT NULL,
28
+ metadata JSONB DEFAULT '{}',
29
+ embedding vector(1536),
30
+ created_at TIMESTAMPTZ DEFAULT now()
31
+ );
32
+
33
+ ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
34
+ CREATE POLICY "owner_access" ON documents FOR ALL
35
+ USING (user_id = auth.uid()) WITH CHECK (user_id = auth.uid());
36
+ CREATE INDEX idx_documents_user_id ON documents (user_id);
37
+ ```
38
+
39
+ ### halfvec Optimization
40
+
41
+ | Type | Storage/dim | 1536-dim | Best for |
42
+ |------|------------|----------|----------|
43
+ | `vector` | 4 bytes | 6 KB | High precision, small datasets |
44
+ | `halfvec` | 2 bytes | 3 KB | Large datasets, cost optimization |
45
+
46
+ ## Index Types
47
+
48
+ ```sql
49
+ -- HNSW (recommended)
50
+ CREATE INDEX idx_docs_embedding ON documents
51
+ USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);
52
+
53
+ -- IVFFlat (legacy, requires existing data)
54
+ CREATE INDEX idx_docs_ivf ON documents
55
+ USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
56
+ ```
57
+
58
+ | Feature | HNSW | IVFFlat |
59
+ |---------|------|---------|
60
+ | Query speed | Faster | Slower |
61
+ | Requires training | No | Yes |
62
+ | Recall quality | Higher | Lower |
63
+ | Recommended | Yes | Only for very large datasets |
64
+
65
+ ## HNSW Parameters
66
+
67
+ | Parameter | Default | Tuning |
68
+ |-----------|---------|--------|
69
+ | `m` | 16 | Higher = better recall, more memory |
70
+ | `ef_construction` | 64 | Higher = better index, slower build |
71
+ | `ef_search` | 40 | `SET hnsw.ef_search = 100;` per session |
72
+
73
+ ## Distance Functions
74
+
75
+ | Operator | Function | Index Ops | Use Case |
76
+ |----------|----------|-----------|----------|
77
+ | `<=>` | Cosine distance | `vector_cosine_ops` | Normalized embeddings (most common) |
78
+ | `<->` | L2 (Euclidean) | `vector_l2_ops` | Spatial/positional data |
79
+ | `<#>` | Inner product (neg) | `vector_ip_ops` | Pre-normalized, max similarity |
80
+
81
+ ## Similarity Search
82
+
83
+ ```sql
84
+ CREATE OR REPLACE FUNCTION match_documents(
85
+ query_embedding vector(1536),
86
+ match_threshold float DEFAULT 0.78,
87
+ match_count int DEFAULT 10,
88
+ p_user_id uuid DEFAULT auth.uid()
89
+ ) RETURNS TABLE (id uuid, title text, content text, similarity float)
90
+ LANGUAGE sql STABLE AS $$
91
+ SELECT d.id, d.title, d.content,
92
+ 1 - (d.embedding <=> query_embedding) AS similarity
93
+ FROM documents d
94
+ WHERE d.user_id = p_user_id
95
+ AND 1 - (d.embedding <=> query_embedding) > match_threshold
96
+ ORDER BY d.embedding <=> query_embedding
97
+ LIMIT match_count;
98
+ $$;
99
+ ```
100
+
101
+ ## Hybrid Search (Vector + Full-Text)
102
+
103
+ ```sql
104
+ CREATE OR REPLACE FUNCTION hybrid_search(
105
+ query_text text, query_embedding vector(1536),
106
+ match_count int DEFAULT 10,
107
+ text_weight float DEFAULT 0.3, vector_weight float DEFAULT 0.7
108
+ ) RETURNS TABLE (id uuid, title text, content text, score float)
109
+ LANGUAGE sql STABLE AS $$
110
+ WITH vector_results AS (
111
+ SELECT id, title, content,
112
+ 1 - (embedding <=> query_embedding) AS vector_score
113
+ FROM documents WHERE user_id = auth.uid()
114
+ ORDER BY embedding <=> query_embedding LIMIT match_count * 2
115
+ ),
116
+ text_results AS (
117
+ SELECT id, title, content,
118
+ ts_rank(to_tsvector('english', content), plainto_tsquery('english', query_text)) AS text_score
119
+ FROM documents WHERE user_id = auth.uid()
120
+ AND to_tsvector('english', content) @@ plainto_tsquery('english', query_text)
121
+ LIMIT match_count * 2
122
+ )
123
+ SELECT COALESCE(v.id, t.id), COALESCE(v.title, t.title), COALESCE(v.content, t.content),
124
+ (COALESCE(v.vector_score, 0) * vector_weight + COALESCE(t.text_score, 0) * text_weight)
125
+ FROM vector_results v FULL OUTER JOIN text_results t ON v.id = t.id
126
+ ORDER BY score DESC LIMIT match_count;
127
+ $$;
128
+ ```
129
+
130
+ ## .NET Integration (Npgsql)
131
+
132
+ ```csharp
133
+ public sealed class DocumentRepository(AppDbContext db)
134
+ {
135
+ public async Task StoreEmbeddingAsync(
136
+ Guid documentId, float[] embedding, CancellationToken ct = default)
137
+ {
138
+ await db.Database.ExecuteSqlInterpolatedAsync(
139
+ $"UPDATE documents SET embedding = {new Vector(embedding)} WHERE id = {documentId}", ct);
140
+ }
141
+
142
+ public async Task<List<DocumentMatch>> SearchSimilarAsync(
143
+ float[] queryEmbedding, int limit = 10, float threshold = 0.78f,
144
+ CancellationToken ct = default)
145
+ {
146
+ return await db.Database.SqlQuery<DocumentMatch>($"""
147
+ SELECT id, title, content,
148
+ 1 - (embedding <=> {new Vector(queryEmbedding)}::vector) AS similarity
149
+ FROM documents
150
+ WHERE 1 - (embedding <=> {new Vector(queryEmbedding)}::vector) > {threshold}
151
+ ORDER BY embedding <=> {new Vector(queryEmbedding)}::vector LIMIT {limit}
152
+ """).ToListAsync(ct);
153
+ }
154
+ }
155
+
156
+ // EF Core registration
157
+ builder.Services.AddDbContext<AppDbContext>(o =>
158
+ o.UseNpgsql(connectionString, npg => npg.UseVector()));
159
+ ```
160
+
161
+ ## Common Mistakes
162
+
163
+ | Wrong | Right | Why |
164
+ |-------|-------|-----|
165
+ | No index on embedding column | HNSW index | Full table scan, extremely slow |
166
+ | `ORDER BY similarity DESC` | `ORDER BY embedding <=> query ASC` | Operator returns distance, not similarity |
167
+ | Mixing embedding dimensions | Consistent dimensions per column | Dimension mismatch causes runtime errors |
168
+ | Full-precision for millions of rows | `halfvec` for large datasets | 2x storage savings, minimal quality loss |
169
+ | Missing RLS on embedding tables | RLS with user/tenant policies | Embeddings contain sensitive content context |