Database Explorer
Instructions
This skill allows read-only access to SQL databases, enabling you to convert natural language queries into SQL queries
and analyze data without requiring the user to write complex SQL by hand.
Available Commands
Use the query-db.ts script with tsx to interact with the database. The script supports the following commands:
All commands support an optional --connection (or -c) flag to specify which database connection to use. If not specified, the default connection is used.
List all tables:
tsx src/query-db.ts tables
# Or with a specific connection:
tsx src/query-db.ts --connection my-postgres-db tables
Introspect entire schema (get all tables and their columns):
tsx src/query-db.ts introspect
# Or with a specific connection:
tsx src/query-db.ts -c my-mysql-db introspect
Describe a specific table:
tsx src/query-db.ts describe <table_name>
# Example:
tsx src/query-db.ts describe users
Execute a SQL query (read-only):
tsx src/query-db.ts query "SELECT * FROM users LIMIT 10"
# With specific connection:
tsx src/query-db.ts --connection=production query "SELECT * FROM users LIMIT 10"
Important Notes
- Read-only mode: Only SELECT, SHOW, DESCRIBE, and EXPLAIN queries are allowed. Any attempt to execute INSERT, UPDATE, DELETE, or DDL statements will be rejected.
- Database configuration: Database connections are defined in the root
config.ts file. Each connection has a name and TypeORM DataSourceOptions:
- A connection named 'default' will be used when
--connection is not specified
- Supported database types: MySQL/MariaDB, PostgreSQL, SQLite
- JSON output: All results are returned as JSON, making them easy to parse and present to the user.
- Multiple connections: Use the
--connection flag to switch between different database connections defined in your config.
Workflow
When the user asks a database question:
First time or unclear schema: Start by introspecting the schema or listing tables to understand the database structure:
tsx src/query-db.ts tables
Understand table structure: If you need details about a specific table:
tsx src/query-db.ts describe users
Convert natural language to SQL: Based on the user's question and the schema, write an appropriate SQL query. Make sure
to correctly format the string according to the DB type, which is given in the config.ts file for the active connection:
SELECT "id", "name", "email" FROM "users" WHERE "createdAt" > '2024-01-01' LIMIT 20;
SELECT `id`, `name`, `email` FROM `users` WHERE `createdAt` > '2024-01-01' LIMIT 20;
Execute the query:
tsx src/query-db.ts query "SELECT id, name, email FROM users WHERE created_at > '2024-01-01' LIMIT 20"
Present results: Format and present the JSON results in a user-friendly way, highlighting key insights. Ask the user if they want to save the report. If yes, save it in output/reports/<connection_name>/<descriptive_filename>.md.
Best Practices
- Cache schema information during a conversation to avoid repeated introspection calls
- Cache last results in temp files in case user wants reports generated
- Use LIMIT clauses to avoid overwhelming results
- For large tables, describe them first before querying
- Explain your SQL queries to the user in plain language
- If a query fails, explain why and suggest alternatives
- Consider performance: use indexes, avoid SELECT * on large tables
- Use aggregate functions (COUNT, SUM, AVG) for statistical queries
Examples
Example 1: Finding recent records
User: "Show me the 10 most recent users"
Assistant:
- First, introspect schema to find user-related tables
- Identify the
users table with relevant columns
- Execute:
tsx src/query-db.ts query "SELECT `id`, `username`, `email`, `created_at` FROM `users` ORDER BY `created_at` DESC LIMIT 10"
- Present the results in a formatted table
- Ask if a report is wanted, if so, save to md file and make sure to include the raw SQL queries that were used.
Example 2: Analyzing data distribution
User: "How many items do we have in each category?"
Assistant:
- Check schema to understand table relationships
- Execute:
tsx src/query-db.ts query "SELECT `category`, COUNT(*) as `item_count` FROM `items` GROUP BY `category` ORDER BY `item_count` DESC"
- Present results with insights
Example 3: Using multiple connections
User: "Compare user counts between development and production databases"
Assistant:
- Query development database:
tsx src/query-db.ts --connection development query "SELECT COUNT(*) as `user_count` FROM `users`"
- Query production database:
tsx src/query-db.ts --connection production query "SELECT COUNT(*) as `user_count` FROM `users`"
- Present comparison with analysis
1---2name: database-explorer3description: Allows read-only access to the SQL database to allow querying and analysis using natural language4---5
6# Database Explorer
7
8## Instructions
9
10This skill allows read-only access to SQL databases, enabling you to convert natural language queries into SQL queries
11and analyze data without requiring the user to write complex SQL by hand.
12
13### Available Commands
14
15Use the `query-db.ts` script with tsx to interact with the database. The script supports the following commands:
16
17All commands support an optional `--connection` (or `-c`) flag to specify which database connection to use. If not specified, the `default` connection is used.
18
191. **List all tables**:
20 ```bash
21 tsx src/query-db.ts tables
22 # Or with a specific connection:
23 tsx src/query-db.ts --connection my-postgres-db tables
24 ```
25
262. **Introspect entire schema** (get all tables and their columns):
27 ```bash
28 tsx src/query-db.ts introspect
29 # Or with a specific connection:
30 tsx src/query-db.ts -c my-mysql-db introspect
31 ```
32
333. **Describe a specific table**:
34 ```bash
35 tsx src/query-db.ts describe <table_name>
36 # Example:
37 tsx src/query-db.ts describe users
38 ```
39
404. **Execute a SQL query** (read-only):
41 ```bash
42 tsx src/query-db.ts query "SELECT * FROM users LIMIT 10"
43 # With specific connection:
44 tsx src/query-db.ts --connection=production query "SELECT * FROM users LIMIT 10"
45 ```
46
47### Important Notes
48
49- **Read-only mode**: Only SELECT, SHOW, DESCRIBE, and EXPLAIN queries are allowed. Any attempt to execute INSERT, UPDATE, DELETE, or DDL statements will be rejected.
50- **Database configuration**: Database connections are defined in the root `config.ts` file. Each connection has a name and TypeORM DataSourceOptions:
51 - A connection named 'default' will be used when `--connection` is not specified
52 - Supported database types: MySQL/MariaDB, PostgreSQL, SQLite
53- **JSON output**: All results are returned as JSON, making them easy to parse and present to the user.
54- **Multiple connections**: Use the `--connection` flag to switch between different database connections defined in your config.
55
56### Workflow
57
58When the user asks a database question:
59
601. **First time or unclear schema**: Start by introspecting the schema or listing tables to understand the database structure:
61 ```bash
62 tsx src/query-db.ts tables
63 ```
64
652. **Understand table structure**: If you need details about a specific table:
66 ```bash
67 tsx src/query-db.ts describe users
68 ```
69
703. **Convert natural language to SQL**: Based on the user's question and the schema, write an appropriate SQL query. Make sure
71 to correctly format the string according to the DB type, which is given in the config.ts file for the active connection:
72 - postgres/sqlite
73 ```
74 SELECT "id", "name", "email" FROM "users" WHERE "createdAt" > '2024-01-01' LIMIT 20;
75 ```
76 - mysql/mariadb
77 ```
78 SELECT `id`, `name`, `email` FROM `users` WHERE `createdAt` > '2024-01-01' LIMIT 20;
79 ```
80
81
824. **Execute the query**:
83 ```bash
84 tsx src/query-db.ts query "SELECT id, name, email FROM users WHERE created_at > '2024-01-01' LIMIT 20"
85 ```
86
875. **Present results**: Format and present the JSON results in a user-friendly way, highlighting key insights. Ask the user if they want to save the report. If yes, save it in `output/reports/<connection_name>/<descriptive_filename>.md`.
88
89### Best Practices
90
91- Cache schema information during a conversation to avoid repeated introspection calls
92- Cache last results in temp files in case user wants reports generated
93- Use LIMIT clauses to avoid overwhelming results
94- For large tables, describe them first before querying
95- Explain your SQL queries to the user in plain language
96- If a query fails, explain why and suggest alternatives
97- Consider performance: use indexes, avoid SELECT * on large tables
98- Use aggregate functions (COUNT, SUM, AVG) for statistical queries
99
100## Examples
101
102### Example 1: Finding recent records
103
104**User**: "Show me the 10 most recent users"
105
106**Assistant**:
1071. First, introspect schema to find user-related tables
1082. Identify the `users` table with relevant columns
1093. Execute:
110 ```bash
111 tsx src/query-db.ts query "SELECT `id`, `username`, `email`, `created_at` FROM `users` ORDER BY `created_at` DESC LIMIT 10"
112 ```
1134. Present the results in a formatted table
1145. Ask if a report is wanted, if so, save to md file and make sure to include the raw SQL queries that were used.
115
116### Example 2: Analyzing data distribution
117
118**User**: "How many items do we have in each category?"
119
120**Assistant**:
1211. Check schema to understand table relationships
1222. Execute:
123 ```bash
124 tsx src/query-db.ts query "SELECT `category`, COUNT(*) as `item_count` FROM `items` GROUP BY `category` ORDER BY `item_count` DESC"
125 ```
1263. Present results with insights
127
128### Example 3: Using multiple connections
129
130**User**: "Compare user counts between development and production databases"
131
132**Assistant**:
1331. Query development database:
134 ```bash
135 tsx src/query-db.ts --connection development query "SELECT COUNT(*) as `user_count` FROM `users`"
136 ```
1372. Query production database:
138 ```bash
139 tsx src/query-db.ts --connection production query "SELECT COUNT(*) as `user_count` FROM `users`"
140 ```
1413. Present comparison with analysis