Client
Learn how to use basic database operations with the Prisma database client.
The database client is an export of the Prisma Client from the @prisma/client package. Prisma generates this client automatically based on your defined schema.
This guide outlines core operations with the database client, such as querying, creating, updating, and deleting records. For in-depth details and advanced usage, visit the Prisma Client documentation.
Query records
To retrieve records from the database, you can use the findMany method provided by the Prisma client:
import { prisma } from '@/lib/db';
const users = await prisma.user.findMany({
orderBy: { createdAt: 'desc' }
});You can also filter records using the where clause:
import { prisma } from '@/lib/db';
const user = await prisma.user.findUnique({
where: { email: 'user@example.com' }
});To limit the number of results, use the take option:
import { prisma } from '@/lib/db';
const recentUsers = await prisma.user.findMany({
orderBy: { createdAt: 'desc' },
take: 10
});Create record
To insert a new record into the database, you can use the create method:
import { prisma } from '@/lib/db';
const user = await prisma.user.create({
data: {
name: 'John Doe',
email: 'john.doe@gmail.com'
}
});By default Prisma returns the full row. You can use select to make the create method more efficient.
Update record
To update an existing record, use the update method:
import { prisma } from '@/lib/db';
const updatedUser = await prisma.user.update({
where: { id: 'some-uuid' },
data: {
name: 'John Doe Updated',
email: 'john.doe.updated@gmail.com'
}
});Delete record
To delete a record from the database, use the delete method:
import { prisma } from '@/lib/db';
const deletedUser = await prisma.user.delete({
where: { id: 'some-uuid' }
});This will remove the record from the database permanently.