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

Datenbank-Seeding und Connection Pooling

Befüllen Sie Ihre Datenbank mit zuverlässigen Seed-Daten und konfigurieren Sie das Prisma Connection Pooling, damit Ihre Next.js-App auch unter Last schnell und stabil bleibt

Lektion 4 von 413 Schritte

Datenbank-Seeding und Connection Pooling ist eine kostenlose Next.js 15 Fullstack (App Router + Server Actions)-Lektion auf CoddyKit. Dies ist Lektion 4 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.

Why Seed a Database?

Seeding fills your database with predictable starter data: admin users, demo records, lookup tables. It lets every developer and every CI run start from the same known state.

Without seeds, manual data entry makes tests flaky and onboarding slow.

The Prisma Seed Script

Prisma runs a seed file you point it at. Create prisma/seed.ts and register it in package.json under the prisma.seed key so prisma db seed knows what to execute.

{
  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  }
}

Writing Seed Logic

Inside the seed file you instantiate the client and create records. Wrap it in an async function and disconnect when finished.

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function main() {
  await prisma.user.create({
    data: { email: 'admin@app.com', name: 'Admin' }
  });
}

main().finally(() => prisma.$disconnect());

Idempotent Seeds with upsert

Running a seed twice should not create duplicates. Use upsert so a record is created if missing and updated if it already exists.

await prisma.user.upsert({
  where: { email: 'admin@app.com' },
  update: {},
  create: { email: 'admin@app.com', name: 'Admin' }
});

Bulk Seeding

To insert many rows efficiently, use createMany. The skipDuplicates option avoids errors on unique conflicts.

await prisma.tag.createMany({
  data: [
    { name: 'news' },
    { name: 'sports' },
    { name: 'tech' }
  ],
  skipDuplicates: true
});

Running the Seed

Trigger the script from the CLI. Prisma also runs it automatically after prisma migrate reset, giving you a clean reseeded database.

npx prisma db seed

The Connection Problem in Serverless

Next.js on Vercel runs serverless functions. Each invocation can open a fresh DB connection, and under load you quickly exhaust the database connection limit.

Connection pooling solves this by reusing a fixed set of connections.

Reusing the Prisma Client

In development, hot reload can spawn many clients. Store a single instance on globalThis so only one client exists per process.

import { PrismaClient } from '@prisma/client';
const g = globalThis as unknown as { prisma?: PrismaClient };
export const prisma = g.prisma ?? new PrismaClient();
if (process.env.NODE_ENV !== 'production') g.prisma = prisma;

Pooling with a Connection URL

Use a pooler such as PgBouncer or Prisma Accelerate. Point your DATABASE_URL at the pooled endpoint and add ?pgbouncer=true so Prisma disables prepared statements that PgBouncer cannot share.

DATABASE_URL="postgresql://user:pass@host:6543/db?pgbouncer=true&connection_limit=1"

Direct URL for Migrations

Pooled connections cannot run migrations. Add a directUrl in your schema that bypasses the pooler so prisma migrate works.

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

Best Practices

Keep your data layer healthy:

  • Make seeds idempotent with upsert
  • Reuse a single client instance
  • Use a pooler in serverless environments
  • Keep a separate direct URL for migrations

Quick Check

Test what you learned about pooling.

Recap

You learned to seed and pool your database:

  • Configure prisma.seed and write idempotent seeds with upsert
  • Bulk insert with createMany
  • Reuse a single Prisma client to avoid leaks
  • Use a pooled DATABASE_URL plus a directUrl for migrations

Now your database starts in a known state and scales safely.

Kostenlos starten

Lerne TypeScript mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
22
Lektionen
88

Häufig gestellte Fragen

Ist die Lektion „Datenbank-Seeding und Connection Pooling“ kostenlos?

Ja — der vollständige Text von „Datenbank-Seeding und Connection Pooling“ 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 „Datenbank-Seeding und Connection Pooling“?

Befüllen Sie Ihre Datenbank mit zuverlässigen Seed-Daten und konfigurieren Sie das Prisma Connection Pooling, damit Ihre Next.js-App auch unter Last schnell und stabil bleibt 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 4 von 4.

Wie lange dauert die Lektion „Datenbank-Seeding und Connection Pooling“?

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)