简体   繁体   English

Prisma 中的嵌套写入

[英]Nested writes in Prisma

My schema looks like this:我的架构如下所示:

model User {
  id             Int       @default(autoincrement()) @id
  createdAt      DateTime  @default(now())
  email          String    @unique
  role           String    @default("user")
  sessions       Session[]
  profile        Profile?
  goalBoard      GoalBoard[]
  team           Team[]
  ...
}

model GoalBoard {
  id          Int         @default(autoincrement()) @id
  active      Boolean    // Whether an active goalBoard
  owner       User        @relation(fields: [ownerId], references: [id])
  ownerId     Int
  createdAt   DateTime    @default(now())
  goal        Goal[]
  ...
}

model Goal {
  id               Int         @default(autoincrement()) @id
  status           String
  createdAt        DateTime    @default(now())
  owner            User        @relation(fields: [ownerId], references: [id])
  ownerId          Int
  goalBoard        GoalBoard   @relation(fields: [goalBoardId], references: [id])
  goalBoardId      Int
  content          String
  goalFrequency    GoalFrequency[]
  task             Task[]
}

model Task {
  id          Int         @default(autoincrement()) @id
  status      String     // incomplete, complete
  createdAt   DateTime    @default(now())
  content     String
  goal        Goal        @relation(fields: [goalId], references: [id])
  goalId      Int
}

I am writing a mutation function which takes an array of goal objects.我正在编写一个带有goal对象数组的变异函数。 These goal objects have nested array of task objects.这些goal对象具有嵌套的task对象数组。 It looks like this:它看起来像这样:

const goals = [
  {
    title: 'string',
    ...
    tasks: [
      {
        deadline: "2020/10/10",
        ...
      }
    ]
  },
  ...
]

How would I handle such structure using Prisma2?我将如何使用 Prisma2 处理这种结构? There are multiple writes & connectOrCreate logic required.需要多个写入和connectOrCreate逻辑。

Here is my failed attempt to write an insertion in the Db.这是我在 Db 中写入插入的失败尝试。 Testing with just one insertion & connection.只需一次插入和连接即可进行测试。

  const returnGoals = await db.goal.create({
    data: {
      content: "test goal",
      owner: {
        connect: {
          id: ctx.session!.userId,
        },
      },
      goalBoard: {
        create: { // warns that create is incorrectly used here
          active: true,
          owner: {
            connect: {
              id: ctx.session!.userId,
            },
          },
        },
      },
    },
  });

Your schema with this example did not have an issue to use this create您在此示例中的架构在使用此创建时没有问题

import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  prisma.$connect();
  const user = await prisma.user.create({
    data: {
      email: 'a@ahmedelywa.com',
    },
  });
  const result = await prisma.goal.create({
    data: {
      content: 'test content',
      status: 'any',
      owner: {
        connect: {
          id: user.id,
        },
      },
      goalBoard: {
        create: {
          active: true,
          owner: {
            connect: {
              id: user.id,
            },
          },
        },
      },
    },
    include: {
      owner: true,
      goalBoard: true,
    },
  });

  console.log(result);
}

main();

result结果

{
  id: 1,
  status: 'any',
  createdAt: 2020-10-04T10:53:40.956Z,
  ownerId: 1,
  goalBoardId: 1,
  content: 'test content',
  owner: {
    id: 1,
    createdAt: 2020-10-04T10:53:40.949Z,
    email: 'a@ahmedelywa.com',
    role: 'user'
  },
  goalBoard: {
    id: 1,
    active: true,
    ownerId: 1,
    createdAt: 2020-10-04T10:53:40.956Z
  }
}

Maybe your issue with the rest of your hide schema content也许你的其余隐藏模式内容有问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM