P
Prisma•10mo ago
Radu

Update connected data

Hello Guys, I am new to prisma! So I have a Lesson Table which has many type of contents dynamically. The lesson has a array relation to Content table,
model Lesson {
id String @id @default(uuid())
title String?
time Int?

contents Content[] @relation("Content")
}

model Content {
id String @id @default(uuid())
content Json

lesson Lesson @relation("Content", fields: [LessonId], references: [id])
LessonId String
}
model Lesson {
id String @id @default(uuid())
title String?
time Int?

contents Content[] @relation("Content")
}

model Content {
id String @id @default(uuid())
content Json

lesson Lesson @relation("Content", fields: [LessonId], references: [id])
LessonId String
}
How can I update or insert if I have an array of Content jsons into Lesson eg [{"type":"text",...},{"type":"video",...},...]? Thanks!
5 Replies
Prisma AI Help
Prisma AI Help•10mo ago
You opted for speed from this bot. Let's conjure up an answer right now!
Prisma AI Help
Prisma AI Help•10mo ago
To update or insert an array of Content JSON objects into a Lesson, you can use Prisma's nested writes feature. Here's how you can do it: 1. To create a new Lesson with Content:
const result = await prisma.lesson.create({
data: {
title: "Your Lesson Title",
time: 60,
contents: {
create: [
{ content: { type: "text", ... } },
{ content: { type: "video", ... } },
// ... more content objects
]
}
},
include: {
contents: true
}
});
const result = await prisma.lesson.create({
data: {
title: "Your Lesson Title",
time: 60,
contents: {
create: [
{ content: { type: "text", ... } },
{ content: { type: "video", ... } },
// ... more content objects
]
}
},
include: {
contents: true
}
});
2. To update an existing Lesson with new Content:
const result = await prisma.lesson.update({
where: { id: "existing-lesson-id" },
data: {
contents: {
createMany: {
data: [
{ content: { type: "text", ... } },
{ content: { type: "video", ... } },
// ... more content objects
]
}
}
},
include: {
contents: true
}
});
const result = await prisma.lesson.update({
where: { id: "existing-lesson-id" },
data: {
contents: {
createMany: {
data: [
{ content: { type: "text", ... } },
{ content: { type: "video", ... } },
// ... more content objects
]
}
}
},
include: {
contents: true
}
});
If you want to replace all existing Content for a Lesson, you can use the deleteMany operation before creating new Content:
const result = await prisma.lesson.update({
where: { id: "existing-lesson-id" },
data: {
contents: {
deleteMany: {},
createMany: {
data: [
{ content: { type: "text", ... } },
{ content: { type: "video", ... } },
// ... more content objects
]
}
}
},
include: {
contents: true
}
});
const result = await prisma.lesson.update({
where: { id: "existing-lesson-id" },
data: {
contents: {
deleteMany: {},
createMany: {
data: [
{ content: { type: "text", ... } },
{ content: { type: "video", ... } },
// ... more content objects
]
}
}
},
include: {
contents: true
}
});
These operations allow you to create or update a Lesson with multiple Content records in a single transaction. The include option is used to return the created or updated Content along with the Lesson in the result. For more detailed information on nested writes and relation queries, you can refer to the Prisma documentation on Relation queries. [Prisma documentation on Nested writes](<https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#neste
Relation queries (Concepts) | Prisma Documentation
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Prisma AI Help
Prisma AI Help•10mo ago
d-writes>) provides more examples and explanations on how to work with related records. šŸ¤” Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Radu
RaduOP•10mo ago
So should I create two different options? One for when I have an ID and update and then another create to add the new content? Because at some point contents maybe be with id eg. [{"id":"id from prisma...","type":"text",...},{"type":"video",...},...] So I am doing like this:
if (content.id) {
await prisma.content.update({
where: {
id: content.id,
},
data: {
content: content.content,
},
});
} else {
await prisma.lesson.update({
where: {
id: lessonId,
},
data: {
contents: {
create: {
content: content.content,
},
},
},
include: {
contents: true,
},
});
}
if (content.id) {
await prisma.content.update({
where: {
id: content.id,
},
data: {
content: content.content,
},
});
} else {
await prisma.lesson.update({
where: {
id: lessonId,
},
data: {
contents: {
create: {
content: content.content,
},
},
},
include: {
contents: true,
},
});
}
For this example "content": { "id": "8970cc41-0779-430d-9fa5-d3f8665ffc3f", "content": { "type": "Text", "content": "A" } } or "content": { "content": { "type": "Text", "content": "B" } } If you have any idea to do it one function please let me know I guess its upsert or something but I cannot figure it out
Nurul
Nurul•10mo ago
Hey šŸ‘‹ You can try to do something like this:
const upsertContent = async (lessonId: string, content: any) => {
return await prisma.content.upsert({
where: {
id: content.id || 'new-id', // Use a placeholder ID if not provided
},
update: {
content: content.content,
},
create: {
content: content.content,
LessonId: lessonId,
},
});
};
const upsertContent = async (lessonId: string, content: any) => {
return await prisma.content.upsert({
where: {
id: content.id || 'new-id', // Use a placeholder ID if not provided
},
update: {
content: content.content,
},
create: {
content: content.content,
LessonId: lessonId,
},
});
};
and can then use this function for each content item, whether it has an ID or not:
const updateLessonContents = async (lessonId: string, contents: any[]) => {
const updatedContents = await Promise.all(
contents.map(content => upsertContent(lessonId, content))
);

return updatedContents;
};
const updateLessonContents = async (lessonId: string, contents: any[]) => {
const updatedContents = await Promise.all(
contents.map(content => upsertContent(lessonId, content))
);

return updatedContents;
};
Docs reference: https://www.prisma.io/docs/orm/prisma-client/queries/crud#update-or-create-records
CRUD (Reference) | Prisma Documentation
How to perform CRUD with Prisma Client.

Did you find this page helpful?