Back to roadmaps prisma Course

What is an ORM and Why Prisma

When building database-backed applications, developers face the challenge of bridging the gap between application logic and relational database tables. Let us explore how Object-Relational Mapping (ORM) solves this problem and why Prisma is the modern choice.


1. The Challenges of Raw SQL Queries

In traditional backend setups, developers write raw SQL queries as strings inside their code:

// Traditional raw SQL query string
const userId = 5;
const users = await db.query("SELECT * FROM users WHERE id = $1", [userId]);

This approach has major flaws:

  • No Type Safety: The return type is typically typed as any, which can lead to bugs if columns are renamed.
  • Bad Developer Experience: IDE editors cannot autocomplete column names or highlight syntax errors inside SQL strings.
  • Security Risks: Incorrect parameter escaping can expose applications to SQL Injection vulnerabilities.

2. What is an ORM?

An Object-Relational Mapping (ORM) library bridges this gap. It maps database tables directly to classes or objects in your programming language. Instead of writing raw SQL commands, you call native language methods to interact with your data.


3. Why Choose Prisma?

Traditional Node.js ORMs (like Sequelize or TypeORM) use classes and decorators to define tables. This setup can feel heavy and is prone to synchronization errors between code and the database.

Prisma takes a different approach by introducing a dedicated schema file and an auto-generated client:

A. Auto-Generated Type Safety

Prisma reads your schema definition and compiles custom TypeScript typings for your database tables. Every query result is fully typed automatically.

B. The Prisma Schema File

Instead of declaring classes and decorators, you define your data models using Prisma intuitive modelling syntax. This single schema file acts as the source of truth for both your database tables and application types.

C. Rich Ecosystem Tools

  • Prisma Client: An auto-generated query builder.
  • Prisma Migrate: A version-controlled database schema migration tool.
  • Prisma Studio: A visual database editor GUI.
Published on Last updated: