Skip to main content

CRUD Methods

Prisma Client provides a comprehensive set of CRUD (Create, Read, Update, Delete) methods for each model in your schema.

Read Operations

findUnique

Retrieve a single record by unique identifier or unique constraint.
findUnique<T>(args: {
  where: WhereUniqueInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T | null>
where
object
required
Unique constraint to identify the record. Must use a unique field or combination of fields.
// By ID
const user = await prisma.user.findUnique({
  where: { id: 1 }
})

// By unique email
const user = await prisma.user.findUnique({
  where: { email: 'alice@prisma.io' }
})

// By compound unique constraint
const post = await prisma.post.findUnique({
  where: {
    authorId_title: {
      authorId: 1,
      title: 'Hello World'
    }
  }
})
return
Model | null
Returns the matching record or null if not found.

findUniqueOrThrow

Like findUnique, but throws an error if no record is found.
findUniqueOrThrow<T>(args: {
  where: WhereUniqueInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T>
return
Model
Returns the matching record or throws PrismaClientKnownRequestError with code P2025 if not found.

findFirst

Retrieve the first record that matches the filter criteria.
findFirst<T>(args?: {
  where?: WhereInput
  orderBy?: OrderByInput
  cursor?: WhereUniqueInput
  take?: number
  skip?: number
  distinct?: FieldName[]
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T | null>
where
object
Filter conditions to match records.
orderBy
object | array
Sort order for selecting the first record.
// Single field
orderBy: { createdAt: 'desc' }

// Multiple fields
orderBy: [
  { createdAt: 'desc' },
  { name: 'asc' }
]
cursor
object
Cursor-based pagination starting point.
take
number
Limit the number of records (used with pagination).
skip
number
Skip a number of records (offset pagination).
distinct
array
Select distinct records based on one or more fields.
distinct: ['email', 'name']
Example:
const user = await prisma.user.findFirst({
  where: { role: 'ADMIN' },
  orderBy: { createdAt: 'desc' }
})

findFirstOrThrow

Like findFirst, but throws an error if no record is found.
findFirstOrThrow<T>(args?: FindFirstArgs): Promise<T>

findMany

Retrieve multiple records that match the filter criteria.
findMany<T>(args?: {
  where?: WhereInput
  orderBy?: OrderByInput
  cursor?: WhereUniqueInput
  take?: number
  skip?: number
  distinct?: FieldName[]
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T[]>
Example:
const users = await prisma.user.findMany({
  where: {
    email: {
      endsWith: '@prisma.io'
    }
  },
  orderBy: { createdAt: 'desc' },
  take: 10,
  skip: 20
})
return
Model[]
Returns an array of matching records (empty array if none found).

Create Operations

create

Create a single new record.
create<T>(args: {
  data: CreateInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T>
data
object
required
Data for creating the new record.
const user = await prisma.user.create({
  data: {
    email: 'alice@prisma.io',
    name: 'Alice',
    posts: {
      create: [
        { title: 'First Post' },
        { title: 'Second Post' }
      ]
    }
  }
})
return
Model
Returns the newly created record.

createMany

Create multiple records in a single transaction.
createMany(args: {
  data: CreateInput[]
  skipDuplicates?: boolean
}): Promise<{ count: number }>
data
array
required
Array of data objects to create.
skipDuplicates
boolean
Skip records that would violate unique constraints instead of throwing an error.
Example:
const result = await prisma.user.createMany({
  data: [
    { email: 'alice@prisma.io', name: 'Alice' },
    { email: 'bob@prisma.io', name: 'Bob' }
  ],
  skipDuplicates: true
})

console.log(`Created ${result.count} users`)
return
{ count: number }
Returns an object with the count of created records.
createMany does not return the created records. Use createManyAndReturn if you need the records.

createManyAndReturn

Create multiple records and return them.
createManyAndReturn<T>(args: {
  data: CreateInput[]
  skipDuplicates?: boolean
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T[]>
Example:
const users = await prisma.user.createManyAndReturn({
  data: [
    { email: 'alice@prisma.io', name: 'Alice' },
    { email: 'bob@prisma.io', name: 'Bob' }
  ]
})
return
Model[]
Returns an array of the newly created records.

Update Operations

update

Update a single record identified by a unique constraint.
update<T>(args: {
  where: WhereUniqueInput
  data: UpdateInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T>
where
object
required
Unique constraint to identify the record to update.
data
object
required
Data to update.
const user = await prisma.user.update({
  where: { id: 1 },
  data: {
    email: 'alice.new@prisma.io',
    posts: {
      create: { title: 'New Post' },
      updateMany: {
        where: { published: false },
        data: { published: true }
      }
    }
  }
})
return
Model
Returns the updated record or throws P2025 if not found.

updateMany

Update multiple records that match the filter criteria.
updateMany(args: {
  where?: WhereInput
  data: UpdateInput
}): Promise<{ count: number }>
Example:
const result = await prisma.user.updateMany({
  where: {
    role: 'USER'
  },
  data: {
    active: true
  }
})

console.log(`Updated ${result.count} users`)
return
{ count: number }
Returns an object with the count of updated records.

updateManyAndReturn

Update multiple records and return them.
updateManyAndReturn<T>(args: {
  where?: WhereInput
  data: UpdateInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T[]>
return
Model[]
Returns an array of the updated records.

upsert

Update an existing record or create a new one if it doesn’t exist.
upsert<T>(args: {
  where: WhereUniqueInput
  create: CreateInput
  update: UpdateInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T>
where
object
required
Unique constraint to check for existing record.
create
object
required
Data to use if creating a new record.
update
object
required
Data to use if updating an existing record.
Example:
const user = await prisma.user.upsert({
  where: { email: 'alice@prisma.io' },
  create: {
    email: 'alice@prisma.io',
    name: 'Alice'
  },
  update: {
    name: 'Alice Updated'
  }
})

Delete Operations

delete

Delete a single record identified by a unique constraint.
delete<T>(args: {
  where: WhereUniqueInput
  select?: SelectInput
  include?: IncludeInput
  omit?: OmitInput
}): Promise<T>
Example:
const user = await prisma.user.delete({
  where: { id: 1 }
})
return
Model
Returns the deleted record or throws P2025 if not found.

deleteMany

Delete multiple records that match the filter criteria.
deleteMany(args?: {
  where?: WhereInput
}): Promise<{ count: number }>
Example:
const result = await prisma.user.deleteMany({
  where: {
    active: false
  }
})

console.log(`Deleted ${result.count} users`)
return
{ count: number }
Returns an object with the count of deleted records.
Calling deleteMany without a where clause will delete ALL records from the table.

Aggregation Operations

count

Count records that match the filter criteria.
count(args?: {
  where?: WhereInput
  orderBy?: OrderByInput
  cursor?: WhereUniqueInput
  take?: number
  skip?: number
  select?: { _all?: boolean } | true
}): Promise<number | object>
Example:
// Simple count
const count = await prisma.user.count()

// Count with filter
const activeUsers = await prisma.user.count({
  where: { active: true }
})

// Count specific fields
const counts = await prisma.user.count({
  select: {
    _all: true,
    email: true
  }
})

aggregate

Perform aggregation operations on numeric fields.
aggregate(args?: {
  where?: WhereInput
  orderBy?: OrderByInput
  cursor?: WhereUniqueInput
  take?: number
  skip?: number
  _count?: boolean | { [field]: boolean }
  _avg?: { [field]: boolean }
  _sum?: { [field]: boolean }
  _min?: { [field]: boolean }
  _max?: { [field]: boolean }
}): Promise<AggregateResult>
Example:
const result = await prisma.user.aggregate({
  _count: { _all: true },
  _avg: { age: true },
  _max: { age: true },
  _min: { age: true },
  where: { active: true }
})

groupBy

Group records by one or more fields and perform aggregations.
groupBy(args: {
  by: FieldName[]
  where?: WhereInput
  orderBy?: OrderByInput
  having?: HavingInput
  take?: number
  skip?: number
  _count?: boolean | { [field]: boolean }
  _avg?: { [field]: boolean }
  _sum?: { [field]: boolean }
  _min?: { [field]: boolean }
  _max?: { [field]: boolean }
}): Promise<GroupByResult[]>
by
array
required
Fields to group by.
having
object
Filter groups based on aggregated values.
Example:
const result = await prisma.user.groupBy({
  by: ['role'],
  _count: { _all: true },
  _avg: { age: true },
  having: {
    age: {
      _avg: { gt: 30 }
    }
  }
})

MongoDB-Specific Operations

findRaw

Execute a raw MongoDB find operation.
findRaw(args: {
  filter?: object
  options?: object
}): Promise<any>
Only available when using the MongoDB provider.

aggregateRaw

Execute a raw MongoDB aggregation pipeline.
aggregateRaw(args: {
  pipeline?: object[]
  options?: object
}): Promise<any>
Only available when using the MongoDB provider.

See Also