LogoStarterkitpro
Features/Databases

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.

.env.local
# Example for MongoDB Atlas
DATABASE_URL="mongodb+srv://<user>:<password>@<cluster-url>/<database-name>?retryWrites=true&w=majority"
 
# Example for a local MongoDB instance
# DATABASE_URL="mongodb://localhost:27017/myapp"
  • 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:

package.json (Relevant Scripts)
{
  "scripts": {
    // ... other scripts
    "db:studio": "prisma studio",
    "db:push": "prisma db push",
    "db:generate": "prisma generate",
    "db:pull:schema": "prisma db pull",
    "db:seed": "prisma db seed"
    // ... other scripts
  }
}

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 your schema.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 to db 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 by DATABASE_URL and updates your schema.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 in package.json under the prisma.seed key, typically prisma/seed.ts). Populates the database with initial data. See the Database Seeding documentation for setup details.

Typical Workflow

  1. Define Models: Modify your prisma/schema.prisma file.
  2. Sync Schema: Run npm run db:push to update your MongoDB schema.
  3. (Optional) Seed Data: Run npm run db:seed to populate with initial data.
  4. Develop: Start your application (npm run dev).
  5. (Optional) Inspect Data: Use npm run db:studio to view/edit data directly.

On this page