A Neovim plugin that provides some utilities to enhance vim-dadbod by managing database connections from environment variables or the macOS Keychain.
- Flexible Connection Management: Load database connection URLs from environment variables or securely from the macOS Keychain.
- Smart Connection Switching: Quickly execute queries against your favorite databases with a command that offers smart completion, prioritizing your most used and recently used connections.
- Seamless
vim-dadbodIntegration: Works on top of the powerfulvim-dadbodplugin.
- tpope/vim-dadbod
- macOS (for Keychain support)
You can install this plugin using your favorite plugin manager.
return {
'tpope/vim-dadbod',
{
'radyz/vim-dadbod-wrapper',
opts = {
env_prefix = 'DADBOD_'
}
}
}The plugin can load connections from environment variables that follow this format:
<PREFIX><CONNECTION_NAME>=<CONNECTION_URL>
export DADBOD_MY_POSTGRES="postgresql://user:pass@host:port/dbname"
export DADBOD_ANOTHER_DB="mysql://user:pass@host:port/dbname"When Neovim starts, vim-dadbod-wrapper will automatically load these and make my_postgres and another_db available for the :DBExec command provided that the env_prefix option is supplied.
For more secure storage, you can store your connection strings in the macOS Keychain. The plugin expects a "generic password" item where the "password" field contains connection details in a CSV format.
Each line in the secret should be in the format: connection_name,connection_url.
Example:
-
Open Keychain Access app.
-
Create a new "generic password" item (click the
+button). -
Set "Keychain Item Name" to a label you'll remember, e.g.,
neovim-dadbod. -
The "Account Name" can be the same or something descriptive.
-
In the "Password" field, enter your connection strings, one per line:
my_keychain_db,postgresql://user:pass@host:port/dbname another_keychain_db,mysql://user:pass@host:port/dbname -
Save the item.
Now you can load these connections into Neovim using the :DBLoadSecret command.
Integration with telescope can be set with require("telescope").load_extension("dadbod_wrapper") somewhere in your config.
Example key bindings with lazy.vim:
return {
'tpope/vim-dadbod',
{
'radyz/vim-dadbod-wrapper',
opts = {
env_prefix = 'DADBOD_'
},
keys = {
{
"<localleader>r",
function()
require("telescope").extensions.dadbod_wrapper.connections(
require("telescope.themes").get_dropdown({
initial_mode = "normal",
})
)
end,
ft = { "sql" },
mode = { "n", "v" },
},
},
}
}When using Treesitter, any query modifying database state will be prompted for confirmation before execution.
This command loads database connections from a specified macOS Keychain item.
:DBLoadSecret <keychain_label><keychain_label>: The "Keychain Item Name" you used when creating the generic password item.
Example:
If you named your keychain item neovim-dadbod, you would run:
:DBLoadSecret neovim-dadbodThis will load my_keychain_db and another_keychain_db and make them available for use.
This command executes a SQL query using a named connection. The query can be the entire buffer content or a visual selection.
:DBExec <connection_name><connection_name>: The name of the connection to use (e.g.,my_postgres,my_keychain_db).
The command provides smart completion for the connection name. Press <Tab> to see a list of available connections, sorted by your most frequently and recently used ones.
Example:
-
Open a buffer and write a SQL query:
SELECT * FROM users LIMIT 10;
-
Execute the query against your
my_postgresdatabase::DBExec my_postgres
-
To execute only a part of the buffer, visually select the query and run the command:
:'<,'>DBExec my_postgres
vim-dadbod will open a new split with the query results.
MIT