ORM là lớp trung gian ánh xạ bảng DB thành object trong code, để bạn thao tác bằng ngôn ngữ lập trình thay vì viết SQL tay. Hai phong cách: Active Record (model tự chứa logic DB — Rails, Sequelize) và Data Mapper (tách object nghiệp vụ khỏi logic DB — Prisma).
Lợi ích: type-safety, quản lý migration có version, trừu tượng hóa DB (đổi DB dễ hơn), bớt code lặp.
Nhược điểm:
- Trừu tượng "rò rỉ": SQL sinh ra không tối ưu (vd đôi khi tạo subquery thay vì JOIN hiệu quả hơn).
- Dễ dính N+1 query nếu không cẩn thận.
- Khó debug khi cần hiểu SQL thực sự chạy, có overhead runtime.
Điểm chốt: ORM nào cũng có "cửa thoát" raw query (Prisma $queryRaw, Sequelize sequelize.query()) — dùng cho query phức tạp cần kiểm soát chặt. Đừng để ORM che hết SQL: vẫn nên biết nó sinh ra gì.
An ORM is a middle layer that maps DB tables to objects in code, so you operate via a programming language instead of hand-writing SQL. Two styles: Active Record (the model holds its own DB logic — Rails, Sequelize) and Data Mapper (separates the domain object from DB logic — Prisma).
Benefits: type-safety, version-controlled migration management, DB abstraction (easier to switch DB), less boilerplate.
Drawbacks:
- A "leaky" abstraction: generated SQL isn't always optimal (e.g. sometimes a subquery instead of a more efficient JOIN).
- Easy to hit the N+1 query problem if careless.
- Harder to debug when you need the real SQL, plus runtime overhead.
Key point: every ORM has a raw-query "escape hatch" (Prisma $queryRaw, Sequelize sequelize.query()) — use it for complex queries needing tight control. Don't let the ORM hide all SQL: you should still know what it generates.