简体   繁体   English

如何使用 Prisma 2 实现社交媒体“关注提要”查询?

[英]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.

相关问题 如何在Javascript中输出数组,如社交媒体Feed? - How to output an array in Javascript like a social media feed? 如何在 Velo 中使用 Javascript 在 Wix ADI 中实现社交媒体授权? - How do I implement social media authorizations in the Wix ADI using Javascript in Velo? 如何使用rxjs Observables实现以下内容? - How to implement following using rxjs Observables? 如何将 Pixlee Social Feed 添加到 React 应用程序? - How to add Pixlee Social Feed to a React application? 如何在模态中将社交媒体按钮作为标题 - How to have social media button as caption in modal 如何从 Vuejs 应用程序在社交媒体上分享? - How to share in social media from a Vuejs app? 当用户使用带有 firebase 的社交媒体登录时,如何只获取名字? - How to get just the first name when a user logs in using social media with firebase? 如何在 prisma 中的查询返回之前添加一个字符串? - How to add a string before a query return in prisma? 如何在 Prisma 中查询/更新自关系? - How to query/update a self-relation in Prisma? 如何使用媒体查询显示/隐藏元素? - How to show/hide elements using media query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM