Documentation
supastarter for Next.jsDatabase

Use database client

Learn how to use the database client in supastarter.

In general you can use the database client in all parts of your application (that are server-side) directly, but we recommend to keep all usage of the database client in the database package in the queriesfolder and exposing specific query/mutation functions instead.

Prisma

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 },
});

Drizzle

The database client is an export of the Drizzle client from the drizzle-orm package. It is automatically generated by Drizzle based on the schema and is exposed as the db object from the database package in the monorepo.

Querying records

To query records from the database, you can use the findMany method on the Drizzle 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.query.user.findMany({
  orderBy: (user, { desc }) => [desc(user.createdAt)],
});

Creating records

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

import { db } from "database";
 
const user = await db.insert(users).values({
  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 Drizzle client.

On this page