<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mostary Jahan</title>
    <description>The latest articles on Forem by Mostary Jahan (@mostary).</description>
    <link>https://forem.com/mostary</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3059431%2F2cdc8b9d-c194-4f83-b532-3f51b0fe0e37.jpg</url>
      <title>Forem: Mostary Jahan</title>
      <link>https://forem.com/mostary</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mostary"/>
    <language>en</language>
    <item>
      <title>A Complete Beginner’s Guide to Prisma</title>
      <dc:creator>Mostary Jahan</dc:creator>
      <pubDate>Sun, 28 Sep 2025 07:27:10 +0000</pubDate>
      <link>https://forem.com/mostary/a-complete-beginners-guide-to-prisma-je3</link>
      <guid>https://forem.com/mostary/a-complete-beginners-guide-to-prisma-je3</guid>
      <description>&lt;p&gt;Working with databases is one of the most essential yet challenging parts of backend development. Whether you’re building a REST API, a GraphQL server, or a full-stack application, you’ll need a way to query and manage your data efficiently. Traditional ORMs often add complexity with heavy abstractions, while raw SQL can feel verbose and error-prone.&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;Prisma&lt;/strong&gt; comes in. Prisma is a next-generation ORM that focuses on developer productivity, type safety, and ease of use. Let’s dive into what makes Prisma a great choice and how you can get started.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Developers Choose Prisma
&lt;/h2&gt;

&lt;p&gt;Prisma stands out from traditional ORMs because of its developer-first approach. Here are some of its key strengths:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Type-Safe Queries: Prisma automatically generates a type-safe client based on your schema. This means fewer runtime errors and instant feedback in your editor.&lt;/li&gt;
&lt;li&gt;Schema-First Approach: You define your data models in &lt;code&gt;schema.prisma&lt;/code&gt;, which is clean, human-readable, and acts as the single source of truth.&lt;/li&gt;
&lt;li&gt;Cross-Database Support: It supports PostgreSQL, MySQL, SQLite, SQL Server, CockroachDB, and MongoDB. Switching databases is smooth since Prisma Client handles most differences.&lt;/li&gt;
&lt;li&gt;Auto-Generated Client: Instead of writing complex queries, you get an intuitive API to interact with your data.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Prisma&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Setting up Prisma is straightforward, especially in a Node.js project. Here’s a step-by-step guide:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Prisma
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install prisma --save-dev
npx prisma init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command initializes Prisma and creates a new &lt;code&gt;prisma/schema.prisma&lt;/code&gt; file along with a &lt;code&gt;.env&lt;/code&gt; file for your database connection string.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Define Your Data Models&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In &lt;code&gt;schema.prisma&lt;/code&gt;, you define your models:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id    Int     @id @default(autoincrement())
  name  String
  email String  @unique
  posts Post[]
}

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  content  String?
  author   User   @relation(fields: [authorId], references: [id])
  authorId Int
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Run Migrations
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx prisma migrate dev --name init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command creates your database tables based on the schema.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use Prisma Client&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now you can query your database with ease:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

async function main() {
  // Create a new user
  const user = await prisma.user.create({
    data: {
      name: "Alice",
      email: "alice@example.com",
      posts: {
        create: { title: "My first post" },
      },
    },
  })

  // Fetch all users
  const users = await prisma.user.findMany({
    include: { posts: true },
  })

  console.log(users)
}

main()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Prisma in Production&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prisma isn’t just for small projects — it’s production-ready. Some powerful features include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prisma Migrate: Safely evolve your schema with migration history.&lt;/li&gt;
&lt;li&gt;Prisma Studio: A modern GUI to view and edit your database data.&lt;/li&gt;
&lt;li&gt;Performance: Queries are optimized and run efficiently across supported databases.&lt;/li&gt;
&lt;li&gt;Flexibility: When Prisma falls short (e.g., very complex joins), you can still write raw SQL queries alongside Prisma Client.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Best Practices with Prisma&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keep your &lt;code&gt;schema.prisma&lt;/code&gt; organized — treat it as the single source of truth.&lt;/li&gt;
&lt;li&gt;Use relations effectively to model real-world entities.&lt;/li&gt;
&lt;li&gt;Regularly run migrations and keep them versioned in your repo.&lt;/li&gt;
&lt;li&gt;Use environment variables (&lt;code&gt;.env&lt;/code&gt;) to manage database URLs securely.&lt;/li&gt;
&lt;li&gt;For performance-critical queries, don’t hesitate to mix in raw SQL.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prisma brings the best of both worlds: the simplicity of an ORM and the control of writing queries directly. It speeds up development, reduces bugs, and keeps your codebase clean.&lt;/p&gt;

&lt;p&gt;While it may not completely replace raw SQL in every scenario, Prisma is an excellent tool for most modern applications. If you’re building with Node.js and want a smoother database experience, Prisma is worth exploring.&lt;/p&gt;

</description>
      <category>prisma</category>
      <category>webdev</category>
      <category>node</category>
      <category>database</category>
    </item>
  </channel>
</rss>
