Drizzle TeamDT
Drizzle Team11mo ago
GT

Filter Empty Relations out

I got both tables as defined in my schema file:
export const employeesTable = table(
    'employees', {
    id: int().autoincrement().primaryKey(),
    fullName: varchar("full_name", { length: 255 }).notNull(),
    cellphone: varchar("cellphone", { length: 15 }).notNull(),
    cpf: varchar( { length: 14 }).notNull().unique(),
});

export const availabilityTable = table(
    'employees_availability', {
        id: int().autoincrement().primaryKey(),
        employeeId: int().notNull().references((): AnyMySqlColumn => employeesTable.id),
        day: int().notNull(),
        startTime: time().notNull(),
        endTime: time().notNull(),
    }
);

...

How could I query all Employees that has availabilities?

An simple query as this one:
database.query.employeesTable.findMany({
  with: { availabilities: true}
});

Would also return empty availabilities, filtering out the result after the query will result in inconsistencies in the result size. How could I resolve that?
Was this page helpful?