Drizzle 쿼리 μœ ν‹Έλ¦¬ν‹°

$count

db.$count()λŠ” count(*)의 μœ ν‹Έλ¦¬ν‹° 래퍼둜, κ·ΈλŒ€λ‘œ μ‚¬μš©ν•˜κ±°λ‚˜ μ„œλΈŒμΏΌλ¦¬λ‘œ μ‚¬μš©ν•  수 μžˆλŠ” 맀우 μœ μ—°ν•œ μ—°μ‚°μžμž…λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ GitHub 토둠을 μ°Έμ‘°ν•˜μ„Έμš”.

const count = await db.$count(users);
//    ^? number

const count = await db.$count(users, eq(users.name, "Dan")); // 필터와 ν•¨κ»˜ μž‘λ™
select count(*) from "users";
select count(*) from "users" where "name" = 'Dan';

μ„œλΈŒμΏΌλ¦¬μ—μ„œ 특히 μœ μš©ν•©λ‹ˆλ‹€:

const users = await db.select({
  ...users,
  postsCount: db.$count(posts, eq(posts.authorId, users.id)),
}).from(users);

κ΄€κ³„ν˜• 쿼리와 ν•¨κ»˜ μ‚¬μš©ν•˜λŠ” 예제

const users = await db.query.users.findMany({
  extras: {
    postsCount: db.$count(posts, eq(posts.authorId, users.id)),
  },
});