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

Add any? and none? as query methods #703

Merged
merged 2 commits into from
Jul 10, 2021

Conversation

matthewmcgarvey
Copy link
Member

Fixes #680

Provides two simple methods to check if there are any records in the database matching the specified conditions. Within avram's codebase, it simplifies the uniqueness checks.

Usage looks like:

UserQuery.any?
UserQuery.new.name("Sally").any?

Comment on lines +77 to +82
{% for crystal_db_alias in [:exec, :scalar, :query, :query_all, :query_one, :query_one?, :query_each] %}
# 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_, args: args)
db.{{ crystal_db_alias.id }}(query, *args_, **named_args, args: args)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we haven't used them before in the codebase, there was a bug here that meant we couldn't call query_all or query_one and the added query_one?. It expects an as named argument that passes in what it should be converted into

Copy link
Member

@jwoertink jwoertink left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! This is a pretty nice method. It seems like it would actually run faster than doing .first? checks. Out of curiosity, have you tried any benchmarks?
Kind of a bummer we will have to start disabling the ameba checks around this, but I get it.

This all looks good 👍

@matthewmcgarvey
Copy link
Member Author

Ran a benchmark comparing first? to any?

CleanShot 2021-07-10 at 15 29 34@2x

@matthewmcgarvey matthewmcgarvey merged commit ca62396 into luckyframework:master Jul 10, 2021
@matthewmcgarvey matthewmcgarvey deleted the any-or-none branch July 10, 2021 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add any? and none? query methods
2 participants