首页 > 解决方案 > 为每个 followBy 用户在数组中创建记录

问题描述

我正在我的用户之间创建一个通知系统。我有以下架构:

model User {
  id                Int     @id @default(autoincrement())
  email             String  @unique
  name              String
  user_name         String  @unique
  password          String
  movies            Movie[]
  notifications     Notification[]
  followedBy        User[]  @relation("UserFollows", references: [id])
  following         User[]  @relation("UserFollows", references: [id])
}

model Notification {
  id                Int     @id @default(autoincrement())
  link              String?
  movie_id          Int?
  message           String
  icon              String?
  thumbnail         String?
  user              User    @relation(fields: [userId], references: [id])
  userId            Int
  watched           Boolean
}

一个用户可以有多个FollowedBy引用。

(我认为)需要的是Notification在用户对象的记录中包含的每个用户对象上推送一条记录FollowedBy

  createNotification: async (_, args, {req, res}) => {
    const notification = await prisma.notification.create({
      where: {
        id: // loop through the current users FollowedBy records
      },
      data: {
        // notification data
      },
    });

    return notification;
  },

标签: ormgraphqlprisma-graphqlprisma2

解决方案


推荐阅读