@everystack/mcp 0.3.2 → 0.4.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 (104) hide show
  1. package/dist/adding-database.md +36 -23
  2. package/dist/cli.md +44 -3
  3. package/dist/core.md +19 -1
  4. package/dist/database-operations.md +236 -0
  5. package/dist/derived-objects.md +225 -0
  6. package/dist/gates/detectors/embedded-data-bundle.d.ts +13 -0
  7. package/dist/gates/detectors/embedded-data-bundle.d.ts.map +1 -0
  8. package/dist/gates/detectors/embedded-data-bundle.js +56 -0
  9. package/dist/gates/detectors/hand-written-migration.d.ts +24 -0
  10. package/dist/gates/detectors/hand-written-migration.d.ts.map +1 -0
  11. package/dist/gates/detectors/hand-written-migration.js +54 -0
  12. package/dist/gates/detectors/secret-in-public-env.d.ts +12 -0
  13. package/dist/gates/detectors/secret-in-public-env.d.ts.map +1 -0
  14. package/dist/gates/detectors/secret-in-public-env.js +38 -0
  15. package/dist/gates/engine.d.ts +28 -0
  16. package/dist/gates/engine.d.ts.map +1 -0
  17. package/dist/gates/engine.js +74 -0
  18. package/dist/gates/registry.d.ts +14 -0
  19. package/dist/gates/registry.d.ts.map +1 -0
  20. package/dist/gates/registry.js +21 -0
  21. package/dist/gates/telemetry.d.ts +47 -0
  22. package/dist/gates/telemetry.d.ts.map +1 -0
  23. package/dist/gates/telemetry.js +121 -0
  24. package/dist/gates/types.d.ts +67 -0
  25. package/dist/gates/types.d.ts.map +1 -0
  26. package/dist/gates/types.js +14 -0
  27. package/dist/governance/cli.d.ts +57 -0
  28. package/dist/governance/cli.d.ts.map +1 -0
  29. package/dist/governance/cli.js +169 -0
  30. package/dist/governance/grounding.d.ts +78 -0
  31. package/dist/governance/grounding.d.ts.map +1 -0
  32. package/dist/governance/grounding.js +299 -0
  33. package/dist/index.cjs +119 -107
  34. package/dist/index.d.ts +2 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +94 -0
  37. package/dist/project-claude-md.md +35 -18
  38. package/dist/prompts/add-feature.d.ts +3 -0
  39. package/dist/prompts/add-feature.d.ts.map +1 -0
  40. package/dist/prompts/add-feature.js +154 -0
  41. package/dist/prompts/claude-md.d.ts +12 -0
  42. package/dist/prompts/claude-md.d.ts.map +1 -0
  43. package/dist/prompts/claude-md.js +87 -0
  44. package/dist/prompts/debug.d.ts +3 -0
  45. package/dist/prompts/debug.d.ts.map +1 -0
  46. package/dist/prompts/debug.js +129 -0
  47. package/dist/prompts/deploy.d.ts +3 -0
  48. package/dist/prompts/deploy.d.ts.map +1 -0
  49. package/dist/prompts/deploy.js +118 -0
  50. package/dist/prompts/design-schema.d.ts +3 -0
  51. package/dist/prompts/design-schema.d.ts.map +1 -0
  52. package/dist/prompts/design-schema.js +97 -0
  53. package/dist/prompts/governance-setup.d.ts +9 -0
  54. package/dist/prompts/governance-setup.d.ts.map +1 -0
  55. package/dist/prompts/governance-setup.js +76 -0
  56. package/dist/prompts/index.d.ts +3 -0
  57. package/dist/prompts/index.d.ts.map +1 -0
  58. package/dist/prompts/index.js +20 -0
  59. package/dist/prompts/new-app.d.ts +3 -0
  60. package/dist/prompts/new-app.d.ts.map +1 -0
  61. package/dist/prompts/new-app.js +203 -0
  62. package/dist/prompts/runbook.d.ts +12 -0
  63. package/dist/prompts/runbook.d.ts.map +1 -0
  64. package/dist/prompts/runbook.js +70 -0
  65. package/dist/prompts/secure.d.ts +3 -0
  66. package/dist/prompts/secure.d.ts.map +1 -0
  67. package/dist/prompts/secure.js +219 -0
  68. package/dist/resources/index.d.ts +12 -0
  69. package/dist/resources/index.d.ts.map +1 -0
  70. package/dist/resources/index.js +182 -0
  71. package/dist/schema-patterns.md +92 -106
  72. package/dist/tools/check-environment.d.ts +25 -0
  73. package/dist/tools/check-environment.d.ts.map +1 -0
  74. package/dist/tools/check-environment.js +281 -0
  75. package/dist/tools/index.d.ts +3 -0
  76. package/dist/tools/index.d.ts.map +1 -0
  77. package/dist/tools/index.js +18 -0
  78. package/dist/tools/project-status.d.ts +28 -0
  79. package/dist/tools/project-status.d.ts.map +1 -0
  80. package/dist/tools/project-status.js +138 -0
  81. package/dist/tools/project-validate.d.ts +19 -0
  82. package/dist/tools/project-validate.d.ts.map +1 -0
  83. package/dist/tools/project-validate.js +323 -0
  84. package/dist/tools/schema-analyze.d.ts +46 -0
  85. package/dist/tools/schema-analyze.d.ts.map +1 -0
  86. package/dist/tools/schema-analyze.js +336 -0
  87. package/package.json +3 -3
  88. package/src/gates/detectors/hand-written-migration.ts +29 -13
  89. package/src/index.ts +1 -1
  90. package/src/prompts/add-feature.ts +4 -4
  91. package/src/prompts/claude-md.ts +4 -2
  92. package/src/prompts/debug.ts +3 -4
  93. package/src/prompts/deploy.ts +17 -8
  94. package/src/prompts/design-schema.ts +45 -59
  95. package/src/prompts/new-app.ts +21 -19
  96. package/src/prompts/secure.ts +3 -3
  97. package/src/resources/adding-database.md +36 -23
  98. package/src/resources/cli.md +44 -3
  99. package/src/resources/core.md +19 -1
  100. package/src/resources/database-operations.md +236 -0
  101. package/src/resources/derived-objects.md +225 -0
  102. package/src/resources/index.ts +15 -1
  103. package/src/resources/project-claude-md.md +35 -18
  104. package/src/resources/schema-patterns.md +92 -106
@@ -1,167 +1,153 @@
1
1
  # Schema Design Patterns
2
2
 
3
- > Drizzle schema design, migrations, and RLS setup for everystack apps.
3
+ > Declaring TABLES with `defineModel` in `db/models/` fields, authz, relations, indexes.
4
4
 
5
5
  ## When to Use
6
- Read this when designing your database schema or adding tables to an existing app.
6
+ Read this when designing your database schema or adding tables to an existing app. This
7
+ resource covers **tables** (`defineModel`). For the derived layer —
8
+ views, materialized views, functions, triggers, standalone sequences — read
9
+ `everystack://derived-objects` (they deploy via `db:reconcile`, not migrations). For the
10
+ operational verbs that move a database (`db:sync`, `db:generate`, `db:plan`/`db:apply`,
11
+ `db:check`), read `everystack://database-operations`.
7
12
 
8
13
  ## Schema Location
9
14
 
10
- Schemas live in `db/schema.ts` (or `db/schema/` directory for larger apps). This file is the single source of truth for both the handler and SSR.
15
+ The whole declared database lives in `db/models/` one `defineModel` per table, composed
16
+ onto a `defineModule`. This is the single source of truth for the handler, SSR, RLS, and
17
+ grants. You do **not** hand-write `db/schema.ts`, and you do **not** run
18
+ `drizzle-kit generate`/`drizzle-kit migrate` — the Model is authored, and everystack
19
+ generates the migration (`db:generate`) and the typed artifact (`db/schema.generated.ts`,
20
+ never edited). Never hand-write a SQL migration.
11
21
 
12
22
  ## Basic Table
13
23
 
14
24
  ```typescript
15
- import { pgTable, serial, text, integer, timestamp, uuid } from 'drizzle-orm/pg-core';
16
-
17
- export const posts = pgTable('posts', {
18
- id: uuid('id').defaultRandom().primaryKey(),
19
- body: text('body').notNull(),
20
- authorId: uuid('author_id').notNull().references(() => users.id),
21
- status: text('status').default('draft').notNull(),
22
- createdAt: timestamp('created_at').defaultNow().notNull(),
23
- updatedAt: timestamp('updated_at').defaultNow().notNull(),
24
- deletedAt: timestamp('deleted_at'),
25
+ import { defineModel, field, can } from '@everystack/model';
26
+
27
+ export const Post = defineModel('posts', {
28
+ fields: {
29
+ id: field.uuid().primaryKey().defaultRandom(),
30
+ body: field.text().notNull(),
31
+ authorId: field.uuid().notNull().references('users', 'id'),
32
+ status: field.text().notNull().default('draft'),
33
+ createdAt: field.timestamptz().notNull().defaultNow(),
34
+ updatedAt: field.timestamptz().notNull().defaultNow(),
35
+ deletedAt: field.timestamptz(),
36
+ },
37
+ abilities: [can('read'), can('create'), can('update'), can('delete')],
25
38
  });
26
39
  ```
27
40
 
41
+ Column names are `snake_case` in the database; the field key is the app-side name — the
42
+ generator maps `authorId` → `author_id` for you.
43
+
28
44
  ## Common Patterns
29
45
 
30
46
  ### UUID vs Serial Primary Keys
31
47
  Prefer UUID for user-facing IDs (prevents enumeration attacks). Use serial for internal-only tables.
32
48
 
33
49
  ```typescript
34
- id: uuid('id').defaultRandom().primaryKey(), // Preferred for API-exposed tables
35
- id: serial('id').primaryKey(), // OK for internal tables
50
+ id: field.uuid().primaryKey().defaultRandom(), // Preferred for API-exposed tables
51
+ id: field.serial().primaryKey(), // OK for internal tables
36
52
  ```
37
53
 
38
54
  ### Timestamps
39
55
  Always include `createdAt` and `updatedAt`:
40
56
  ```typescript
41
- createdAt: timestamp('created_at').defaultNow().notNull(),
42
- updatedAt: timestamp('updated_at').defaultNow().notNull(),
57
+ createdAt: field.timestamptz().notNull().defaultNow(),
58
+ updatedAt: field.timestamptz().notNull().defaultNow(),
43
59
  ```
44
60
 
45
61
  ### Soft Delete
46
62
  Add `deletedAt` for soft-deletable tables:
47
63
  ```typescript
48
- deletedAt: timestamp('deleted_at'),
64
+ deletedAt: field.timestamptz(),
49
65
  ```
50
66
  Configure in handler: `softDelete: { column: 'deletedAt', tables: ['posts'] }`.
51
67
 
52
68
  ### Foreign Keys
53
69
  ```typescript
54
- authorId: uuid('author_id').notNull().references(() => users.id),
70
+ authorId: field.uuid().notNull().references('users', 'id'),
55
71
  ```
56
72
 
57
73
  ### Enums
58
74
  ```typescript
59
- import { pgEnum } from 'drizzle-orm/pg-core';
60
-
61
- export const roleEnum = pgEnum('role', ['user', 'admin']);
62
-
63
- export const users = pgTable('users', {
64
- role: roleEnum('role').default('user').notNull(),
65
- });
75
+ role: field.enum('role', ['user', 'admin']).notNull().default('user'),
66
76
  ```
67
77
 
68
- ## Relations (Drizzle)
78
+ ## Relations
69
79
 
70
- ```typescript
71
- import { relations } from 'drizzle-orm';
80
+ Declare relations on the Model — the generator emits both the Drizzle relations (used by
81
+ the relational query API, `db.query.posts.findMany({ with: { author: true } })`) and the
82
+ handler's embedding config, so they can't drift apart.
72
83
 
73
- export const postsRelations = relations(posts, ({ one, many }) => ({
74
- author: one(users, { fields: [posts.authorId], references: [users.id] }),
75
- comments: many(comments),
76
- }));
84
+ ```typescript
85
+ export const Post = defineModel('posts', {
86
+ fields: { /* … */ },
87
+ relations: {
88
+ author: { model: 'users', from: 'authorId', to: 'id' },
89
+ comments: { model: 'comments', from: 'id', to: 'postId', many: true },
90
+ },
91
+ abilities: [can('read')],
92
+ });
77
93
  ```
78
94
 
79
- Drizzle relations are used by the relational query API (`db.query.posts.findMany({ with: { author: true } })`). The handler also needs its own `relations` config for embedding.
80
-
81
95
  ## Indexes
82
96
 
83
97
  ```typescript
84
- import { index, uniqueIndex } from 'drizzle-orm/pg-core';
85
-
86
- export const posts = pgTable('posts', {
87
- // columns...
88
- }, (table) => [
89
- index('posts_author_id_idx').on(table.authorId),
90
- uniqueIndex('posts_slug_idx').on(table.slug),
91
- ]);
98
+ import { defineModel, field, can, index } from '@everystack/model';
99
+
100
+ export const Post = defineModel('posts', {
101
+ fields: { /* … */ },
102
+ indexes: [
103
+ index(['authorId']),
104
+ index(['slug']).unique(),
105
+ ],
106
+ abilities: [can('read')],
107
+ });
92
108
  ```
93
109
 
94
- ## Migration Workflow
95
-
96
- ```bash
97
- # Generate migration from schema changes
98
- npx drizzle-kit generate
99
-
100
- # Apply migrations locally
101
- npx drizzle-kit migrate
110
+ ## Authorization is declared, not migrated
102
111
 
103
- # Apply migrations on deployed Lambda
104
- everystack db:migrate
105
- ```
112
+ You never hand-write `CREATE ROLE`, `GRANT`, or `CREATE POLICY`. Abilities on the Model
113
+ compile to grants + RLS, and `db:sync`/`db:apply` deploy them. `can('read')` grants `anon`
114
+ + `authenticated`; `can('read', { role: 'admin' })` narrows to a role. RLS is required —
115
+ the CI gate (`db:check`) refuses a table without it. See `everystack://security` for the
116
+ policy patterns the abilities compile to.
106
117
 
107
- Migration files go in `drizzle/` directory. Each migration is a SQL file.
108
-
109
- ### RLS Migration Template
110
-
111
- After creating tables, add a migration for roles and policies:
112
-
113
- ```sql
114
- -- Create roles (idempotent)
115
- DO $$ BEGIN
116
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'authenticator') THEN
117
- CREATE ROLE authenticator LOGIN NOINHERIT;
118
- END IF;
119
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'anon') THEN
120
- CREATE ROLE anon NOLOGIN;
121
- END IF;
122
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'authenticated') THEN
123
- CREATE ROLE authenticated NOLOGIN;
124
- END IF;
125
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'admin') THEN
126
- CREATE ROLE admin NOLOGIN;
127
- END IF;
128
- END $$;
129
-
130
- GRANT anon TO authenticator;
131
- GRANT authenticated TO authenticator;
132
- GRANT admin TO authenticator;
133
-
134
- -- Table grants
135
- GRANT USAGE ON SCHEMA public TO anon, authenticated, admin;
136
- GRANT SELECT ON posts TO anon;
137
- GRANT SELECT, INSERT, UPDATE, DELETE ON posts TO authenticated;
138
- GRANT SELECT, INSERT, UPDATE, DELETE ON posts TO admin;
139
-
140
- -- Enable RLS
141
- ALTER TABLE posts ENABLE ROW LEVEL SECURITY;
142
- ALTER TABLE posts FORCE ROW LEVEL SECURITY;
143
-
144
- -- Policies (see everystack://security for templates)
118
+ ```typescript
119
+ export const Post = defineModel('posts', {
120
+ fields: { /* … */ },
121
+ abilities: [
122
+ can('read'), // anon + authenticated
123
+ can('create', { role: 'authenticated' }),
124
+ can('update', { role: 'authenticated', own: 'authorId' }), // row ownership
125
+ can('delete', { role: 'admin' }),
126
+ ],
127
+ });
145
128
  ```
146
129
 
147
130
  ## Handler Config for Schema
148
131
 
132
+ The handler config is **derived** from the Models (`deriveHandlerConfig(models)`), so
133
+ `exposedTables`, `hiddenColumns`, `protectedFields`, and `relations` come from the
134
+ declarations rather than a hand-kept parallel list:
135
+
149
136
  ```typescript
150
- createHandler(db, schema, {
151
- exposedTables: ['posts', 'profiles'], // Only these are API-accessible
152
- hiddenColumns: { users: ['passwordHash', 'resetToken'] },
153
- protectedFields: { profiles: ['role', 'deletedAt'] },
154
- relations: {
155
- posts: { author: { table: 'users', from: 'authorId', to: 'id' } },
156
- },
157
- });
137
+ import { deriveHandlerConfig } from '@everystack/model';
138
+ import { models } from '../db/models';
139
+
140
+ createHandler(db, schema, deriveHandlerConfig(models));
158
141
  ```
159
142
 
160
143
  ## Gotchas
161
144
 
162
- - Drizzle column names (camelCase) and SQL column names (snake_case) are both specified: `authorId: uuid('author_id')`
163
- - The handler's `relations` config uses Drizzle property names, not SQL column names
164
- - `exposedTables` uses Drizzle table variable names, not SQL table names
165
- - Always run `drizzle-kit generate` after schema changes
166
- - RLS migration must be run AFTER table creation migration
167
- - Connect as `authenticator` in production (never RDS master/superuser)
145
+ - Field keys are app-side names (camelCase); the database column is `snake_case` the
146
+ generator maps `authorId` `author_id`.
147
+ - Edit the Model, then move the database with `db:sync` (dev) or `db:generate` +
148
+ `db:plan`/`db:apply` (protected stages). Never run `drizzle-kit generate`.
149
+ - RLS and grants are declared with `can()`, never hand-written; `db:check` fails a table
150
+ without RLS.
151
+ - `db/schema.generated.ts` is a generated artifact — never edit it; `db:check` refuses
152
+ drift.
153
+ - Connect as `authenticator` in production (never RDS master/superuser).