← Back to Node.js Course | Chapter 9: Database Integration | Lesson 5 of 7

ORM basics

An ORM maps database tables to JavaScript objects so you write less raw SQL.

In this page:

  1. ORM basics
Syntax
javascript
const Model = sequelize.define('ModelName', {
  field: DataTypes.TYPE
});
const records = await Model.findAll();

ORM basics

Object-relational mappers such as Sequelize, Prisma and TypeORM define models in code and generate queries. They speed up development, offer migrations and validation, but can hide costly queries. Learn the SQL they generate for performance work.

Note: Log generated SQL in development to catch N+1 queries.

Example: ORM basics

bash
const { Sequelize, DataTypes } = require("sequelize");
const db = new Sequelize("sqlite::memory:");
const User = db.define("User", { name: DataTypes.STRING, age: DataTypes.INTEGER });
await db.sync();
await User.create({ name: "Ada", age: 36 });
console.log((await User.findAll({ where: { age: 36 } })).length);

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Ignoring generated SQL
  2. N+1 query problems
  3. Treating an ORM as a replacement for understanding SQL
Chapter Summary
  • ORMs map tables to objects
  • Popular: Prisma, Sequelize, TypeORM
  • They generate SQL
  • Watch for N+1 queries
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.