Allows read-only access to the SQL database to allow querying and analysis using natural language
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.
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"
config.ts file. Each connection has a name and TypeORM DataSourceOptions:
--connection is not specified--connection flag to switch between different database connections defined in your config.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.
User: "Show me the 10 most recent users"
Assistant:
users table with relevant columnstsx src/query-db.ts query "SELECT `id`, `username`, `email`, `created_at` FROM `users` ORDER BY `created_at` DESC LIMIT 10"
User: "How many items do we have in each category?"
Assistant:
tsx src/query-db.ts query "SELECT `category`, COUNT(*) as `item_count` FROM `items` GROUP BY `category` ORDER BY `item_count` DESC"
User: "Compare user counts between development and production databases"
Assistant:
tsx src/query-db.ts --connection development query "SELECT COUNT(*) as `user_count` FROM `users`"
tsx src/query-db.ts --connection production query "SELECT COUNT(*) as `user_count` FROM `users`"