loan
loan

Reputation: 75

How to convert typescript interface into prisma model

i am trying Prisma with psql database but im not realy good in data modeling ^^

i am trying to convert this typescript interface (simplified) into a prisma model for a psql database

export enum StarterEnum {
  MANUAL,
  ELECTRIC,
  DUAL,
}

interface StarterEngine {
  name: StarterEnum;
  weight: number;
}

interface PowerEngine {
  hp: number;
  rpm: number;
}

export interface Engine {
  id: string;
  brand: string;
  model: string;
  power?: PowerEngine;
  starter?: StarterEngine[];
}

StarterEngine and PowerEngine are specific to only one engine a relational table is mandatory for that or there is a solution to keep all in one table ?

Here is my actual model (but with two table) and power_hp and power_rpm need to be linked (like mandatory if at least one is filled)

enum StarterEnum {
  MANUAL
  ELECTRIC
  DUAL
}

model Starter {
  id       String      @id @default(cuid())
  name     StarterEnum
  weight   Float
  Engine   Engine?     @relation(fields: [engineId], references: [id])
  engineId String?
}

model Engine {
  id        String    @id @default(cuid())
  brand     String
  model     String
  power_hp  Float
  power_rpm Float
  Starter   Starter[]
}

thanks everyone

Upvotes: 2

Views: 2231

Answers (1)

Victor Iris
Victor Iris

Reputation: 323

From your descriptions, it sounds like you need to split this into more than one table. Reasons:

  1. RPM and HP are mandatory if at least one is present. To avoid edge cases with validation on your API layer, make this a one-to-one optional relationship so that you can either store or skip them together directly on the Database.

  2. On the comments you mention that an Engine may have multiple starters. Then, starter must be a one-to-many relationship.

Note: Starter and Power do not require to have an independent ID. It would be simpler and cleaner to use the id of the engine they reference. In the case of power it can only be one Engine, so you can use the same id of the relation. In the case of starter, you have different types from an enum. And, assuming they cannot repeat you can use that as a composite key altogether with the engine id. Otherwise, add an independent id. Also, I recommend you to always set a default for the ids. It will save you time later on when creating your application.

Here is the schema:

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Engine {
  id       String          @id @default(cuid())
  brand    String
  model    String
  starters EngineStarter[]
  power    EnginePower?
}

model EngineStarter {
  engineId String
  engine   Engine      @relation(fields: [engineId], references: [id])
  type     StarterType
  weight   Float

  @@id([engineId, type])
}

model EnginePower {
  engineId String @id
  engine   Engine @relation(fields: [engineId], references: [id])
  hp       Float
  rpm      Float
}

enum StarterType {
  MANUAL
  ELECTRIC
  DUAL
}

Upvotes: 1

Related Questions