Pawan
Pawan

Reputation: 857

How to define models with nested objects in prisma?

I am trying to migrate from mongoose to Prisma. Here is the model I have defined in mongoose which contains nested objects.

const sourceSchema = new Schema(
    {
        data: {
            national: {
                oldState: {
                    type: Array
                },
                currentState: {
                    type: Array
                }
            },
            sports: {
                oldState: {
                    type: Array
                },
                currentState: {
                    type: Array
                }
            }

        }
        
    }
);

Please guide me on how can I write the model in Prisma for the mongoose schema with nested objects.

Upvotes: 4

Views: 7520

Answers (1)

Nurul Sundarani
Nurul Sundarani

Reputation: 7588

You are looking for composite types to store nested objects in Prisma.

Here's how you can define the models:

// 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 = "mongodb"
  url      = env("DATABASE_URL")
}

model Source {
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  national stateType
  sports   stateType
}

type stateType {
  oldState     oldState
  currentState currentState
}

type oldState {
  type String[]
}

type currentState {
  type String[]
}

Upvotes: 10

Related Questions