I want to ask about best practice how you guys do when saving and updating data to database using array of object.
For example i have this AoJ:
[
{
package_id: 1,
posts: [
{id: 1, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "Post 1", price: 3000}
{id: 2, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "Post 2", price: 3000}
{id: 3, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "post 3", price: 3000}
{id: 4, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "post 4", price: 3000}
]
}
];
For every array I will create a new row on database with new ID.
Here is what I want
How to update the data if i want to remove id 3? What is the best practice to detect that some record should be removed from package?
For now my solution is:
[
{
package_id: 1,
posts: [
{id: 1, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "Post 1", price: 3000, remove: false}
{id: 2, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "Post 2", price: 3000, remove: false}
{id: 3, post_id: "1d1479c6-c114-46d5-becd-e4715c14e57d", name: "post 3", price: 3000, remove: true}
{id: 4, post_id: "84f37e37-d050-4efd-bd08-811ab893959f", name: "post 4", price: 3000, remove: false}
]
}
];
So on updating package_id 1 posts when it see a remove: true
it will remove it.
What do you guys think? Any suggestion to make it more simple?
NB: I can't use delete everything and post new one method. because every package_id posts has relationship to other table