Very clean base source of NodeJS (Express) using DI with Inversify + Service - Repository Pattern with Sequelize
- The controller-service-repository structure: What is Controller-Service-Repotiory structure
- Dependency injection with inversify
- Typescript ORM with sequelize-typescript which based on SequelizeORM
- Database migration with umzug
- Redis with ioredis (including some base helper for caching)
- DTOs using class-transformer
- Request schema valitate using Joi
- Microservice message handler for Kafka, RabbitMQ,...
- Global response formatter
- Global error handler
- Base repository + base service
- Swagger API Documentation: auto mapping Typescript DTO to swagger schema using class-to-swagger-schema
- API security using JWT + RBAC
- Linter with eslint
- Format source with prettier
- Linting git commint message using husky
- Manage application config with config
- Dockerize with default Dockerfile config
The guide for using each entity in the structure please read the *.guide.md in each folder.
npm run start:dev
npm run build
npm run start:prod
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
- Create new Model Class in src/models
- Create new repository and its repository interface in src/repository
- Create new service and its service interface in src/service
- Create new controller and its controller interface in src/controller
- Combine all to container in src/container
- Create new route in src/route
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
- 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
npm run lint
Lint fix:
npm run lint:fix
Check:
npm run prettier
Apply format:
npm run prettier:fix
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.
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