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.
20 21 22 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 20 def db @db end |
#mongo_db ⇒ Object (readonly)
Returns the value of attribute mongo_db.
20 21 22 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 20 def mongo_db @mongo_db end |
Instance Method Details
#adapter_version ⇒ Object
151 152 153 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 151 def adapter_version ::Mongo::VERSION end |
#connected? ⇒ Boolean
check if the database can be connected to.
24 25 26 27 28 29 30 31 32 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 24 def connected? begin db true rescue ::Mongo::Error => e false end end |
#delete(collection, query) ⇒ Object
134 135 136 137 138 139 140 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 134 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
143 144 145 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 143 def drop_collection(collection) db[collection].drop end |
#drop_database ⇒ Object
147 148 149 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 147 def drop_database db.database.drop end |
#insert(collection, values) ⇒ Object
48 49 50 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 48 def insert(collection, values) db[collection].insert_one(values) end |
#query(collection, query) ⇒ Object
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 82 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 values = Volt::DataTransformer.transform(result) do |value| if defined?(VoltTime) && value.is_a?(Time) value = VoltTime.from_time(value) else value end end values end |
#update(collection, values) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/mongo/lib/mongo_adaptor_server.rb', line 52 def update(collection, values) values = values.nested_stringify_keys values = Volt::DataTransformer.transform(values) do |value| if defined?(VoltTime) && value.is_a?(VoltTime) value.to_time else value end end 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? msg = error. if (msg[/^E11000/] || msg[/^insertDocument :: caused by :: 11000 E11000/]) && msg['$_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 |