Class: BetterAuth::Hanami::SequelAdapter
- Inherits:
-
Adapters::Base
- Object
- Adapters::Base
- BetterAuth::Hanami::SequelAdapter
- Includes:
- Adapters::JoinSupport
- Defined in:
- lib/better_auth/hanami/sequel_adapter.rb
Constant Summary collapse
- WHERE_OPERATORS =
%w[eq ne gt gte lt lte in not_in contains starts_with ends_with].freeze
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
- .from_container(container, options, allow_memory_fallback: false) ⇒ Object
- .from_hanami(options, container: nil, allow_memory_fallback: false) ⇒ Object
- .memory_fallback(options, allow_memory_fallback: false) ⇒ Object
- .safe_fetch(container, key) ⇒ Object
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, connection:) ⇒ SequelAdapter
constructor
A new instance of SequelAdapter.
- #transaction ⇒ Object
- #update(model:, where:, update:) ⇒ Object
- #update_many(model:, where:, update:, returning: false) ⇒ Object
Constructor Details
#initialize(options, connection:) ⇒ SequelAdapter
Returns a new instance of SequelAdapter.
56 57 58 59 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 56 def initialize(, connection:) super() @connection = connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
15 16 17 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 15 def connection @connection end |
Class Method Details
.from_container(container, options, allow_memory_fallback: false) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 26 def self.from_container(container, , allow_memory_fallback: false) 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(, allow_memory_fallback: allow_memory_fallback) unless gateway connection = gateway.respond_to?(:connection) ? gateway.connection : gateway new(, connection: connection) end |
.from_hanami(options, container: nil, allow_memory_fallback: false) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 17 def self.from_hanami(, container: nil, allow_memory_fallback: false) if container.nil? && defined?(::Hanami) && ::Hanami.respond_to?(:app) container = ::Hanami.app end return memory_fallback(, allow_memory_fallback: allow_memory_fallback) unless container from_container(container, , allow_memory_fallback: allow_memory_fallback) end |
.memory_fallback(options, allow_memory_fallback: false) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 44 def self.memory_fallback(, allow_memory_fallback: false) if .respond_to?(:production?) && .production? && !allow_memory_fallback raise Error, "Hanami db.gateway is required in production. Set config.allow_memory_fallback = true to use volatile memory storage intentionally." end 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() end |
.safe_fetch(container, key) ⇒ Object
38 39 40 41 42 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 38 def self.safe_fetch(container, key) container[key] rescue KeyError nil end |
Instance Method Details
#count(model:, where: nil) ⇒ Object
124 125 126 127 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 124 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
61 62 63 64 65 66 67 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 61 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)) lookup = create_lookup(model, input) lookup ? find_one(model: model, where: [lookup]) : input end |
#delete(model:, where:) ⇒ Object
114 115 116 117 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 114 def delete(model:, where:) delete_many(model: model, where: where) nil end |
#delete_many(model:, where:) ⇒ Object
119 120 121 122 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 119 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 73 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 || []) join_config = normalized_join(model, join) requested_select = select ? Array(select).map { |field| storage_key(field) } : nil effective_select = select_fields_for_join(requested_select, join_config) dataset = apply_select(model, dataset, effective_select) if effective_select dataset = apply_order(model, dataset, sort_by) if sort_by dataset = dataset.limit(coerce_pagination(limit, "limit")) if limit dataset = dataset.offset(coerce_pagination(offset, "offset")) if offset records = dataset.all.map { |row| normalize_record(model, row) } records = attach_joins(model, records, join_config) trim_unrequested_select_fields(records, requested_select, join_config) if requested_select records end |
#find_one(model:, where: [], select: nil, join: nil) ⇒ Object
69 70 71 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 69 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
129 130 131 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 129 def transaction connection.transaction { yield self } end |
#update(model:, where:, update:) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 91 def update(model:, where:, update:) model = model.to_s existing = find_one(model: model, where: where) return nil unless existing update_many(model: model, where: where, update: update) lookup = record_lookup(model, existing) lookup ? find_one(model: model, where: [lookup]) : find_one(model: model, where: where) end |
#update_many(model:, where:, update:, returning: false) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 101 def update_many(model:, where:, update:, returning: false) model = model.to_s existing = returning ? find_many(model: model, where: where) : [] attributes = physical_attributes(model, transform_input(model, update, "update", true)) apply_where(model, table_dataset(model), where || []).update(attributes) return unless returning existing.map do |record| lookup = record_lookup(model, record) lookup ? find_one(model: model, where: [lookup]) : record end end |