簡體   English   中英

我在 prisma 中使用 nest js 和 mongodb,我想添加三個集合,我該怎么做?

[英]i'm using nest js and mongodb with prisma and i want to add three collection in how can i do?

我想添加三個 model 的 prisma 就像。 模式.prisma

model women{
id  string @id @default(auto()) @map("_id") @db.ObjectId
womenname: string;
womengender: string
womendob: string
womenincome: string
}
model men{
id  string @id @default(auto()) @map("_id") @db.ObjectId
malename: string;
malegender: string
maledob: string
maleincome: string
}
model child{
id  string @id @default(auto()) @map("_id") @db.ObjectId
childname: string;
childgender: string
childdob: string
}

我如何將這三個 model 添加到一個 model 中。就像這樣。

model information{
male: //male model data,
women: // women model data,
child: //child model data
} 

Prisma 的文檔中已經對此進行了描述,您有兩種選擇

  • 使用復合類型,因此每個實體都有單獨的文檔,您可以通過父 model 訪問它們,例如 users.tasks
model User {
  id     String  @id @default(auto()) @map("_id") @db.ObjectId
  tasks  Task[]
}

model Task {
  id              String   @id @default(auto()) @map("_id") @db.ObjectId
  user            Product  @relation(fields: [userId], references: [id])
  userId          String   @db.ObjectId
}
  • 或者,如果您只想擁有接口(一種定義集合中的屬性可以具有的屬性的類型),您可以像下面這樣定義一個自定義類型,以用戶和任務的相同示例為例
model User {
  id   String @id @default(auto()) @map("_id") @db.ObjectId
  tasks Task[]
}

// This will not be a different collection but it will work as "rules" or schema for the attributes inside the user.tasks property

type Task {
  title       String
  description String
}

使用最后一個示例,您將擁有類似的東西


// users document
{
  "_id": "SOME RANDOM ID",
  "tasks": [
    {
      "title": "SOME TITLE",
      "description": "SOME DESCRIPTION"
    }

  ]
}

您也可以嘗試在此處更深入地查看文檔https://www.prisma.io/docs/concepts/components/prisma-client/composite-types

我希望它有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM