MongoDB
Configuring and managing the default MongoDB database with Prisma.
StarterKitPro comes pre-configured with MongoDB as the default database, managed via the Prisma ORM.
1. Configuration (.env.local
)
The crucial step is setting your MongoDB connection string in the .env
file located at the root of your project.
- Replace
<user>
,<password>
,<cluster-url>
, and<database-name>
with your actual MongoDB Atlas credentials and database name. - If using a local MongoDB instance, adjust the URL accordingly.
Prisma uses this DATABASE_URL
to connect to your database for schema management, migrations (pushing), and data operations.
2. Database Management Scripts (package.json
)
StarterKitPro includes several helpful scripts in package.json
for managing your MongoDB database via Prisma:
Script Explanations:
-
npm run db:studio
: Opens Prisma Studio, a GUI tool to view and manipulate data in your database. Great for development and debugging. -
npm run db:push
: Pushes the current state of your Prisma schema (schema.prisma
) to the database. It updates the database schema to match your models without creating migration files. This is typically used during development with MongoDB.MongoDB & Migrations
MongoDB with Prisma primarily uses
db push
for schema synchronization during development. Traditional SQL-style migration files (prisma migrate dev
) are less common with MongoDB's flexible schema nature, though possible. -
npm run db:generate
: Generates the Prisma Client based on your schema. This needs to be run after any changes to yourschema.prisma
file to update the TypeScript types and database access methods available in your application. -
npm run db:reset
: Use with caution! This command drops the database, reapplies the schema (similar todb push
), and optionally runs the seed script. It's useful for completely resetting your development database. -
npm run db:pull:schema
: Introspects the existing database defined byDATABASE_URL
and updates yourschema.prisma
file to match the database's current schema. Useful if the database schema was changed externally. -
npm run db:seed
: Executes the database seeding script (configured inpackage.json
under theprisma.seed
key, typicallyprisma/seed.ts
). Populates the database with initial data. See the Database Seeding documentation for setup details.
Typical Workflow
- Define Models: Modify your
prisma/schema.prisma
file. - Sync Schema: Run
npm run db:push
to update your MongoDB schema. - (Optional) Seed Data: Run
npm run db:seed
to populate with initial data. - Develop: Start your application (
npm run dev
). - (Optional) Inspect Data: Use
npm run db:studio
to view/edit data directly.