Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add database exploring for mongodb #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions autoload/db_ui/schemas.vim
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ let s:bigquery = {
\ 'requires_stdin': v:true,
\ }

let s:mongodb_dbs_query = '"\n" + db.adminCommand("listDatabases").databases.map(x => x.name).join("\n")'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to consider the case where the database name is defined in the url, for example mongodb://localhost:27017/my_test_db.
In that situation, we should just show table names.
We already do similar thing for mysql/mariadb here. We can convert that to a hash map and consider mongodb and mongodb+srv as part of that.

I added this myself to see how it looks, and noticed that table names in drawer got db. prepended, like db.users. We can probably solve that in the schemes_tables_query somehow.

let s:mongodb_tables_query = 'db.adminCommand("listDatabases").databases.flatMap(d => {use(d.name); return db.getCollectionNames().map(c => [d.name, c].join(","))}).join("\n")'
let s:mongodb = {
\ 'callable': 'filter',
\ 'args': ['--quiet'],
\ 'schemes_query': s:mongodb_dbs_query,
\ 'schemes_tables_query': s:mongodb_tables_query,
\ 'parse_results': {results, min_len -> s:results_parser(results[1:-2], '\,', min_len)},
\ 'requires_stdin': v:true,
\ }

let s:schemas = {
\ 'postgres': s:postgresql,
Expand All @@ -217,6 +227,8 @@ let s:schemas = {
\ 'mariadb': s:mysql,
\ 'oracle': s:oracle,
\ 'bigquery': s:bigquery,
\ 'mongodb': s:mongodb,
\ 'mongodb+srv': s:mongodb,
\ }

if !exists('g:db_adapter_postgres')
Expand Down
12 changes: 11 additions & 1 deletion autoload/db_ui/table_helpers.vim
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ let s:sqlserver = {
\ 'Describe': 'exec sp_help ''{schema}.{table}''',
\ }

let s:mongodb = {
\ 'List': 'use("{schema}"); db["{table}"].find()',
\ 'FindOne': 'use("{schema}"); db["{table}"].findOne()',
\ 'FindOneByObjectId': 'use("{schema}"); db["{table}"].findOne({_id: ObjectId("") })',
\ 'FindOneByStringId': 'use("{schema}"); db["{table}"].findOne({_id: "" })',
\ }

let s:helpers = {
\ 'bigquery': s:bigquery,
\ 'postgresql': s:postgres,
Expand All @@ -190,7 +197,7 @@ let s:helpers = {
\ 'oracle': s:oracle,
\ 'sqlite': s:sqlite,
\ 'sqlserver': s:sqlserver,
\ 'mongodb': { 'List': '{table}.find()'},
\ 'mongodb': s:mongodb,
\ }

let s:all = {}
Expand All @@ -201,12 +208,15 @@ endfor

let s:all.postgres = s:all.postgresql
let s:all.sqlite3 = s:all.sqlite
let s:all['mongodb+srv'] = s:all.mongodb

let s:scheme_map = {
\ 'postgres': 'postgresql',
\ 'postgresql': 'postgres',
\ 'sqlite3': 'sqlite',
\ 'sqlite': 'sqlite3',
\ 'mongodb+srv': 'mongodb',
\ 'mongodb': 'mongodb+srv',
\ }

function! db_ui#table_helpers#get(scheme) abort
Expand Down
Loading