Skip to main content

Command Palette

Search for a command to run...

Modern Database Access: Prisma, Drizzle, and ORMs Explained

Updated
10 min readView as Markdown
Modern Database Access: Prisma, Drizzle, and ORMs Explained
S

Building Web & GenAI Software that (usually) work | Son of proud parents.

Across the Internet, every modern application you use, from social media platforms to e-commerce stores, relies heavily on data. But application code does not just hold data in temporary memory; it needs a reliable, permanent system to store, retrieve, and manage it. To do this, we use databases. But how does our JavaScript or TypeScript code actually talk to the database safely and efficiently?

In this blog, we will understand why applications need databases, the difference between SQL and NoSQL, the problems with raw queries, and how modern tools like ORMs, Prisma, and Drizzle solve these problems step by step.

1. First, let’s understand why applications need databases

When you run an application, the data lives in the server's RAM (memory). But when you restart the server, the RAM is cleared. We need databases to store data permanently on a disk so it is never lost.

Structured vs Unstructured Data

  • Structured Data: Highly organized, typically stored in tables with rows and columns. It is easy to search and filter.

  • Unstructured Data: Disorganized data like text files, images, or social media posts.

Common examples of what we store: Imagine you are building a user dashboard. You need to store user profiles. A structured database record looks like this:

const user = {
  id: 1,
  name: "Suprabhat",
  age: 23,
  email: "suprabhat@example.com",
  role: "admin"
};

Beyond users, applications must permanently store:

  • Orders: What a user bought, when, and for how much.

  • Products: Inventory levels, prices, and descriptions.

  • Payments: Transaction IDs, statuses, and billing histories.

The role of the database in modern applications is to act as the single source of truth. Without it, your application would forget everything the moment it crashes or restarts.

2. Now let’s understand SQL vs NoSQL Databases

Databases generally fall into two main categories.

What SQL databases are SQL (Structured Query Language) databases are relational. They store data in strict tables with predefined schemas (columns and data types). They are excellent for complex queries and ensuring data integrity.

  • Common examples: PostgreSQL, MySQL, SQLite.

What NoSQL databases are NoSQL databases are non-relational. They store data in flexible formats like JSON documents, key-value pairs, or graphs. They do not require a strict schema upfront.

  • Common examples: MongoDB, Redis, Cassandra.

When to choose each approach

  • Use SQL when your data is highly relational (e.g., Users have many Orders, Orders have many Products) and you need strict accuracy, like in financial or payment systems.

  • Use NoSQL when your data structure changes frequently, you are handling massive amounts of unstructured data, or you need rapid, horizontal scaling for things like real-time analytics or content management.

3. The Problem with Raw Database Queries

Before modern tools existed, developers had to write raw SQL strings directly inside their application code.

// Raw SQL Query Example
const result = await db.query("SELECT * FROM users WHERE age = 23 AND role = 'admin'");

While this works, it creates several major problems:

  1. Repetitive Code: You end up writing the same SELECT, INSERT, and UPDATE strings over and over again across hundreds of files.

  2. Security Concerns: If you concatenate user input directly into raw SQL strings, your app becomes vulnerable to SQL Injection attacks, where hackers can delete or steal your entire database.

  3. Maintainability Issues: If you rename a database column from user_name to full_name, you have to manually find and update every single raw SQL string in your codebase. If you miss one, your app crashes.

  4. Scaling Operations: As your app grows, managing complex joins and relationships using raw strings becomes incredibly difficult to read and debug.

4. What is an ORM?

To solve the problems of raw queries, the industry created ORMs (Object-Relational Mappers).

Definition of ORM An ORM is a library that automatically converts data between your database (which uses tables and rows) and your application code (which uses objects and classes). Instead of writing SQL strings, you write standard code in your programming language.

Why ORMs exist They exist to provide Type Safety and Autocomplete. If you misspell a column name, your code editor will show a red error line before you even run the app.

Benefits of ORMs

  • Security: They automatically sanitize inputs, preventing SQL injection.

  • Developer Experience (DX): You get autocomplete for your database tables and columns.

  • Database Agnostic: You can switch from PostgreSQL to MySQL without rewriting all your queries.

Tradeoffs of using ORMs

  • Performance Overhead: Translating code into SQL takes extra processing time.

  • The "N+1" Query Problem: ORMs can sometimes accidentally generate hundreds of hidden database queries in a loop, slowing down your app if you aren't careful.

5. Understanding Prisma

Prisma is currently one of the most popular next-generation ORMs, especially in the TypeScript and Node.js ecosystem.

What Prisma is Prisma is a Schema-first ORM. This means you define your entire database structure in a single, dedicated file called schema.prisma.

How it works:

  1. You write your models in the Prisma schema language.

  2. You run a command (prisma generate).

  3. Prisma reads your schema and generates a custom, fully type-safe database client specifically tailored to your exact database structure.

Developer experience benefits Because Prisma generates a custom client, the autocomplete in your code editor is flawless. It knows exactly what tables exist, what columns they have, and what data types they expect.

Prisma Ecosystem Overview Prisma is more than just an ORM. It includes:

  • Prisma Client: The query builder.

  • Prisma Migrate: The migration system.

  • Prisma Studio: A visual UI to view and edit your database data directly in the browser.

6. Understanding Drizzle

Drizzle ORM is a newer, lightweight alternative that has gained massive popularity among developers who want type safety without the heavy overhead of traditional ORMs.

What Drizzle is Drizzle follows an SQL-first philosophy. Instead of inventing a new schema language, you define your database schema using standard TypeScript code that closely mirrors actual SQL.

Lightweight architecture Unlike Prisma, Drizzle does not generate a custom client. It infers types directly from your TypeScript schema definitions. This makes it incredibly fast, lightweight, and perfect for Edge environments (like Cloudflare Workers) where bundle size matters.

Drizzle vs traditional ORMs Traditional ORMs try to hide SQL from you completely. Drizzle embraces SQL. If you know SQL, Drizzle feels like writing SQL, but with the safety of TypeScript autocomplete.

7. Key differences between Prisma and Drizzle

Feature Prisma Drizzle
Philosophy Schema-first (Custom language) SQL-first (TypeScript code)
Developer Experience Feels like magic, very beginner-friendly Close to the metal, great for SQL experts
Performance Slower (runs on a custom Rust engine) Faster, extremely lightweight
Learning Curve Low (Easy to learn) Medium (Requires SQL knowledge)
Bundle Size Large (Requires generated client) Tiny (No generation step needed)
Ecosystem Maturity Very mature, massive community Newer, but growing rapidly

8. Database Migrations

Why migrations are needed In software development, your code changes every day. But your database also needs to change. If you add a new feature that requires a phone_number column, you cannot just delete the database and start over—you would lose all your users!

Schema evolution and Versioning Migrations are like Git for your database. They are a history of SQL files that track every change made to your database schema over time.

Migration workflows

  1. You change your schema file (add a new column).

  2. You run a migration command (e.g., prisma migrate dev or drizzle-kit generate).

  3. The tool compares your new schema to the old one and generates a SQL script (e.g., ALTER TABLE users ADD COLUMN phone_number VARCHAR;).

  4. This script is saved in a folder and applied to the database.

Common migration challenges Migrations can be scary in production. If a migration locks a massive table while adding a column, it can take the entire application offline for minutes. Both Prisma and Drizzle provide tools to help plan and test these migrations safely before they hit production.

9. Designing Data Models

Before writing code, you must design how your data relates to the real world.

Entities and relationships An "Entity" is a table (like User or Post). How they connect is called a relationship.

  • One-to-One (1:1): One record relates to exactly one other record.

    • Example: A User has exactly one Profile (bio, avatar).
  • One-to-Many (1:N): One record relates to many records.

    • Example: A User (name: "Suprabhat", age: 23) can write many Posts. But each Post belongs to only one User.
  • Many-to-Many (M:N): Many records relate to many other records.

    • Example: Students and Classes. A student can take many classes, and a class can have many students. This usually requires a "join table" in SQL databases to map the connections.

Modeling real-world systems Good data modeling prevents data duplication. Instead of saving the user's name inside every single post they write, you save the userId inside the post. This ensures that if Suprabhat changes his name, it updates everywhere instantly.

10. Choosing the Right Tool

Choosing between Raw SQL, Prisma, or Drizzle depends on what you need for your specific project.

Startup projects

  • When to use Prisma: If you are a startup or a solo developer trying to build an MVP (Minimum Viable Product) as fast as possible. Prisma's schema-first approach and visual Studio tool will save you hundreds of hours.

Enterprise applications & High Performance

  • When to use Drizzle: If you are building a high-scale enterprise app, a serverless edge function, or a system where raw performance and low memory footprint are critical. Drizzle gives you the speed of raw SQL with the safety of TypeScript.

Team experience

  • If your team consists of junior developers or frontend engineers who do not know SQL well, Prisma is the safest choice.

  • If your team consists of backend engineers and database administrators who love writing optimized SQL queries, Drizzle (or even raw SQL with a query builder like Kysely) will make them much happier.

Long-term maintenance Prisma is backed by a massive company and has a vast ecosystem of third-party plugins. Drizzle is open-source, lightweight, and gives you total control over the generated SQL, making it easier to debug complex database bottlenecks in the long run.

Conclusion

Modern database access has evolved far beyond writing raw, insecure SQL strings. Tools like ORMs bridge the gap between your application code and your permanent data storage.

Choosing between Prisma and Drizzle depends on what you need. If you want rapid development, a visual UI, and a gentle learning curve, go with Prisma. If you want raw speed, lightweight architecture, and a SQL-first philosophy, Drizzle is your friend. Understanding these tools helps you see how the "invisible" data layer of the internet works securely and efficiently every time a user logs in, places an order, or updates their profile!