Nested writes in Prisma

14.4k views Asked by At

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. These goal objects have nested array of task objects. It looks like this:

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

How would I handle such structure using Prisma2? There are multiple writes & connectOrCreate logic required.

Here is my failed attempt to write an insertion in the 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,
            },
          },
        },
      },
    },
  });
1

There are 1 answers

0
Ahmed Elywa On

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: '[email protected]',
    },
  });
  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: '[email protected]',
    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