Better-TS-Stack
Modules

Database Module

PostgreSQL with Prisma or Drizzle, or MongoDB with Mongoose

Supported combinations

DatabaseLibraryExpressNext.jsTanStack
PostgreSQLPrisma 7YesYesYes
PostgreSQLDrizzleYesYesYes
MongoDBMongoose 9YesYesYes

Prisma 7 does not support MongoDB, so the CLI offers Mongoose for MongoDB projects.

The CLI does not start a database for you. Run PostgreSQL or MongoDB yourself, or use a hosted database, and use a separate database for each application.

Prisma and PostgreSQL

Set DATABASE_URL in .env to your PostgreSQL connection string. In Prisma 7 the connection URL is set in prisma.config.ts, not in schema.prisma. The generated prisma.config.ts reads it from .env with dotenv.

The Prisma client is generated into:

  • src/generated/prisma for Express (CommonJS)
  • generated/ for Next.js and TanStack Start, imported from generated/client

All three templates use the PostgreSQL driver adapter (@prisma/adapter-pg) and reuse one client instance during development reloads.

npm run prisma:generate
npm run prisma:migrate
npm run build

npm run build generates the client automatically through a prebuild script. Before your first npm run dev or npm run type:check, run npm run prisma:generate yourself.

No seed script is included. If you need seed data, add a seed file and configure it in prisma.config.ts.

The generated models depend on the auth option:

  • Express includes a User model used by JWT auth.
  • Next.js and TanStack Start with Better Auth include User, Session, Account, and Verification models. Run the migration before you sign up.

prisma:migrate runs prisma migrate dev, which is meant for development. In production, run prisma migrate deploy as part of your deployment.

Drizzle and PostgreSQL

The schema and connection files are:

  • Express: src/lib/schema.ts and src/lib/db.ts
  • Next.js: lib/schema.ts and lib/db.ts
  • TanStack Start: src/lib/schema.ts and src/lib/db.ts

Every framework also gets drizzle.config.ts, the PostgreSQL driver, and these scripts:

npm run db:generate
npm run db:migrate
npm run db:studio

Set DATABASE_URL before running migrations.

With Express JWT auth, users are stored through Drizzle. With Better Auth in Next.js or TanStack Start, the schema contains the four Better Auth tables, and the schema is passed to the Drizzle client so the adapter can find them.

Mongoose and MongoDB

Set MONGODB_URI in .env. The module adds a connection helper and a sample User model: src/lib/db.ts and src/models/User.ts in Express and TanStack Start, or lib/db.ts and models/User.ts in Next.js.

In Express, the server connects to MongoDB before it starts listening, and JWT auth uses the Mongoose User model. The model is registered in a way that survives development reloads.

In Next.js and TanStack Start, Better Auth uses the native MongoDB adapter and its own collections, configured in lib/auth.ts (Next.js) or src/lib/auth.ts (TanStack Start). The Mongoose helper is still there for your own models. Call connectDB() in server code before you query them. It reuses one connection and throws an error if it can't connect, instead of exiting the process.

Transactions and first sign-up (Next.js and TanStack Start)

Better Auth in Next.js and TanStack Start uses MongoDB transactions, which need a replica set or MongoDB Atlas. With Docker selected, the Compose db service already runs MongoDB as a replica set (see Docker). Create the auth collections before you start the server. Otherwise, the first sign-up can fail because MongoDB tries to create indexes inside the transaction:

// Run once in mongosh, in your application's database.
db.createCollection("user");
db.createCollection("session");
db.createCollection("account");
db.createCollection("verification");

If you run a standalone MongoDB server locally, you can turn transactions off in lib/auth.ts (Next.js) or src/lib/auth.ts (TanStack Start) with mongodbAdapter(db, { client, transaction: false }). See the MongoDB adapter docs. Writes are then not atomic, so keep transactions on in production.

Keep database credentials out of source control.

On this page