Sequelize
Overview
Sequelize is a mature, promise-based ORM for Node.js that targets SQL databases including PostgreSQL, MySQL, MariaDB, SQLite, and SQL Server. It provides model definitions, associations, eager/lazy loading, transactions, and read replication. Read this when working in an established JavaScript/TypeScript codebase that uses Sequelize or when you want a battle-tested relational ORM.
Official sources
Install / setup
npm install --save sequelize
Core concepts
- Sequelize instance: the connection object created with a connection URI or options, plus the chosen dialect driver.
- Models: defined via
sequelize.define() or by extending Model; map to tables with typed attributes.
- Associations:
hasOne, hasMany, belongsTo, belongsToMany express relationships and generate helper methods.
- Querying: finder methods (
findAll, findOne, findByPk) with where operators and include for eager loading.
- Transactions: managed (
sequelize.transaction(async t => …)) or unmanaged, for atomic multi-statement work.
- Migrations & seeders: managed via the separate Sequelize CLI.
Best practices
- Install the dialect-specific driver alongside Sequelize (e.g.
pg, mysql2, sqlite3) as the docs require.
- Use managed transactions so commit/rollback is handled automatically on success/error.
- Prefer migrations over
sync({ force: true }) in production, which drops and recreates tables.
- Use
include deliberately and select only needed attributes to avoid N+1 and over-fetching.
Common pitfalls
- Running
sequelize.sync({ force: true }) against a real database → drops all tables; use migrations instead.
- Forgetting to install the dialect driver → connection fails at startup; install the matching driver package.
Examples
const { Sequelize, DataTypes } = require('sequelize')
const sequelize = new Sequelize(process.env.DATABASE_URL)
const User = sequelize.define('User', {
name: { type: DataTypes.STRING },
})
await sequelize.authenticate()
const ada = await User.create({ name: 'Ada' })
Further reading
Related skills
- ../typeorm — decorator-based TS/JS ORM alternative
- ../knex — lower-level SQL query builder Sequelize sits above
1---2name: sequelize3description: Mature promise-based JS/TS ORM for SQL databases with models, associations, and transactions; consult for established Node.js relational data access.4domain: stack5category: orm6tags: [orm, nodejs, javascript, typescript, sql, transactions]7official_sources:8 - https://sequelize.org/docs/v6/9 - https://github.com/sequelize/sequelize10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/sequelize13---1415# Sequelize1617## Overview18Sequelize is a mature, promise-based ORM for Node.js that targets SQL databases including PostgreSQL, MySQL, MariaDB, SQLite, and SQL Server. It provides model definitions, associations, eager/lazy loading, transactions, and read replication. Read this when working in an established JavaScript/TypeScript codebase that uses Sequelize or when you want a battle-tested relational ORM.1920## Official sources21- Docs: https://sequelize.org/docs/v6/22- Repo: https://github.com/sequelize/sequelize23- Install / getting started: https://sequelize.org/docs/v6/getting-started/2425## Install / setup26```bash27npm install --save sequelize28```2930## Core concepts31- **Sequelize instance**: the connection object created with a connection URI or options, plus the chosen dialect driver.32- **Models**: defined via `sequelize.define()` or by extending `Model`; map to tables with typed attributes.33- **Associations**: `hasOne`, `hasMany`, `belongsTo`, `belongsToMany` express relationships and generate helper methods.34- **Querying**: finder methods (`findAll`, `findOne`, `findByPk`) with `where` operators and `include` for eager loading.35- **Transactions**: managed (`sequelize.transaction(async t => …)`) or unmanaged, for atomic multi-statement work.36- **Migrations & seeders**: managed via the separate Sequelize CLI.3738## Best practices39- Install the dialect-specific driver alongside Sequelize (e.g. `pg`, `mysql2`, `sqlite3`) as the docs require.40- Use managed transactions so commit/rollback is handled automatically on success/error.41- Prefer migrations over `sync({ force: true })` in production, which drops and recreates tables.42- Use `include` deliberately and select only needed attributes to avoid N+1 and over-fetching.4344## Common pitfalls45- Running `sequelize.sync({ force: true })` against a real database → drops all tables; use migrations instead.46- Forgetting to install the dialect driver → connection fails at startup; install the matching driver package.4748## Examples49```js50const { Sequelize, DataTypes } = require('sequelize')51const sequelize = new Sequelize(process.env.DATABASE_URL)5253const User = sequelize.define('User', {54 name: { type: DataTypes.STRING },55})5657await sequelize.authenticate()58const ada = await User.create({ name: 'Ada' })59```6061## Further reading62- https://sequelize.org/docs/v6/core-concepts/model-basics/ — model basics63- https://sequelize.org/docs/v6/other-topics/transactions/ — transactions6465## Related skills66- ../typeorm — decorator-based TS/JS ORM alternative67- ../knex — lower-level SQL query builder Sequelize sits above