Database
Simple database using Prisma.
Table of Contents
Overview
The Database Plugin helps you set up and manage your database with ease. It's built on Prisma, making it simple to work with your database schema and queries.
With just a few commands, you can add tables, update schemas, and interact with your data.
If you're using FastStartup, this plugin saves you time and lets you focus on building your product.
Key Features
- Quick setup: Set up your database with a few simple commands.
- Prisma-powered: Use Prisma for database management and migrations.
- Extendable schema: Add or update tables in the
schema.prisma
file. - Easy import: Access your database by importing the plugin anywhere in your app.
- Prisma Studio: Run a user-friendly UI for your database.
Use Cases
The Database Plugin is perfect for:
- SaaS products needing a structured database.
- Adding user accounts, subscriptions, or custom data.
- Managing dynamic tables as your app grows.
It works out of the box. You won't have to configure Prisma manually.
Getting Started
Follow these steps to set up the Database Plugin:
1. Configure Environment Variable
Set your database URL in your .env
file:
DATABASE_URL="your_database_connection_url"
You can check .env.example
for a sample setup.
2. Run Migrations and Generate Prisma Client
In your terminal, run these commands:
npm run prisma:migrate
npm run prisma:generate
This will apply migrations and generate the Prisma client for your project.
3. Open Prisma Studio (Optional)
If you want a visual interface to manage your data, run:
npm run prisma:studio
Prisma Studio lets you view and edit your tables in the browser.
4. Import Prisma and Use the Database
To use the database, just import Prisma anywhere in your code:
import { prisma } from "@/plugins/database";
// Example query: Fetch all users
const users = await prisma.user.findMany();
5. Update or Add Tables
Edit your schema.prisma
file to add or update tables. For example:
model User {
id Int @id @default(autoincrement())
name String
email String @unique
}
Run npm run prisma:migrate
after making changes to apply updates to your database.
API Overview
You can use Prisma's full API to interact with your database.
Example Queries
Find all users:
const users = await prisma.user.findMany();
Create a new user:
await prisma.user.create({
data: { name: "John Doe", email: "john@example.com" },
});
Update a user:
await prisma.user.update({
where: { id: 1 },
data: { name: "Jane Doe" },
});
Delete a user:
await prisma.user.delete({ where: { id: 1 } });
Comparison to Alternatives
Raw SQL
Using raw SQL queries requires manual setup and custom code for migrations. It takes more time and effort.
With the Database Plugin, Prisma handles migrations, schema management, and queries for you.
Other ORMs
Compared to other ORMs, Prisma is more developer-friendly. It has type safety, a great API, and Prisma Studio for managing data visually.
The Database Plugin makes it even easier by setting up Prisma for you in FastStartup.
FAQ
Q: How do I add a new table?
A: Update the schema.prisma
file with the new model. Then run:
npm run prisma:migrate
Q: Can I use Prisma Studio in production?
A: No. Prisma Studio is for development only. Never expose it in production.
Q: Where do I find my database schema?
A: The schema is located in the schema.prisma
file.
Q: How do I change the database connection?
A: Update the DATABASE_URL
in your .env
file.
The Database Plugin makes database management simple. You can focus on building features, not configuring your database.
Build, Launch, and Grow Your SaaS
Get everything you need to launch your SaaS and land your first paying user—starting today.