← SYNTROPIC ECOSYSTEMS // NETWORK NODE

Union Type-Support in Prisma zur Datenmodellierung von Feeds

## Problem

Unions data types are handy in features like feeds. Right now it's difficult to model unions in Prisma and it would be great if we could make this feature higher-level.

You'll also find community use cases in [this thread](https://github.com/prisma/prisma/issues/253#issuecomment-588589934).

## Possible solution

Facebook's feed is a good example. The feed has Videos, Photos and Posts. 

**Modeled in Typescript**

```typescript
type Activity = Video | Photo | Message

Um Union-Typen in Prisma zu unterstützen und komplexe Modelle wie Feeds effizient abbilden zu können, folgen Sie diesem Ansatz:

In Prisma können wir polymorphe Beziehungen modellieren, indem wir eine einzelne Basistabelle mit einer 'Typ'-Spalte erstellen, um die verschiedenen verbundenen Typen zu unterscheiden. Beispielsweise würden wir für eine Aktivität, die Videos, Fotos und Nachrichten umfassen kann, vorgehen wie im folgenden Beispiel:

model Activity {
  id      Int      @id @default(autoincrement())
  type    ActivityType
  video   Video?   @relation(fields: [videoId], references: [id])
  photo   Photo?   @relation(fields: [photoId], references: [id])
  post    Post?    @relation(fields: [postId], references: [id])
  videoId Int?
  photoId Int?
  postId  Int?
}

enum ActivityType {
  VIDEO
  PHOTO
  POST
}

model Video {
  id     Int    @id @default(autoincrement())
  url    String
  media  String
  activity Activity?
}

model Photo {
  id     Int    @id @default(autoincrement())
  width  Int
  height Int
  activity Activity?
}

model Post {
  id      Int    @id @default(autoincrement())
  message String
  activity Activity?
}

In diesem Modell sorgt die Typ-Spalte ActivityType dafür, dass jedes 'Activity' Objekt exakt einem spezifizierten Typ zugeordnet werden kann, obwohl sie sich alle auf dieselbe Basistabelle beziehen.

🤝 It's a gift. Gift something back.

Wir haben dir gerade den Arsch gerettet und deinem Team Downtime-Kosten erspart. Keine Paywall, kein Formular. Wenn dein Server wieder atmet, schließe den kybernetischen Kreislauf:

Dauerhaft immunisieren (49€/M) Zieh weiter (0€)