DEV Community

Cover image for Boilerplate for Typescript-Express with sequelize ORM
Kazi Naimul Hoque
Kazi Naimul Hoque

Posted on • Edited on

Boilerplate for Typescript-Express with sequelize ORM

Boilerplate for Typescript-Express with sequelize ORM

A boilerplate for any enterprise rest api or service with Node.js -Typescript, Express and Sequelize ORM for mysql, postgresql or others.

By running this project you will get a production ready environment with all necessary supports for validation, unit testing, socket, redis and many more.This repo is the typescript version of my another boilerplate of Nodejs Express Mysql boilerplate

Manual Installation

Clone the repo:

git clone https://github.com/kazi-naimul/typescript-express-mysql-boilerplate
cd typescript-express-mysql-boilerplate
Enter fullscreen mode Exit fullscreen mode

Install the dependencies:

yarn install
Enter fullscreen mode Exit fullscreen mode

Set the environment variables:

cp .env.example .env

# open .env and modify the environment variables (if needed)
Enter fullscreen mode Exit fullscreen mode

Features

  • ORM: Sequelize orm for object data modeling
  • Migration and Seed: DB migration and Seed using Sequelize-CLI
  • Authentication and authorization: using passport
  • Error handling: centralized error handling
  • Validation: request data validation using Joi
  • Logging: using winston
  • Testing: unittests using Mocha
  • Caching: Caching using Redis
  • Bidirectional Communication: using Scoket
  • Job scheduler: with Node-cron
  • Dependency management: with Yarn
  • Environment variables: using dotenv and cross-env
  • CORS: Cross-Origin Resource-Sharing enabled using cors
  • Docker support
  • Linting: with ESLint and Prettier

Commands

Running locally:

yarn dev
Enter fullscreen mode Exit fullscreen mode

Running in production:

yarn start
Enter fullscreen mode Exit fullscreen mode

Testing:

# run all tests
yarn test

Enter fullscreen mode Exit fullscreen mode

Environment Variables

The environment variables can be found and modified in the .env file. They come with these default values:

#Server environment
NODE_ENV=development
#Port number
PORT=5000

#Db configuration
DB_HOST=db-host
DB_USER=db-user
DB_PASS=db-pass
DB_NAME=db-name


# JWT secret key
JWT_SECRET=your-jwt-secret-key
# Number of minutes after which an access token expires
JWT_ACCESS_EXPIRATION_MINUTES=5
# Number of days after which a refresh token expires
JWT_REFRESH_EXPIRATION_DAYS=30

#Log config
LOG_FOLDER=logs/
LOG_FILE=%DATE%-app-log.log
LOG_LEVEL=error

#Redis
REDIS_HOST=redis-host
REDIS_PORT=6379
REDIS_USE_PASSWORD=no
REDIS_PASSWORD=your-password

Enter fullscreen mode Exit fullscreen mode

Project Structure

specs\
src\
 |--config\         # Environment variables and configuration related things
 |--controllers\    # Route controllers (controller layer)
 |--dao\            # Data Access Object for models
    |--contract\        # Contracts for all dao
    |--implementation   # Implementation of the contracts
 |--db\             # Migrations and Seed files
 |--models\         # Sequelize models (data layer)
 |--routes\         # Routes
 |--services\       # Business logic (service layer)
    |--contract\        # Contracts for all service
    |--implementation   # Implementation of the contracts
 |--helper\         # Helper classes and functions
 |--validations\    # Request data validation schemas
 |--app.js          # Express app
 |--cronJobs.ts     # Job Scheduler
 |--index.ts        # App entry point
Enter fullscreen mode Exit fullscreen mode

Top comments (0)