Class: Volt::DataStore::MongoAdaptorServer
- Inherits:
-
BaseAdaptorServer
- Object
- BaseAdaptorServer
- Volt::DataStore::MongoAdaptorServer
- Defined in:
- app/mongo/lib/mongo_adaptor_server.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#mongo_db ⇒ Object
readonly
Returns the value of attribute mongo_db.
Instance Method Summary collapse
- #adapter_version ⇒ Object
-
#connected? ⇒ Boolean
check if the database can be connected to.
- #delete(collection, query) ⇒ Object
-
#drop_collection(collection) ⇒ Object
remove the collection entirely.
- #drop_database ⇒ Object
- #insert(collection, values) ⇒ Object
- #query(collection, query) ⇒ Object
- #update(collection, values) ⇒ Object
Instance Attribute Details
#db ⇒ Object (readonly)
Returns the value of attribute db.
19 20 21 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 19 def db @db end |
#mongo_db ⇒ Object (readonly)
Returns the value of attribute mongo_db.
19 20 21 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 19 def mongo_db @mongo_db end |
Instance Method Details
#adapter_version ⇒ Object
134 135 136 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 134 def adapter_version ::Mongo::VERSION end |
#connected? ⇒ Boolean
check if the database can be connected to.
23 24 25 26 27 28 29 30 31 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 23 def connected? begin db true rescue ::Mongo::Error => e false end end |
#delete(collection, query) ⇒ Object
117 118 119 120 121 122 123 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 117 def delete(collection, query) if query.key?('id') query['_id'] = query.delete('id') end db[collection].delete_one(query) end |
#drop_collection(collection) ⇒ Object
remove the collection entirely
126 127 128 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 126 def drop_collection(collection) db[collection].drop end |
#drop_database ⇒ Object
130 131 132 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 130 def drop_database db.database.drop end |
#insert(collection, values) ⇒ Object
47 48 49 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 47 def insert(collection, values) db[collection].insert_one(values) end |
#query(collection, query) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 73 def query(collection, query) if ENV['DB_LOG'] && collection.to_s != 'active_volt_instances' Volt.logger.info("Query: #{collection}: #{query.inspect}") end allowed_methods = %w(find skip limit sort) result = db[collection] query.each do |query_part| method_name, *args = query_part unless allowed_methods.include?(method_name.to_s) fail "`#{method_name}` is not part of a valid query" end args = args.map do |arg| if arg.is_a?(Hash) arg = arg.stringify_keys end arg end if method_name == 'find' && args.size > 0 qry = args[0] to_mongo_id!(qry) end result = result.send(method_name, *args) end if result.is_a?(::Mongo::Collection::View) result = result.to_a.map do |hash| # Return id instead of _id to_volt_id!(hash) # Volt expects symbol keys hash.symbolize_keys end#.tap {|v| puts "QUERY: " + v.inspect } end result end |
#update(collection, values) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 51 def update(collection, values) values = values.nested_stringify_keys to_mongo_id!(values) # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's begin db[collection].insert_one(values) rescue => error # Really mongo client? if error.[/^E11000/] && error.['$_id_'] # Update because the id already exists update_values = values.dup id = update_values.delete('_id') db[collection].update_one({ '_id' => id }, update_values) else return { error: error. } end end nil end |