4

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.

1 Answer 1

11

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[]
}

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for helping. I would like to add one tip for those stuck while defining their Prisma model. First, connect your database with Prisma. Then create dummy data in your database. Now call npx prisma db pull this will automatically define the model according to the data structure in the database. Doing the following steps will either give you your desired model or it will give some direction on what to search for in the documentation. This is how I got my answer. The above concept is Prisma Introspection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.