如何在 prisma 中定义具有嵌套对象的模型?

Paw*_*wan 3 prisma

我正在尝试从 mongoose 迁移到 Prisma。这是我在猫鼬中定义的模型,其中包含嵌套对象。

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

        }
        
    }
);

Run Code Online (Sandbox Code Playgroud)

请指导我如何在 Prisma 中为带有嵌套对象的猫鼬模式编写模型。

Nur*_*ani 5

您正在寻找复合类型来在 Prisma 中存储嵌套对象。

以下是定义模型的方法:

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

Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的帮助。我想为那些在定义 Prisma 模型时遇到困难的人添加一个提示。首先,将您的数据库与 Prisma 连接。然后在数据库中创建虚拟数据。现在调用“npx prisma db pull”,这将根据数据库中的数据结构自动定义模型。执行以下步骤将为您提供所需的模型,或者为在文档中搜索内容提供一些指导。这就是我得到答案的方式。上面的概念是``Prisma Introspection`` (3认同)