30 lines
711 B
Ruby
30 lines
711 B
Ruby
|
# frozen_string_literal: true
|
||
|
|
||
|
require 'mongo'
|
||
|
require_relative 'dbmanager_module'
|
||
|
|
||
|
module MongoDBManager
|
||
|
include DatabaseManager
|
||
|
|
||
|
def initialize_db(username = nil, password = nil)
|
||
|
credentials = username && password ? { user: username, password: password } : {}
|
||
|
@client = Mongo::Client.new(['127.0.0.1:27017'], { database: 'my_db' }.merge(credentials))
|
||
|
end
|
||
|
|
||
|
def insert(table, data)
|
||
|
@client[table].insert_one(data)
|
||
|
end
|
||
|
|
||
|
def query(table, condition)
|
||
|
@client[table].find(condition).to_a
|
||
|
end
|
||
|
|
||
|
def update(table, query, update)
|
||
|
@client[table].find_one_and_update(query, { '$set' => update })
|
||
|
end
|
||
|
|
||
|
def delete(table, query)
|
||
|
@client[table].find_one_and_delete(query)
|
||
|
end
|
||
|
end
|