Improved flow
This commit is contained in:
164
docs/cheatsheets/database.md
Normal file
164
docs/cheatsheets/database.md
Normal file
@@ -0,0 +1,164 @@
|
||||
# Database Cheatsheet
|
||||
|
||||
## Quick Reference
|
||||
|
||||
| Key | Action |
|
||||
|-----|--------|
|
||||
| `<leader>Du` | Toggle DBUI |
|
||||
| `<leader>Da` | Add connection |
|
||||
| `<leader>Df` | Find DB buffer |
|
||||
|
||||
## Opening DBUI
|
||||
|
||||
```
|
||||
<leader>Du Toggle database UI
|
||||
<leader>Da Add new connection
|
||||
```
|
||||
|
||||
## Connection Strings
|
||||
|
||||
### PostgreSQL
|
||||
|
||||
```
|
||||
postgresql://user:password@localhost:5432/dbname
|
||||
postgres://user:password@localhost/dbname
|
||||
|
||||
# With SSL
|
||||
postgresql://user:password@host:5432/dbname?sslmode=require
|
||||
```
|
||||
|
||||
### SQLite
|
||||
|
||||
```
|
||||
sqlite:path/to/database.db
|
||||
sqlite:./local.db
|
||||
sqlite::memory:
|
||||
```
|
||||
|
||||
## DBUI Navigation
|
||||
|
||||
```
|
||||
<cr> Toggle node / Execute query
|
||||
o Open node
|
||||
R Refresh
|
||||
d Delete
|
||||
r Rename
|
||||
S Sort by
|
||||
H Toggle help
|
||||
q Close
|
||||
```
|
||||
|
||||
## Writing Queries
|
||||
|
||||
1. `<leader>Du` - Open DBUI
|
||||
2. Navigate to database
|
||||
3. Press `<cr>` on "New Query"
|
||||
4. Write SQL
|
||||
5. `<leader>Rs` or `<cr>` to execute
|
||||
|
||||
## Query Execution
|
||||
|
||||
In a `.sql` buffer connected to DBUI:
|
||||
|
||||
```
|
||||
<cr> Execute query under cursor
|
||||
<leader>Rs Execute query (visual: selected)
|
||||
```
|
||||
|
||||
## Table Helpers
|
||||
|
||||
Right-click or press `<cr>` on a table for quick actions:
|
||||
|
||||
### PostgreSQL
|
||||
- Count: `SELECT COUNT(*) FROM table`
|
||||
- First10: `SELECT * FROM table LIMIT 10`
|
||||
- Schema: `\d+ table`
|
||||
|
||||
### SQLite
|
||||
- Count: `SELECT COUNT(*) FROM table`
|
||||
- First10: `SELECT * FROM table LIMIT 10`
|
||||
- Schema: `.schema table`
|
||||
|
||||
## Buffer Management
|
||||
|
||||
```
|
||||
<leader>Df Find DB buffer
|
||||
<leader>Dr Rename DB buffer
|
||||
<leader>Dl Last query info
|
||||
```
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### Connect to Database
|
||||
|
||||
1. `<leader>Da` - Add connection
|
||||
2. Enter connection string
|
||||
3. Give it a name
|
||||
4. Connection saved for future sessions
|
||||
|
||||
### Explore Schema
|
||||
|
||||
1. `<leader>Du` - Open DBUI
|
||||
2. Expand database node
|
||||
3. Expand "Tables" node
|
||||
4. `<cr>` on table for helpers
|
||||
|
||||
### Run Ad-hoc Query
|
||||
|
||||
1. `<leader>Du` - Open DBUI
|
||||
2. Navigate to database
|
||||
3. New Query
|
||||
4. Write SQL:
|
||||
```sql
|
||||
SELECT * FROM users WHERE active = true LIMIT 10;
|
||||
```
|
||||
5. Execute with `<cr>`
|
||||
|
||||
### Export Results
|
||||
|
||||
After running query:
|
||||
1. Results appear in split
|
||||
2. `:w filename.csv` to save
|
||||
|
||||
## Tips
|
||||
|
||||
### Multiple Databases
|
||||
|
||||
- Add multiple connections
|
||||
- Switch by selecting in DBUI
|
||||
- Each buffer tracks its connection
|
||||
|
||||
### Environment Variables
|
||||
|
||||
Connection strings can use env vars:
|
||||
```
|
||||
postgresql://$DB_USER:$DB_PASS@$DB_HOST/$DB_NAME
|
||||
```
|
||||
|
||||
### SSH Tunnels
|
||||
|
||||
For remote databases, set up SSH tunnel first:
|
||||
```bash
|
||||
ssh -L 5432:localhost:5432 user@server
|
||||
```
|
||||
|
||||
Then connect to `localhost:5432`.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Connection Failed
|
||||
|
||||
- Check credentials
|
||||
- Verify database is running
|
||||
- Check firewall/network
|
||||
|
||||
### Missing Adapter
|
||||
|
||||
Run `:checkhealth` and verify:
|
||||
- `vim-dadbod` installed
|
||||
- Database client available (psql, sqlite3)
|
||||
|
||||
### Slow Queries
|
||||
|
||||
- Add `LIMIT` to large tables
|
||||
- Use `EXPLAIN` to analyze
|
||||
Reference in New Issue
Block a user