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

Provide get_optional #4

Closed
heroin-moose opened this issue Jan 17, 2022 · 10 comments
Closed

Provide get_optional #4

heroin-moose opened this issue Jan 17, 2022 · 10 comments

Comments

@heroin-moose
Copy link

Since there is no easy way to check why sqlx query failed it would be nice to have something similar to https://docs.rs/sqlx/0.5.10/sqlx/trait.Executor.html#tymethod.fetch_optional

@kurtbuilds
Copy link
Owner

kurtbuilds commented Jan 17, 2022

This is straight-forward - I can add this later today.

@kurtbuilds
Copy link
Owner

I looked into this. What struct are you wanting fetch_optional on? I checked, and SelectQueryBuilder.fetch_optional does already exist.

I (believe I) just fixed the docs issue, though it seems like it takes time for changes to propagate from crate to docs.rs. Once it does though, hopefully the API will be more discoverable.

@heroin-moose
Copy link
Author

Do I understand correctly that SelectQueryBuilder is e.g. select()?

Then

Example::select().filter("name = ?").bind(&name).fetch_optional(pool).await?

vs

Example::get_optional(&name, pool).await?

IMHO the second one is far easier to grasp.

@kurtbuilds
Copy link
Owner

Yeah, that first query is how to do it with the current API.

To confirm, the current get_one API takes an pkey field, which is typically id, not name. Are you asking for a get_optional on the pkey field?

It could make sense to add this API. I'm confused how common this use case is though. What's the scenario where you want to get an id, but you're not sure if it exists yet? (How would you even have the ID value?)

@heroin-moose
Copy link
Author

To confirm, the current get_one API takes an pkey field, which is typically id, not name. Are you asking for a get_optional on the pkey field?

Yep.

It could make sense to add this API. I'm confused how common this use case is though. What's the scenario where you want to get an id, but you're not sure if it exists yet? (How would you even have the ID value?)

Sometimes ID is not an ID but a slug or a name.

@kurtbuilds
Copy link
Owner

Why would the pkey field be the slug or name? That seems quite brittle, e.g. if you need to change an article's title.

@heroin-moose
Copy link
Author

heroin-moose commented Jan 18, 2022

However if you have a list of servers that must have unique names this simplifies the API.

@kurtbuilds
Copy link
Owner

You're presenting multiple examples, which makes me think you're talking about hypothetical requirements, rather than trying to solve a problem you're actively encountering as you build a service. We strive to build the API based on real-world usage. Unless you're in that situation, I'm going to push off further discussion on an API like you're suggesting here.

@heroin-moose
Copy link
Author

heroin-moose commented Jan 18, 2022

Nah, I'm asking because that's what I'm missing writing a server. It's not a web service and one of the main route to use it is via CLI. So basically when user types foobarctl do this <name> it's easier to ask the API by name instead of by id because CLI tool won't cache them.

@heroin-moose
Copy link
Author

However, you may be right that there are better ways to do what I want. I'm going to close it.

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

No branches or pull requests

2 participants