Class: Mongify::Database::NoSqlConnection
- Inherits:
-
BaseConnection
- Object
- BaseConnection
- Mongify::Database::NoSqlConnection
- Defined in:
- lib/mongify/database/no_sql_connection.rb
Overview
No Sql Connection configuration
Basic format should look something like this:
no_sql_connection {} do
adapter "mongodb"
host "localhost"
database "my_database"
end
Possible attributes:
adapter
host
database
username
password
port
Options:
:force => true # This will force a database drop before processing
You’re also able to set attributes via the options
Constant Summary collapse
- REQUIRED_FIELDS =
Required fields for a no sql connection
%w{host database}
Constants inherited from BaseConnection
BaseConnection::AVAILABLE_FIELDS, BaseConnection::STRING_FIELDS
Instance Method Summary collapse
-
#adapter(name = nil) ⇒ Object
Sets and/or returns a adapter It takes care of renaming adapter(‘mongo’) to ‘mongodb’.
-
#ask_to_drop_database ⇒ Object
Asks user permission to drop the database.
-
#connection ⇒ Object
(also: #client)
Returns a mongo client connection NOTE: If forced? is true, the first time a connection is made, it will ask to drop the database before continuing.
-
#connection_string ⇒ Object
Returns a connection string that can be used to build a Mongo Connection (Currently this isn’t used due to some issue early on in development).
-
#create_pre_mongified_id_index(collection_name) ⇒ Object
Creates a pre_mongified_id index to ensure speedy lookup for collections via the pre_mongified_id.
-
#db ⇒ Object
Returns the database from the connection.
-
#drop_mongified_index(collection_name) ⇒ Object
Removes pre_mongified_id from collection.
-
#find_one(collection_name, query) ⇒ Object
Finds one item from a collection with the given query.
-
#forced? ⇒ Boolean
Returns true if :force was set to true This will force a drop of the database upon connection.
-
#get_id_using_pre_mongified_id(collection_name, pre_mongified_id) ⇒ Object
Returns a row of a item from a given collection with a given pre_mongified_id.
-
#has_connection? ⇒ Boolean
Returns true or false depending if we have a connection to a mongo server.
-
#initialize(options = {}) ⇒ NoSqlConnection
constructor
A new instance of NoSqlConnection.
-
#insert_into(collection_name, row) ⇒ Object
Inserts into the collection a given row.
-
#remove_pre_mongified_ids(collection_name) ⇒ Object
Removes pre_mongified_id from all records in a given collection.
- #select_by_query(collection, query) ⇒ Object
-
#select_rows(collection) ⇒ Object
Returns a hash of all the rows from the database of a given collection.
-
#setup_connection_adapter ⇒ Object
Sets up a connection to the database using Mongo::Client (2.x driver).
-
#update(collection_name, id, attributes) ⇒ Object
Updates a collection item with a given ID with the given attributes.
-
#upsert(collection_name, row) ⇒ Object
Upserts into the collection a given row.
-
#valid? ⇒ Boolean
Returns true or false depending if the given attributes are present and valid to make up a connection to a mongo server.
Methods inherited from BaseConnection
#method_missing, #respond_to?, #to_hash
Constructor Details
#initialize(options = {}) ⇒ NoSqlConnection
Returns a new instance of NoSqlConnection.
32 33 34 35 36 |
# File 'lib/mongify/database/no_sql_connection.rb', line 32 def initialize(={}) super = adapter 'mongodb' if adapter.nil? || adapter.downcase == "mongo" end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Mongify::Database::BaseConnection
Instance Method Details
#adapter(name = nil) ⇒ Object
Sets and/or returns a adapter It takes care of renaming adapter(‘mongo’) to ‘mongodb’
40 41 42 |
# File 'lib/mongify/database/no_sql_connection.rb', line 40 def adapter(name=nil) super(name) end |
#ask_to_drop_database ⇒ Object
Asks user permission to drop the database
184 185 186 187 188 |
# File 'lib/mongify/database/no_sql_connection.rb', line 184 def ask_to_drop_database if UI.ask("Are you sure you want to drop #{database} database?") drop_database end end |
#connection ⇒ Object Also known as: client
Returns a mongo client connection NOTE: If forced? is true, the first time a connection is made, it will ask to drop the database before continuing
78 79 80 81 82 |
# File 'lib/mongify/database/no_sql_connection.rb', line 78 def connection return @connection if @connection @connection = setup_connection_adapter @connection end |
#connection_string ⇒ Object
Returns a connection string that can be used to build a Mongo Connection (Currently this isn’t used due to some issue early on in development)
46 47 48 |
# File 'lib/mongify/database/no_sql_connection.rb', line 46 def connection_string "#{@adapter}://#{@host}#{":#{@port}" if @port}" end |
#create_pre_mongified_id_index(collection_name) ⇒ Object
Creates a pre_mongified_id index to ensure speedy lookup for collections via the pre_mongified_id
178 179 180 |
# File 'lib/mongify/database/no_sql_connection.rb', line 178 def create_pre_mongified_id_index(collection_name) client[collection_name].indexes.create_one({ 'pre_mongified_id' => 1 }) end |
#db ⇒ Object
Returns the database from the connection
99 100 101 |
# File 'lib/mongify/database/no_sql_connection.rb', line 99 def db @db ||= client.database end |
#drop_mongified_index(collection_name) ⇒ Object
Removes pre_mongified_id from collection
169 170 171 172 173 174 |
# File 'lib/mongify/database/no_sql_connection.rb', line 169 def drop_mongified_index(collection_name) index_names = client[collection_name].indexes.collect { |idx| idx['name'] } if index_names.include?("pre_mongified_id_1") client[collection_name].indexes.drop_one('pre_mongified_id_1') end end |
#find_one(collection_name, query) ⇒ Object
Finds one item from a collection with the given query
149 150 151 |
# File 'lib/mongify/database/no_sql_connection.rb', line 149 def find_one(collection_name, query) client[collection_name].find(query).first end |
#forced? ⇒ Boolean
Returns true if :force was set to true This will force a drop of the database upon connection
58 59 60 |
# File 'lib/mongify/database/no_sql_connection.rb', line 58 def forced? !!['force'] end |
#get_id_using_pre_mongified_id(collection_name, pre_mongified_id) ⇒ Object
Returns a row of a item from a given collection with a given pre_mongified_id
154 155 156 157 |
# File 'lib/mongify/database/no_sql_connection.rb', line 154 def get_id_using_pre_mongified_id(collection_name, pre_mongified_id) doc = client[collection_name].find('pre_mongified_id' => pre_mongified_id).first doc ? doc['_id'] : nil end |
#has_connection? ⇒ Boolean
Returns true or false depending if we have a connection to a mongo server
88 89 90 91 92 93 94 95 96 |
# File 'lib/mongify/database/no_sql_connection.rb', line 88 def has_connection? # In mongo 2.x, we ping the server to check connectivity begin client.database.command(ping: 1) true rescue Mongo::Error => e false end end |
#insert_into(collection_name, row) ⇒ Object
Inserts into the collection a given row
113 114 115 |
# File 'lib/mongify/database/no_sql_connection.rb', line 113 def insert_into(collection_name, row) client[collection_name].insert_one(row) end |
#remove_pre_mongified_ids(collection_name) ⇒ Object
Removes pre_mongified_id from all records in a given collection
160 161 162 163 |
# File 'lib/mongify/database/no_sql_connection.rb', line 160 def remove_pre_mongified_ids(collection_name) drop_mongified_index(collection_name) client[collection_name].update_many({}, { '$unset' => { 'pre_mongified_id' => 1} }) end |
#select_by_query(collection, query) ⇒ Object
108 109 110 |
# File 'lib/mongify/database/no_sql_connection.rb', line 108 def select_by_query(collection, query) client[collection].find(query) end |
#select_rows(collection) ⇒ Object
Returns a hash of all the rows from the database of a given collection
104 105 106 |
# File 'lib/mongify/database/no_sql_connection.rb', line 104 def select_rows(collection) client[collection].find end |
#setup_connection_adapter ⇒ Object
Sets up a connection to the database using Mongo::Client (2.x driver)
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongify/database/no_sql_connection.rb', line 63 def setup_connection_adapter hosts = ["#{host}:#{port || 27017}"] = { database: database } if username && password [:user] = username [:password] = password end Mongo::Client.new(hosts, ) end |
#update(collection_name, id, attributes) ⇒ Object
Updates a collection item with a given ID with the given attributes
118 119 120 |
# File 'lib/mongify/database/no_sql_connection.rb', line 118 def update(collection_name, id, attributes) client[collection_name].replace_one({"_id" => id}, attributes) end |
#upsert(collection_name, row) ⇒ Object
Upserts into the collection a given row
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/mongify/database/no_sql_connection.rb', line 123 def upsert(collection_name, row) # We can't use the save method of the Mongo collection # The reason is that it detects duplicates using _id # but we should detect it using pre_mongified_id instead # because in the case of sync, same rows are identified by their original sql ids if row.has_key?(:pre_mongified_id) || row.has_key?('pre_mongified_id') id = row[:pre_mongified_id] || row['pre_mongified_id'] duplicate = find_one(collection_name, {"pre_mongified_id" => id}) if duplicate update(collection_name, duplicate[:_id] || duplicate["_id"], row) else insert_into(collection_name, row) end else # no pre_mongified_id, use replace_one with upsert option if row[:_id] || row["_id"] id = row[:_id] || row["_id"] client[collection_name].replace_one({"_id" => id}, row, upsert: true) else insert_into(collection_name, row) end end end |
#valid? ⇒ Boolean
Returns true or false depending if the given attributes are present and valid to make up a connection to a mongo server
52 53 54 |
# File 'lib/mongify/database/no_sql_connection.rb', line 52 def valid? super && @database.present? end |