Add the ability to map a query's result using the query builder #1637
L-Mario564
started this conversation in
Ideas
Replies: 1 comment 4 replies
-
This is already possible using the |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone. I'm here to propose a feature that could enhance the query builder without changing the core API.
What I propose is that the query builder should expose some way for the developer to transform the result of a query after it has already been mapped, meaning that the underlying SQL is not changed at all. I'll provide an example so you can get a better idea as to how this would work:
In the above example, we're transforming the query's result via the
$transform
method, which accepts a callback that, in this case, serves the purpose of returning only the first row. This doesn't change anything at the database level, as the callback function would be called after the query is executed and the result is mapped by the ORM.Currently, you'd have to do something like this to achieve the same thing:
The example is very simple, therefore, the difference is barely noticeable and you could definitely argue that the above is more readable than the first snippet, but what about more advanced examples? What if we wanted to apply multiple transformations? Abstracting the callback into reusable functions?
Here's a slightly more advanced example:
findFirst
andfindFirstOrThrow
are commonly requested features in the core API of Drizzle and this would be a nice way to incorporate those, with the added bonus of letting the user customize the behavior of said functions, and of course, you can do a lot more advanced things with this feature.Not the best writer here, so apologies if the wording is a little weird, but hope that the examples were enough to get my point across. Would like to know what the Drizzle community and team think about this enhancement. I can also to file a PR that implements this feature if this is something that the community wants.
Beta Was this translation helpful? Give feedback.
All reactions