Skip to content

Instantly share code, notes, and snippets.

@coutinhomarco
Last active May 18, 2024 13:51
Show Gist options
  • Save coutinhomarco/5af53431c142eba58e4fa989d174fff8 to your computer and use it in GitHub Desktop.
Save coutinhomarco/5af53431c142eba58e4fa989d174fff8 to your computer and use it in GitHub Desktop.
Understanding CQRS and Using `express-typescript-bullmq` CLI Tool

Understanding CQRS and Using express-typescript-bullmq CLI Tool

CQRS Architecture

Command Query Responsibility Segregation (CQRS) is a design pattern that separates the read and write operations of a data store. This approach provides multiple benefits, including scalability, optimized queries, and improved maintainability.

Key Concepts

  1. Commands: Represent the intent to perform a change. These are write operations that modify the state.
  2. Queries: Represent the intent to retrieve data. These are read operations that do not modify the state.
  3. Segregation: By separating the read and write operations, each can be optimized independently. For instance, read operations can use optimized views or caches, while write operations can enforce strict validation rules.

Benefits

  • Scalability: Read and write operations can be scaled independently.
  • Performance: Optimized queries for read operations can improve performance.
  • Security: Different permissions can be applied to read and write operations.

Using express-typescript-bullmq CLI Tool

The express-typescript-bullmq CLI tool helps you quickly set up a Node.js project with Express, TypeScript, BullMQ, and Redis, following best practices and a CQRS-inspired architecture.

Features

  • Generates a Node.js project with a predefined structure.
  • Sets up Express for handling HTTP requests.
  • Uses TypeScript for type safety.
  • Configures BullMQ for job processing.
  • Integrates Redis for data storage and message brokering.
  • Sets up ESLint for code quality and linting.
  • Includes sample controllers, routes, models, and services.

Installation

To install the CLI tool globally, run:

npm install -g express-typescript-bullmq

Usage

After installing, use the CLI to generate a new project:

express-typescript-bullmq

You will be prompted to enter a project name. The CLI will create a new directory with the specified name and generate the project structure inside it.

Project Structure

The generated project follows a CQRS-inspired structure:

project-name/
├── src/
│   ├── @types/
│   │   ├── custom-types.d.ts
│   │   └── ServiceResponse.ts
│   ├── api/
│   │   ├── controllers/
│   │   │   └── user/
│   │   │       ├── user.command.controller.ts
│   │   │       └── user.query.controller.ts
│   │   ├── models/
│   │   │   └── user.model.ts
│   │   ├── routes/
│   │   │   └── user.routes.ts
│   │   ├── services/
│   │   │   └── user/
│   │   │       ├── user.command.service.ts
│   │   │       └── user.query.service.ts
│   │   ├── utils/
│   │   │   ├── middleware/
│   │   │   │   └── auth.ts
│   │   │   └── validations/
│   │   │       └── user.validations.ts
│   ├── config/
│   │   └── bullmq.ts
│   ├── app.ts
├── .gitignore
├── package.json
├── README.md
└── tsconfig.json

This structure separates the read (query) and write (command) operations, aligning with the CQRS pattern principles.

Getting Started

  1. Install Dependencies: Navigate to the project directory and install the dependencies:

    cd project-name
    npm install
  2. Run the Project: Start the development server:

    npm run dev

Conclusion

The express-typescript-bullmq CLI tool simplifies the setup process for Node.js projects with a CQRS-inspired structure, leveraging the power of Express, TypeScript, BullMQ, and Redis. Happy coding!


For more details, reach out to Marco Coutinho.


Additional Resources by Marco Coutinho

  • Step-by-Step Guide to Setting Up an EC2 Instance with Nginx and SSL
    View Gist

  • Adding a Second API to Same EC2
    View Gist

Feel free to reach out if you have any questions or need further details!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment