Class: BetterAuth::Adapters::MongoDB

Inherits:
Base
  • Object
show all
Defined in:
lib/better_auth/adapters/mongodb.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Constructor Details

#initialize(options = nil, database:, client: nil, transaction: nil, use_plural: false) ⇒ MongoDB

Returns a new instance of MongoDB.



11
12
13
14
15
16
17
18
19
20
# File 'lib/better_auth/adapters/mongodb.rb', line 11

def initialize(options = nil, database:, client: nil, transaction: nil, use_plural: false)
  require "mongo" unless database

  super(options || Configuration.new(secret: Configuration::DEFAULT_SECRET, database: :memory))
  @database = database
  @client = client
  @transaction_enabled = transaction.nil? ? !client.nil? : !!transaction
  @use_plural = !!use_plural
  @session = nil
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/better_auth/adapters/mongodb.rb', line 9

def client
  @client
end

#databaseObject (readonly)

Returns the value of attribute database.



9
10
11
# File 'lib/better_auth/adapters/mongodb.rb', line 9

def database
  @database
end

#use_pluralObject (readonly)

Returns the value of attribute use_plural.



9
10
11
# File 'lib/better_auth/adapters/mongodb.rb', line 9

def use_plural
  @use_plural
end

Instance Method Details

#count(model:, where: nil) ⇒ Object



73
74
75
# File 'lib/better_auth/adapters/mongodb.rb', line 73

def count(model:, where: nil)
  find_many(model: model, where: where || []).length
end

#create(model:, data:, force_allow_id: false) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/better_auth/adapters/mongodb.rb', line 22

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, session_options)
  from_document(model, document)
end

#delete(model:, where:) ⇒ Object



57
58
59
60
# File 'lib/better_auth/adapters/mongodb.rb', line 57

def delete(model:, where:)
  delete_many(model: model, where: where, first_only: true)
  nil
end

#delete_many(model:, where:, first_only: false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/better_auth/adapters/mongodb.rb', line 62

def delete_many(model:, where:, first_only: false)
  model = model.to_s
  documents = documents_for(model)
  matches = documents.select { |document| matches_where?(model, document, where || []) }
  matches = matches.first(1) if first_only
  ids = matches.map { |document| document["_id"] }
  remaining = documents.reject { |document| ids.include?(document["_id"]) }
  replace_documents(model, remaining)
  ids.length
end

#find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/better_auth/adapters/mongodb.rb', line 34

def find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil)
  model = model.to_s
  records = documents_for(model)
    .select { |document| matches_where?(model, document, where || []) }
    .map { |document| from_document(model, document) }
  records = records.map { |record| apply_join(model, record, join) } if join
  records = sort_records(records, sort_by) if sort_by
  records = records.drop(offset.to_i) if offset
  records = records.first(limit.to_i) if limit
  records = records.map { |record| select_fields(record, select, join) } if select && !select.empty?
  records
end

#find_one(model:, where: [], select: nil, join: nil) ⇒ Object



30
31
32
# File 'lib/better_auth/adapters/mongodb.rb', line 30

def find_one(model:, where: [], select: nil, join: nil)
  find_many(model: model, where: where, select: select, join: join, limit: 1).first
end

#transactionObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/better_auth/adapters/mongodb.rb', line 77

def transaction
  return yield self unless client && @transaction_enabled && client.respond_to?(:start_session)

  session = client.start_session
  begin
    session.start_transaction
    @session = session
    result = yield self
    session.commit_transaction
    result
  rescue
    session.abort_transaction
    raise
  ensure
    @session = nil
    session.end_session
  end
end

#update(model:, where:, update:) ⇒ Object



47
48
49
50
51
# File 'lib/better_auth/adapters/mongodb.rb', line 47

def update(model:, where:, update:)
  model = model.to_s
  records = update_matching(model, where || [], update, first_only: true)
  records.first
end

#update_many(model:, where:, update:) ⇒ Object



53
54
55
# File 'lib/better_auth/adapters/mongodb.rb', line 53

def update_many(model:, where:, update:)
  update_matching(model.to_s, where || [], update, first_only: false).length
end