How to Set Up Sequelize with TypeScript and Express (Step-by-Step)
Are you tired of managing raw SQL queries in your Node.js applications? Looking for a cleaner way to work with relational databases without sacrificing flexibility?
In this guide, you’ll learn how to set up Sequelize, TypeScript, and Express in a modern Node.js project. By the end, you’ll have a working API connected to a MySQL database running in Docker — a production-ready foundation for building scalable backends.
🚀 Complete JavaScript Guide (Beginner + Advanced)
🚀 NodeJS – The Complete Guide (MVC, REST APIs, GraphQL, Deno)
What is Sequelize?
Sequelize is a powerful ORM (Object-Relational Mapper) for Node.js that allows you to interact with relational databases using JavaScript/TypeScript instead of raw SQL.
It supports:
- MySQL
- PostgreSQL
- SQLite
- MSSQL
With Sequelize you can:
- Define models instead of writing SQL
- Run migrations
- Create associations
- Add validations
- Scale your database layer cleanly
Project Setup
To save time, start with a basic Node.js project scaffolded with:
- TypeScript
- ESLint
- Prettier
- Jest
- dotenv-cli
- tsc-watch
Make sure TypeScript source maps are enabled to simplify debugging in development.
Install Express and Middleware
Install the core API dependencies:
npm install express morgan cors npm install -D @types/express @types/morgan @types/cors
What these do:
express→ Web frameworkmorgan→ Logs HTTP requestscors→ Enables cross-origin requests
Create the Express Server
Create a file:
src/server.ts
Set up the server:
- Initialize Express
- Disable
x-powered-byheader - Add middleware:
- morgan
- JSON parser
- urlencoded parser
- cors
- Create a health check route
Example health route:
app.get("/health", (req, res) => {
res.json({ ok: true });
});
Export a createServer() function that returns the Express app.
Start the Server
Create:
src/index.ts
Import and start the server:
const server = createServer();
server.listen(3000, () => {
console.log("API is running");
});
Test it with:
- VS Code REST Client
- Postman
- curl
Request:
GET http://localhost:3000/health
You should see:
{ "ok": true }
Set Up MySQL with Docker
Instead of SQLite, use the same database locally that you use in production.
Create:
docker-compose.yaml
Example setup:
- MySQL 8 image
- Database name
- Root password
- Port mapping
- Persistent volume
Start the database:
docker compose up -d
Stop it:
docker compose down
Your database will now run locally inside Docker.
Install Sequelize + TypeScript Support
Install dependencies:
npm install sequelize sequelize-typescript mysql2 reflect-metadata npm install -D @types/validator
Enable decorators in tsconfig.json:
{
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
Create a Repository Layer
Organize Sequelize inside a repository.
Create:
src/data/repository/BaseRepository.ts
Initialize Sequelize with:
- host
- port
- database
- username
- password
- dialect: mysql
Export a repository instance.
Environment Variables
Create a .env file:
PORT=3000 DB_HOST=localhost DB_PORT=33061 DB_NAME=sequelize_api DB_USER=root DB_PASSWORD=secret
Config File
Create:
src/config.ts
This file should:
- Read environment variables
- Cast types
- Provide defaults
Keeping configuration centralized makes scaling easier.
Test Database Connection
Inside your server startup:
await sequelize.authenticate();
console.log("Successfully connected to the database");
If there’s an error, log it.
Run the dev server:
npm run dev
Make sure Docker is running.
You should now see:
- API running
- Database connected
Why Use This Setup?
This stack gives you:
- Type safety with TypeScript
- Clean architecture with Express
- ORM power with Sequelize
- Real database locally via Docker
- Scalable project structure
Perfect for:
- REST APIs
- SaaS apps
- Production Node backends
- Teaching backend development
What’s Next?
Now that Sequelize is connected, the next step is:
👉 Creating models
👉 Defining associations
👉 Building API routes
Once you add models, Sequelize becomes incredibly powerful for managing relational data in a structured way.
Final Thoughts
Setting up Sequelize with TypeScript and Express might seem like a lot at first, but once the foundation is in place, development becomes much faster and cleaner.
If you’re building serious Node.js applications, this stack is a solid long-term choice.