TanStack Start (Prisma ORM v7)
Learn how to use Prisma ORM in a TanStack Start app
Introduction
Section titled “Introduction”TanStack Start runs server functions alongside your React routes, which is where you call Prisma ORM to read from a Prisma Postgres database.
This guide shows you how to integrate Prisma ORM with a Prisma Postgres database in a TanStack Start project from scratch.
Quick start
Section titled “Quick start”Run one command to scaffold a TanStack Start project with Prisma ORM and Prisma Postgres ready to go:
bunx create-prisma@stable --template tanstack-startpnpm create prisma@stable --template tanstack-startyarn create prisma@stable --template tanstack-startnpm create prisma@stable -- --template tanstack-startOr follow the steps below to set it up manually.
Prerequisites
Section titled “Prerequisites”1. Set up your project
Section titled “1. Set up your project”To begin, create a new TanStack Start project.
bun create @tanstack/start@latestpnpm create @tanstack/start@latestyarn create @tanstack/start@latestnpm create @tanstack/start@latestThis will create a new folder called tanstack-start-prisma and provision a new Prisma Postgres database. At the end of the setup, the CLI will display the database connection string:
● Database Connection
│
│ Connection String:
│
│ postgresql://username:password@db.prisma.io:5432/postgres
●Navigate to your project directory:
cd tanstack-start-prismaThen, add the connection string you copied to your .env file:
DATABASE_URL="postgresql://username:password@db.prisma.io:5432/postgres"2. Configure Prisma
Section titled “2. Configure Prisma”2.1. Define your Prisma Schema
Section titled “2.1. Define your Prisma Schema”In schema.prisma, the model for our todos is defined below the generator and datasource blocks:
generator client {
provider = "prisma-client"
output = "../app/generated/prisma"
}
datasource db {
provider = "postgresql"
}
model Todo {
id Int @id @default(autoincrement())
title String
createdAt DateTime @default(now())
}This creates a Todo model that will be pushed to the database
2.2. Run migrations and generate Prisma Client
Section titled “2.2. Run migrations and generate Prisma Client”Now, run the following command to create the database tables:
bun run db:migrate --name initpnpm run db:migrate --name inityarn db:migrate --name initnpm run db:migrate -- --name initGenerate the Prisma Client needed for the project:
bun run db:generatepnpm run db:generateyarn db:generatenpm run db:generate2.3. Seed the database
Section titled “2.3. Seed the database”Then seed the project with the seed.ts file in the prisma/ directory:
bun run db:seedpnpm run db:seedyarn db:seednpm run db:seedAnd open Prisma Studio to inspect your data:
bun run db:studiopnpm run db:studioyarn db:studionpm run db:studio3. Integrate Prisma into TanStack Start
Section titled “3. Integrate Prisma into TanStack Start”3.1 The Prisma Client
Section titled “3.1 The Prisma Client”Instead of creating a new Prisma Client instance in each file, TanStack Start has a db.ts that
creates a single instance that can be shared globally
import { PrismaClient } from "./generated/prisma/client.js";
import { PrismaPg } from "@prisma/adapter-pg";
const adapter = new PrismaPg({
connectionString: process.env.DATABASE_URL!,
});
declare global {
var __prisma: PrismaClient | undefined;
}
export const prisma = globalThis.__prisma || new PrismaClient({ adapter });
if (process.env.NODE_ENV !== "production") {
globalThis.__prisma = prisma;
}3.2 Fetch data on load
Section titled “3.2 Fetch data on load”First, import the necessary modules. Then, create a server function using the createServerFn function. This function will fetch the data from the database using the .findMany() method
import { createFileRoute } from "@tanstack/react-router";import { createServerFn } from "@tanstack/react-start";import { prisma } from '../db';export const Route = createFileRoute("/")({ component: Home,});const getTodos = createServerFn({ method: "GET" }).handler(async () => { return prisma.todo.findMany();});function Home() { return ( <div> </div> );}TanStack Start allows functions to run on load with loader functions in the createFileRoute function. Fetch the users and their posts on load with this code:
import { createFileRoute } from '@tanstack/react-router';import { createServerFn } from '@tanstack/react-start';import { prisma } from '../db';export const Route = createFileRoute("/")({ component: Home, loader: () => { return getTodos(); },});const getTodos = createServerFn({ method: "GET" }).handler(async () => { return prisma.todo.findMany();});function Home() { return ( <div> <h1>Todos</h1> </div> );}Store the response from the loader in the main component using Route.useLoaderData():
import { createServerFn } from "@tanstack/react-start";import { createFileRoute } from "@tanstack/react-router";import { prisma } from '../db';export const Route = createFileRoute("/")({ component: Home, loader: () => { return getTodos(); },});const getTodos = createServerFn({ method: "GET" }).handler(async () => { return prisma.todo.findMany();});function Home() { const todos = Route.useLoaderData(); return ( <div> <h1>Todos</h1> </div> );}3.3 Display the todos
Section titled “3.3 Display the todos”Next, you'll update the home page to display the data retrieved from your database.
Map over the todos and display them in a list:
import { createFileRoute } from '@tanstack/react-router';
import { createServerFn } from '@tanstack/react-start';
import { prisma } from '../db';
export const Route = createFileRoute('/')({
component: App,
loader: () => getTodos(),
});
const getTodos = createServerFn({ method: 'GET' }).handler(async () => {
return prisma.todo.findMany();
});
function App() {
const todos = Route.useLoaderData();
return (
<div>
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.title}</li>
))}
</ul>
</div>
);
}This setup will display the todos on your page, fetched directly from your database.
Next steps
Section titled “Next steps”Your TanStack Start app now loads todos from a Prisma Postgres database through a server function. A few suggestions for what to do next:
- Expand your Prisma models to handle more complex data relationships.
- Implement additional CRUD operations.
- Check out Prisma Postgres to see how you can scale your application.