Database

Database

TLDR; Create a new PostgreSQL database on Supabase and add database credentials to your .env.local file as suggested in .env.local.example.

Connecting Your Codebase to a Database

CodebaseUp leverages the power of Prisma ORM, a Node.js and TypeScript ORM, to provide an intuitive and efficient way to work with databases. This documentation will guide you through the process of connecting your CodebaseUp project to a database of your choice.

Database Flexibility

Prisma seamlessly integrates with various databases and service providers. While CodebaseUp initially selects PostgreSQL, you have the flexibility to switch to MySQL, SQLite, MongoDB, and more, depending on your project's requirements.

You can create a database in the cloud using services like Supabase, Neon, Vercel Postgres, and others. Connecting it to Prisma is as simple as configuring the following environment variables:

POSTGRES_DATABASE_URL= // Pooled connection string (URI)
POSTGRES_DIRECT_URL= // Main connection string (URI)
POSTGRES_USER=
POSTGRES_HOST=
POSTGRES_PASSWORD=
POSTGRES_DATABASE=

Prisma Schema: Human-Readable Data Modeling

The Prisma schema allows you to declare your database tables in a human-readable way, making data modeling a delightful experience. You can define your data models manually or introspect them from an existing database. Prisma serves as a server-side library that empowers developers to read and write data to the database efficiently and safely.

You'll find the Prisma schema in packages/database/prisma/schema.prisma:

generator client {
  provider = "prisma-client-js"
}
 
datasource db {
  provider = "postgresql"
  url = env("POSTGRES_DATABASE_URL") // uses connection pooling
  directUrl = env("POSTGRES_DIRECT_URL") // uses a direct connection
}
 
model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
}
 
...

By default, the schema comes with base models allowing you to immediately use authentication (e.g. Google OAuth 2.0, magic link login, etc.).

Prisma Client: Type-Safe Database Queries

Prisma Client is a query builder tailored to your schema. With auto-completion, you can easily construct queries without the need for extensive documentation. Remember that Prisma can only be used server-side.

// Find a user by email
const userByEmail = await prisma.user.findUnique({
  where: {
    email: "john@doe.com",
  },
});

Prisma Migrate: Hassle-Free Migrations

Prisma Migrate simplifies the migration process by auto-generating SQL migrations from your Prisma schema. These migration files are fully customizable, providing you with complete control and flexibility, from local development to production environments.

The initial migrations are included in the codebase. You can find them at packages/database/prisma/migrations.

Every time you make some changes to the Prisma schema, you create new migrations by running:

pnpm run db:migrate:dev

It'll create migration files on your localhost and will synchronize it with your cloud database. These migration files are meant to be included in your version control system (usually Git).

Further reading

Supabase - Neon - Vercel Postgres

Prisma