Create-if-not-exists multiple explicit many-to-many relations

(note: I'm new to using Prisma and unfamiliar with the syntax) I have Playlist and Video models, with an explicit many-to-many relationship (to hold extra data). Say I have an array of Video primary keys, how do I add these videos to a playlist, if they aren't already in it? My closest working example is this. To my understanding, upsert is actually an "create-if-not-exists, otherwise update" operation, but with an empty object for update thus working similarly to what I want (albeit slightly more complicated). However, here I am only adding 1 video to the playlist at once. Is there a way to add multiple videos (aka creating multiple many-to-many relations to the join table between Playlist & Video), ideally in one query?
const id = /* Playlist's ID */
const videoUrl = /* Video's Url, which is its primary key */
// I want to use an array of videoUrls instead, not just one

const updatedPlaylist = await prisma.playlist.update({
where: { id },
include: { videos: { include: { video: true } } },
data: {
videos: {
upsert: {
where: {
playlistId_videoUrl: {
playlistId: id,
videoUrl: videoUrl,
},
},
create: {
addedAt: new Date(),
video: {
connect: { url: videoUrl },
},
},
update: {},
},
},
},
});
const id = /* Playlist's ID */
const videoUrl = /* Video's Url, which is its primary key */
// I want to use an array of videoUrls instead, not just one

const updatedPlaylist = await prisma.playlist.update({
where: { id },
include: { videos: { include: { video: true } } },
data: {
videos: {
upsert: {
where: {
playlistId_videoUrl: {
playlistId: id,
videoUrl: videoUrl,
},
},
create: {
addedAt: new Date(),
video: {
connect: { url: videoUrl },
},
},
update: {},
},
},
},
});
0 Replies
No replies yetBe the first to reply to this messageJoin