Hey everyone, welcome to today’s lesson! Imagine you’re building a blogging app—something real, something you’d actually use. You’ve got users reading posts, filtering them by tags or dates, and an admin dashboard to add or tweak posts. Sounds cool, right? But here’s the catch: managing all that data with raw SQL queries can get messy fast. What if there was a way to talk to your database using clean, simple JavaScript or TypeScript instead? That’s where Object-Relational Mapping (ORM) systems come in—they’re like a translator between your code and your database. Today, we’re diving into Prisma, a modern ORM that’s going to make your life as a full-stack developer so much easier. By the end, you’ll see how Prisma can power our blogging app with less headache and more fun!
An ORM, or Object-Relational Mapping, is a tool that lets you interact with your database using objects and code you already know, instead of writing complex SQL. Think of it like this: instead of manually telling your database, “Hey, grab all posts from January,” you just write something like the following:
prisma.post.findMany({
where: {
date: { gt: '2025-01-01' }
}
})
It’s intuitive and saves time! Prisma stands out because it’s modern, TypeScript-friendly, and gives you a clean way to define your database structure (called a schema). For our blogging app, we’ll use Prisma to manage posts, tags, and admin actions. Let’s set it up step-by-step and see it in action!
SELECT * FROM posts WHERE id = 1;
. With Prisma, it’s just prisma.post.findUnique({ where: { id: 1 } })
. Easy, right?prisma.post.findMany({ where: { tags: { some: { name: "tech" } }, date: { gte: new Date("2025-01-01") } } })
. It’s still readable!Post
and Tag
), and Prisma generates a client to query them.Let’s get Prisma running for our app. We’ll assume we’re using a PostgreSQL database (though Prisma works with others too). Here’s how we set it up:
npm install prisma --save-dev
and npm install @prisma/client
.npx prisma init
. This creates a prisma
folder with a schema.prisma
file and a .env
file for your database connection.Define the Schema: Open schema.prisma
and define our blogging app’s data:
model Post {
id Int @id @default(autoincrement())
title String
content String
date DateTime
tags Tag[] @relation("PostTags")
}
model Tag {
id Int @id @default(autoincrement())
name String @unique
posts Post[] @relation("PostTags")
}
This sets up posts with titles, content, dates, and a many-to-many relationship with tags.
.env
, add your database URL, like DATABASE_URL="postgresql://user:password@localhost:5432/blog"
.npx prisma db push
to sync your schema with the database, then npx prisma generate
to create the Prisma client.PrismaClient
to query them.Now, let’s use Prisma in our blogging app’s backend (say, a Node.js server with TypeScript). First, create a Prisma client:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
Simple Query: Get a post by ID for the client side:
const post = await prisma.post.findUnique({ where: { id: 1 } });
This fetches a post to display when a user clicks it.
Advanced Query: Filter posts for the “Latest Tech” route:
const techPosts = await prisma.post.findMany({
where: {
tags: { some: { name: "tech" } },
date: { gte: new Date("2025-01-01") },
},
include: { tags: true },
});
This grabs all 2025 tech posts with their tags—perfect for our filter page!
Admin Action: Add a new post:
const newPost = await prisma.post.create({
data: {
title: "My First Post",
content: "Hello, world!",
date: new Date(),
tags: { connect: [{ name: "intro" }] },
},
});
Admins can use this to publish posts with existing tags.
findMany
for lists (like filtered routes), findUnique
for single items, and create
/update
for admin tasks.Let’s tie it all into our app. On the client side, users visit /posts/tech
and see tech posts from 2025. On the admin side, you’ve got a form to add posts. Prisma powers both:
create
to add posts and update
to edit them (e.g., prisma.post.update({ where: { id: 1 }, data: { title: "Updated Title" } })
).This setup keeps our app fast, organized, and fun to build!
Alright, team, let’s wrap it up! Today, we’ve learned how Prisma turns database chaos into coding bliss for our blogging app. Key points:
Best Practices: Keep your schema simple at first, use include
for relationships, and test queries in the Prisma Studio (npx prisma studio
). Pitfalls to Avoid: Don’t overcomplicate queries early on—start basic and build up. Prisma’s your new best friend—let’s make this blogging app shine!
Let's talk about how you can simplify access to your database using ORM systems, such as Prisma!
Hey everyone! Let's talk ORM and why is it handy to use them!
Databases can be tricky with raw SQL, right? ORMs let us use JavaScript or TypeScript to talk to them instead. It’s like having a superpower for our blogging app—less mess, more fun!
Prisma’s a modern ORM that’s perfect for our app. It’s type-safe, easy to set up, and makes managing app database access a breeze. Let’s dive in!
We continue building a blogging app where users filter posts by tags or dates, and admins add or edit them. Prisma’s going to make it happen smoothly!
So, how can ORM help us write queries?
An ORM maps database tables to objects in your code.
Instead of SQL like this ...
we write a type safe code like this! Simple and clean!
With SQL, you’d fetch a post with this query.
Prisma does it with less fuss, more focus!
For our blogging app, this means we can filter posts or add new ones without wrestling with SQL syntax. It’s all about coding faster and smarter.
To start working with Prisma, please install the necessary packages and run the init command. This will create an example of the Prisma schema.
Please open the newly generated file named "schema.prisma" in the Prisma directory and replace it with the following content! This is our blogging app’s schema. It defines posts and tags with a relationship—so users can filter by tags, and admins can assign them.
Next, set up the path for your local SQLite database in your ".env" file. Without this, Prisma would not know where to store the data. Check out the schema file, do you see where this variable is used?
Then, run the following commands to load the database schema into your database and generate a type-safe library to access your data! Please note that running "push" command also generates the code for your client.
Let's put Prisma to use! Let’s say a user clicks a post on our app. We need to grab it by ID for the client side.
First, lets create our query tool. This line connects to the database.
findUnique command grabs one post by ID. The include configuration automatically creates a join and pulls its tags too—great for showing users what the post’s about!
Now, we can use this function to power our client-side post page. It’s simple but solves a real need in our app. You can also imagine that it would be a great idea to cache the result!
Now, our goal is to fetch tech posts from 2025. Let’s filter with Prisma!
findMany fetches multiple posts—perfect for a list page.
The "tags" property filters for posts with the “tech” tag. This line makes our filtering magic happen!
The "date" property ensures we only get 2025 posts. It’s how we keep the list fresh.
The "include" add tag details for display. It’s all about giving users the full picture! as a result we obtain a list of tech posts from 2025—ready for our app’s filter route!
While we did not covered mutations yet, check how easy and intuitive it is to create a new post!
The "create" command adds a new post to the database. This will become the heart of our admin feature.
Check out also how to link existing tags like “tech” to the post. It’s how we categorise content fast!
But, how can you quickly access the underlying databse?
Prisma comes equipped with a web interface calles Prisma Studio that allows you to access and modify data. Run it with the following command.
Visual Studio also provides many database access plugins and all you have to do is search for them!
Also, there are many commercial or freely available software solutions that you can use, such as Table Plus or Data Grip, which is free for students!
Prisma simplifies database work with type-safe queries. It’s perfect for filtering posts or managing admin tasks in our app!
Start with a clear schema, use include for relationships, and test with Prisma Studio. Keep it simple and build up!
Try adding an update query for admins or a new filter route. Prisma’s got your back—let’s make this app awesome!