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,
    });
Was this page helpful?