Class: BetterAuth::Hanami::SequelAdapter

Inherits:
Adapters::Base
  • Object
show all
Includes:
Adapters::JoinSupport
Defined in:
lib/better_auth/hanami/sequel_adapter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, connection:) ⇒ SequelAdapter

Returns a new instance of SequelAdapter.



50
51
52
53
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 50

def initialize(options, connection:)
  super(options)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



13
14
15
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 13

def connection
  @connection
end

Class Method Details

.from_container(container, options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 24

def self.from_container(container, options)
  gateway = if container.respond_to?(:key?) && container.key?("db.gateway")
    container["db.gateway"]
  elsif container.respond_to?(:[]) && safe_fetch(container, "db.gateway")
    container["db.gateway"]
  end
  return memory_fallback(options) unless gateway

  connection = gateway.respond_to?(:connection) ? gateway.connection : gateway
  new(options, connection: connection)
end

.from_hanami(options, container: nil) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 15

def self.from_hanami(options, container: nil)
  if container.nil? && defined?(::Hanami) && ::Hanami.respond_to?(:app)
    container = ::Hanami.app
  end
  return memory_fallback(options) unless container

  from_container(container, options)
end

.memory_fallback(options) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 42

def self.memory_fallback(options)
  Kernel.warn(
    "[better_auth-hanami] SequelAdapter: using BetterAuth::Adapters::Memory " \
    "(no Hanami container or db.gateway). Persisted auth data will not survive process restart."
  )
  BetterAuth::Adapters::Memory.new(options)
end

.safe_fetch(container, key) ⇒ Object



36
37
38
39
40
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 36

def self.safe_fetch(container, key)
  container[key]
rescue KeyError
  nil
end

Instance Method Details

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



108
109
110
111
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 108

def count(model:, where: nil)
  model = model.to_s
  apply_where(model, table_dataset(model), where || []).count
end

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



55
56
57
58
59
60
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 55

def create(model:, data:, force_allow_id: false)
  model = model.to_s
  input = transform_input(model, data, "create", force_allow_id)
  table_dataset(model).insert(physical_attributes(model, input))
  find_one(model: model, where: [{field: "id", value: input.fetch("id")}])
end

#delete(model:, where:) ⇒ Object



98
99
100
101
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 98

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

#delete_many(model:, where:) ⇒ Object



103
104
105
106
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 103

def delete_many(model:, where:)
  model = model.to_s
  apply_where(model, table_dataset(model), where || []).delete
end

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



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 66

def find_many(model:, where: [], sort_by: nil, limit: nil, offset: nil, select: nil, join: nil)
  model = model.to_s
  dataset = table_dataset(model)
  dataset = apply_where(model, dataset, where || [])
  dataset = apply_select(model, dataset, select) if select
  dataset = apply_order(model, dataset, sort_by) if sort_by
  dataset = dataset.limit(Integer(limit)) if limit
  dataset = dataset.offset(Integer(offset)) if offset

  records = dataset.all.map { |row| normalize_record(model, row) }
  attach_joins(model, records, join)
end

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



62
63
64
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 62

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

#transactionObject



113
114
115
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 113

def transaction
  connection.transaction { yield self }
end

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



79
80
81
82
83
84
85
86
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 79

def update(model:, where:, update:)
  model = model.to_s
  existing = find_one(model: model, where: where, select: ["id"])
  return nil unless existing

  update_many(model: model, where: where, update: update)
  find_one(model: model, where: [{field: "id", value: existing.fetch("id")}])
end

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



88
89
90
91
92
93
94
95
96
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 88

def update_many(model:, where:, update:, returning: false)
  model = model.to_s
  existing = returning ? find_many(model: model, where: where, select: ["id"]) : []
  attributes = physical_attributes(model, transform_input(model, update, "update", true))
  apply_where(model, table_dataset(model), where || []).update(attributes)
  return unless returning

  existing.map { |record| find_one(model: model, where: [{field: "id", value: record.fetch("id")}]) }
end