Skip to content
Tutorials Center
My learning

Topics

TypeORM and Sequelize Tutorial

Learn TypeORM and Sequelize step by step: 37 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 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.
  2. 2 Installing and Connecting Install TypeORM and Sequelize, choose a database driver, create a DataSource or Sequelize instance, test the connection, and see connection settings in a runnable stand-in.
  3. 3 Your First Entity and Model Define your first TypeORM entity and Sequelize model, save and read a row, and see how a class becomes a table through a runnable class-to-table stand-in.
  4. 4 Project Setup and Configuration Organize a Node.js project for TypeORM or Sequelize: folders, tsconfig decorator flags, per-environment settings, the Sequelize CLI config, and a registry pattern you can run.

Models and Entities

  1. 5 TypeORM Entities and Columns Define TypeORM entities with @Entity and @Column, set column names and options, share fields with abstract base classes and embedded objects, and see metadata in a runnable stand-in.
  2. 6 Sequelize Models and init() Define Sequelize models with Model.init and sequelize.define, set tableName, underscored and paranoid options, use getters, setters and virtual fields, with a runnable stand-in.
  3. 7 Data Types and Column Options Map strings, numbers, decimals, dates, JSON and enums to column types in TypeORM and Sequelize, avoid float money bugs, and use transformers and defaults with runnable stand-ins.
  4. 8 Primary Keys and Generated Values Choose between auto-increment, UUID and composite primary keys in TypeORM and Sequelize, see how IDs are generated, and compare them with runnable examples.
  5. 9 Validation, Hooks and Timestamps Validate data with Sequelize validators and class-validator, run code before and after saves with hooks and subscribers, and manage timestamps and soft deletes, with runnable stand-ins.

Querying

  1. 10 CRUD Operations Create, read, update and delete rows with TypeORM repositories and Sequelize models, understand save vs insert, upsert and bulk operations, and try the same flow in a runnable stand-in.
  2. 11 Filtering with Operators Filter rows with TypeORM find operators (MoreThan, Like, In, Between) and Sequelize Op operators, combine AND and OR, handle null, and see the SQL built by a runnable stand-in.
  3. 12 Sorting, Pagination and Selecting Columns Sort results, paginate with take/skip and limit/offset, switch to keyset pagination for big tables, and select only the columns you need in TypeORM and Sequelize, with runnable demos.
  4. 13 Query Builders and Raw SQL Build complex queries with the TypeORM QueryBuilder, run Sequelize raw queries with replacements and bind parameters, and avoid SQL injection, with a runnable fluent builder stand-in.
  5. 14 Aggregates and Grouping Compute count, sum, average, min and max, group rows with GROUP BY and HAVING, and map results in TypeORM and Sequelize, with runnable SQL stand-ins you can edit.

Relations

  1. 15 Many-to-One and One-to-Many Relations Model one-to-many relations with TypeORM decorators and Sequelize hasMany and belongsTo, understand foreign keys and the owning side, and join tables with a runnable stand-in.
  2. 16 One-to-One Relations Model one-to-one relations with TypeORM @OneToOne and @JoinColumn and Sequelize hasOne and belongsTo, choose the owning side, enforce uniqueness, and test it in a runnable stand-in.
  3. 17 Many-to-Many Relations Model many-to-many relations with TypeORM @ManyToMany and @JoinTable and Sequelize belongsToMany, add extra columns with an explicit join entity, and see junction tables in a runnable demo.
  4. 18 Loading Strategies and Cascades Load relations with TypeORM relations and Sequelize include, choose join or separate queries, understand eager and lazy loading, avoid N+1 queries, and use cascade options safely.

Migrations

  1. 19 Why You Need Migrations Learn why schema migrations exist, what a migration table records, and how up and down steps keep every environment on the same database structure.
  2. 20 Migrations in TypeORM Learn TypeORM migrations: the MigrationInterface, QueryRunner, migration:generate, migration:run and revert, plus how to review generated SQL before it ships.
  3. 21 Migrations in Sequelize Use sequelize-cli to create, run and undo migrations, write queryInterface steps, track state in SequelizeMeta and add seed files for starter data.
  4. 22 Safe Schema Changes in Production Change a live database without breaking running code: the expand and contract pattern, backfills in batches, safe renames, NOT NULL columns and index builds.

Transactions

  1. 23 Transaction Basics Understand transactions before using an ORM: ACID, BEGIN, COMMIT and ROLLBACK, a money-transfer example, savepoints and when you need one.
  2. 24 Transactions in TypeORM and Sequelize Compare managed and manual transactions in TypeORM (EntityManager, QueryRunner) and Sequelize, pass the transaction correctly and run code after commit.
  3. 25 Locking and Isolation Levels What isolation levels prevent, how the lost update happens, and how to fix it with FOR UPDATE locks or optimistic version columns in TypeORM and Sequelize.
  4. 26 Deadlocks and Retries Why deadlocks and serialization failures happen, how to avoid them with consistent lock order, and a safe retry helper with backoff for TypeORM and Sequelize.

Testing

  1. 27 Test Databases and Fixtures Choose a test database strategy for ORM code: in-memory SQLite or a real engine, rollback or fresh schema per test, and reliable fixtures.
  2. 28 Testing Queries with node:test Write real tests for repository code with node:test and assert: arrange data, act, assert on rows, test constraints, transactions and migrations up and down.
  3. 29 Factories and Mocks Build test data factories with defaults and overrides, write fake repositories for fast unit tests and use node:test mock functions to spy on calls.

Performance and Patterns

  1. 30 Finding and Fixing N+1 Queries What the N+1 query problem is, how to count queries to catch it, and how to fix it with joins, batched IN queries and the DataLoader pattern.
  2. 31 Indexes and Reading Query Plans How indexes speed up queries, how to read EXPLAIN output, which columns to index, composite index order and how to declare indexes in the ORMs.
  3. 32 Pagination Patterns Paginate correctly: LIMIT and OFFSET with skip and take, the cost and drift of offset paging, and fast stable keyset (cursor) pagination.
  4. 33 Repository and Unit of Work Structure ORM code with repositories and a unit of work: Active Record versus Data Mapper, a service layer and one transaction boundary per use case.
  5. 34 Connection Pooling and Caching Size the connection pool in TypeORM and Sequelize, avoid leaks and exhaustion, and add a cache-aside layer with expiry and invalidation.

Projects and Practice

  1. 35 Project: Blog API with Migrations Build a blog API end to end: migrations, repositories, a transactional service, keyset pagination and an HTTP layer tested with fetch.
  2. 36 Project: Inventory Ledger Build an inventory service that never oversells: a movements ledger, atomic conditional updates, idempotency keys and a reconciliation query.
  3. 37 Practice Exercises: Migrations, Transactions and Queries Six hands-on exercises on migrations, transactions, lost updates, N+1 queries, keyset pagination and idempotency, each with a starter and a solution.