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

adding additional menu for MongoDB enabling accessing Mongos on differen... #67

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
1 change: 1 addition & 0 deletions menu/databases.menu
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<< mysql/
<< memcache/
<< mongodb/
<< mongo2
<< couch/
<< riak/
<< local storage/
2 changes: 2 additions & 0 deletions menu/mongo2/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> put here the hostname of your MongoDB server
mongoserver.domain
119 changes: 119 additions & 0 deletions menu/mongo2/mongo2_index.rb
Original file line number Diff line number Diff line change
@@ -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