#Upsert in prisma mongodb

3 messages · Page 1 of 1 (latest)

rare valve
#

Does upsert work in mongodb prisma connector? If yes, is there a way I can use upsert and connect together? My problem is I have a model User, I want to search based on its id. Now I want to update the user if the id is found but if not, I want to create a permissions entry also connecting my user id. Here's the code for it

const { id: userId, ...rest } = request;
  const response = await db.user.upsert({
    where: { id: userId },
    update: rest,
    create: {
      ...rest,
      Permissions: {
        create: {
          access: true,
          email: rest.email,
        },
        connect: {
          subAccountId: userId,
        },
      },
    },
  })

This gives me a type error Type '{ subAccountId: string; }' is not assignable to type 'PermissionsWhereUniqueInput | PermissionsWhereUniqueInput[] | undefined'. Type '{ subAccountId: string; }' is not assignable to type 'undefined'

fathom rune
#

Hey 👋
MongoDB does support Upsert. Can you share your relevant models so that I can try to further investigate?

rare valve
#

Hey @fathom rune . Sure, this is the Permissions model

model Permissions {
  id           String         @id @default(auto()) @map("_id") @db.ObjectId
  email        String
  subAccountId String     
  SubAccount   SubAccount @relation(fields: [subAccountId], references: [id], onDelete: Cascade)
  access       Boolean

  @@index([subAccountId])
}

This is user model

model User {
  id           String         @id @default(auto()) @map("_id") @db.ObjectId
  connectAccountId String?                   @default("")
  name             String
  subAccountLogo   String
  createdAt        DateTime                  @default(now())
  updatedAt        DateTime                  @updatedAt
  companyEmail     String
  companyPhone     String
  goal             Int                       @default(5)
  address          String
  city             String
  zipCode          String
  state            String
  country          String
  agencyId         String
  Agency           Agency                    @relation(fields: [agencyId], references: [id], onDelete: Cascade)
  SidebarOption    SubAccountSidebarOption[]
  Permissions      Permissions[]
  Funnels          Funnel[]
  Media            Media[]
  Contact          Contact[]
  Trigger          Trigger[]
  Automation       Automation[]
  Pipeline         Pipeline[]
  Tags             Tag[]
  Notification     Notification[]

  @@index([agencyId])
}

The typeerror is coming on connect part. Another problem is I dont really want to pass this userId in connect. I want to pass the id that would be auto-created on creating User model (notice that I have removed id from my user object at the time of creation). So, is there any way to do it. Thanks