$count #3119
Replies: 8 comments 26 replies
-
@AlexBlokh This doesn't appear to work with planetscale driver even though the types say they do. Getting |
Beta Was this translation helpful? Give feedback.
-
I love the addition. It's indeed a bit too verbose without this. @AlexBlokh have you considered adding this into the query builder API? |
Beta Was this translation helpful? Give feedback.
-
I'm getting a typescript type error.
|
Beta Was this translation helpful? Give feedback.
-
Will the support of this be added to a export let usersCountView = github.materializedView("users_count")
.as(qb =>
qb.$count().from(usersTable) // Property $count does not exist on type QueryBuilder
) |
Beta Was this translation helpful? Give feedback.
-
Will this ever get documented? There is a guide about counting but there you use |
Beta Was this translation helpful? Give feedback.
-
If the following works: const users = await db.query.users.findMany({
extras: {
postsCount: db.$count(posts, eq(posts.authorId, users.id)),
},
}); could this be added to the RQB v1
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
@AlexBlokh The examples you provided in this discussion, as well as in the docs here and here are wrong code and not working. Unfortunately the docs are not open source so I cant provide a PR, but I feel like none of Drizzle Team is looking into Github issues/PRs anyway? 1. With select import { users } from '.models';
const users = await db.select({
...users,
postsCount: db.$count(posts, eq(posts.authorId, users.id)),
}).from(users); -> TS2448: Block-scoped variable users used before its declaration. 2. With Query import { users } from '.models';
const users = await db.query.users.findMany({
extras: {
postsCount: db.$count(posts, eq(posts.authorId, users.id)),
},
}); -> TS2448: Block-scoped variable users used before its declaration. -> TS2741: Property fieldAlias is missing in type PgCountBuilder<PgSession<any, any, any>> but required in type Aliased |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In recent release we've introduced
$count
API and I wanted to shine some light on why we decided to implement one.We pursue SQL-likeness with Drizzle, yet sometimes pure SQL is just bad, this is how you'd do
count
with our query builder:not that bad you might say, yet
count
type would be unknown, since we didn't explicitly tell oursql
tag the type of a column, which would beThat still wouldn't work, because PostgreSQL will return count as
bigint
and MySQL will return count asdecimal
which will be returned asstring
, so if we want something robust - we should do:These all the above are reasons why we sometimes introduce utilities like
$count
, we will be prefixing util function like this one with$
to separate them from query builder APIswe've designed it to be used as a subquery too:
this way you can easily filter out only users with at least one post
and you can use it with our relational queries too:
Beta Was this translation helpful? Give feedback.
All reactions