can’t set members array in mongodb with prisma

can’t set members array in mongodb with prisma


0

I use Prisma, apollo-graphql, and Mongoose to create chat applications but I have this problem, MongoDB gets only _id when I try to create a chat.

Prisma Schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id       String @id @default(auto()) @map("_id") @db.ObjectId
  userName String
  avatar   String
  email    String

  conversation Conversation[]
  Participant  Participant[]
}

model Conversation {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  members Participant[]

  User   User?   @relation(fields: [userId], references: [id])
  userId String? @db.ObjectId
}

model Participant {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  user   User   @relation(fields: [userId], references: [id])
  userId String

  conversation   Conversation @relation(fields: [conversationId], references: [id])
  conversationId String

  hasSeenLastMessage Boolean
}

Conversation Resolver:

 Mutation: {
    createConversation: async (
      _: any,
      args: any,
      { prisma }: GraphqlContext
    ) => {
      try {

        const newMember = args.conversationInput.members.map((id: any) => {
          return {
            userId: id.id,
            hasSeenLastMessage: false,
          };
        });

        const newConversation = await prisma.conversation.create({
          data: {
            members: {
              create: newMember,
            },
            
          },
          include: { members: true },
        });

        console.log(newConversation);

        return {
          success: true,
          error: null,
        };
      } catch (error) {
        throw error;
      }
    },
  },

but when I try console.log(newConversation) it gives me the correct objects, I mean the problem is in Prisma, I am new in Prisma, and may I have some issue


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *