Class: BetterAuth::Hanami::SequelAdapter
- Inherits:
-
Adapters::Base
- Object
- Adapters::Base
- BetterAuth::Hanami::SequelAdapter
- Defined in:
- lib/better_auth/hanami/sequel_adapter.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
- .from_container(container, options) ⇒ Object
- .from_hanami(options, container: nil) ⇒ 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.
39 40 41 42 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 39 def initialize(, connection:) super() @connection = connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
10 11 12 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 10 def connection @connection end |
Class Method Details
.from_container(container, options) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 21 def self.from_container(container, ) 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 BetterAuth::Adapters::Memory.new() unless gateway connection = gateway.respond_to?(:connection) ? gateway.connection : gateway new(, connection: connection) end |
.from_hanami(options, container: nil) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 12 def self.from_hanami(, container: nil) if container.nil? && defined?(::Hanami) && ::Hanami.respond_to?(:app) container = ::Hanami.app end return BetterAuth::Adapters::Memory.new() unless container from_container(container, ) end |
.safe_fetch(container, key) ⇒ Object
33 34 35 36 37 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 33 def self.safe_fetch(container, key) container[key] rescue KeyError nil end |
Instance Method Details
#count(model:, where: nil) ⇒ Object
97 98 99 100 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 97 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
44 45 46 47 48 49 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 44 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
87 88 89 90 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 87 def delete(model:, where:) delete_many(model: model, where: where) nil end |
#delete_many(model:, where:) ⇒ Object
92 93 94 95 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 92 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
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 55 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
51 52 53 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 51 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
102 103 104 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 102 def transaction connection.transaction { yield self } end |
#update(model:, where:, update:) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 68 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/better_auth/hanami/sequel_adapter.rb', line 77 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 |