Next.js 15 Fullstack (App Router + Server Actions) · บทเรียน

การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ

เติมฐานข้อมูลด้วยข้อมูลตั้งต้นที่เชื่อถือได้ และกำหนดค่าการใช้กลุ่มการเชื่อมต่อของ Prisma เพื่อให้แอป Next.js รวดเร็วและเสถียรภายใต้ภาระงาน

บทเรียน 4 จาก 413 ขั้นตอน

การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ เป็นบทเรียน Next.js 15 Fullstack (App Router + Server Actions) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Next.js 15 Fullstack (App Router + Server Actions) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Next.js 15 Fullstack (App Router + Server Actions) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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.

เริ่มต้นได้ฟรี

เรียนรู้ TypeScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
22
บทเรียน
88

คำถามที่พบบ่อย

บทเรียน “การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Next.js 15 Fullstack (App Router + Server Actions) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Next.js 15 Fullstack (App Router + Server Actions) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ”

เติมฐานข้อมูลด้วยข้อมูลตั้งต้นที่เชื่อถือได้ และกำหนดค่าการใช้กลุ่มการเชื่อมต่อของ Prisma เพื่อให้แอป Next.js รวดเร็วและเสถียรภายใต้ภาระงาน คุณปฏิบัติ Next.js 15 Fullstack (App Router + Server Actions) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Next.js 15 Fullstack (App Router + Server Actions) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Next.js 15 Fullstack (App Router + Server Actions) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Next.js 15 Fullstack (App Router + Server Actions) นี้ได้ไหม

ได้ บทเรียน Next.js 15 Fullstack (App Router + Server Actions) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตั้งค่า Prisma ORM
  2. CRUD ด้วย Server Actions
  3. การย้ายแบบแผนฐานข้อมูล
  4. การเติมข้อมูลฐานข้อมูลและการใช้กลุ่มการเชื่อมต่อ
← กลับไปที่ Next.js 15 Fullstack (App Router + Server Actions)