Documentation
supastarter for TanStack Startsupastarter for TanStack StartDatabase

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 keeping database access in the database package in the queries folder and exposing specific query/mutation functions instead.

The database client is an export of the Drizzle client from the drizzle-orm package. It is configured from the schema and 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.