Veritabanı Tohumlama ve Bağlantı Havuzlama
Veritabanınızı güvenilir başlangıç verileriyle doldurun ve Next.js uygulamanızın yük altında hızlı ve kararlı kalması için Prisma bağlantı havuzlamasını yapılandırın.
Veritabanı Tohumlama ve Bağlantı Havuzlama, CoddyKit'te ücretsiz bir Next.js 15 Fullstack (App Router + Server Actions) dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Next.js 15 Fullstack (App Router + Server Actions) öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Next.js 15 Fullstack (App Router + Server Actions) kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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 seedThe 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.seedand write idempotent seeds withupsert - Bulk insert with
createMany - Reuse a single Prisma client to avoid leaks
- Use a pooled
DATABASE_URLplus adirectUrlfor migrations
Now your database starts in a known state and scales safely.
Sıkça Sorulan Sorular
“Veritabanı Tohumlama ve Bağlantı Havuzlama” dersi ücretsiz mi?
Evet — “Veritabanı Tohumlama ve Bağlantı Havuzlama” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Next.js 15 Fullstack (App Router + Server Actions) kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Next.js 15 Fullstack (App Router + Server Actions) kursu toplamda 4 dersten oluşur.
“Veritabanı Tohumlama ve Bağlantı Havuzlama” dersinde ne öğreneceğim?
Veritabanınızı güvenilir başlangıç verileriyle doldurun ve Next.js uygulamanızın yük altında hızlı ve kararlı kalması için Prisma bağlantı havuzlamasını yapılandırın. Next.js 15 Fullstack (App Router + Server Actions) ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Next.js 15 Fullstack (App Router + Server Actions) öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Next.js 15 Fullstack (App Router + Server Actions), başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.
“Veritabanı Tohumlama ve Bağlantı Havuzlama” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Next.js 15 Fullstack (App Router + Server Actions) dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Next.js 15 Fullstack (App Router + Server Actions) dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Prisma ORM'yi Kurma
- Sunucu Eylemleriyle CRUD
- Veritabanı Şeması Göçleri
- Veritabanı Tohumlama ve Bağlantı Havuzlama