Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

coldter/ts-express-starter

Repository files navigation

🚀 Getting Started

Installation

Clone the repository

  git clone <git repo URI>

Switch to the repo folder

  cd <your Folder>

Install dependencies

  npm install

💎 The Package Features

  • Application

  • Development

  • Database

✨ ESLint, Prettier :: Code Formatter

Prettier is an opinionated code formatter.

ESLint, Find and fix problems in your JavaScript code

It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.

  1. Install VSCode Extension Prettier, ESLint

  2. CMD + Shift + P (Mac Os) or Ctrl + Shift + P (Windows)

  3. Format Selection With

  4. Configure Default Formatter...

  5. Prettier - Code formatter

🗂 Code Structure

.
├── dist/ [the compiled output]
├── docs/ [openapi documentation]
├── src/ [the source code]/
│   ├── config/ [configuration files, including the env file variables]/
│   │   └── env.ts
│   ├── constants/ [constants]/
│   │   ├── common.constants.ts
│   │   └── HttpStatusCodes.ts
│   ├── database/ [database configuration (including sequelize instance and models initializations)]/
│   │   ├── sequelize/
│   │   │   ├── migrations/ [sequelize migrations]
│   │   │   ├── seeders/ [sequelize seeders]
│   │   │   └── sequelize-cli.js [sequelize cli configuration]
│   │   └── index.ts [database initialization]
│   ├── exceptions/ [custom exceptions]/
│   │   └── HttpException.ts
│   ├── interfaces/ [interfaces]/
│   │   ├── app/ [interfaces for the app and its business logic]
│   │   └── express/ [some app according custom interfaces for the framework]
│   ├── models/ [sequelize models]/
│   │   └── index.ts [exports all models which are then imported in the database/index.ts]
│   ├── server/
│   │   ├── controllers/ [controllers]
│   │   ├── helpers/ [app specific helpers]
│   │   ├── middlewares/ [middlewares]
│   │   ├── routes/ [routes]
│   │   ├── schemas/ [schemas (yup schemas for validations)]
│   │   ├── services/ [services]
│   │   └── server.ts [server initialization exports express App]
│   ├── tests/ [tests]/
│   │   ├── e2e/ [end to end tests]
│   │   └── -- other tests --
│   ├── utils/ [utils]/
│   │   └── logger.ts [logger]
│   └── main.ts [main bootstrap file]
├── .editorconfig
├── .env.example
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── jest.config.ts
├── nodemon.json
├── .nvmrc
├── package.json
├── package-lock.json
├── .prettierignore
└── .prettierrc

.Env Schema

NODE_ENV='development' # development, production, test

# mysql config
DATABASE_HOST='127.0.0.1'
DATABASE_USER='root'
DATABASE_PASSWORD='root'
DATABASE_PORT=3306
DATABASE_SCHEMA='ts-express'
DATABASE_POOL_MAX=10
DATABASE_POOL_MIN=0
DATABASE_POOL_ACQUIRE=10000
DATABASE_POOL_IDLE=30000

#server config
PORT=3000

#jwt config
JWT_SECRET='MY_SERUPER_DUPER_SECURE_HARD_TO_GUESS_SECRET'
JWT_TOKEN_EXPIRE_TIME_IN_HOURS=576 #24 days

Passing the Environment::

To pass an environment --> NODE_ENV={your environment}

The app will pick corresponding .env file::
eg.:: if you run NODE_ENV=production npm start then the production.env file will be used by the app...

| NOTE:: look for casing when passing the node environment

The database server is --> MYSQL RECOMMENDED version v8.0 or higher

The orm that is use --> Sequelize V6

NPM scripts

  • start: starts the app with nodemon
  • start:prod: runs the builded app in production mode
  • start:dev: start the app in development mode with nodemon
  • clean: removes the dist folder
  • clean:all: removes the dist folder and node_modules
  • build: builds the app with swc
  • build:tsc: builds the app with tsc !!
  • lint: runs eslint
  • lint:fix: runs eslint with fix
  • format: formats the code with prettier
  • ts:check: checks the typescript code with tsc
  • db:migrate: runs the migrations
  • db:migrate:undo: undoes the last migration
  • db:migrate:undo:all: undoes all migrations
  • db:migrate:status: shows the migration status
  • db:migration:generate: generates a new migration with the given name
  • db:seed:generate: generates a new seed with the given name
  • db:seed: runs the seed
  • db:seed:all: runs all seeds
  • test: runs all tests
  • test:e2e: runs the e2e tests excluding the tests in the e2e folder

Dev Container

The project is configured to run in a dev container. This means that you can run the project in a container without having to install any dependencies like database on your local machine.

Prerequisites

  • Docker
  • System with at least 8GB of RAM
  • 4 CPU cores
  • 20GB of free disk space
  • VSCode with the Remote - Containers extension installed

Setup

  1. Open the project in VSCode
  2. Press F1 or Ctrl+Shift+P and select Remote-Containers: Reopen in Container

This will start building the container and installing all dependencies. Once finished, you can start the server by running npm start:dev in the terminal.