0Pricing
Next.js 15 Fullstack (App Router + Server Actions) · Lektion

Prisma ORM einrichten

Konfigurieren und integrieren Sie Prisma ORM in Ihr Next.js-Projekt, um mit verschiedenen Datenbanken zu interagieren.

Prisma ORM einrichten ist eine kostenlose Next.js 15 Fullstack (App Router + Server Actions)-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Next.js 15 Fullstack (App Router + Server Actions)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What is an ORM?

Welcome! In this lesson, we'll set up Prisma ORM in a Next.js project. But first, what's an ORM?

  • ORM stands for Object-Relational Mapping.
  • It's a technique that lets you interact with your database using your programming language's objects instead of writing raw SQL.
  • Think of it as a translator between your code (e.g., JavaScript objects) and your database (e.g., SQL tables).

Why Choose Prisma?

Prisma is a modern ORM that's popular with Next.js for good reasons:

  • Type Safety: It generates a type-safe client, preventing many runtime errors.
  • Developer Experience: Intuitive API, powerful migrations, and automatic code completion.
  • Modern Stack: Designed for TypeScript and modern JavaScript, fitting perfectly with Next.js.
  • Database Support: Works with PostgreSQL, MySQL, SQLite, SQL Server, and MongoDB.

Installing Prisma

Let's start by adding Prisma to your Next.js project. You'll need two main packages:

  • prisma: The Prisma CLI (Command Line Interface) for development tasks.
  • @prisma/client: The Prisma Client library for interacting with your database in code.

Run this command in your project's root:

npm install prisma @prisma/client

Initialize Prisma

After installation, you need to initialize Prisma in your project. This command sets up the basic Prisma structure, including the prisma/schema.prisma file.

It also creates a .env file to hold your database connection string.

npx prisma init

Connect Your Database

The .env file created by prisma init contains a DATABASE_URL variable. This is where you tell Prisma how to connect to your database.

For local development, SQLite is often used for simplicity. For production, you might use PostgreSQL or MySQL.

# .env
DATABASE_URL="postgresql://user:password@host:port/database?schema=public"

# Example for SQLite (local file)
# DATABASE_URL="file:./dev.db"

The Prisma Schema File

Your prisma/schema.prisma file is the heart of your Prisma setup. It defines three key blocks:

  • datasource: Specifies your database provider (e.g., postgresql, sqlite).
  • generator: Configures the Prisma Client (usually prisma-client-js).
  • model: Defines your database tables (models) and their fields.

Defining Your First Model

Let's define a simple User model in your prisma/schema.prisma file. This model will represent a table in your database.

  • id: Unique identifier, auto-incrementing.
  • email: User's email, must be unique.
  • name: User's name, optional.
// prisma/schema.prisma

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

Generating Prisma Client

After defining or changing your models in schema.prisma, you must generate the Prisma Client. This command reads your schema and creates the type-safe client library.

This client is what you'll import and use in your Next.js application to interact with your database.

npx prisma generate

Applying Migrations

To create or update your database tables based on your schema.prisma, you use migrations. The migrate dev command:

  • Compares your schema to the database.
  • Generates SQL migration files.
  • Applies these migrations to your database.

It also generates the Prisma Client automatically after migration.

npx prisma migrate dev --name init

Using Prisma Client

Once Prisma is set up and generated, you can use the PrismaClient in your Next.js server components or API routes to perform database operations. Here's a basic example:

import { PrismaClient } from '@prisma/client';

async function main() {
  const prisma = new PrismaClient();
  console.log("Prisma Client successfully initialized!");

  // In a real app, you'd perform database operations:
  // const users = await prisma.user.findMany();
  // console.log(users);

  await prisma.$disconnect();
  console.log("Prisma Client disconnected.");
}

main().catch(console.error);

Quick Check: Prisma Commands

You've learned about several key Prisma CLI commands. Which command is used to generate the type-safe Prisma Client after you've modified your schema.prisma file?

Recap: Setting Up Prisma

Great job! You've learned the essential steps to integrate Prisma ORM into your Next.js project:

  • Installed Prisma CLI and client.
  • Initialized Prisma and configured your database URL.
  • Defined data models in schema.prisma.
  • Generated the Prisma Client.
  • Applied migrations to sync your schema with the database.
  • Understood how to instantiate and use PrismaClient.

Next, we'll dive into performing CRUD operations with Prisma!

Häufig gestellte Fragen

Ist die Lektion „Prisma ORM einrichten“ kostenlos?

Ja — der vollständige Text von „Prisma ORM einrichten“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Next.js 15 Fullstack (App Router + Server Actions)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Next.js 15 Fullstack (App Router + Server Actions)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Prisma ORM einrichten“?

Konfigurieren und integrieren Sie Prisma ORM in Ihr Next.js-Projekt, um mit verschiedenen Datenbanken zu interagieren. Du übst Next.js 15 Fullstack (App Router + Server Actions) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Next.js 15 Fullstack (App Router + Server Actions) zu starten?

Keine Vorkenntnisse erforderlich. Next.js 15 Fullstack (App Router + Server Actions) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Prisma ORM einrichten“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Next.js 15 Fullstack (App Router + Server Actions)-Lektion Code schreiben und ausführen?

Ja. Jede Next.js 15 Fullstack (App Router + Server Actions)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Prisma ORM einrichten
  2. CRUD mit Server Actions
  3. Migrationen von Datenbankschemas
  4. Datenbank-Seeding und Connection Pooling
← Zurück zu Next.js 15 Fullstack (App Router + Server Actions)