[英]How to implement a social media “following feed” query using Prisma 2?
I have a question related to writing a "social media" style feed query, but in this case I'm using Prisma 2. I have a music application where I'd like to display a feed of all Albums
from Artists
that a User
follows.我有一个与编写“社交媒体”样式提要查询有关的问题,但在这种情况下,我使用的是 Prisma 2。我有一个音乐应用程序,我想在其中显示
User
关注的Artists
的所有Albums
的提要.
In my schema.prisma
, I have the following models:在我的
schema.prisma
中,我有以下模型:
User用户
model User {
id Int @id @default(autoincrement())
email String @unique
following Artist[]
}
Artist艺术家
model Artist {
id Int @id @default(autoincrement())
name String
followers User[]
albums Album[]
}
Prisma creates an implicit join table called UserToArtist
to track when a User
follows an Artist
Prisma 创建一个名为
UserToArtist
的隐式连接表来跟踪User
何时关注Artist
Album专辑
model Album {
id Int @id @default(autoincrement())
title String
date DateTime <-- this is an ISO date string
artists Artist[] <-- an album can have several artists
}
Prisma creates an implicit join table called ArtistToAlbum
to track which Artists
are associated to certain Albums
. Prisma 创建一个名为
ArtistToAlbum
的隐式连接表来跟踪哪些Artists
与某些Albums
相关联。
So how can I create a Prisma query to find all albums of artists that a user follows ordered by Album
date?那么如何创建一个 Prisma 查询来查找用户按照
Album
日期排序的所有艺术家专辑呢? Here was my first attempt, but this doesn't seem to be correct and the albums aren't sorted by date:这是我的第一次尝试,但这似乎不正确,专辑未按日期排序:
const results = await ctx.db.album.findMany({
where: {
date: {
gt: moment().startOf('day').toISOString()
},
artists: {
some: {
followers: {
some: {
id: args.userId, <-- the userId for the feed
},
},
},
},
},
orderBy: {
date: 'asc',
},
})
Notice how I have to filter down through two some
statements?请注意我必须如何通过两个
some
语句进行过滤? Just doesn't feel right... What recommendations do you have for a query like this?只是感觉不对……对于这样的查询,您有什么建议? Thanks!
谢谢!
You can use the fluent API to grab all the artists that a user follows, and it should look something like this:您可以使用流畅的 API 抓取用户关注的所有艺术家,它应该看起来像这样:
const followedArtists = await ctx.db.user.findOne({ where: { id: args.userId, } }).following()
From here you can grab the albums from all of those artists and then sort them in your server code to then be sent back as the query result从这里您可以获取所有这些艺术家的专辑,然后在您的服务器代码中对它们进行排序,然后作为查询结果发回
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.