Drizzle ORM:推断包括关系的模式类型

huangapple go评论288阅读模式
英文:

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 &quot;drizzle-orm&quot;
import { integer, pgTable, primaryKey } from &quot;drizzle-orm/pg-core&quot;

import { privileges } from &quot;@config/db/schema/privilege&quot;
import { roles, type Role } from &quot;@config/db/schema/role&quot;

export const rolePrivileges = pgTable(&quot;role_privileges&quot;, {
   roleId: integer(&quot;role_id&quot;).notNull().references(() =&gt; roles.id, { onDelete: &quot;cascade&quot; }),
   privilege: privileges(&quot;privilege&quot;)
}, (rolePrivileges) =&gt; ({
   pk: primaryKey(rolePrivileges.roleId, rolePrivileges.privilege)
}))

export const rolePrivilegesRelations = relations(rolePrivileges, ({ one }) =&gt; ({
   role: one(roles, {
      fields: [rolePrivileges.roleId],
      references: [roles.id]
   })
}))

export type RolePrivilege = InferModel&lt;typeof rolePrivileges&gt;

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&lt;typeof rolePrivileges&gt; &amp; {
   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

huangapple
  • 本文由 发表于 2023年8月5日 15:23:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76840558.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定