@withmata/blueprints 0.2.0

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 (77) hide show
  1. package/.claude/commands/audit.md +179 -0
  2. package/.claude/commands/discover.md +92 -0
  3. package/.claude/commands/new-blueprint.md +265 -0
  4. package/.claude/commands/new-project.md +230 -0
  5. package/.claude/commands/scaffold-auth.md +310 -0
  6. package/.claude/commands/scaffold-db.md +270 -0
  7. package/.claude/commands/scaffold-foundation.md +158 -0
  8. package/.cursor/commands/audit.md +179 -0
  9. package/.cursor/commands/discover.md +92 -0
  10. package/.cursor/commands/new-blueprint.md +265 -0
  11. package/.cursor/commands/new-project.md +230 -0
  12. package/.cursor/commands/scaffold-auth.md +310 -0
  13. package/.cursor/commands/scaffold-db.md +270 -0
  14. package/.cursor/commands/scaffold-foundation.md +158 -0
  15. package/.opencode/commands/audit.md +183 -0
  16. package/.opencode/commands/discover.md +96 -0
  17. package/.opencode/commands/new-blueprint.md +269 -0
  18. package/.opencode/commands/new-project.md +234 -0
  19. package/.opencode/commands/scaffold-auth.md +314 -0
  20. package/.opencode/commands/scaffold-db.md +274 -0
  21. package/.opencode/commands/scaffold-foundation.md +162 -0
  22. package/ENGINEERING.md +47 -0
  23. package/blueprints/discovery/product-discovery/BLUEPRINT.md +361 -0
  24. package/blueprints/discovery/product-discovery/templates/archetype-template.md +143 -0
  25. package/blueprints/discovery/product-discovery/templates/product-brief.md +65 -0
  26. package/blueprints/discovery/product-discovery/templates/product-thesis.md +64 -0
  27. package/blueprints/discovery/product-discovery/templates/research-summary.md +43 -0
  28. package/blueprints/features/auth-better-auth/BLUEPRINT.md +794 -0
  29. package/blueprints/features/auth-better-auth/files/client/auth-client.ts +31 -0
  30. package/blueprints/features/auth-better-auth/files/client/context/organization.ts +236 -0
  31. package/blueprints/features/auth-better-auth/files/client/hooks/use-create-organization.ts +45 -0
  32. package/blueprints/features/auth-better-auth/files/client/hooks/use-has-permission.ts +26 -0
  33. package/blueprints/features/auth-better-auth/files/client/hooks/use-organization.ts +64 -0
  34. package/blueprints/features/auth-better-auth/files/client/schema/auth.ts +21 -0
  35. package/blueprints/features/auth-better-auth/files/client/schema/organization.ts +51 -0
  36. package/blueprints/features/auth-better-auth/files/client/types/organization.ts +20 -0
  37. package/blueprints/features/auth-better-auth/files/db/auth-schema.ts +184 -0
  38. package/blueprints/features/auth-better-auth/files/db/drizzle.config.ts +21 -0
  39. package/blueprints/features/auth-better-auth/files/middleware/route-protection.ts +84 -0
  40. package/blueprints/features/auth-better-auth/files/server/auth-db.ts +18 -0
  41. package/blueprints/features/auth-better-auth/files/server/auth.ts +159 -0
  42. package/blueprints/features/auth-better-auth/files/server/env.ts +44 -0
  43. package/blueprints/features/auth-better-auth/files/server/middleware.ts +144 -0
  44. package/blueprints/features/db-drizzle-postgres/BLUEPRINT.md +596 -0
  45. package/blueprints/features/db-drizzle-postgres/files/db/drizzle.config.ts +18 -0
  46. package/blueprints/features/db-drizzle-postgres/files/db/env.example +3 -0
  47. package/blueprints/features/db-drizzle-postgres/files/db/package.json +33 -0
  48. package/blueprints/features/db-drizzle-postgres/files/db/src/client.ts +34 -0
  49. package/blueprints/features/db-drizzle-postgres/files/db/src/example-entity.ts +73 -0
  50. package/blueprints/features/db-drizzle-postgres/files/db/src/index.ts +8 -0
  51. package/blueprints/features/db-drizzle-postgres/files/db/src/scripts/seed.ts +50 -0
  52. package/blueprints/features/db-drizzle-postgres/files/db/tsconfig.json +13 -0
  53. package/blueprints/features/tailwind-v4/BLUEPRINT.md +29 -0
  54. package/blueprints/features/ui-shared-components/BLUEPRINT.md +35 -0
  55. package/blueprints/foundation/monorepo-turbo/BLUEPRINT.md +378 -0
  56. package/blueprints/foundation/monorepo-turbo/files/apps/web/app/globals.css +2 -0
  57. package/blueprints/foundation/monorepo-turbo/files/apps/web/app/layout.tsx +23 -0
  58. package/blueprints/foundation/monorepo-turbo/files/apps/web/app/page.tsx +7 -0
  59. package/blueprints/foundation/monorepo-turbo/files/apps/web/env.ts +13 -0
  60. package/blueprints/foundation/monorepo-turbo/files/apps/web/next.config.ts +12 -0
  61. package/blueprints/foundation/monorepo-turbo/files/apps/web/package.json +28 -0
  62. package/blueprints/foundation/monorepo-turbo/files/apps/web/postcss.config.mjs +5 -0
  63. package/blueprints/foundation/monorepo-turbo/files/apps/web/tsconfig.json +18 -0
  64. package/blueprints/foundation/monorepo-turbo/files/config/tailwind-config/package.json +14 -0
  65. package/blueprints/foundation/monorepo-turbo/files/config/tailwind-config/postcss.config.mjs +5 -0
  66. package/blueprints/foundation/monorepo-turbo/files/config/tailwind-config/shared-styles.css +88 -0
  67. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/base.json +19 -0
  68. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/nextjs.json +12 -0
  69. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/package.json +5 -0
  70. package/blueprints/foundation/monorepo-turbo/files/config/typescript-config/react-library.json +7 -0
  71. package/blueprints/foundation/monorepo-turbo/files/root/biome.json +46 -0
  72. package/blueprints/foundation/monorepo-turbo/files/root/gitignore +35 -0
  73. package/blueprints/foundation/monorepo-turbo/files/root/package.json +25 -0
  74. package/blueprints/foundation/monorepo-turbo/files/root/pnpm-workspace.yaml +5 -0
  75. package/blueprints/foundation/monorepo-turbo/files/root/turbo.json +30 -0
  76. package/dist/index.js +453 -0
  77. package/package.json +53 -0
@@ -0,0 +1,310 @@
1
+ # Scaffold Auth Blueprint
2
+
3
+ Scaffold the `auth-better-auth` blueprint into the current project. Read the full blueprint first, then adapt it to this project's stack.
4
+
5
+ ## Step 1: Read the Blueprint
6
+
7
+ Read the full BLUEPRINT.md and all code files from the auth blueprint:
8
+
9
+ ```
10
+ blueprints/features/auth-better-auth/BLUEPRINT.md
11
+ blueprints/features/auth-better-auth/files/**/*
12
+ ```
13
+
14
+ If these files are not in the current project, look for them at `~/Documents/WithMata/my-blueprints/blueprints/features/auth-better-auth/`. If still not found, ask the user for the path to their blueprints repo.
15
+
16
+ ## Step 1.5: Read Project Context
17
+
18
+ Check if `.project-context.md` exists in the target project root:
19
+
20
+ - **If it exists**: read it to understand:
21
+ - Product name and scope (from Discovery)
22
+ - npm scope, workspace layout, server framework (from Foundation)
23
+ - What other blueprints are already installed (from Installed Blueprints)
24
+ - Use this information to pre-fill configuration questions in Step 3 and adapt file paths in Step 4
25
+
26
+ - **If it does not exist**: proceed normally — explore the project manually (Step 2) and ask all configuration questions (Step 3)
27
+
28
+ When project context provides clear answers, skip the corresponding questions in Step 3. For example:
29
+ - Context says `server_framework: hono` -> do not ask about server framework
30
+ - Context says `npm_scope: "@acme"` -> use `@acme` for package references
31
+ - Context has `auth-better-auth` in Installed Blueprints -> warn about existing install
32
+
33
+ ## Step 1.7: Check Blueprint Dependencies
34
+
35
+ Read the Blueprint Dependencies section from `auth-better-auth/BLUEPRINT.md`.
36
+
37
+ For `features/db-drizzle-postgres` (required):
38
+ - Check `.project-context.md` for `db-drizzle-postgres` under `## Installed Blueprints`
39
+ - If installed: proceed normally
40
+ - If NOT installed, check filesystem:
41
+ - Monorepo: does `packages/db/` exist?
42
+ - Single-repo: does `src/db/` exist?
43
+ - If db directory exists (but not in project context): note it, proceed normally — the scaffold command already handles merging
44
+ - If neither installed nor directory exists:
45
+ - Ask: "Auth requires a database package. The db-drizzle-postgres blueprint establishes the schema-group pattern that auth builds on. Install it first? (Y/n)"
46
+ - If yes: run `/scaffold-db` first, then continue with auth
47
+ - If no: warn about missing patterns, proceed with creating a minimal db structure for auth only
48
+
49
+ For `foundation/monorepo-turbo` (recommended):
50
+ - If not present: mention once: "Tip: The monorepo-turbo foundation blueprint provides workspace structure that auth integrates with. Consider running `/scaffold-foundation` first."
51
+ - Continue without blocking.
52
+
53
+ ## Step 2: Understand the Target Project
54
+
55
+ Before making any changes, explore the target project to understand:
56
+
57
+ ### 2a. Detect Project Type
58
+
59
+ Check `.project-context.md` for `project_type`. If not present, detect:
60
+
61
+ - **Monorepo indicators**: `pnpm-workspace.yaml`, `turbo.json`, `nx.json`, `lerna.json`, or `"workspaces"` in root `package.json`
62
+ - **Single-repo indicators**: Single `package.json` at root, `src/` directory, no workspace config
63
+
64
+ Confirm with user: "This looks like a [monorepo|single-repo] project. Is that right?"
65
+
66
+ Record the project type in `.project-context.md` if not already there.
67
+
68
+ ### 2b. Explore Structure
69
+
70
+ **If monorepo:**
71
+ 1. What workspaces exist? Where do apps, APIs, and packages live?
72
+ 2. Is there a Hono server, Express server, or is it Next.js-only? Where are API routes?
73
+ 3. Is Drizzle already set up? Is there a shared DB package?
74
+ 4. Is this Next.js? What version? Is there an existing middleware.ts?
75
+
76
+ **If single-repo:**
77
+ 1. What framework is this? (Next.js, Node/Express, Hono, Bun?)
78
+ 2. Does `src/db/` or `lib/db/` exist?
79
+ 3. What import convention is used? (`@/` paths, `#` subpath imports, relative?)
80
+ 4. Is there an existing `middleware.ts`?
81
+
82
+ **Both:**
83
+ 5. Existing auth to replace or integrate with?
84
+ 6. UI library? Styling approach?
85
+ 7. Package manager?
86
+ 8. Validation — Zod, t3-env?
87
+ 9. Data fetching — SWR, TanStack Query?
88
+
89
+ Skip exploration for anything already documented in `.project-context.md`.
90
+
91
+ ## Step 3: Ask Clarifying Questions
92
+
93
+ Before scaffolding, ask the user about their preferences. Use the blueprint's "Hard Requirements vs Recommended Defaults" table. Specifically ask:
94
+
95
+ 1. **Organization plugin** — "Do you need multi-tenant organizations (teams/workspaces with member management and invitations), or is this a single-tenant app?"
96
+
97
+ 2. **Database setup** — "Should auth use a separate database, or share the existing app database?" (Recommend separate if no strong preference.)
98
+
99
+ 3. **Social providers** — "Which social login providers do you want? Google is included by default. Any others (GitHub, Discord, etc.)?"
100
+
101
+ 4. **Email provider** — "The blueprint uses Resend for transactional emails (verification, password reset, invitations). Do you want to use Resend, or a different provider?"
102
+
103
+ 5. **Defaults check** — "The blueprint recommends these tools (which may already match your project). Want to go with the defaults, or change any?"
104
+ - Zod for validation
105
+ - t3-env for environment variable validation
106
+ - SWR (`useSWRMutation`) for auth mutation hooks
107
+ - react-hook-form for auth forms
108
+
109
+ Skip questions where the answer is obvious from the project structure or from `.project-context.md`.
110
+
111
+ ## Step 4: Adapt and Scaffold
112
+
113
+ Based on the project structure, project context, and user answers, adapt the blueprint files:
114
+
115
+ ### 4a. Database Layer
116
+
117
+ **If monorepo:**
118
+ - Copy/adapt `files/db/drizzle.config.ts` → `packages/db/drizzle/auth/drizzle.config.ts`
119
+ - Copy `files/db/auth-schema.ts` as a reference (the actual schema will be auto-generated)
120
+ - Add the `"./auth"` export to `packages/db/package.json`
121
+ - Add `auth:generate` and `auth:migrate` scripts to `packages/db/package.json`
122
+
123
+ **If single-repo:**
124
+ - Copy/adapt `files/db/drizzle.config.ts` → `drizzle/auth/drizzle.config.ts` (at project root)
125
+ - Adjust schema paths to `"./src/db/auth/schema.ts"` (relative to root)
126
+ - Create `src/db/auth/` directory for auth schema
127
+ - No separate package.json export needed — use direct imports
128
+
129
+ ### 4b. Server Layer
130
+
131
+ **If monorepo:**
132
+ - Copy/adapt `files/server/auth.ts` → `apis/server/src/auth/index.ts`
133
+ - Replace `{{APP_NAME}}`, `{{EMAIL_FROM}}`, `{{SCOPE}}`
134
+ - Copy/adapt `files/server/auth-db.ts` → `apis/server/src/db/auth.ts`
135
+ - Copy/adapt `files/server/middleware.ts` → `apis/server/src/middleware/auth.ts`
136
+ - Merge auth env vars into `apis/server/src/env.ts`
137
+
138
+ **If single-repo:**
139
+ - Copy/adapt `files/server/auth.ts` → `src/lib/auth/index.ts`
140
+ - Replace `{{APP_NAME}}`, `{{EMAIL_FROM}}`
141
+ - Adjust DB import to use direct path: `import { db } from "@/db/auth/client"` or `#db/auth/client`
142
+ - Copy/adapt `files/server/auth-db.ts` → `src/lib/db/auth.ts`
143
+ - Default to Next.js API routes (no separate Hono server)
144
+ - Merge auth env vars into existing `env.ts` (or create one)
145
+
146
+ **Both:**
147
+ - If the project uses a different server framework than Hono, adapt the middleware while keeping the core `auth.api.getSession({ headers })` pattern
148
+
149
+ ### 4c. Mount Auth Handler
150
+
151
+ **If monorepo with Hono server:**
152
+ - Add CORS config + `app.on(["POST", "GET"], "/api/auth/*", ...)` handler in `apis/server/`
153
+
154
+ **If single-repo or Next.js API routes:**
155
+ - Create `app/api/auth/[...all]/route.ts` with `toNextJsHandler(auth)`
156
+ - No CORS config needed (same origin)
157
+
158
+ **Other frameworks:** Follow the platform integration section in BLUEPRINT.md
159
+
160
+ ### 4d. Client Layer
161
+
162
+ **If monorepo:**
163
+ - Copy client files to `apps/web/auth/`
164
+ - Add `"#auth/*": "./auth/*"` import alias to `apps/web/package.json`
165
+
166
+ **If single-repo:**
167
+ - Copy client files to `src/auth/`
168
+ - Add `"#auth/*": "./src/auth/*"` import alias to root `package.json` (or use `@/auth/*` if that's the convention)
169
+
170
+ **Both:**
171
+ - Adapt `files/client/auth-client.ts` — adjust baseURL for deployment setup
172
+ - If using org plugin: copy all `files/client/context/`, `hooks/`, `schema/`, `types/` files
173
+ - If NOT using org plugin: only copy `files/client/schema/auth.ts`
174
+
175
+ ### 4e. Route Protection
176
+
177
+ - Copy/adapt `files/middleware/route-protection.ts` into the project's Next.js middleware
178
+ - If middleware.ts already exists, MERGE the auth logic into it — don't overwrite
179
+ - If using Hono standalone mode: add API proxy rewrites to `next.config.ts`
180
+
181
+ ### 4f. Project Scripts
182
+
183
+ **If monorepo:**
184
+ - Add `auth:generate` script to the server package (runs `@better-auth/cli generate`)
185
+ - Add `auth:generate`, `auth:migrate`, `auth:setup` to root `package.json` (Turbo wrappers)
186
+ - Add `auth:generate` and `auth:migrate` tasks to `turbo.json` (both `cache: false`)
187
+
188
+ **If single-repo:**
189
+ - Add scripts directly to root `package.json`:
190
+ - `auth:generate` -> `npx @better-auth/cli@latest generate --config ./src/lib/auth/index.ts --output ./src/db/auth/schema.ts --yes`
191
+ - `auth:migrate` -> `drizzle-kit generate --config ./drizzle/auth/drizzle.config.ts && drizzle-kit migrate --config ./drizzle/auth/drizzle.config.ts`
192
+ - `auth:setup` -> `pnpm auth:generate && pnpm auth:migrate`
193
+ - No Turbo tasks needed
194
+
195
+ ## Step 5: Update Project Context
196
+
197
+ Append an entry to `.project-context.md` under `## Installed Blueprints`:
198
+
199
+ ```yaml
200
+ ### auth-better-auth
201
+ blueprint: auth-better-auth
202
+ choices:
203
+ database: <user's choice>
204
+ auth_db_isolation: <separate|shared>
205
+ server_framework: <hono|next.js>
206
+ organization_plugin: <true|false>
207
+ social_providers: [<list>]
208
+ email_provider: <resend|other>
209
+ env_validation: <t3-env|other>
210
+ form_validation: <zod|other>
211
+ data_fetching: <swr|other>
212
+ files_created:
213
+ - <list of all files created or modified>
214
+ env_vars_added:
215
+ - <list of env vars>
216
+ scripts_added:
217
+ - <list of scripts>
218
+ ```
219
+
220
+ If `.project-context.md` does not exist, create it with the standard header and the Installed Blueprints section. Log a note: "No discovery or foundation context found. Consider running `/discover` and `/scaffold-foundation` for a complete project context."
221
+
222
+ Also append any significant architectural decisions to the `## Architecture Decisions` section.
223
+
224
+ ### Inject Maintenance Rules
225
+
226
+ Append the condensed maintenance rules to the target project's rules file(s):
227
+
228
+ 1. If `CLAUDE.md` exists in the target project → append rules there
229
+ 2. If `AGENTS.md` exists in the target project → append rules there
230
+ 3. If both exist → append to both (keep them in sync)
231
+ 4. If neither exists → ask the user which AI coding tool(s) they use:
232
+ - Claude Code → create CLAUDE.md with a `## Maintenance Rules` section
233
+ - OpenCode or Cursor → create AGENTS.md with a `## Maintenance Rules` section (both tools read AGENTS.md)
234
+ - Multiple tools → create all applicable files
235
+
236
+ The maintenance rules content is identical regardless of which file it goes into:
237
+
238
+ ```markdown
239
+ ### auth-better-auth maintenance
240
+ - After modifying Better Auth server config, plugins, or providers: run `pnpm auth:generate && pnpm auth:migrate`
241
+ - After updating `better-auth` package: run `pnpm auth:generate`, check for schema changes, migrate if needed
242
+ - After adding/changing env vars: update all `.env` files and verify `pnpm dev` starts clean
243
+ - After changing `BETTER_AUTH_URL` or `FRONTEND_DOMAIN`: verify CORS config and OAuth callback URLs
244
+ - After any auth changes: verify `{BETTER_AUTH_URL}/api/auth/ok` responds
245
+ - Never edit the auto-generated auth schema directly — modify the Better Auth config and regenerate
246
+ ```
247
+
248
+ If the `## Maintenance Rules` heading already exists (from another blueprint), only add the `### auth-better-auth maintenance` subsection — do not duplicate the heading.
249
+
250
+ ## Step 6: Output Summary
251
+
252
+ After scaffolding, provide the user with a clear summary:
253
+
254
+ ### Packages to Install
255
+
256
+ List every package that needs to be installed, grouped by workspace:
257
+
258
+ ```
259
+ # Server (apis/server or wherever auth lives)
260
+ pnpm add better-auth drizzle-orm pg resend @t3-oss/env-core zod dotenv
261
+
262
+ # Client (apps/web)
263
+ pnpm add better-auth swr react-hook-form @hookform/resolvers zod @t3-oss/env-nextjs
264
+
265
+ # DB package (packages/db) — dev deps
266
+ pnpm add -D drizzle-kit pg
267
+ ```
268
+
269
+ Adjust based on what's already installed in the project.
270
+
271
+ ### Environment Variables
272
+
273
+ List every env var that needs to be set, with example values:
274
+
275
+ ```env
276
+ # Server
277
+ BETTER_AUTH_URL=http://localhost:4000
278
+ BETTER_AUTH_SECRET= # Generate: openssl rand -base64 32
279
+ AUTH_DATABASE_URL= # PostgreSQL connection URL
280
+ FRONTEND_DOMAIN=http://localhost:3000
281
+ GOOGLE_CLIENT_ID= # From Google Cloud Console
282
+ GOOGLE_CLIENT_SECRET= # Redirect URI: {BETTER_AUTH_URL}/api/auth/callback/google
283
+ RESEND_API_KEY= # From resend.com
284
+
285
+ # Client (.env.local in web app)
286
+ NEXT_PUBLIC_API_URL=http://localhost:4000
287
+ NEXT_PUBLIC_FRONTEND_URL=http://localhost:3000
288
+ ```
289
+
290
+ ### Manual Steps Remaining
291
+
292
+ List anything that can't be automated:
293
+
294
+ 1. **Create a PostgreSQL database** for auth (if using separate auth DB)
295
+ 2. **Set up Google OAuth** — create OAuth 2.0 credentials in Google Cloud Console, add redirect URI
296
+ 3. **Set up Resend** — create an account, verify your domain, get API key
297
+ 4. **Run auth setup** — `pnpm auth:setup` (generates schema + runs migrations)
298
+ 5. **Build auth UI pages** — follow the Auth Flows section in BLUEPRINT.md to create the pages matching your design system
299
+ 6. **Test the auth flow** — sign up, verify email, sign in, create org (if applicable)
300
+
301
+ ### Files Created
302
+
303
+ List every file that was created or modified, with a one-line description.
304
+
305
+ ## Important Notes
306
+
307
+ - If the Better Auth skills are installed (`npx skillsadd better-auth/skills`), invoke `/best-practices` for quick-reference on configuration. If not available, refer to the Better Auth docs and References section in BLUEPRINT.md.
308
+ - When adapting code, preserve the `// CONFIGURE:` comments so the user can find customization points later.
309
+ - If the project structure differs significantly from the blueprint's assumptions, explain the differences and suggest how to adapt rather than forcing the blueprint's structure.
310
+ - Never overwrite existing files without asking. If a file exists (like `middleware.ts` or `env.ts`), merge the auth additions into it.
@@ -0,0 +1,270 @@
1
+ # Scaffold DB Blueprint
2
+
3
+ Scaffold the `db-drizzle-postgres` blueprint into the current project. Read the full blueprint first, then adapt it to this project's stack.
4
+
5
+ ## Step 1: Read the Blueprint
6
+
7
+ Read the full BLUEPRINT.md and all template files from the db blueprint:
8
+
9
+ ```
10
+ blueprints/features/db-drizzle-postgres/BLUEPRINT.md
11
+ blueprints/features/db-drizzle-postgres/files/**/*
12
+ ```
13
+
14
+ If these files are not in the current project, look for them at `~/Documents/WithMata/my-blueprints/blueprints/features/db-drizzle-postgres/`. If still not found, ask the user for the path to their blueprints repo.
15
+
16
+ ## Step 1.5: Read Project Context
17
+
18
+ Check if `.project-context.md` exists in the target project root:
19
+
20
+ - **If it exists**: read it to understand:
21
+ - Product name and scope (from Discovery)
22
+ - npm scope, workspace layout, TypeScript config package name (from Foundation)
23
+ - What other blueprints are already installed (from Installed Blueprints)
24
+ - Use this information to pre-fill configuration questions in Step 3 and adapt file paths in Step 4
25
+
26
+ - **If it does not exist**: proceed normally — explore the project manually (Step 2) and ask all configuration questions (Step 3)
27
+
28
+ When project context provides clear answers, skip the corresponding questions in Step 3. For example:
29
+ - Context says `npm_scope: "@acme"` -> use `@acme` for package references
30
+ - Context has `db-drizzle-postgres` in Installed Blueprints -> warn about existing install
31
+ - Context has `auth-better-auth` installed -> `packages/db/` likely exists already
32
+
33
+ ## Step 2: Understand the Target Project
34
+
35
+ Before making any changes, explore the target project to understand:
36
+
37
+ ### 2a. Detect Project Type
38
+
39
+ Check `.project-context.md` for `project_type`. If not present, detect:
40
+
41
+ - **Monorepo indicators**: `pnpm-workspace.yaml`, `turbo.json`, `nx.json`, `lerna.json`, or `"workspaces"` in root `package.json`
42
+ - **Single-repo indicators**: Single `package.json` at root, `src/` directory, no workspace config
43
+
44
+ Confirm with user: "This looks like a [monorepo|single-repo] project. Is that right?"
45
+
46
+ Record the project type in `.project-context.md` if not already there (add `project_type: monorepo` or `project_type: single-repo` under `## Foundation`).
47
+
48
+ ### 2b. Explore Structure
49
+
50
+ **If monorepo:**
51
+ 1. What workspaces exist? Where do apps, APIs, and packages live?
52
+ 2. Does `packages/db/` already exist? (The auth blueprint may have created it.) If so, what exports, scripts, and schema groups are already present?
53
+ 3. What npm scope is used? Check root or any workspace `package.json`.
54
+ 4. What is the shared TypeScript config package name? Check `config/typescript-config/` or similar.
55
+ 5. Does `turbo.json` exist? Are there any existing db-related tasks?
56
+ 6. Does `pnpm-workspace.yaml` include `packages/*`?
57
+
58
+ **If single-repo:**
59
+ 1. Does `src/db/` already exist? If so, what's in it?
60
+ 2. What framework is this? (Next.js, Node/Express, Hono, Bun?)
61
+ 3. What import convention is used? (`@/` paths, `#` subpath imports, relative?)
62
+ 4. Is there an existing `.env` or `.env.local`?
63
+
64
+ **Both:**
65
+ - Package manager — pnpm, npm, yarn, or bun?
66
+
67
+ Skip exploration for anything already documented in `.project-context.md`.
68
+
69
+ ## Step 3: Ask Clarifying Questions
70
+
71
+ Before scaffolding, ask the user about their preferences:
72
+
73
+ 1. **Schema group name** — "What should your main schema group be called? This groups related database tables together (e.g., 'core' for your main application data, 'app' for a simpler name). Default: `core`"
74
+
75
+ 2. **Include example entity?** — "Include an example entity file demonstrating the full Drizzle pattern (pgTable, pgEnum, indexes, type exports)? Recommended if this is your first time using Drizzle." (Default: yes)
76
+
77
+ 3. **Include seed script?** — "Include a seed script template for populating development data?" (Default: yes)
78
+
79
+ 4. **Connection helper** — "Should the database connection helper live in the db package (simpler — import and use directly), or will each consuming app create its own connection (more flexible — each app controls its own pool settings)?" (Default: in db package)
80
+
81
+ 5. **Existing `packages/db/`** — If it already exists (e.g., from the auth blueprint): "I found an existing `packages/db/` package. I'll add the new schema group to it without overwriting existing files. Does that sound right?"
82
+
83
+ Skip questions where the answer is obvious from the project structure or from `.project-context.md`.
84
+
85
+ ## Step 4: Adapt and Scaffold
86
+
87
+ Based on the project structure, project context, and user answers, adapt the blueprint files.
88
+
89
+ ### 4a. Project Setup
90
+
91
+ **If monorepo — Package Setup (or merge if exists):**
92
+
93
+ If `packages/db/` does NOT exist:
94
+ - Create the full package: `package.json`, `tsconfig.json`, `.env.example`
95
+ - Replace all placeholders:
96
+ - `{{SCOPE}}` -> the project's npm scope (e.g., `@acme`)
97
+ - `{{GROUP}}` -> the schema group name (e.g., `core`)
98
+ - `{{GROUP_UPPER}}` -> uppercase for env vars (e.g., `CORE`)
99
+
100
+ If `packages/db/` already exists:
101
+ - Merge the new schema group's export into existing `package.json` exports
102
+ - Merge new scripts into existing scripts (don't overwrite existing ones like `build`, `typecheck`, `clean`)
103
+ - Skip `tsconfig.json` if it already exists and looks correct
104
+ - Add `{{GROUP_UPPER}}_DATABASE_URL` to existing `.env.example`
105
+
106
+ **If single-repo — Directory Setup:**
107
+
108
+ - Create `src/db/` directory (no separate `package.json` — use root)
109
+ - Add `{{GROUP_UPPER}}_DATABASE_URL` to root `.env.example` (or `.env` if that's the convention)
110
+ - No separate `tsconfig.json` needed — inherits from root
111
+ - Replace placeholders in files: `{{GROUP}}`, `{{GROUP_UPPER}}`
112
+ - Skip `{{SCOPE}}` — use direct imports instead (`@/db/{{GROUP}}` or `#db/{{GROUP}}`)
113
+
114
+ ### 4b. Schema Group Files
115
+
116
+ - Create `src/{{GROUP}}/` directory
117
+ - Copy example entity file (if user said yes) — replace all placeholders
118
+ - Create barrel `index.ts` with re-exports
119
+ - Copy `client.ts` helper (if user chose "in db package") — replace placeholders
120
+
121
+ ### 4c. Drizzle Config
122
+
123
+ - **Monorepo:** Create `packages/db/drizzle/{{GROUP}}/` directory
124
+ - **Single-repo:** Create `drizzle/{{GROUP}}/` at project root
125
+ - Copy `drizzle.config.ts` — replace `{{GROUP}}`, `{{GROUP_UPPER}}`
126
+ - Adjust schema file paths based on project type:
127
+ - Monorepo: `"./src/{{GROUP}}/example.ts"` (relative to `packages/db/`)
128
+ - Single-repo: `"./src/db/{{GROUP}}/example.ts"` (relative to project root)
129
+ - Verify schema file paths match actual files created in 4b
130
+
131
+ ### 4d. Seed Script (optional)
132
+
133
+ - Create `src/scripts/` directory (if it doesn't exist)
134
+ - Copy `seed.ts` — replace placeholders, adjust entity imports
135
+ - If a seed script already exists, ask before overwriting
136
+
137
+ ### 4e. Project Integration
138
+
139
+ **If monorepo:**
140
+ - Add `{{GROUP}}:generate`, `{{GROUP}}:migrate` tasks to `turbo.json` (both `cache: false`) — merge into existing tasks
141
+ - Add convenience scripts to root `package.json`:
142
+ - `db:generate` -> `turbo run {{GROUP}}:generate --filter={{SCOPE}}/db --no-cache`
143
+ - `db:migrate` -> `turbo run {{GROUP}}:migrate --filter={{SCOPE}}/db --no-cache`
144
+ - `db:studio` -> `turbo run drizzle:studio:{{GROUP}} --filter={{SCOPE}}/db --no-cache`
145
+ - `db:seed` -> `turbo run seed --filter={{SCOPE}}/db --no-cache`
146
+ - Verify `pnpm-workspace.yaml` includes `packages/*`
147
+
148
+ **If single-repo:**
149
+ - Add scripts directly to root `package.json` (direct drizzle-kit calls, no Turbo):
150
+ - `db:generate` -> `drizzle-kit generate --config ./drizzle/{{GROUP}}/drizzle.config.ts`
151
+ - `db:migrate` -> `drizzle-kit generate --config ./drizzle/{{GROUP}}/drizzle.config.ts && drizzle-kit migrate --config ./drizzle/{{GROUP}}/drizzle.config.ts`
152
+ - `db:studio` -> `drizzle-kit studio --config ./drizzle/{{GROUP}}/drizzle.config.ts`
153
+ - `db:seed` -> `tsx src/db/scripts/seed.ts`
154
+ - Add drizzle-orm, dotenv as dependencies and drizzle-kit, pg, @types/pg, tsx as devDependencies
155
+ - No `turbo.json` changes needed
156
+
157
+ ## Step 5: Update Project Context
158
+
159
+ Append an entry to `.project-context.md` under `## Installed Blueprints`:
160
+
161
+ ```yaml
162
+ ### db-drizzle-postgres
163
+ blueprint: db-drizzle-postgres
164
+ choices:
165
+ schema_group: <group name>
166
+ include_example: <true|false>
167
+ include_seed: <true|false>
168
+ connection_helper: <in-db-package|per-app>
169
+ files_created:
170
+ - <list of all files created or modified>
171
+ env_vars_added:
172
+ - <GROUP_UPPER>_DATABASE_URL
173
+ scripts_added:
174
+ db_package:
175
+ - <group>:generate
176
+ - <group>:migrate
177
+ - drizzle:studio:<group>
178
+ - seed
179
+ root:
180
+ - db:generate
181
+ - db:migrate
182
+ - db:studio
183
+ - db:seed
184
+ turbo_tasks:
185
+ - <group>:generate
186
+ - <group>:migrate
187
+ ```
188
+
189
+ If `.project-context.md` does not exist, create it with the standard header and the Installed Blueprints section. Log a note: "No discovery or foundation context found. Consider running `/discover` and `/scaffold-foundation` for a complete project context."
190
+
191
+ ### Inject Maintenance Rules
192
+
193
+ Append the condensed maintenance rules to the target project's rules file(s):
194
+
195
+ 1. If `CLAUDE.md` exists in the target project -> append rules there
196
+ 2. If `AGENTS.md` exists in the target project -> append rules there
197
+ 3. If both exist -> append to both (keep them in sync)
198
+ 4. If neither exists -> ask the user which AI coding tool(s) they use:
199
+ - Claude Code -> create CLAUDE.md with a `## Maintenance Rules` section
200
+ - OpenCode or Cursor -> create AGENTS.md with a `## Maintenance Rules` section (both tools read AGENTS.md)
201
+ - Multiple tools -> create all applicable files
202
+
203
+ The maintenance rules content is identical regardless of which file it goes into:
204
+
205
+ ```markdown
206
+ ### db-drizzle-postgres maintenance
207
+ - After creating a new schema file: add it to `drizzle/<group>/drizzle.config.ts` schema array AND the group's `index.ts` barrel export
208
+ - After any schema change: run `pnpm <group>:generate && pnpm <group>:migrate`
209
+ - After adding a new schema group: create drizzle config, add package.json export, add scripts (see BLUEPRINT.md "Adding a New Schema Group")
210
+ - After updating drizzle-orm/drizzle-kit: run generate and check migration diff for unexpected changes
211
+ - Never use glob patterns in drizzle.config.ts schema — always list files explicitly, excluding index.ts
212
+ ```
213
+
214
+ If the `## Maintenance Rules` heading already exists (from another blueprint), only add the `### db-drizzle-postgres maintenance` subsection — do not duplicate the heading.
215
+
216
+ ## Step 6: Output Summary
217
+
218
+ After scaffolding, provide the user with a clear summary:
219
+
220
+ ### Packages to Install
221
+
222
+ List every package that needs to be installed. Adjust based on project type and what's already installed:
223
+
224
+ **If monorepo:**
225
+ ```bash
226
+ # In packages/db/
227
+ pnpm add drizzle-orm dotenv
228
+ pnpm add -D drizzle-kit pg @types/pg tsx typescript
229
+ ```
230
+
231
+ **If single-repo:**
232
+ ```bash
233
+ # At project root
234
+ pnpm add drizzle-orm dotenv
235
+ pnpm add -D drizzle-kit pg @types/pg tsx
236
+ ```
237
+
238
+ ### Environment Variables
239
+
240
+ ```env
241
+ # Monorepo: packages/db/.env | Single-repo: .env at root
242
+ {{GROUP_UPPER}}_DATABASE_URL=postgresql://user:password@localhost:5432/{{GROUP}}_db
243
+ ```
244
+
245
+ ### Manual Steps Remaining
246
+
247
+ 1. **Create a PostgreSQL database** — `createdb {{GROUP}}_db` (or use your database hosting provider)
248
+ 2. **Set DATABASE_URL** — update the `.env` file with your actual connection string
249
+ 3. **Install dependencies** — `pnpm install` from the project root
250
+ 4. **Generate initial migration** — `pnpm db:generate`
251
+ 5. **Apply migration** — `pnpm db:migrate`
252
+ 6. **Verify** — `pnpm db:studio` (should open Drizzle Studio showing your tables)
253
+ 7. **(Optional) Run seed** — `pnpm db:seed` (inserts sample data)
254
+
255
+ ### Files Created
256
+
257
+ List every file that was created or modified, with a one-line description.
258
+
259
+ ### Next Steps
260
+
261
+ - Replace the example entity with your actual domain entities
262
+ - For each new entity: copy the example file pattern, then follow the "Adding New Entities" checklist in BLUEPRINT.md
263
+ - If you need authentication: run `/scaffold-auth` — it will add auth schemas to the existing `packages/db/`
264
+
265
+ ## Important Notes
266
+
267
+ - When adapting code, preserve the `// CONFIGURE:` comments so the user can find customization points later.
268
+ - If the project structure differs significantly from the blueprint's assumptions, explain the differences and suggest how to adapt rather than forcing the blueprint's structure.
269
+ - Never overwrite existing files without asking. If a file exists (like `package.json` or `turbo.json`), merge the new additions into it.
270
+ - If `packages/db/` already exists from the auth blueprint, be especially careful to merge — not overwrite — the `package.json` exports, scripts, and dependencies.