棱镜,更新 scalarList/array
Prisma, update scalarList/array
我正在做 Spotify 克隆,我正在尝试将歌曲添加到播放列表,但我的查询不起作用,直到此时,按照 prisma 文档上的文档,一切都很好,但我做不到这个查询,每次都会出错,所以如果有人能告诉我,我该如何用一个例子来做这个,我将不胜感激。
我的问题是,有了这个架构,我如何才能将歌曲添加到播放列表?有两个模型受查询影响,歌曲(我要添加的地方)和播放列表。
我的架构:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
firstName String
lastName String
password String
playlists Playlist[]
}
// here
model Song {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
artist Artist @relation(fields: [artistId], references: [id])
artistId Int
playlists Playlist[]
duration Int
url String
}
model Artist {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
songs Song[]
name String @unique
}
// here
model Playlist {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
songs Song[]
user User @relation(fields: [userId], references: [id])
userId Int
}
我正在尝试添加这样的歌曲:
let songId = 1;
let playlistId = 1;
let lists;
// get the playlists the song is part of
lists = await prisma.song.findFirst({
select: {
playlists: true
},
where: {
id: +songId
}
})
// get the playlist data i need
const list = await prisma.playlist.findUnique({
where: {
id: playlistId
}
})
// create the array for update with the data
// plus the data I want to add
lists = [ ...lists.playlists, list ]
// trying to update the old array with the new data (lists)
// this is what i'm doing wrong, help please
await prisma.song.update({
where: { id: +songId },
data:{
playlists: lists
}
})
经过多次尝试,我终于得到了我想要的,如果有人知道更好的方法请告诉我,我想学习,现在这是我的解决方案:
我需要将每个值作为 id 发送:playlistId
const song = await prisma.song.findUnique({
select: {
playlists: true
},
where: {
id: +songId
}
})
// get an array of objects, id: playlistId
const songPlaylistsIds = song.playlists.map( playlist => ({id: playlist.id}))
// I prepare the array with the content that already exists plus the new content that I want to add:
const playlists = [...songPlaylistsIds, { id: playlistId}]
await prisma.song.update({
where: { id: +songId },
data:{
playlists: {
// finally for each object in the array i get, id: playlistId and it works.
set: playlists.map( playlistSong => ({ ...playlistSong }))
}
}
})
我这样做的问题:我错误地认为它应该像播放列表一样简单:列表我想将内容更改为新内容但我做不到,我需要发送值逐个。
获取播放列表内容时的另一个错误。我有完整的对象,但只需要发送 ID。
最后,在prisma的文档中,有set,push这样的方法,但是这个方法不行,至少我不知道push是怎么实现的
我正在做 Spotify 克隆,我正在尝试将歌曲添加到播放列表,但我的查询不起作用,直到此时,按照 prisma 文档上的文档,一切都很好,但我做不到这个查询,每次都会出错,所以如果有人能告诉我,我该如何用一个例子来做这个,我将不胜感激。
我的问题是,有了这个架构,我如何才能将歌曲添加到播放列表?有两个模型受查询影响,歌曲(我要添加的地方)和播放列表。
我的架构:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
firstName String
lastName String
password String
playlists Playlist[]
}
// here
model Song {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
artist Artist @relation(fields: [artistId], references: [id])
artistId Int
playlists Playlist[]
duration Int
url String
}
model Artist {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
songs Song[]
name String @unique
}
// here
model Playlist {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
songs Song[]
user User @relation(fields: [userId], references: [id])
userId Int
}
我正在尝试添加这样的歌曲:
let songId = 1;
let playlistId = 1;
let lists;
// get the playlists the song is part of
lists = await prisma.song.findFirst({
select: {
playlists: true
},
where: {
id: +songId
}
})
// get the playlist data i need
const list = await prisma.playlist.findUnique({
where: {
id: playlistId
}
})
// create the array for update with the data
// plus the data I want to add
lists = [ ...lists.playlists, list ]
// trying to update the old array with the new data (lists)
// this is what i'm doing wrong, help please
await prisma.song.update({
where: { id: +songId },
data:{
playlists: lists
}
})
经过多次尝试,我终于得到了我想要的,如果有人知道更好的方法请告诉我,我想学习,现在这是我的解决方案:
我需要将每个值作为 id 发送:playlistId
const song = await prisma.song.findUnique({
select: {
playlists: true
},
where: {
id: +songId
}
})
// get an array of objects, id: playlistId
const songPlaylistsIds = song.playlists.map( playlist => ({id: playlist.id}))
// I prepare the array with the content that already exists plus the new content that I want to add:
const playlists = [...songPlaylistsIds, { id: playlistId}]
await prisma.song.update({
where: { id: +songId },
data:{
playlists: {
// finally for each object in the array i get, id: playlistId and it works.
set: playlists.map( playlistSong => ({ ...playlistSong }))
}
}
})
我这样做的问题:我错误地认为它应该像播放列表一样简单:列表我想将内容更改为新内容但我做不到,我需要发送值逐个。 获取播放列表内容时的另一个错误。我有完整的对象,但只需要发送 ID。 最后,在prisma的文档中,有set,push这样的方法,但是这个方法不行,至少我不知道push是怎么实现的