Describing my database schema:
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 // Indicates whether a goalBoard is active
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 developing a mutation function that accepts an array of goal
objects. Each goal
object contains a nested array of task
objects structured like this:
const goals = [
{
title: 'string',
...
tasks: [
{
deadline: "2020/10/10",
...
}
]
},
...
]
How can I manage this data structure using Prisma2? Multiple writes and connectOrCreate
logic will be needed.
Below is my unsuccessful attempt at inserting data into the database. I am testing with just one insertion and connection.
const returnGoals = await db.goal.create({
data: {
content: "test goal",
owner: {
connect: {
id: ctx.session!.userId,
},
},
goalBoard: {
create: { // throws a warning about incorrect usage of 'create' here
active: true,
owner: {
connect: {
id: ctx.session!.userId,
},
},
},
},
},
});