Remix 3
Add Prisma ORM to a Remix 3 app, seed a PostgreSQL database, render users from a controller, and deploy to Prisma Compute.
Introduction
This guide shows you how to use Prisma ORM in a Remix 3 app. You create a project, seed a PostgreSQL database, render users on the home page, and deploy the app to Prisma Compute, Prisma's app hosting service.
In Remix 3, a controller handles requests and passes database results to a component that renders the page. See the Remix 3 release candidate announcement for an overview of the new framework. For apps built with Remix 2, see the React Router guide.
Prerequisites
- Node.js 24.11 or newer
- A PostgreSQL connection string. Use your own database, or run
npx create-db@latestto create a Prisma Postgres database. - A Prisma account to deploy to Compute.
1. Create a Remix app
Create a project with the Remix CLI and install its dependencies:
bunx remix@latest new my-app
cd my-app
bun installThe starter defines routes in app/routes.ts and handles the home page in app/actions/controller.tsx. Its app/router.ts exports the router used by the server.
2. Add Prisma ORM
Add Prisma ORM to the existing project with orm init, using --yes to accept the default prompts:
bunx prisma@latest orm init --yes --target postgres --authoring pslThe command selects PostgreSQL and writes your schema in Prisma Schema Language (psl). It installs Prisma ORM and creates src/prisma/contract.prisma for your schema, src/prisma/db.ts for the database client, and prisma.config.ts for the CLI.
Create a .env file at the project root with your PostgreSQL connection string. If you used create-db, copy the connection string it prints, and open its separate claim URL in your browser to save the database in your Prisma account before it expires after 24 hours:
DATABASE_URL="postgres://user:password@localhost:5432/mydb"Both the CLI and the database client load this file automatically.
3. Define and initialize the database
Prisma ORM calls your schema a contract. Replace the starter schema with a user model, keeping // use prisma-8 so the CLI uses the schema format for Prisma ORM 8:
// use prisma-8
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}Generate the TypeScript definitions for checking your queries and a JSON file containing your Prisma model. Then create the database table:
bunx prisma contract emit
bunx prisma db initprisma contract emit generates these files for your queries and deployment:
src/prisma/contract.d.tsexportsContract, a TypeScript type describing your tables and columns for checking queries.src/prisma/contract.jsonstores the same model in Prisma's JSON format for the client and deployment tools to read.
Include both files when you commit your project to Git; the starter's .gitignore excludes neither file. Leave src/prisma/db.ts as generated: it uses Contract to check your queries and reads contract.json at runtime. prisma db init creates the User table with unique emails and saves a description of that schema in your project's migrations directory.
4. Seed users
Create a script that adds sample users and skips existing emails when you run it again. db.orm is the client's query API, and db.orm.public.User queries the User table. PostgreSQL groups tables into schemas; this guide uses its default schema, public:
import { db } from "./db.ts";
try {
for (const user of [
{ email: "alice@prisma.io", name: "Alice" },
{ email: "bob@prisma.io", name: "Bob" },
]) {
const existing = await db.orm.public.User.where({ email: user.email }).first();
if (!existing) await db.orm.public.User.create(user);
}
console.log("Seeded Alice and Bob");
} finally {
await db.close();
}Run it from the project root:
node src/prisma/seed.tsSeeded Alice and Bob5. Render users on the home page
Replace app/actions/home-page.tsx with a Remix component that displays the queried users. Remix passes each component a Handle, an object containing its props. Read the users passed by the controller from handle.props.users, and return a function containing the JSX as shown below. The starter's Document component supplies the page's HTML document:
import type { Handle } from "remix/component";
import { Document } from "./document.tsx";
type User = { email: string; name: string | null };
export function HomePage(handle: Handle<{ users: User[] }>) {
return () => (
<Document title="Prisma + Remix">
<main>
<h1>Users</h1>
<ul>
{handle.props.users.map((user) => (
<li key={user.email}>
{user.name ?? "Anonymous"} ({user.email})
</li>
))}
</ul>
</main>
</Document>
);
}Replace the home page controller so it reads users with Prisma ORM and passes them to the component. Keep the assets import from ../assets.ts and the async assets(context) handler shown below; they serve the JavaScript files the browser needs.
Use context.render(...) in the controller's request handler to return the component as an HTML response:
import { createController } from "remix/router";
import { db } from "../../src/prisma/db.ts";
import { assets } from "../assets.ts";
import { routes } from "../routes.ts";
import { HomePage } from "./home-page.tsx";
export default createController(routes, {
actions: {
async assets(context) {
return (await assets.fetch(context.request)) ?? new Response("Not Found", { status: 404 });
},
async home(context) {
const users = await db.orm.public.User.orderBy((user) => user.id.asc()).all();
return context.render(<HomePage users={users} />);
},
},
});Import db in controller.tsx; don't import it in home-page.tsx or files served to the browser. Don't call db.close() in a controller; the connection pool is shared across requests.
6. Run and verify
Check the types, then start the development server:
bun run typecheck
bun run devOpen http://localhost to see the seeded users:
Users
Alice (alice@prisma.io)
Bob (bob@prisma.io)You can also check the rendered HTML with curl http://localhost:44100. If the port is already in use, run PORT=44101 npm run dev and open http://localhost.
Press Ctrl+C to stop the development server before continuing.
7. Deploy to Prisma Compute
Your first deployment creates a Prisma project containing this app's server and a new Prisma Postgres database, separate from your local database. Prisma Composer is the TypeScript configuration library you use to describe this deployment.
7.1. Declare the app
Run the following commands and create all deployment files in the project root. Install the deployment packages and build tool:
bun add @prisma/composer @prisma/composer-prisma-cloud
bun add --dev esbuildCreate data-contract.ts so your deployment configuration can use the schema you generated. dataContract() returns an object describing your PostgreSQL schema for Composer; export it as userData so the server and database configuration use the same schema:
import { dataContract } from "@prisma/composer-prisma-cloud/orm";
import type { Contract } from "./src/prisma/contract.d.ts";
import contractJson from "./src/prisma/contract.json" with { type: "json" };
export const userData = dataContract<Contract>(contractJson);Create service.ts to describe the server's startup file, dist/server.mjs. Add database: postgres(userData) to deps so deployment knows which database schema the server needs. These files describe deployment work; prisma deploy creates the hosted database and server, then supplies the database connection:
import node from "@prisma/composer/node";
import { compute } from "@prisma/composer-prisma-cloud";
import { postgres } from "@prisma/composer-prisma-cloud/orm";
import { userData } from "./data-contract.ts";
export default compute({
name: "web",
deps: { database: postgres(userData) },
// Package an existing build; run npm run build before deploying.
build: node({
module: import.meta.url, // The source file's URL makes dir relative to service.ts.
dir: "./dist",
entry: "server.mjs",
}),
});A Composer module groups the server and database into one app. Create module.ts to define the app named my-remix-app. Use provision() to declare the resources the CLI creates or updates when you run prisma deploy, and pass the created database to the server through deps:
import { module } from "@prisma/composer";
import { postgres } from "@prisma/composer-prisma-cloud/orm";
import { userData } from "./data-contract.ts";
import web from "./service.ts";
export default module("my-remix-app", ({ provision }) => {
const database = provision(postgres({ name: "database", contract: userData, config: "./prisma.config.ts" }));
provision(web, { deps: { database } });
});Replace prisma.config.ts with the configuration below to deploy to Compute in us-east-1. The extensions array lists the deployment tools: prismaCloud() creates the hosted resources, and nodeBuild() packages your existing dist directory.
The config: "./prisma.config.ts" setting in module.ts tells Composer to read this file for the hosted database's migrations. Set orm.migrations.dir to the project-root migrations directory; step 7.3 generates the files there. Deployment applies them to the new hosted database, while local commands use the connection from your .env.
Keep state: prismaState() so Composer saves the server and database IDs in the hosted project and updates those resources on future deploys:
import 'dotenv/config';
import { defineConfig as composer } from '@prisma/composer/config';
import { nodeBuild } from '@prisma/composer/node/control';
import { prismaCloud, prismaState } from '@prisma/composer-prisma-cloud/control';
import { definePrismaConfig } from '@prisma/cli-engine';
import { defineConfig as ormConfig } from '@prisma/orm-postgres/config';
export default definePrismaConfig({
composer: composer({
entry: './module.ts',
extensions: [prismaCloud({ region: 'us-east-1' }), nodeBuild()],
state: prismaState(),
}),
orm: ormConfig({
contract: "./src/prisma/contract.prisma",
migrations: { dir: "./migrations" },
db: {
connection: process.env['DATABASE_URL']!,
},
}),
});7.2. Package the server and assets
Create a separate server startup file for Compute. service.load() reads the values supplied by deployment when the server starts, including the database connection string. service.port() provides the port assigned by Compute. Set the database URL before loading Prisma, and seed users before accepting requests:
import * as http from "node:http";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createRequestListener } from "remix/node-fetch-server";
import service from "./service.ts";
process.chdir(dirname(fileURLToPath(import.meta.url)));
process.env.NODE_ENV = "production";
process.env.DATABASE_URL = service.load().database.url;
const { db } = await import("./src/prisma/db.ts");
for (const user of [
{ email: "alice@prisma.io", name: "Alice" },
{ email: "bob@prisma.io", name: "Bob" },
]) {
if (!(await db.orm.public.User.where({ email: user.email }).first())) {
await db.orm.public.User.create(user);
}
}
const { router } = await import("./app/router.ts");
http.createServer(createRequestListener(router.fetch)).listen(service.port(), "0.0.0.0");The deployed Remix server needs the app's source files to generate browser JavaScript and CSS. Create build.mjs to copy app, public, and tsconfig.json into dist, bundle the server startup file, and write the package list for the deployed server:
import { cp, mkdir, rm, readFile, writeFile } from "node:fs/promises";
import { build } from "esbuild";
await rm("dist", { recursive: true, force: true });
await mkdir("dist", { recursive: true });
await build({
entryPoints: ["compute-server.ts"],
outfile: "dist/server.mjs",
bundle: true,
// Leave these out of the bundle; package.json's build command installs them in dist/node_modules.
external: ["remix/*", "@prisma/orm-postgres/*", "dotenv/config"],
platform: "node",
format: "esm",
});
for (const path of ["app", "public", "tsconfig.json"]) {
await cp(path, `dist/${path}`, { recursive: true });
}
const { dependencies: installed } = JSON.parse(await readFile("package.json", "utf8"));
const dependencies = Object.fromEntries(["remix", "@prisma/orm-postgres", "dotenv"].map((name) => [name, installed[name]]));
await writeFile("dist/package.json", JSON.stringify({ type: "module", dependencies }));Add a build script to the existing scripts in package.json. Compute runs on Linux x64, so use the flags below to install Linux binaries for Remix's JavaScript and CSS compilers. --libc=glibc selects binaries for glibc (the GNU C Library), which Compute uses:
{
"scripts": {
"build": "node build.mjs && npm install --prefix dist --omit=dev --ignore-scripts --os=linux --cpu=x64 --libc=glibc"
}
}The starter's .gitignore excludes .env, and the build script does not copy it into dist.
7.3. Build and deploy
The earlier db init also saved a description of the schema it created locally in your project's migrations directory. migration plan uses that description to write a migration that creates the same User table in a fresh hosted database.
Generate the migration files, then sign in through the browser. Check which workspace will contain your app; a workspace groups Prisma projects under your account:
bunx prisma migration plan --name init
bunx prisma auth login
bunx prisma auth whoamiTo switch between workspaces you've signed into on this computer, run npx prisma auth workspace list, then npx prisma auth workspace use "Your workspace name" with the name shown in the list. If your workspace is absent, run npx prisma auth login again and choose it during browser sign-in.
Check existing project names before deploying:
bunx prisma project listFor your first deployment, choose an unused project name. If my-remix-app is listed, replace "my-remix-app" in module.ts with an unused name such as "my-remix-app-2". Keep that name for later deploys.
Build and deploy:
bun run build
bunx prisma deployKeep the generated migrations directory with your project. Deployment applies these migrations before starting the server.
my-remix-app
├─ database postgres-database db_abc123
└─ web compute-service cps_abc123
https://xyz.ewr.prisma.buildThe app name in module.ts selects the project in your current workspace. The first deploy creates that project; later deploys update its server and database using the IDs saved by prismaState().
Open the printed URL: the page shows Alice and Bob from the deployed database. In your browser's developer tools, open Network and reload the page; requests under /assets/ should return 200.
For automatic deployments and previews per Git branch, see Deploy on push.
Use with your agent
Next steps
- Query your data: filtering, sorting, and pagination.
- Update your schema as your app grows.
- Remix routing and controllers for more pages and actions.
Last updated on
