Skip to content

Commit

Permalink
bug mongo and sql maps
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Orfo committed Oct 30, 2024
1 parent e0886e6 commit 4402d72
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
12 changes: 1 addition & 11 deletions ftplugin/javascript.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@ local db = setup.db

if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine == "mongo" then
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] then
logger:info(string.format("Database set to [%s]", connection.name))
vim.api.nvim_set_keymap('v', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
else
pcall(vim.keymap.del, 'v', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.close)
end
else
logger:info("No database configured.")
Expand Down
12 changes: 1 addition & 11 deletions ftplugin/sql.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@ local db = setup.db

if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and connection.engine ~= "mongo" then
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] then
logger:info(string.format("Database set to [%s]", connection.name))
vim.api.nvim_set_keymap('v', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', setup.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
else
pcall(vim.keymap.del, 'v', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.execute)
pcall(vim.keymap.del, 'n', setup.commands.close)
end
else
logger:info("No database configured.")
Expand Down
37 changes: 37 additions & 0 deletions lua/dbeer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ function M.setup(opts)
end

util.logger:debug("Configuration: " .. vim.inspect(M.SETTINGS))

local function map_or_unmap(connection, is_mongo)
if connection.name and connection.dbname and connection.engine and require 'dbeer.engines'.db[connection.engine] and is_mongo then
vim.api.nvim_set_keymap('v', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.execute, '<cmd>lua require("dbeer.core").run()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('n', M.SETTINGS.commands.close, '<cmd>lua require("dbeer.core").close()<CR>',
{ noremap = true, silent = true })
else
pcall(vim.keymap.del, 'v', M.SETTINGS.commands.execute)
pcall(vim.keymap.del, 'n', M.SETTINGS.commands.execute)
pcall(vim.keymap.del, 'n', M.SETTINGS.commands.close)
end
end

vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.js",
callback = function()
local db = M.SETTINGS.db
if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
map_or_unmap(connection, connection.engine == "mongo")
end
end,
})

vim.api.nvim_create_autocmd("BufEnter", {
pattern = "*.sql",
callback = function()
local db = M.SETTINGS.db
if db.connections then
local connection = db.connections[require 'dbeer'.default_db]
map_or_unmap(connection, connection.engine ~= "mongo")
end
end,
})
end

return M

0 comments on commit 4402d72

Please sign in to comment.