Count in relation selct

hello how to correctly do count from other table with condition in relation select. Attribute translationLanguageCount
import { db } from "@/db/db";
import { operator } from "@/models/operator";
import { sql } from "drizzle-orm";
import { SQL } from "drizzle-orm/sql/sql";

export const visibleColumns = {
id: true,
name: true,
surname: true,
email: true,
languageId: true,
};

export const list = (where?: SQL) =>
db.query.operator.findMany({
columns: visibleColumns,
extras: {
fullName: sql`CONCAT
(
${operator.name},
' ',
${operator.surname}
)`.as("fullname"),
},
with: {
language: {
columns: {
name: true,
},
},
operatorsLanguages: {
extras: {
translationLanguageCount:
sql`SELECT COUNT(*) FROM operatorsLanguages WHERE operatorId = ${operator.id}`.as(
"translationLanguagesCount"
),
},
},
},
where,
});
import { db } from "@/db/db";
import { operator } from "@/models/operator";
import { sql } from "drizzle-orm";
import { SQL } from "drizzle-orm/sql/sql";

export const visibleColumns = {
id: true,
name: true,
surname: true,
email: true,
languageId: true,
};

export const list = (where?: SQL) =>
db.query.operator.findMany({
columns: visibleColumns,
extras: {
fullName: sql`CONCAT
(
${operator.name},
' ',
${operator.surname}
)`.as("fullname"),
},
with: {
language: {
columns: {
name: true,
},
},
operatorsLanguages: {
extras: {
translationLanguageCount:
sql`SELECT COUNT(*) FROM operatorsLanguages WHERE operatorId = ${operator.id}`.as(
"translationLanguagesCount"
),
},
},
},
where,
});
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server