My TypeScript Software Design & Architecture book just prelaunched! Check out solidbook.io.
Close

Settting up a Sequelize, TypeScript, Node.js, Jest Project [Boilerplate with Repo] - Sequelize

Jun 24th, 2019 / 2 min read / Share / Edit on GitHub
For building Node.js applications, Sequelize is my personal favourite ORM. I've battle-tested it in production with a handful of real-life projects and it's still my first choice for interacting with a relational database. Here's a walkthrough of my production setup with Node.js, TypeScript and Jest.

I'm currently working on an open-source TypeScript/Node.js application called White Label where users can trade vinyl with each other. I'm building it for the purpose of teaching developers how to organize complex domain-logic using Domain-Driven Design.

In that project, I've gone the traditional route of using a relational database.

And my favourite ORM for interacting with relational databases is Sequelize.

In this quick article, I'll walk you though my project setup.

Want to just see the code? Check it out here, https://github.com/stemmlerjs/white-label.


The Sequelize ORM

Sequelize has been around for a while. When I first started building apps with Node.js in 2014, Sequelize was the most mature ORM around at the time.

There are some other awesome looking ORMs out there today, but because I've personally battle-tested Sequelize over time, it's normally been my first choice. They've even released official v5 TypeScript typings.

white-label
  └ bin 
    └ www.ts    # This is the startup script that boots the webserver
  └ dist        # Builds go here
  └ scripts  
    └ db
      └ create.js
      └ delete.js
  └ sequelize
    └ config
    └ migrations
    └ seeders
    runner.ts   # 
  .env
  .sequelizerc
  .jest.config.js

app.js
import express from 'express'
import bodyParser from 'body-parser'
import cors from 'cors'
import { commentRepo } from './repos';
import { stripTrailingSlash } from './utils';
const morgan = require('morgan')
const helmet = require('helmet')
const compression = require('compression')
const rateLimit = require('express-rate-limit')

const isProduction = process.env.KS_IS_PROD !== 'false'

const origin = {
  // TODO: Uncomment
  // origin: isProduction ? 'https://khalilstemmler.com' : '*',
  origin: isProduction ? '*' : '*',
}

const app = express()

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors(origin))
app.use(compression())
app.use(helmet())
app.use(morgan('combined'))

const limiter = rateLimit({
  windowMs: 1 * 60 * 1000, // 1 minute
  max: 5, // 5 requests,
})


Discussion

Liked this? Sing it loud and proud 👨‍🎤.


0 Comments

Be the first to leave a comment

Submit

Stay in touch!



About the author

Khalil Stemmler

Khalil Stemmler is a Developer / Designer and co-founder of Univjobs. He frequently publishes articles about Domain-Driven Design and Advanced TypeScript & Node.js best practices for large-scale applications.



View more in Software Design



You may also enjoy...

A few more related articles

Domain Knowledge & Interpretation of the Single Responsibility Principle | SOLID Node.js + TypeScript
Jun 13th, 2019 / 9 min read
The Single Responsibility Principle specifies that a class or function should only have one reason to change. Admittedly, that's n...
SOLID Principles: The Software Developer's Framework to Robust & Maintainable Code [with Examples]
May 18th, 2019 / 15 min read
The SOLID principles are a set of software design principles that teach us how we can structure our functions and classes to be as...
UML Cheatsheet | Resource
Apr 26th, 2019 / 1 min read
Here's a handy-dandy UML cheatsheet.
The 6 Most Common Types of Logic in Large Applications [with Examples]
Sep 16th, 2019 / 12 min read
In this article, you'll learn about the Clean Architecture, why we should separate the concerns of large applications into layers,...

Want to be notified when new content comes out?

Join 2000+ other developers learning about Domain-Driven Design and Enterprise Node.js.

I won't spam ya. 🖖 Unsubscribe anytime.

Get updates