Class: BetterAuth::Adapters::MongoDB
- Inherits:
-
Base
- Object
- Base
- BetterAuth::Adapters::MongoDB
- Defined in:
- lib/better_auth/mongo_adapter.rb
Defined Under Namespace
Classes: MongoAdapterError
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#use_plural ⇒ Object
readonly
Returns the value of attribute use_plural.
Instance Method Summary collapse
- #count(model:, where: nil) ⇒ Object
- #create(model:, data:, force_allow_id: false) ⇒ Object
- #delete(model:, where:) ⇒ Object
- #delete_many(model:, where:) ⇒ Object
- #find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil) ⇒ Object
- #find_one(model:, where: [], select: nil, join: nil) ⇒ Object
-
#initialize(options = nil, database:, client: nil, transaction: nil, use_plural: false, session: nil) ⇒ MongoDB
constructor
A new instance of MongoDB.
- #transaction ⇒ Object
- #update(model:, where:, update:) ⇒ Object
- #update_many(model:, where:, update:) ⇒ Object
Constructor Details
#initialize(options = nil, database:, client: nil, transaction: nil, use_plural: false, session: nil) ⇒ MongoDB
Returns a new instance of MongoDB.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/better_auth/mongo_adapter.rb', line 22 def initialize( = nil, database:, client: nil, transaction: nil, use_plural: false, session: nil) require "mongo" unless database super( || Configuration.new(secret: Configuration::DEFAULT_SECRET, database: :memory)) @database = database @client = client @transaction_enabled = transaction.nil? ? !client.nil? : !!transaction @use_plural = !!use_plural @session = session end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
20 21 22 |
# File 'lib/better_auth/mongo_adapter.rb', line 20 def client @client end |
#database ⇒ Object (readonly)
Returns the value of attribute database.
20 21 22 |
# File 'lib/better_auth/mongo_adapter.rb', line 20 def database @database end |
#use_plural ⇒ Object (readonly)
Returns the value of attribute use_plural.
20 21 22 |
# File 'lib/better_auth/mongo_adapter.rb', line 20 def use_plural @use_plural end |
Instance Method Details
#count(model:, where: nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/better_auth/mongo_adapter.rb', line 97 def count(model:, where: nil) pipeline = [ {"$match" => mongo_filter(model.to_s, where || [])}, {"$count" => "total"} ] row = collection_for(model.to_s).aggregate(pipeline, ).to_a.first return 0 unless row (row["total"] || row[:total] || 0).to_i end |
#create(model:, data:, force_allow_id: false) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/better_auth/mongo_adapter.rb', line 33 def create(model:, data:, force_allow_id: false) model = model.to_s record = transform_input(model, data, "create", force_allow_id) document = to_document(model, record) collection_for(model).insert_one(document, ) from_document(model, document) end |
#delete(model:, where:) ⇒ Object
87 88 89 90 |
# File 'lib/better_auth/mongo_adapter.rb', line 87 def delete(model:, where:) collection_for(model.to_s).delete_one(mongo_filter(model.to_s, where || []), ) nil end |
#delete_many(model:, where:) ⇒ Object
92 93 94 95 |
# File 'lib/better_auth/mongo_adapter.rb', line 92 def delete_many(model:, where:) result = collection_for(model.to_s).delete_many(mongo_filter(model.to_s, where || []), ) result.respond_to?(:deleted_count) ? result.deleted_count : result.to_i end |
#find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/better_auth/mongo_adapter.rb', line 45 def find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil) model = model.to_s pipeline = [{"$match" => mongo_filter(model, where || [])}] pipeline.concat(join_stages(model, join)) if join pipeline << {"$project" => projection_for(model, select, join)} if select && !select.empty? pipeline << {"$sort" => {sort_field(model, sort_by) => sort_direction(sort_by)}} if sort_by pipeline << {"$skip" => offset.to_i} if offset pipeline << {"$limit" => limit.to_i} if limit collection_for(model) .aggregate(pipeline, ) .to_a .map { |document| from_document(model, stringify_document(document), join: join) } end |
#find_one(model:, where: [], select: nil, join: nil) ⇒ Object
41 42 43 |
# File 'lib/better_auth/mongo_adapter.rb', line 41 def find_one(model:, where: [], select: nil, join: nil) find_many(model: model, where: where, select: select, join: join, limit: 1).first end |
#transaction ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/better_auth/mongo_adapter.rb', line 108 def transaction return yield self unless client && @transaction_enabled && client.respond_to?(:start_session) session = client.start_session begin session.start_transaction adapter = self.class.new(, database: database, client: client, transaction: @transaction_enabled, use_plural: use_plural, session: session) result = yield adapter session.commit_transaction result rescue session.abort_transaction raise ensure session.end_session end end |
#update(model:, where:, update:) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/better_auth/mongo_adapter.rb', line 60 def update(model:, where:, update:) model = model.to_s data = transform_input(model, update, "update", true) document = to_document(model, data) document.delete("_id") result = collection_for(model).find_one_and_update( mongo_filter(model, where || []), {"$set" => document}, .merge(return_document: :after) ) result = unwrap_update_result(result) result ? from_document(model, stringify_document(result)) : nil end |
#update_many(model:, where:, update:) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/better_auth/mongo_adapter.rb', line 74 def update_many(model:, where:, update:) model = model.to_s data = transform_input(model, update, "update", true) document = to_document(model, data) document.delete("_id") result = collection_for(model).update_many( mongo_filter(model, where || []), {"$set" => document}, ) result.respond_to?(:modified_count) ? result.modified_count : result.to_i end |