Prisma ClientFeatures
Check Existence (TypeScript)
Overview
The Prisma client lets you check whether a certain record exists in the database using the $exists
property. For each model type in your datamodel, the Prisma client exposes one function on this property named after the model (but lowercased). Each of these functions receives a a filter object as input argument that specifies the conditions for the record and returns a boolean value that's true
if there's at least one database record matching the specified filter, false
otherwise.
The input object has the same type that's used for filtering lists.
Examples
Check if a user with a certain ID exists:
const userExists: boolean = prisma.$exists.user({
id: 'cjli6tko8005t0a23fid7kke7',
})
Check if user called Alice
or Bob
exists:
const userExists: boolean = prisma.$exists.user({
name_in: ['Alice', 'Bob'],
})
Check if there are links that have prisma
or graphql
in their descriptions and were created in 2018:
const linkExists: boolean = await prisma.$exists.link({
AND: [
{
OR: [
{
description_contains: 'graphql',
},
{
description_contains: 'prisma',
},
],
},
{
AND: [
{
createdAt_gt: '2017',
},
{
createdAt_lt: '2019',
},
],
},
],
})