From f58df0a4e6b4671511d123a51c79f56e651fe0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wieche=C4=87?= Date: Fri, 21 Feb 2014 08:33:17 +0100 Subject: [PATCH] adding additional menu for MongoDB enabling accessing Mongos on different hosts than local --- menu/databases.menu | 1 + menu/mongo2/default.conf | 2 + menu/mongo2/mongo2_index.rb | 119 ++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 menu/mongo2/default.conf create mode 100644 menu/mongo2/mongo2_index.rb diff --git a/menu/databases.menu b/menu/databases.menu index a459fa40..81a5d8bd 100644 --- a/menu/databases.menu +++ b/menu/databases.menu @@ -1,6 +1,7 @@ << mysql/ << memcache/ << mongodb/ +<< mongo2 << couch/ << riak/ << local storage/ diff --git a/menu/mongo2/default.conf b/menu/mongo2/default.conf new file mode 100644 index 00000000..48c296d4 --- /dev/null +++ b/menu/mongo2/default.conf @@ -0,0 +1,2 @@ +> put here the hostname of your MongoDB server +mongoserver.domain diff --git a/menu/mongo2/mongo2_index.rb b/menu/mongo2/mongo2_index.rb new file mode 100644 index 00000000..8c0bac3d --- /dev/null +++ b/menu/mongo2/mongo2_index.rb @@ -0,0 +1,119 @@ +module Xiki + class Mongo2 + MENU = %` + - .collections/ + - docs/ + > Create + @foo.save({_id:"a", txt:"b"}) + + > Show + @foo.find() + @foo.find({_id:"a"}) + + > Update + @foo.update({_id:"a"}, {txt:"bbb"}) + + > Delete + @foo.remove({_id:"a"}) + @conf/ + ` + + def self.collections db=nil, collection=nil, doc=nil + conf = yield[:conf] + + # cut off all non-host stuff from the configuration + dbhost = conf.sub(/\A> .+\n/, '').gsub(/\n/, "") + + collection.sub!(/\/$/, '') if collection + + # /, so list databases + + if ! db + json = self.run('printjson(db._adminCommand("listDatabases"))', dbhost) + o = JSON[json] + return o["databases"].map{|d| "#{d['name']}/"} + end + + # /db/, so list collections + if ! collection + json = self.run('printjson(db.getCollectionNames())', dbhost, db) + o = JSON[json] + return o.map{|d| "#{d}/"} + end + + # /db/, so list records + if ! doc + txt = self.run("db.#{collection}.find()", dbhost, db) + txt = %Q`> No documents yet. Create one?\n{ "_id":"id", "name":"Steve", "description":"whatever"}` if txt == "" + return txt.gsub(/^/, '| ') + end + + # /db/doc, so save + + doc.sub!(/^\| /, '') + command = "db.#{collection}.save(#{doc})" + + txt = self.run(command, dbhost, db) # .gsub(/^/, '| ') + + "@flash/- saved!" + + end + + def self.run command, dbhost, db=nil + + command << ".forEach(printjson)" if command =~ /.find\(/ + cmd = "mongo #{dbhost} --eval '#{command}'" + +# raise cmd + if db == nil + txt = `mongo #{dbhost} --eval '#{command}'` + else + txt = `mongo #{dbhost}/#{db} --eval '#{command}'` + end + + if txt =~ /couldn't connect to server/ + raise "> Couldn't connect - maybe it's not configured? + @mongo2/@conf/ + ".unindent + end + + txt.sub /(.+\n){2}/, '' # Delete first 2 lines + end + + def self.init + + # - Is this being called anywhere? + # - Maybe have item in menu that saves it to startup + # - maybe just shows file syntax for adding to a file! + + Launcher.add(/^db\./) do |l| # General db... lines + l.strip! + txt = self.run l + Tree.under "#{txt.strip}\n", :escape=>'| ', :no_slash=>1 + end + + Launcher.add(/^(\w+)\.(save|update)\(/) do |l| # Shortcut for foo.save() + l.strip! + l = "db.#{l}" + txt = self.run l + Tree.under "done#{txt.strip}\n", :escape=>'| ', :no_slash=>1 + end + + Launcher.add(/^(\w+)\.find\(/) do |l| # Shortcut for foo.find() + l.strip! + l = "db.#{l}" + txt = self.run l + Tree.under "#{txt.strip}\n", :escape=>'| ', :no_slash=>1 + end + + Launcher.add(/^(\w+)\.remove\(/) do |l| # Shortcut for foo.remove() + l.strip! + l = "db.#{l}" + txt = self.run l + Tree.under "#{txt.strip}\n", :escape=>'| ', :no_slash=>1 + end + + end + end + Mongo2.init +end