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

Prisma ORM'yi Kurma

Çeşitli veritabanlarıyla etkileşim kurmak için Prisma ORM'yi Next.js projenize yapılandırıp entegre edin.

Prisma ORM'yi Kurma, CoddyKit'te ücretsiz bir Next.js 15 Fullstack (App Router + Server Actions) dersidir. Bu, 4 dersinin 1. 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.

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!

Sıkça Sorulan Sorular

“Prisma ORM'yi Kurma” dersi ücretsiz mi?

Evet — “Prisma ORM'yi Kurma” 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.

“Prisma ORM'yi Kurma” dersinde ne öğreneceğim?

Çeşitli veritabanlarıyla etkileşim kurmak için Prisma ORM'yi Next.js projenize yapılandırıp entegre edin. 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 1. dersidir.

“Prisma ORM'yi Kurma” 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

  1. Prisma ORM'yi Kurma
  2. Sunucu Eylemleriyle CRUD
  3. Veritabanı Şeması Göçleri
  4. Veritabanı Tohumlama ve Bağlantı Havuzlama
← Next.js 15 Fullstack (App Router + Server Actions) Sayfasına Dön