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
# Prisma
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
DATABASE_URL="file:./db.sqlite"
DATABASE_PROVIDER="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 the datasource
block and updating the connection string in the environment variables.
Connecting to a Remote Database
To connect to a remote database, follow the respective guides for:
- PlanetScale/MySQL/PostgreSQL (opens in a new tab)
- Supabase (opens in a new tab)
- MongoDB (opens in a new tab)
Make sure to update the environment variables in NextSaaS accordingly.
# Prisma
# https://www.prisma.io/docs/reference/database-reference/connection-urls#env
DATABASE_URL="{database_connection_url}"
DATABASE_PROVIDER="{database_provider}"
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
Resource | Link |
---|---|
Prisma Documentation | https://www.prisma.io/docs/ (opens in a new tab) |
Prisma GitHub Repository | https://github.com/prisma/prisma (opens in a new tab) |
Prisma Migrate Playground | https://playground.prisma.io/guides (opens in a new tab) |
NextAuth.js Prisma Adapter | https://next-auth.js.org/adapters/prisma (opens in a new tab) |
Planetscale Connection Guide | https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/relational-databases/connect-your-database-typescript-planetscale (opens in a new tab) |