What Is an ORM? How Apps Talk to Databases, Explained
An ORM lets app code work with database rows as ordinary objects instead of raw SQL. What it does, Prisma and Drizzle examples, migrations, and what to ask AI.
Mythex Team · · 5 min read
An ORM (object-relational mapper) is a code library that sits between an app and a relational database. Instead of writing database queries in SQL, developers work with data as ordinary objects in their programming language — db.booking.create(...) or Booking.find(...) — and the ORM translates that into SQL, runs it and turns the results back into objects. Popular examples are Prisma and Drizzle for TypeScript, SQLAlchemy for Python and Entity Framework Core for C#.
Why ORMs matter when you build with AI
When an AI app builder adds a database to your app, it usually also adds an ORM to talk to it. You won't have to write ORM code, but it helps to know it's there:
- Your data model lives in code. The ORM's schema file is often the clearest description of your app's data: which tables exist, which fields each has, and how they connect. Reading it is a quick way to check the AI understood your app.
- Changes to the database go through migrations. When you ask for "add a phone number to customers", the AI updates the model and creates a migration. Knowing that helps you understand why a change might need to be applied before it works live.
- Safety by default. ORMs build queries in a way that protects against SQL injection, one of the most common security holes in apps that assemble queries by hand.
- Speed traps. ORMs make it easy to accidentally run hundreds of small queries where one would do. It's a common cause of slow pages in AI-built apps.
An everyday analogy
Imagine a British traveller and a Japanese shopkeeper who don't share a language, working through an interpreter.
The traveller (your app) says, "I'd like the three cheapest teas you have." The interpreter (the ORM) turns it into precise Japanese (SQL), passes it to the shopkeeper (the database), and translates the answer back into English.
The traveller never learns Japanese, and for most conversations that's fine. But for a complicated negotiation, it can help to speak directly — which is why most ORMs let developers write raw SQL when needed.
How an ORM works
Databases like Postgres store data in tables of rows and columns and are queried with SQL. Programming languages work with objects: a booking with a date, a customer and a status. The ORM maps one to the other:
| Database side | ORM / code side |
|---|---|
Table bookings | Model Booking |
| Row | One booking object |
Column starts_at | Field startsAt |
Foreign key customer_id | Relation booking.customer |
SELECT, INSERT, UPDATE, DELETE | find, create, update, delete methods |
Those four operations are the CRUD actions almost every app is built on.
A worked example: bookings with Prisma
Here is a simplified Prisma schema for a salon booking app. It describes two tables and how they're linked:
model Customer {
id Int @id @default(autoincrement())
name String
email String @unique
bookings Booking[]
}
model Booking {
id Int @id @default(autoincrement())
startsAt DateTime
service String
customer Customer @relation(fields: [customerId], references: [id])
customerId Int
}
In plain words: a customer has a name, a unique email and many bookings; each booking has a start time, a service and belongs to one customer.
Now the app wants "Lena's upcoming bookings, soonest first":
const bookings = await db.booking.findMany({
where: {
customer: { email: "lena@example.com" },
startsAt: { gte: new Date() },
},
orderBy: { startsAt: "asc" },
});
The ORM turns that into SQL roughly like this, runs it and returns a list of booking objects:
SELECT b.* FROM "Booking" b
JOIN "Customer" c ON c.id = b."customerId"
WHERE c.email = $1 AND b."startsAt" >= $2
ORDER BY b."startsAt" ASC;
The $1 and $2 are placeholders: the email and date are sent separately from the query text, which is what prevents SQL injection.
Migrations
If you then ask for "let customers add a phone number", the AI adds phone String? to the Customer model (the ? means optional) and generates a migration — a small SQL script like ALTER TABLE "Customer" ADD COLUMN "phone" TEXT;. Migrations are saved in order, so every copy of the database can be brought to the same structure, and the history of changes is kept.
Common ORMs by language
| Language | Common ORMs |
|---|---|
| TypeScript / JavaScript | Prisma, Drizzle, TypeORM, Sequelize |
| Python | SQLAlchemy, Django ORM, SQLModel |
| C# | Entity Framework Core |
| Java | Hibernate (often via Spring Data JPA) |
| Ruby | Active Record |
ORMs vary in how much they hide SQL. Prisma uses its own schema language and query style; Drizzle deliberately stays close to SQL's shape while keeping everything typed. Lighter tools called query builders, such as Knex or Kysely for TypeScript, help build SQL in code without mapping rows to full objects.
Key terms
| Term | Meaning |
|---|---|
| Model | The code definition of a table and its fields. |
| Schema | The full description of all tables, fields and relationships. |
| Relation | A link between models, such as a booking belonging to a customer. |
| Migration | A versioned script that changes the database structure. |
| Query builder | A lighter tool for building SQL in code without the full object mapping. |
| N+1 problem | Running one query for a list, then one more for each item — slow at scale. |
| Raw SQL | Hand-written SQL, used when the ORM gets in the way. |
Common misconceptions
- "An ORM is a database." It isn't. It's a translator. You still need a database such as Postgres.
- "With an ORM you don't need to understand your data." Poorly designed tables are just as painful through an ORM. A good data model matters more than the tool.
- "ORMs are always slow." Everyday queries are fine. Slowness usually comes from patterns such as N+1 queries or missing indexes, which can be fixed.
- "ORM means no SQL injection risk, ever." Standard ORM methods are safe, but raw SQL written with pasted-in user input is still dangerous.
- "Editing the schema file changes the live database." Only once a migration is created and applied to that database.
What to ask your AI builder
- "Show me the database schema and explain each table and relationship in plain English."
- "Which ORM does this project use, and where are the migrations?"
- "Before changing any table, tell me whether existing data will be affected, and never drop a column that has data without asking."
- "Check the slowest pages for N+1 queries and add indexes where lists are filtered or sorted."
- "Make sure every query that uses user input goes through the ORM or uses parameters — no string-built SQL."
ORMs in Mythex
When your app needs to save data, Mythex adds a dedicated Postgres database to the project — ask in chat or use the /database command — and injects its connection details into the workspace and published app (Database). The agent then writes the code that reads and saves your data. If you have a preference — "use Prisma", "use SQLAlchemy", or "plain SQL, no ORM" — say so in chat. Ask it to walk you through the schema whenever you want to check the structure; you can read the files in the code editor. For the bigger picture, see what is a database and SQL vs NoSQL.
Questions
What is an ORM in simple terms?
An ORM (object-relational mapper) is a code library that sits between your app and a relational database. It lets developers read and save data as ordinary objects in their programming language, and it writes the SQL queries for them behind the scenes.
What are some examples of ORMs?
Common ORMs include Prisma and Drizzle for TypeScript and JavaScript, SQLAlchemy and the Django ORM for Python, Entity Framework Core for C#, Hibernate for Java, and Active Record in Ruby on Rails.
Is an ORM better than writing SQL?
It depends on the job. An ORM makes everyday reads and writes quicker to write, safer and easier to change, while hand-written SQL gives full control for complex reports or performance-critical queries. Most ORMs let you drop down to raw SQL when you need to.
What is a migration?
A migration is a small, versioned script that changes the database's structure, such as adding a table or a column. ORMs can generate migrations from your data model so every copy of the database — on a laptop, in testing and in production — ends up with the same structure.