docs
Setup Remote Database

Here's the enhanced version of the "Database" markdown document:

Database

💡

NextSaaS utilizes Prisma as its ORM. We've seamlessly integrated it with NextAuth and Stripe.

Setting Up the Database

Default Database Configuration

.env
# Prisma
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
DATABASE_URL="file:./db.sqlite"

The default database is an SQLite database, suitable for development and quick proof-of-concept setups. However, it's not recommended for production. You can switch to PostgreSQL or MySQL by changing the provider in prisma/schema.prisma and updating the connection string in the environment variables.

prisma/schema.prisma
// Configure database, see below:
// https://next-auth.js.org/adapters/prisma#create-the-prisma-schema
datasource db {
    provider = "sqlite"
    url      = env("DATABASE_URL")
 
    // This emulates relations in the Prisma client.
    // If you use MySQL providers such as PlanetScale, you will need to uncomment this line.
    // relationMode = "prisma"
}

Connecting to a Remote Database

To connect to a remote database, follow the respective guides for:

Make sure to update the environment variables in NextSaaS accordingly.

.env
# Prisma
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
# If you use Supabase, you might need to change the port number from 6543 to 5432 in the URL
# See the discussion here - https://github.com/prisma/prisma/discussions/24412
DATABASE_URL="{remote_database_connection_url}"
 

Creating tables is as simple as running npx prisma db push.

Exploring Prisma (Optional)

Prisma Client

The Prisma Client, located at src/lib/db.ts, is globally instantiated and exported for use in your API routes. We recommend using this instance instead of importing it separately in each file.

Schema

The Prisma schema file (/prisma/schema.prisma) defines your database schema and models. It's used to generate the Prisma Client.

Integration with NextAuth.js

NextSaaS integrates NextAuth with Prisma. The schema file is preconfigured with recommended values for models like User, Session, Account, and VerificationToken, as per NextAuth.js documentation (opens in a new tab).

Seeding Your Database (Optional)

Seeding your database (opens in a new tab) with test data can help you kickstart development. To set up seeding, create a seed.ts file in the /prisma directory and add a seed script to your package.json. You'll need a TypeScript runner like tsx to execute the seed script.

{
  "scripts": {
    "db-seed": "NODE_ENV=development prisma db seed"
  },
  "prisma": {
    "seed": "tsx prisma/seed.ts"
  }
}
import { db } from "../src/server/db";

async function main() {
  // Example seeding
  const id = "cl9ebqhxk00003b600tymydho";
  await db.example.upsert({
    where: {
      id,
    },
    create: {
      id,
    },
    update: {},
  });
}

main()
  .then(async () => {
    await db.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await db.$disconnect();
    process.exit(1);
  });

Run pnpm db-seed (or npm/yarn) to seed your database.

Useful Resources

ResourceLink
Prisma Documentationhttps://www.prisma.io/docs/ (opens in a new tab)
Prisma GitHub Repositoryhttps://github.com/prisma/prisma (opens in a new tab)
Prisma Migrate Playgroundhttps://playground.prisma.io/guides (opens in a new tab)
NextAuth.js Prisma Adapterhttps://next-auth.js.org/adapters/prisma (opens in a new tab)
Planetscale Connection Guidehttps://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/relational-databases/connect-your-database-typescript-planetscale (opens in a new tab)