Naming the return type #10
Replies: 6 comments 5 replies
-
|
For now I'm not sure how to implement this nicely or if this could be a nice addition to Squirrel, there's some things to address. For example, what if someone gives the same name to two returned values? This opens to a couple of questions:
For now I'll keep this on hold and see what the main pain points are using the library and if this could actually be a nice addition to address those, thank you so much for the suggestion! |
Beta Was this translation helpful? Give feedback.
-
|
Yeah I have no obvious solution in mind either. But option two actually sounds appealing (from a user perspective at least) -- it would allow me to have the same exact type returned from different queries which would in turn allow me to use that one type for different things (e.g. json encoders, other business rules and so on). For example now I have to create two JSON encoders for the exact same thing which is pretty annoying: -- get_job_listings.sql
select id, title, description from job_listing
-- get_job_listing_by_id.sql
select id, title, description from job_listing where id = $1fn encode_job_listing(job_listing: GetJobListingsRow) {
json.object([
#("description", json.nullable(job_listing.description, of: json.string)),
#("id", json.int(job_listing.id)),
#("title", json.string(job_listing.title)),
])
}
fn encode_job_listing_by_id(job_listing: GetJobListingByIdRow) {
json.object([
#("description", json.nullable(job_listing.description, of: json.string)),
#("id", json.int(job_listing.id)),
#("title", json.string(job_listing.title)),
])
}The most common use case would probably be where you get a list of rows and then get the same row but by id like above, but I'm certain there are other scenarios as well. I think the ability to control both the name of the type and whether it's shared between queries would be a powerful feature and allow me to generate useful types from squirrel that can be passed around in my app instead of squirrel being a 1:1 with the db/query and then require additional mapping to other custom types. |
Beta Was this translation helpful? Give feedback.
-
|
Maybe there could be |
Beta Was this translation helpful? Give feedback.
-
|
Here's more feedback from Mark! |
Beta Was this translation helpful? Give feedback.
-
|
IMO the behavior should try to match as closely as possible the behaviors you expect in actual gleam code. That means treating the directory of sql files as 1:1 with gleam modules. So if you have But it'd refer to the different sql files instead |
Beta Was this translation helpful? Give feedback.
-
|
The returning annotation seems a good idea. In addition squirrel could detect and report identical return types that are not annotated, and ask to do so (but still process the files); in this way the user is suggested and guided into choosing whether to use different types or not. There would be no need to offer an option to remove the warnings, as this should be done by annotations. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Just like you can name the generated function with the
-- name:comment it would be nice if you could name the return type as well as this type tends to get spread out in the code base as you pass it around in functions.So instead of:
generating:
GetJobListingsRowyou could provide a name:
that would generate:
JobListingBeta Was this translation helpful? Give feedback.
All reactions