module Moongoon::Database
Overview
Used to connect to a MongoDB database instance.
Defined in:
Instance Method Summary
-
#after_connect(&block : Proc(Nil))
Pass a block that will get executed after the database has been successfully connected and after the scripts are run.
-
#after_connect_before_scripts(&block : Proc(Nil))
Pass a block that will get executed after the database has been successfully connected but before the scripts are run.
-
#before_connect(&block : Proc(Nil))
Pass a block that will get executed before the server tries to connect to the database.
-
#connect(database_url : String = "mongodb://localhost:27017", database_name : String = "database", *, options : Mongo::Options? = nil, max_retries = nil, reconnection_delay = 5.seconds)
Connects to MongoDB.
-
#connection_with_lock(lock_name : String, *, delay = 0.5.seconds, abort_if_locked = false, &block : Proc(Mongo::Client, Mongo::Database, Nil))
Acquires a database lock and yields the client and database objects.
Instance Method Detail
Pass a block that will get executed after the database has been successfully connected and after the scripts are run.
Moongoon::Database.after_connect {
# ... #
}
Pass a block that will get executed after the database has been successfully connected but before the scripts are run.
Moongoon::Database.after_connect_before_scripts {
# ... #
}
Pass a block that will get executed before the server tries to connect to the database.
Moongoon::Database.before_connect {
puts "Before connecting…"
}
Connects to MongoDB.
Use an instance of Mongo::Options
as the options argument to customize the Mongo::Client
instance.
Will retry up to max_retries times to connect to the database. If max_retries is nil, will retry infinitely.
Will sleep for reconnection_delay between attempts.
# Arguments are all optional, their default values are the ones defined below:
Moongoon.connect("mongodb://localhost:27017", "database", options = nil, max_retries: nil, reconnection_delay: 5.seconds)
Acquires a database lock and yields the client and database objects.
Will acquire a lock named lock_name, polling the DB every delay to check the lock status. If abort_if_locked is true the block will not be executed and this method will return if the lock is acquired already.
# If another connection uses the "query" lock, it will wait
# until this block has completed before perfoming its own work.
Moongoon.connection_with_lock "query" { |client, db|
collection = db["some_collection"]
data = collection.find query
pp data
}