From af3f0603aa505ed346578bc398add90318096485 Mon Sep 17 00:00:00 2001 From: Jens Claes <896093+entropitor@users.noreply.github.com> Date: Wed, 7 Aug 2024 10:29:03 +0200 Subject: [PATCH] feat: add database exploring for mongodb --- autoload/db_ui/schemas.vim | 12 ++++++++++++ autoload/db_ui/table_helpers.vim | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/autoload/db_ui/schemas.vim b/autoload/db_ui/schemas.vim index 81732d8..b1a7c15 100644 --- a/autoload/db_ui/schemas.vim +++ b/autoload/db_ui/schemas.vim @@ -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")' +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, @@ -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') diff --git a/autoload/db_ui/table_helpers.vim b/autoload/db_ui/table_helpers.vim index 14c3c18..65e3f90 100644 --- a/autoload/db_ui/table_helpers.vim +++ b/autoload/db_ui/table_helpers.vim @@ -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, @@ -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 = {} @@ -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