Skip to content

Commit

Permalink
chore: Model.db now returns raw Db connection, Model.query the query …
Browse files Browse the repository at this point in the history
…object for the table
  • Loading branch information
swiknaba committed May 19, 2024
1 parent 4c7d46f commit d5d195c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 38 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ Delete keeps the original object intact. Returns `true` if the record was delete
success = user.delete # => T::Boolean

# or delete by any query:
User.db.where('...').delete # => Integer, number of deleted records
User.query.where('...').delete # => Integer, number of deleted records
```

To build more complex queries, Sequel can be used directly:

```ruby
query = User.db.where({ name: 'John' })
query = User.query.where({ name: 'John' })
query = query.where('...') # "query" is a 'Sequel::Dataset' that you can chain as you like
query = query.limit(10)

Expand Down Expand Up @@ -217,7 +217,7 @@ module Airports
# FROM "airports"
# WHERE (("name" ILIKE 'xx%') OR ("id" ILIKE 'xx%'))
#
query = Airport.db.where(Sequel.ilike(:name, "#{search}%"))
query = Airport.query.where(Sequel.ilike(:name, "#{search}%"))
query = query.or(Sequel.ilike(:id, "#{search}%"))

Airport.resolve(query)
Expand Down
4 changes: 2 additions & 2 deletions lib/kirei/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ def class; super; end # rubocop:disable all
def update(hash)
hash[:updated_at] = Time.now.utc if respond_to?(:updated_at) && hash[:updated_at].nil?
self.class.wrap_jsonb_non_primivitives!(hash)
self.class.db.where({ id: id }).update(hash)
self.class.query.where({ id: id }).update(hash)
self.class.find_by({ id: id })
end

# Delete keeps the original object intact. Returns true if the record was deleted.
# Calling delete multiple times will return false after the first (successful) call.
sig { returns(T::Boolean) }
def delete
count = self.class.db.where({ id: id }).delete
count = self.class.query.where({ id: id }).delete
count == 1
end

Expand Down
9 changes: 6 additions & 3 deletions lib/kirei/model/base_class_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ def resolve_first(hash); end
sig { abstract.returns(T.untyped) }
def table_name; end

sig { abstract.returns(T.untyped) }
def query; end

sig { abstract.returns(T.untyped) }
def db; end

sig { abstract.returns(Integer) }
sig { abstract.returns(T.untyped) }
def human_id_length; end

sig { abstract.returns(String) }
sig { abstract.returns(T.untyped) }
def human_id_prefix; end

sig { abstract.returns(String) }
sig { abstract.returns(T.untyped) }
def generate_human_id; end
end
end
Expand Down
15 changes: 10 additions & 5 deletions lib/kirei/model/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def model_name
end

sig { override.returns(Sequel::Dataset) }
def query
db[table_name.to_sym]
end

sig { override.returns(Sequel::Database) }
def db
App.raw_db_connection[table_name.to_sym]
App.raw_db_connection
end

sig do
Expand All @@ -35,12 +40,12 @@ def db
).returns(T::Array[T.attached_class])
end
def where(hash)
resolve(db.where(hash))
resolve(query.where(hash))
end

sig { override.returns(T::Array[T.attached_class]) }
def all
resolve(db.all)
resolve(query.all)
end

# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
Expand All @@ -67,7 +72,7 @@ def create(hash)
all_attributes["updated_at"] = Time.now.utc
end

pkey = T.let(db.insert(all_attributes), String)
pkey = T.let(query.insert(all_attributes), String)

T.must(find_by({ id: pkey }))
end
Expand All @@ -92,7 +97,7 @@ def wrap_jsonb_non_primivitives!(attributes)
).returns(T.nilable(T.attached_class))
end
def find_by(hash)
resolve_first(db.where(hash))
resolve_first(query.where(hash))
end

# Extra or unknown properties present in the Hash do not raise exceptions at
Expand Down
2 changes: 1 addition & 1 deletion spec/test_app/app/services/airports/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.call(search)
# FROM "airports"
# WHERE (("name" ILIKE 'xx%') OR ("id" ILIKE 'xx%'))
#
query = Airport.db.where(Sequel.ilike(:name, "#{search}%"))
query = Airport.query.where(Sequel.ilike(:name, "#{search}%"))
query = query.or(Sequel.ilike(:id, "#{search}%"))

Kirei::Services::Result.new(result: Airport.resolve(query))
Expand Down
13 changes: 8 additions & 5 deletions spec/test_app/db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
# typed: strict
# frozen_string_literal: true

Airport.db.delete
Airport.query.delete

test = Airport.new(
id: Airport.generate_human_id,
name: "A test airport with a human ID",
)
test.save

muc = Airport.new(
id: "MUC",
name: "Munich Airport"
)
muc.save

ber = Airport.new(
id: "BER",
name: "Berlin Brandenburg Airport"
)
ber.save

sfo = Airport.new(
id: "SFO",
name: "San Francisco International Airport"
)
sfo.save

Airport.db.transaction do
test.save
muc.save
ber.save
sfo.save
end
48 changes: 29 additions & 19 deletions spec/test_app/sorbet/rbi/gems/kirei@0.3.0.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d5d195c

Please sign in to comment.