I am working on an Express App which uses Drizzle as ORM connected to Postgres Database. When I Infered the type of a specific schema, only the declared columns are added as attributes of the generated type. Is it possible to include the type for the declared relations?
Here is the code for the scenario above:
import { relations, type InferModel } from "drizzle-orm"
import { integer, pgTable, primaryKey } from "drizzle-orm/pg-core"
import { privileges } from "@config/db/schema/privilege"
import { roles, type Role } from "@config/db/schema/role"
export const rolePrivileges = pgTable("role_privileges", {
roleId: integer("role_id").notNull().references(() => roles.id, { onDelete: "cascade" }),
privilege: privileges("privilege")
}, (rolePrivileges) => ({
pk: primaryKey(rolePrivileges.roleId, rolePrivileges.privilege)
}))
export const rolePrivilegesRelations = relations(rolePrivileges, ({ one }) => ({
role: one(roles, {
fields: [rolePrivileges.roleId],
references: [roles.id]
})
}))
export type RolePrivilege = InferModel<typeof rolePrivileges>
I tried to manually add the type for the relations by changing the value of type RolePrivilege to the code below and it worked, but I wanted to know if there is a more direct and less tedious way in doing so:
export type RolePrivilege = InferModel<typeof rolePrivileges> & {
role: Role
}
I was recently looking into the same thing and judging from the Discord answers I've seen, I'm pretty sure that's the current strategy to combine them (and what I am currently implementing in my project).
It's on their radar tho - here's a link to the backlog request for this feature: https://github.com/drizzle-team/drizzle-orm/issues/695