Documentation
Database

Use database client

The database client is an export of the Prisma client from the @prisma/client package. It is automatically generated by Prisma based on the schema and is exposed as the db object from the database package in the monorepo.

This guide covers the basic operations of the database client, such as querying, creating, updating, and deleting records. To learn more about the Prisma client, check out the official documentation.

Querying records

To query records from the database, you can use the findMany method on the Prisma client. This method accepts an object with optional where, orderBy, skip, and take fields to filter, order, and paginate the results.

Here is an example of querying all users from the database:

import { db } from "database";
 
const users = await db.user.findMany({
  orderBy: { createdAt: "desc" },
});

Creating records

To create a new record in the database, you can use the create method on the Prisma client.

import { db } from "database";
 
const user = await db.user.create({
  data: {
    email: "test@test.com",
    name: "Test User",
    password: "password",
  },
});

Updating records

To update an existing record in the database, you can use the update method on the Prisma client.

import { db } from "database";
 
const updatedUser = await db.user.update({
  where: { id: 1 },
  data: {
    name: "Updated User",
  },
});

Deleting records

To delete a record from the database, you can use the delete method on the Prisma client.

import { db } from "database";
 
const deletedUser = await db.user.delete({
  where: { id: 1 },
});

On this page