Advanced aggregations in db.select()
#3984
AlexBlokh
started this conversation in
Show and tell
Replies: 1 comment
-
This is awesome |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
As a subsequent improvement to subqueries in
db.select()
, we want to further improve our API with introduction of advanced SQL aggregation patterns.First of all we want to enable developers with support of
json_agg
,row_to_json
andcoalesce
functions, which are powering our relational queries under the hoodwe use
lateral joins
in dialect that support them for performance reasons, in others above queries with sub-queries inselect
would be mostly identicalTo enable this API we would need to introduce subqueries support in
db.select()
and enable support fordb.select(aggregationOperator).from(table)
apart fromRecord<string, SQL | Table | Column ... >
We've spent quite some time internally to design the response type of
db.select(jsonAgg())
ordb.select(count())
or other top level aggregation functions, since drizzle is meant to follow SQL-like convention for SQL query build both in statement building and response type.Though we need to take DX in consideration too, that's why we do have
db.$count
operator explicitly distinguished with$
to solve DX problem ofWe took in consideration mental model of developers querying database via database browsers.
When they do:
they expect to see tabular data with column names and rows, as opposed to:
is when they care only about a single number.
Taking all that in count we decided to let users pass top level
db.select(count()).from(users)
which will translate directly toselect count(*) from users
sql, while result value and result type we flatten:this API will unlock further convenient aggregations like
and most importantly will let you build your own advanced relational queries with ease:
we've already played around with
coalesce
typing, which will trim| null
while preserving type of an entity if both are arraysBeta Was this translation helpful? Give feedback.
All reactions