Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

ORM Concepts and TypeORM vs Sequelize

Learn what an object-relational mapper does, how Active Record and Data Mapper differ, and how TypeORM and Sequelize compare, with runnable stand-ins you can edit.

Relational databases store rows in tables. Your Node.js code works with objects. An object-relational mapper (ORM) is the layer between the two: you describe tables as classes or models, and the ORM builds SQL, runs it, and turns the rows back into objects. TypeORM and Sequelize are the two long-standing ORMs for Node.js, and this track teaches both side by side.

What runs on this site. Neither library can run in the in-browser runner, because both need npm packages and a database driver. All TypeORM and Sequelize code in this track is read-only (```ts blocks). Every runnable example is a clearly labelled stand-in: a small hand-written mapper on top of Node's built-in node:sqlite. The stand-ins show the ideas (what SQL gets built, how rows become objects) without pretending to be the real libraries. When a lesson shows output, it comes from the stand-in.

What an ORM buys you

Without an ORM you write SQL strings and map rows by hand:

Node.js Hand-written SQL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

That is fine for one query. Across hundreds of queries the repetition causes typos, inconsistent mapping and SQL injection risks when strings get concatenated. An ORM gives you:

  • Typed models that describe each table once.
  • Query builders and finders that generate parameterized SQL.
  • Relations (users have posts) loaded with one option instead of hand-written joins.
  • Migrations, validation, hooks and transactions in one toolkit.

It is not free. ORMs hide SQL, which can hide slow queries (see the performance chapter), and complex reports are often easier in raw SQL. A good habit is to use the ORM for everyday work and drop to raw SQL where it is clearer.

Active Record vs Data Mapper

The two libraries lean on different patterns.

  • Active Record: the object knows how to save itself. user.save() lives on the model. Sequelize works this way (models have methods like User.create() and user.save()), and TypeORM supports it too through BaseEntity.
  • Data Mapper: plain objects know nothing about the database; a separate repository moves them in and out. TypeORM encourages this by default (dataSource.getRepository(User)).
Node.js Two patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Neither is better in general. Active Record is quick to read in small apps. Data Mapper keeps entities free of database code, which helps testing and larger codebases.

How the two libraries compare

Topic TypeORM Sequelize
Language focus TypeScript first, decorators JavaScript first, TypeScript supported
Model definition Classes with @Entity, @Column Model.init() or sequelize.define()
Pattern Data Mapper (Active Record optional) Active Record
Query API Find options and QueryBuilder Finders with Op operators, plus raw queries
Migrations CLI generates from entity changes CLI creates files you write by hand
Databases PostgreSQL, MySQL/MariaDB, SQLite, SQL Server, Oracle, MongoDB and more PostgreSQL, MySQL/MariaDB, SQLite, SQL Server, Db2 and more
flowchart LR
  A["Your code"] --> B["Model or entity"]
  B --> C["ORM (TypeORM / Sequelize)"]
  C --> D["SQL + parameters"]
  D --> E[("Database")]
  E --> C
  C --> B

The ORM sits between your objects and the database, translating in both directions.

Which should you pick?

Choose TypeORM when you want decorators, strong typing from the class definitions and the Data Mapper pattern, for example in a NestJS service. Choose Sequelize when you prefer explicit model definitions, a long track record and plain JavaScript compatibility. Both are mature, both are widely deployed, and the concepts transfer: once you know one, the other takes an afternoon to read.

For this track every chapter explains one idea, shows the TypeORM way, shows the Sequelize way, and gives you a runnable stand-in for the underlying concept.

A tiny mapper to play with

Here is the kind of stand-in used later in this track. It builds parameterized SQL from an object, which is the heart of every ORM finder.

Node.js A mini mapper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main job of an ORM?

    An ORM maps objects to tables and builds the SQL for you.
  2. 2. Which pattern does Sequelize primarily use?

    Sequelize models carry their own methods, which is the Active Record pattern.
  3. 3. In the Data Mapper pattern, who knows about the database?

    Entities stay plain; a repository persists them.
  4. 4. Why are the runnable examples in this track called stand-ins?

    Library code is read-only here; stand-ins on node:sqlite illustrate the concepts.

Summary

  • An ORM maps classes to tables and builds parameterized SQL.
  • Active Record (Sequelize, optional in TypeORM) puts persistence methods on the model.
  • Data Mapper (TypeORM default) keeps entities plain and uses repositories.
  • ORMs save repetition but can hide slow SQL; know when to use raw queries.
  • In this track the real libraries are read-only and every runnable block is a labelled stand-in.

Next: Installing and Connecting