/ExpressJS-clean-base-source

Very clean base source of NodeJS (Express) using DI with Inversify + Service - Repository Pattern with Sequelize

Primary LanguageTypeScript

ExpressJS-clean-basesource

Author: haphuthinh

Very clean base source of NodeJS (Express) using DI with Inversify + Service - Repository Pattern with Sequelize

What this base source including?

Structure

alt text

The guide for using each entity in the structure please read the *.guide.md in each folder.

Usage

Development:

npm run start:dev

Production:

npm run build
npm run start:prod

Start using docker compose

On first start up:

mkdir -p docker-data/data/redis docker-data/data/postgres/data docker-data/data/postgres/config docker-data/data/redis docker-data/data/media && sudo chmod -R a+rwx docker-data/

Then:

docker compose up --build -d

How to create new API Endpoint:

  1. Create new Model Class in src/models
  2. Create new repository and its repository interface in src/repository
  3. Create new service and its service interface in src/service
  4. Create new controller and its controller interface in src/controller
  5. Combine all to container in src/container
  6. Create new route in src/route

Create new model

Go to ./src/model Example:

@Table({
  timestamps: false,
  tableName: "dogs",
})
export class Dog extends Model {
  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  name!: string;

  @Column({
    type: DataType.STRING,
    allowNull: false,
  })
  breed!: string;

  @Column({
    type: DataType.BOOLEAN,
    allowNull: true,
    defaultValue: true,
  })
  isGoodBoy!: boolean;
}

This using sequelize-typescript

Migrations

  • Create new migration:
NAME=migration-name.ts npm run migrate:create

With [migration-name] is the name of the migration you want.

  • Apply migration:
npm run migrate:up
  • Migrate down:
npm run migrate:down
  • Revert all migration:
npm run migrate:revert-all

Lint source

npm run lint

Lint fix:

npm run lint:fix

Format source

Check:

npm run prettier

Apply format:

npm run prettier:fix

Commit message guideline

Commit Message Format

Each commit message consists of a header, a body and a footer. The header has a special format that includes a type, a scope and a subject:

<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>

The header is mandatory and the scope of the header is optional.

The word should be one of the rules items you have written in your .commitlintrc.json file and the is the module/component you are working on.

Samples:

docs(changelog): update changelog to beta.5
fix(release): need to depend on latest rxjs and zone.js

The version in our package.json gets copied to the one we publish, and users need the latest of these.

Type

Must be one of the following:

  • build: Changes that affect the build system or external
  • dependencies (example scopes: gulp, broccoli, npm)
  • ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
  • docs: Documentation only changes
  • feat: A new feature
  • fix: A bug fix
  • perf: A code change that improves performance
  • refactor: A code change that neither fixes a bug nor adds a feature
  • style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
  • test: Adding missing tests or correcting existing tests