supatool 0.3.7 → 0.4.1
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/README.md +60 -281
- package/dist/bin/helptext.js +4 -3
- package/dist/bin/supatool.js +182 -74
- package/dist/generator/client.js +1 -2
- package/dist/generator/crudGenerator.js +22 -23
- package/dist/generator/docGenerator.js +12 -13
- package/dist/generator/rlsGenerator.js +21 -21
- package/dist/generator/sqlGenerator.js +20 -21
- package/dist/generator/typeGenerator.js +6 -7
- package/dist/index.js +23 -23
- package/dist/parser/modelParser.js +3 -4
- package/dist/sync/config.js +10 -10
- package/dist/sync/definitionExtractor.js +589 -321
- package/dist/sync/fetchRemoteSchemas.js +59 -43
- package/dist/sync/generateMigration.js +42 -42
- package/dist/sync/parseLocalSchemas.js +14 -11
- package/dist/sync/seedGenerator.js +15 -14
- package/dist/sync/sync.js +75 -140
- package/dist/sync/utils.js +19 -7
- package/dist/sync/writeSchema.js +22 -22
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,330 +1,109 @@
|
|
|
1
1
|
# Supatool
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**The AI-Native Schema Management CLI for Supabase.** Extract database schemas into LLM-friendly structures, generate `llms.txt` catalogs, and manage seeds without drowning your AI's context.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
- Generate TypeScript CRUD functions from Supabase types or model YAML
|
|
8
|
-
- Output human-readable and AI-friendly schema/index files
|
|
9
|
-
- Flexible environment/configuration and batch processing
|
|
10
|
-
- Simple CLI with help and documentation
|
|
5
|
+
[](https://www.npmjs.com/package/supatool)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
11
7
|
|
|
12
|
-
|
|
8
|
+
## Why Supatool?
|
|
13
9
|
|
|
14
|
-
|
|
10
|
+
Modern AI coding tools (Cursor, Claude, MCP) often struggle with large database schemas. Typical issues include:
|
|
11
|
+
- **Token Waste:** Reading the entire schema at once consumes 10k+ tokens.
|
|
12
|
+
- **Lost Context:** Frequent API calls to fetch table details via MCP lead to fragmented reasoning.
|
|
13
|
+
- **Inaccuracy:** AI misses RLS policies or complex FK relations split across multiple files.
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
npm install -g supatool
|
|
18
|
-
# or
|
|
19
|
-
yarn global add supatool
|
|
20
|
-
# or
|
|
21
|
-
pnpm add -g supatool
|
|
22
|
-
```
|
|
15
|
+
**Supatool solves this** by reorganizing your Supabase schema into a highly searchable, indexed, and modular structure that helps AI "understand" your DB with minimal tokens.
|
|
23
16
|
|
|
24
|
-
|
|
17
|
+
---
|
|
25
18
|
|
|
26
|
-
|
|
19
|
+
## Key Features
|
|
27
20
|
|
|
28
|
-
Extract and
|
|
21
|
+
- **Extract (AI-Optimized)** – DDL, RLS, and Triggers are bundled into **one file per table**. AI gets the full picture of a table by opening just one file.
|
|
22
|
+
- **llms.txt Catalog** – Automatically generates a standard `llms.txt` listing all OBJECTS, RELATIONS (FKs), and RPC dependencies. This serves as the "Map" for AI agents.
|
|
23
|
+
- **Multi-Schema Support** – Group objects by schema (e.g., `public`, `agent`, `auth`) with proper schema-qualification in SQL.
|
|
24
|
+
- **Seed for AI** – Export table data as JSON. Includes a dedicated `llms.txt` for seeds so AI can see real data structures.
|
|
25
|
+
- **Safe Deploy** – Push local schema changes with `--dry-run` to preview DDL before execution.
|
|
26
|
+
- **CRUD (Deprecated)** – Legacy code generation is still available but discouraged in favor of LLM-native development.
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
# Set connection string in environment (recommended)
|
|
32
|
-
echo "SUPABASE_CONNECTION_STRING=postgresql://..." >> .env.local
|
|
28
|
+
---
|
|
33
29
|
|
|
34
|
-
|
|
35
|
-
supatool extract --all -o supabase/schemas
|
|
36
|
-
|
|
37
|
-
# Extract only tables and views
|
|
38
|
-
supatool extract -o supabase/schemas
|
|
39
|
-
|
|
40
|
-
# Extract specific tables with pattern
|
|
41
|
-
supatool extract -t "user_*" -o supabase/schemas
|
|
42
|
-
|
|
43
|
-
# Extract from specific schemas (comma-separated)
|
|
44
|
-
supatool extract --all --schema public,auth,extensions -o supabase/schemas
|
|
45
|
-
|
|
46
|
-
# Alternative: specify connection directly
|
|
47
|
-
supatool extract --all -c "postgresql://..." -o supabase/schemas
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
**Output structure:**
|
|
51
|
-
```
|
|
52
|
-
supabase/schemas/
|
|
53
|
-
├── index.md # Human-readable index with comments
|
|
54
|
-
├── llms.txt # AI-friendly structured data with comments
|
|
55
|
-
├── tables/ # Table definitions with comments
|
|
56
|
-
├── views/ # View definitions with comments
|
|
57
|
-
├── rls/ # RLS policies
|
|
58
|
-
└── rpc/ # Functions & triggers
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### Generate CRUD Code
|
|
62
|
-
|
|
63
|
-
Generate TypeScript CRUD functions from Supabase types:
|
|
30
|
+
## Quick Start
|
|
64
31
|
|
|
65
32
|
```bash
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
# Generate from schema SQL files
|
|
70
|
-
supatool gen:schema-crud --include-views --react-query
|
|
71
|
-
|
|
72
|
-
# Generate from model YAML
|
|
73
|
-
supatool gen:crud model.yaml
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
**Output:** `src/integrations/supabase/crud-autogen/`
|
|
77
|
-
|
|
78
|
-
### Environment Configuration
|
|
79
|
-
|
|
80
|
-
For security and convenience, set your connection string in environment variables:
|
|
81
|
-
|
|
82
|
-
```bash
|
|
83
|
-
# Create .env.local file with your connection details
|
|
84
|
-
cat > .env.local << EOF
|
|
85
|
-
SUPABASE_CONNECTION_STRING=postgresql://user:password@host:port/database
|
|
86
|
-
# Alternative: use DATABASE_URL
|
|
87
|
-
DATABASE_URL=postgresql://user:password@host:port/database
|
|
88
|
-
EOF
|
|
89
|
-
|
|
90
|
-
# Now you can run commands without -c flag
|
|
91
|
-
supatool extract --all -o supabase/schemas
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
**Supported environment variables:**
|
|
95
|
-
- `SUPABASE_CONNECTION_STRING` (preferred)
|
|
96
|
-
- `DATABASE_URL` (fallback)
|
|
97
|
-
- `SUPATOOL_MAX_CONCURRENT` (max concurrent table processing, default: 20, max: 50)
|
|
98
|
-
|
|
99
|
-
### Additional Commands
|
|
100
|
-
|
|
101
|
-
```bash
|
|
102
|
-
# Show help for all commands
|
|
103
|
-
supatool help
|
|
33
|
+
npm install -g supatool
|
|
34
|
+
# Set your connection string
|
|
35
|
+
export SUPABASE_CONNECTION_STRING="postgresql://postgres:[password]@db.[ref].supabase.co:5432/postgres"
|
|
104
36
|
|
|
105
|
-
#
|
|
106
|
-
supatool
|
|
107
|
-
```
|
|
37
|
+
# Extract schema and generate AI-ready docs
|
|
38
|
+
supatool extract --schema public,auth -o supabase/schemas
|
|
108
39
|
|
|
109
|
-
## Database Comments
|
|
110
|
-
|
|
111
|
-
Supatool automatically extracts and includes PostgreSQL comments in all generated files. Comments enhance documentation and AI understanding of your schema.
|
|
112
|
-
|
|
113
|
-
- Table, view, function, and type comments are included in generated SQL and documentation.
|
|
114
|
-
- AI-friendly index files (llms.txt) and Markdown index (index.md) include comments for better context.
|
|
115
|
-
|
|
116
|
-
## VSCode/Cursor Integration
|
|
117
|
-
|
|
118
|
-
Add these tasks to `.vscode/tasks.json` for quick access:
|
|
119
|
-
|
|
120
|
-
> **Note:** Restart VSCode/Cursor after installing supatool to ensure the command is recognized.
|
|
121
|
-
|
|
122
|
-
```json
|
|
123
|
-
{
|
|
124
|
-
"version": "2.0.0",
|
|
125
|
-
"tasks": [
|
|
126
|
-
{
|
|
127
|
-
"label": "Extract Schema and Generate CRUD",
|
|
128
|
-
"type": "shell",
|
|
129
|
-
"command": "supatool extract --all -o supabase/schemas && supatool gen:schema-crud --react-query",
|
|
130
|
-
"group": "build",
|
|
131
|
-
"dependsOn": "setup-env"
|
|
132
|
-
},
|
|
133
|
-
{
|
|
134
|
-
"label": "setup-env",
|
|
135
|
-
"type": "shell",
|
|
136
|
-
"command": "echo 'Ensure SUPABASE_CONNECTION_STRING is set in .env.local'",
|
|
137
|
-
"group": "build"
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
"label": "Generate Types and CRUD (Legacy)",
|
|
141
|
-
"type": "shell",
|
|
142
|
-
"command": "mkdir -p shared && npx supabase gen types typescript --project-id ${input:projectId} --schema public > shared/types.ts && supatool crud --force",
|
|
143
|
-
"group": "build"
|
|
144
|
-
}
|
|
145
|
-
],
|
|
146
|
-
"inputs": [
|
|
147
|
-
{
|
|
148
|
-
"id": "projectId",
|
|
149
|
-
"description": "Supabase project ID",
|
|
150
|
-
"default": "your_project_id"
|
|
151
|
-
}
|
|
152
|
-
]
|
|
153
|
-
}
|
|
154
40
|
```
|
|
155
41
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
CRUD utility files for the `apps` table will be generated in `src/integrations/supabase/crud-autogen/` by default.
|
|
42
|
+
### Output Structure
|
|
159
43
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
deleteAppsRow,
|
|
171
|
-
} from 'src/integrations/supabase/crud-autogen/apps';
|
|
172
|
-
|
|
173
|
-
// Select multiple rows with filters (NEW: destructuring parameters)
|
|
174
|
-
const apps = await selectAppsRowsWithFilters({ filters: { status: 'active' } });
|
|
175
|
-
|
|
176
|
-
// Select a single row with filters
|
|
177
|
-
const app = await selectAppsSingleRowWithFilters({ filters: { id: 'your-app-id' } });
|
|
178
|
-
|
|
179
|
-
// Select by ID (NEW: destructuring parameters)
|
|
180
|
-
const appById = await selectAppsRowById({ id: 'your-app-id' });
|
|
181
|
-
|
|
182
|
-
// Insert new row (NEW: destructuring parameters)
|
|
183
|
-
const newApp = await insertAppsRow({ data: { name: 'New App', status: 'active' } });
|
|
184
|
-
|
|
185
|
-
// Update row (NEW: destructuring parameters)
|
|
186
|
-
const updatedApp = await updateAppsRow({
|
|
187
|
-
id: 'your-app-id',
|
|
188
|
-
data: { name: 'Updated Name' }
|
|
189
|
-
});
|
|
44
|
+
```text
|
|
45
|
+
supabase/schemas/
|
|
46
|
+
├── llms.txt # 🗺️ THE ENTRY POINT: Read this first to understand the DB map
|
|
47
|
+
├── schema_index.json # 🤖 For JSON-parsing agents
|
|
48
|
+
├── schema_summary.md # 📄 Single-file overview for quick human/AI scanning
|
|
49
|
+
├── README.md # Navigation guide
|
|
50
|
+
└── [schema_name]/
|
|
51
|
+
├── tables/ # table_name.sql (DDL + RLS + Triggers)
|
|
52
|
+
├── views/
|
|
53
|
+
└── rpc/
|
|
190
54
|
|
|
191
|
-
// Delete row (NEW: destructuring parameters)
|
|
192
|
-
const success = await deleteAppsRow({ id: 'your-app-id' });
|
|
193
55
|
```
|
|
194
56
|
|
|
195
|
-
|
|
196
|
-
- You can use filter objects for flexible queries.
|
|
197
|
-
- See the generated file for each table in `src/integrations/supabase/crud-autogen/` for details.
|
|
57
|
+
---
|
|
198
58
|
|
|
199
|
-
##
|
|
59
|
+
## Best Practices for AI Agents (Cursor / Claude / MCP)
|
|
200
60
|
|
|
201
|
-
|
|
202
|
-
If your table's primary key is not named `id`, the `selectById`, `update`, and `delete` functions will not be generated for that table.
|
|
203
|
-
|
|
204
|
-
## Repository
|
|
61
|
+
To get the best results from your AI coding assistant, follow these steps:
|
|
205
62
|
|
|
206
|
-
|
|
63
|
+
1. **Start with the Map:** Always ask the AI to read `supabase/schemas/llms.txt` first.
|
|
64
|
+
2. **Targeted Reading:** Once the AI identifies the relevant tables from the catalog, instruct it to open only those specific `.sql` files.
|
|
65
|
+
3. **Understand Relations:** Use the `RELATIONS` section in `llms.txt` to help the AI write accurate JOINs without reading every file.
|
|
66
|
+
4. **RPC Context:** If using functions, refer to `RPC_TABLES` in `llms.txt` to know which tables are affected.
|
|
207
67
|
|
|
208
|
-
|
|
68
|
+
---
|
|
209
69
|
|
|
210
|
-
|
|
70
|
+
## Commands
|
|
211
71
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
### Database-First Development
|
|
215
|
-
|
|
216
|
-
Extract your existing database schema and generate CRUD code using [Supabase's declarative schema workflow](https://supabase.com/docs/guides/local-development/declarative-database-schemas):
|
|
72
|
+
### Extract
|
|
217
73
|
|
|
218
74
|
```bash
|
|
219
|
-
# 1. Set connection string in .env.local
|
|
220
|
-
echo "SUPABASE_CONNECTION_STRING=postgresql://..." >> .env.local
|
|
221
|
-
|
|
222
|
-
# 2. Extract all database objects to supabase/schemas (declarative schema format)
|
|
223
75
|
supatool extract --all -o supabase/schemas
|
|
76
|
+
# Options:
|
|
77
|
+
# --schema public,agent Specify schemas
|
|
78
|
+
# -t "user_*" Filter tables by pattern
|
|
79
|
+
# --force Clear output dir before writing (prevents orphan files)
|
|
224
80
|
|
|
225
|
-
# 3. Generate migrations from declarative schema
|
|
226
|
-
supabase db diff -f initial_schema
|
|
227
|
-
|
|
228
|
-
# 4. Generate TypeScript types with Supabase CLI
|
|
229
|
-
npx supabase gen types typescript --local > src/types/database.ts
|
|
230
|
-
|
|
231
|
-
# 5. Generate CRUD functions with React Query support
|
|
232
|
-
supatool gen:schema-crud --include-views --react-query
|
|
233
81
|
```
|
|
234
82
|
|
|
235
|
-
###
|
|
83
|
+
### Seed
|
|
236
84
|
|
|
237
|
-
|
|
85
|
+
Export specific tables for AI reference or testing:
|
|
238
86
|
|
|
239
87
|
```bash
|
|
240
|
-
|
|
241
|
-
supatool create model.yaml
|
|
88
|
+
supatool seed --tables tables.yaml
|
|
242
89
|
|
|
243
|
-
# 2. Generate everything from model
|
|
244
|
-
supatool gen:all model.yaml
|
|
245
90
|
```
|
|
246
91
|
|
|
247
|
-
|
|
92
|
+
*Outputs JSON files and a `llms.txt` index in `supabase/seeds/`.*
|
|
248
93
|
|
|
249
|
-
|
|
94
|
+
### Deploy
|
|
250
95
|
|
|
251
96
|
```bash
|
|
252
|
-
|
|
253
|
-
echo "SUPABASE_CONNECTION_STRING=postgresql://..." >> .env.local
|
|
254
|
-
|
|
255
|
-
# Extract all database objects to categorized directories (recommended)
|
|
256
|
-
supatool extract --all -o supabase/schemas
|
|
257
|
-
# Output: supabase/schemas/{index.md,tables/,views/,rls/,rpc/}
|
|
258
|
-
# Compatible with: supabase db diff
|
|
259
|
-
|
|
260
|
-
# Extract only tables and views
|
|
261
|
-
supatool extract -o supabase/schemas
|
|
262
|
-
# Output: supabase/schemas/{index.md,tables/,views/}
|
|
263
|
-
|
|
264
|
-
# Extract to single directory (for simple projects)
|
|
265
|
-
supatool extract --no-separate -o supabase/schemas
|
|
266
|
-
# Output: supabase/schemas/{index.md,*.sql}
|
|
267
|
-
|
|
268
|
-
# Extract with pattern matching
|
|
269
|
-
supatool extract -t "user_*" -o ./user-tables
|
|
270
|
-
|
|
271
|
-
# Extract from specific schemas (default: public)
|
|
272
|
-
supatool extract --all --schema public,auth,extensions -o supabase/schemas
|
|
273
|
-
|
|
274
|
-
# Alternative: specify connection directly
|
|
275
|
-
supatool extract --all -c "postgresql://..." -o supabase/schemas
|
|
276
|
-
```
|
|
277
|
-
|
|
278
|
-
## Seed Command (v0.3.5+)
|
|
279
|
-
|
|
280
|
-
Export selected table data from your remote Supabase DB as AI-friendly seed JSON files.
|
|
281
|
-
|
|
282
|
-
### Usage
|
|
97
|
+
supatool deploy --dry-run
|
|
283
98
|
|
|
284
99
|
```
|
|
285
|
-
supatool seed --tables tables.yaml --connection <CONNECTION_STRING>
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
- `tables.yaml` example:
|
|
289
|
-
```yaml
|
|
290
|
-
tables:
|
|
291
|
-
- users
|
|
292
|
-
- public.orders
|
|
293
|
-
```
|
|
294
|
-
- Output: `supabase/seeds/<timestamp>_supatool/{table}_seed.json`
|
|
295
|
-
- Each file contains a snapshot of the remote DB table at the time of export.
|
|
296
|
-
|
|
297
|
-
### Example output (users_seed.json)
|
|
298
|
-
```json
|
|
299
|
-
{
|
|
300
|
-
"table": "public.users",
|
|
301
|
-
"fetched_at": "2024-07-05T11:16:00Z",
|
|
302
|
-
"fetched_by": "supatool v0.3.5",
|
|
303
|
-
"note": "This data is a snapshot of the remote DB at the above time. For AI coding reference. You can update it by running the update command again.",
|
|
304
|
-
"rows": [
|
|
305
|
-
{ "id": 1, "name": "Taro Yamada", "email": "taro@example.com" },
|
|
306
|
-
{ "id": 2, "name": "Hanako Suzuki", "email": "hanako@example.com" }
|
|
307
|
-
]
|
|
308
|
-
}
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
> **Warning:** Do not include sensitive or personal data in seed files. Handle all exported data with care.
|
|
312
100
|
|
|
313
|
-
|
|
101
|
+
---
|
|
314
102
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
- Note: `llms.txt` is not generated inside each timestamped subfolder, only in `supabase/seeds/`.
|
|
103
|
+
## Repository
|
|
318
104
|
|
|
319
|
-
|
|
320
|
-
```
|
|
321
|
-
# AI seed data index (generated by supatool)
|
|
322
|
-
# fetched_at: 2024-07-05T11:16:00Z
|
|
323
|
-
# folder: 20240705_1116_supatool
|
|
324
|
-
public.users: users_seed.json (2 rows) # User account table
|
|
325
|
-
public.orders: orders_seed.json (5 rows)
|
|
326
|
-
```
|
|
105
|
+
[GitHub](https://github.com/idea-garage/supatool) · [npm](https://www.npmjs.com/package/supatool)
|
|
327
106
|
|
|
328
|
-
|
|
107
|
+
---
|
|
329
108
|
|
|
330
|
-
|
|
109
|
+
*Developed with ❤️ for the Supabase community. Use at your own risk. Always backup your DB before deployment.*
|
package/dist/bin/helptext.js
CHANGED
|
@@ -12,14 +12,15 @@ Usage:
|
|
|
12
12
|
Commands:
|
|
13
13
|
extract Extract database objects from Supabase
|
|
14
14
|
gen:types Generate TypeScript types from model YAML
|
|
15
|
-
gen:crud Generate CRUD TypeScript code from model YAML
|
|
15
|
+
gen:crud Generate CRUD TypeScript code from model YAML [deprecated - prefer writing code with LLM]
|
|
16
16
|
gen:docs Generate Markdown documentation from model YAML
|
|
17
17
|
gen:sql Generate SQL (tables, relations, RLS/security) from model YAML
|
|
18
18
|
gen:rls Generate RLS/security SQL from model YAML
|
|
19
19
|
gen:all Generate all outputs from model YAML
|
|
20
20
|
create Generate a template model YAML
|
|
21
|
-
crud Generate CRUD code from Supabase type definitions
|
|
22
|
-
|
|
21
|
+
crud Generate CRUD code from Supabase type definitions [deprecated - prefer writing code with LLM]
|
|
22
|
+
deploy Deploy local schema changes to remote (recommended)
|
|
23
|
+
sync Sync local and remote schemas [deprecated - use deploy]
|
|
23
24
|
seed Export selected table data as AI-friendly seed JSON
|
|
24
25
|
config:init Generate configuration template
|
|
25
26
|
help Show help
|