Hey guys, what is the prisma.exists. equivalent in...
# orm-help
e
Hey guys, what is the prisma.exists. equivalent in prisma 2?
r
@Edmir Suljic 👋 Finding the value itself should do it.
Could you share your use case?
e
Hmm so I have an enum in my DB that a user can only have one each of. If a user wants to create another one that they already have, I want to throw an error. In prisma 1 it would be something like
const exists = prisma.exists.User({ device })
(not quite but you get the idea)
So I kind of want the equivalent in prisma 2
I was trying this
Copy code
const deviceExists = await prisma.user.findUnique({
        where: {
            tags: {
                has: args.data.deviceType
            }
        }
    })
But that didnt work
r
Could you share your schema for
User
and
Tag
?
e
Ah yes I thought the tag part is where I messed up. How do you define it in schema?
Copy code
type User {
  id: ID!
  name: String!
  email: String
  password: String!
  profile: Profile
  devices: [Device!]!
}
r
This doesn’t look like a Prisma 2 schema. Could you share your
schema.prisma
models?
e
Yeah sorry
Copy code
model User {
  id       String    @id @default(uuid())
  name     String
  email    String @unique
  password String
  profile  Profile?
  devices Device[]
  createdAt DateTime @default(now())
  updatedAt DateTime @default(now())
}
I thought you meant the graphql one
r
Device is an enum?
e
No device contains devicetype that is an enum
Copy code
model Device {
  id String @default(uuid()) @id
  deviceType DeviceType
  user User @relation(fields: [userId], references: [id])
  userId String
  volume Int @default(0)
  changedVolumeTime DateTime @default(now())
  batteryCharge Int @default(0)
  lastCharge DateTime @default(now())
}
r
And you want to check if the User has a device that contains that enum right?
e
yeah
r
This should work:
Copy code
await prisma.user.findFirst({where: {
    id: 'id',
    devices: {some: {deviceType: 'Type'}}
}})
e
It sure did. Thanks a lot man!
🙌 1