英文:
Drizzle ORM: Infer type of schema including the relations
问题
以下是翻译好的代码部分:
我正在开发一个使用 Drizzle 作为与 Postgres 数据库连接的 ORM 的 Express 应用程序。当我推断特定模式的类型时,只有声明的列会作为生成的类型的属性添加。是否可以包括已声明关系的类型?
这是上述情况的代码:
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>
我尝试通过将类型 RolePrivilege 的值手动更改为下面的代码来手动添加关系的类型,它可以工作,但我想知道是否有更直接和不那么繁琐的方法来做到这一点:
export type RolePrivilege = InferModel<typeof rolePrivileges> & {
role: Role
}
英文:
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
}
答案1
得分: 1
最近我也在研究同样的问题,根据我在Discord上看到的回答,我相当确定这是目前将它们合并的策略(也是我目前在我的项目中正在实施的策略)。
不过他们已经注意到这个问题了 - 这里是关于此功能的待办请求链接:
https://github.com/drizzle-team/drizzle-orm/issues/695
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论