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,
How can I update or insert if I have an array of Content jsons into Lesson eg [{"type":"text",...},{"type":"video",...},...]?
Thanks!
5 Replies
You opted for speed from this bot. Let's conjure up an answer right now!
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:
2. To update an existing Lesson with new Content:
If you want to replace all existing Content for a Lesson, you can use the
deleteMany operation before creating new Content:
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#nesteRelation 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.
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.
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:
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 outHey š
You can try to do something like this:
and can then use this function for each content item, whether it has an ID or not:
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.