Skip to content

Commit 5a8e16c

Browse files
committed
select docs refactoring, added $count docs and query utils section
1 parent a4f7965 commit 5a8e16c

File tree

4 files changed

+243
-81
lines changed

4 files changed

+243
-81
lines changed

src/components/markdown/$count.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Section from '@components/markdown/Section.astro';
2+
3+
`db.$count()` is a utility wrapper of `count(*)`, it is a very flexible operator which can be used as is or as a subquery, more details in our [GitHub discussion](https://github.com/drizzle-team/drizzle-orm/discussions/3119).
4+
5+
<Section>
6+
```ts
7+
const count = await db.$count(users);
8+
// ^? number
9+
10+
const count = await db.$count(users, eq(users.name, "Dan")); // works with filters
11+
```
12+
```sql
13+
select count(*) from "users";
14+
select count(*) from "users" where "name" = 'Dan';
15+
```
16+
</Section>
17+
18+
It is exceptionally useful in [subqueries](/docs/select#select-from-subquery):
19+
20+
```ts
21+
const users = await db.select({
22+
...users,
23+
postsCount: db.$count(posts, eq(posts.authorId, users.id)),
24+
}).from(users);
25+
```
26+
27+
usage example with [relational queries](/docs/rqb)
28+
```ts
29+
const users = await db.query.users.findMany({
30+
extras: {
31+
postsCount: db.$count(posts, eq(posts.authorId, users.id)),
32+
},
33+
});
34+
```

src/content/documentation/docs/_meta.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
["update", "Update"],
6868
["delete", "Delete"],
6969
["operators", "Filters"],
70+
["query-utils", "Utils"],
7071
["joins", "Joins"],
7172
["sql", "Magic sql`` operator"],
7273
"Performance",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Tab from '@components/markdown/Tab.astro';
2+
import Tabs from '@components/markdown/Tabs.astro';
3+
import Callout from '@components/markdown/Callout.astro';
4+
import Section from '@components/markdown/Section.astro';
5+
import IsSupportedChipGroup from '@mdx/IsSupportedChipGroup.astro';
6+
import $count from '@mdx/$count.mdx';
7+
8+
# Drizzle query utils
9+
10+
### $count
11+
<$count/>
12+

0 commit comments

Comments
 (0)