Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up crystal-db proxy methods with correct usage #801

Open
jwoertink opened this issue Jan 25, 2022 · 1 comment
Open

Clean up crystal-db proxy methods with correct usage #801

jwoertink opened this issue Jan 25, 2022 · 1 comment
Labels
clarify api Rename/remove/add something to make the API easier to understand

Comments

@jwoertink
Copy link
Member

# Methods without a block
{% for crystal_db_alias in [:exec, :scalar, :query, :query_all, :query_one, :query_one?] %}
# Same as crystal-db's `DB::QueryMethods#{{ crystal_db_alias.id }}` but with instrumentation
def {{ crystal_db_alias.id }}(query, *args_, args : Array? = nil, queryable : String? = nil, **named_args)
publish_query_event(query, args_, args, queryable) do
run do |db|
db.{{ crystal_db_alias.id }}(query, *args_, **named_args, args: args)
end
end
end
# Same as crystal-db's `DB::QueryMethods#{{ crystal_db_alias.id }}` but with instrumentation
def self.{{ crystal_db_alias.id }}(query, *args_, args : Array? = nil, queryable : String? = nil, **named_args)
new.{{ crystal_db_alias.id }}(query, *args_, **named_args, args: args, queryable: queryable)
end
{% end %}
# Methods with a block
{% for crystal_db_alias in [:query, :query_all, :query_one, :query_one?, :query_each] %}
# Same as crystal-db's `DB::QueryMethods#{{ crystal_db_alias }}` but with instrumentation
def {{ crystal_db_alias.id }}(query, *args_, args : Array? = nil, queryable : String? = nil, **named_args)
publish_query_event(query, args_, args, queryable) do
run do |db|
db.{{ crystal_db_alias.id }}(query, *args_, args: args) do |*yield_args|
yield *yield_args
end
end
end
end
# Same as crystal-db's `DB::QueryMethods#{{ crystal_db_alias }}` but with instrumentation
def self.{{ crystal_db_alias.id }}(query, *args_, args : Array? = nil, queryable : String? = nil, **named_args)
new.{{ crystal_db_alias.id }}(query, *args_, args: args, queryable: queryable) do |*yield_args|
yield *yield_args
end
end
{% end %}

Using these low-level crystal-db methods give a false sense of unified code in how they work. For example, it looks like you can do this, but you can't...

# assume this returns a single SpecialModel
AppDatabase.query(sql, args: [1], as: SpecialModel)

# assume this returns an Array(SpecialModel)
AppDatabase.query_all(sql, args: [1], as: SpecialModel)

The issue here is that query actually returns a ResultSet which could either be a single record or an array of records... There's no as: named arg for query.

Ref: will/crystal-pg#202

The reason we map these though is so we can properly log them through Avram (and Dexter). I'm sure there's other wrong uses in here, but they should all be cleaned up.

@jwoertink jwoertink added the clarify api Rename/remove/add something to make the API easier to understand label Jan 25, 2022
@matthewmcgarvey
Copy link
Member

Important thing to point out is that this code also allows us to maintain database connections and transactions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clarify api Rename/remove/add something to make the API easier to understand
Projects
None yet
Development

No branches or pull requests

2 participants