Prisma schema - Build up a custom object from a relation

345 views Asked by At

Can I build a custom object using a relation, and the properties that it references?

For example, if I have two models like this, where User is populated during authentication:

model User {
  id            String    @id @default(cuid()) @map("_id")
  name          String?
  email         String?   @unique
}

model Post {
  id          String   @id @default(auto()) @map("_id") @db.ObjectId
  title       String
  description String
  authorId    String
  authorEmail String
  authorName  String
  user        User     @relation(fields: [authorId, authorEmail, authorName], references: [id, email, name])
}

This will take the user details and push them, individually, into authorId, authorName and authorEmail. What I would like is some way of loading them into an embedded document called author, like so:

model Post {
  id          String   @id @default(auto()) @map("_id") @db.ObjectId
  title       String
  description String
  author      Author
  user        User     @relation(fields: [author], references: [id, email, name])
}

type Author {
  id    String
  name  String
  email String
}

Is this at all possible? I feel like I've tried everything.

0

There are 0 answers