{"id":13847382,"url":"https://github.com/kyhsa93/nestjs-rest-cqrs-example","last_synced_at":"2025-05-15T05:03:41.738Z","repository":{"id":43497917,"uuid":"209052343","full_name":"kyhsa93/nestjs-rest-cqrs-example","owner":"kyhsa93","description":"Example for Nest.js, MySQL, Redis, REST api, CQRS, DDD","archived":false,"fork":false,"pushed_at":"2025-03-08T13:52:35.000Z","size":1510,"stargazers_count":789,"open_issues_count":2,"forks_count":142,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-03-31T00:03:30.128Z","etag":null,"topics":["cqrs","ddd-cqrs","ddd-example","docker","docker-compose","dockerfile","mysql","nest","nestjs","typeorm","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kyhsa93.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-17T12:48:29.000Z","updated_at":"2025-03-30T20:30:42.000Z","dependencies_parsed_at":"2024-01-15T20:49:49.955Z","dependency_job_id":"1a1daa27-f295-410b-8d84-4e7e71377577","html_url":"https://github.com/kyhsa93/nestjs-rest-cqrs-example","commit_stats":{"total_commits":236,"total_committers":4,"mean_commits":59.0,"dds":0.07627118644067798,"last_synced_commit":"b48ecc5dd7b0dc7de59408ea680eab274bf9abe3"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyhsa93%2Fnestjs-rest-cqrs-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyhsa93%2Fnestjs-rest-cqrs-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyhsa93%2Fnestjs-rest-cqrs-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyhsa93%2Fnestjs-rest-cqrs-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kyhsa93","download_url":"https://codeload.github.com/kyhsa93/nestjs-rest-cqrs-example/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247574084,"owners_count":20960496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cqrs","ddd-cqrs","ddd-example","docker","docker-compose","dockerfile","mysql","nest","nestjs","typeorm","typescript"],"created_at":"2024-08-04T18:01:18.528Z","updated_at":"2025-04-07T01:04:38.358Z","avatar_url":"https://github.com/kyhsa93.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Nest CQRS Rest api example\n\n[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fkyhsa93%2Fnestjs-rest-cqrs-example\u0026count_bg=%2379C83D\u0026title_bg=%23555555\u0026icon=\u0026icon_color=%23E7E7E7\u0026title=hits\u0026edge_flat=false)](https://hits.seeyoufarm.com)\n\nThis is sample nestjs project.\n- link: https://nestjs.com\n\n## Introduction\n### Purpose of this Repository\nThis is a main goals of this repository:\n- Showing how you can implement application for microservice architecture\n\nDomain Driven Design and CQRS can be used to design individual services in msa.\n\nThis repository presents examples of applications using domain-driven design and CQRS using nest, a nodejs framework using typescript.\n\nThe domain-driven design consists of four layers, each consisting of interface, application, domain, and infrastructure.\n\nEach layer depends on a different layer, and we want to start with the most important layer, the domain layer.\n\nIt also briefly summarizes how CQRS is implemented with domain driven design and how event driven appears in this configuration.\n\n### Domain Layer\n---\nThe domain layer is the core layer in domain-driven design and the highest level layer that does not depend on any layer.\n\nHere, a model consisting of aggregate, entity, and vo is implemented, and the model has core business logic inside.\n\nIn addition, domain events generated by business logic execution and the repository interface of the domain model are defined here.\n\nThe repository basically manages a collection of domain objects.\n\nIn addition, with a separated interface pattern, the interface of the repository exists in the domain layer and the implementation is in the infrastructure layer.\n\nHere, when creating a domain object, the factory pattern is used to use the factory.\n\nEvents defined here are called domain events and are issued when domain logic is executed, and domain events are received by domain event handlers in the application layer and execute logic according to the event.\n\n### Application Layer\n---\nThe application layer depends on the domain layer and executes the domain logic of the domain model.\n\nIn addition, domain events generated by executing domain logic are handled through domain event handlers.\n\nIntegration events that are issued to services in external contexts are defined here, domain events received by domain event handlers are processed, and these integration events are published through the integration event publisher.\n\nIn addition to the integration event publisher, adapters for external communication are defined here as interfaces.\n\nThis allows you to configure integration with other services of msa in a synchronous or asynchronous manner.\n\nIn the application layer, the implementation of cqrs inside the service is shown.\n\nYou define command and query objects, and implement handlers that execute them.\n\nCommand and query are implemented separately, and the handler that handles them is also implemented and executed separately.\n\nThe point to note here is that access to the domain is done only by command, so all events are triggered by command.\n\nEach command and command handler creates a domain object or gets it from the repository, executes the domain logic, and stores it through the repository.\n\nOn the other hand, query is processed through query handler, but executes only the query defined as interface without accessing the domain and returns the result.\n\n### Interface Layer\n---\nIn the interface layer, it depends on the application layer, generates necessary commands and queries and executes them through the bus.\n\nThis layer defines interfaces provided to users or other services, receives http, message, etc., verifies the request and returns a response if necessary.\n\n### Infrastructure Layer\n---\nThe infrastructure layer is responsible for implementing detailed technology at each other layer.\n\nThese can be technical contents such as repository in domain, publisher, adapter in application, controller in interface, etc.\n\nExamples include database implementation and access, communication or messaging systems, caching or external communication technologies.\n\nBy having all of these detailed implementations in the infrastructure layer, you can isolate technical changes so that changes do not spread to other parts.\n\nPlease refer to the image below.[[image source]](http://seedstack.org/docs/business/layers/)\n\n\u003cimg width=\"502\" alt=\"Screenshot 2020-12-15 at 11 32 25 PM\" src=\"https://user-images.githubusercontent.com/20676870/102228546-2b27b480-3f2e-11eb-8312-453fa669612e.png\"\u003e\n\n## Getting started\nThis is REST api made by node.js, nest, redis, mysql with typescript.\n\nSo you have to get node.js environment, redis for cache, mysql for database, know typescript syntax.\n\n### Prerequisites\n---\nPlease install node.js and I recommend to use docker for your database.\n\nMy recommand node.js version is dubnium and latest docker version.\n\n* Install node.js: [https://nodejs.org/en/download/](https://nodejs.org/en/download/)\n\n* Install Docker Desktop for MAC: [https://docs.docker.com/docker-for-mac/install/](https://docs.docker.com/docker-for-mac/install/)\n\n* Install Docker Desktop for Windows: [https://docs.docker.com/docker-for-windows/install/](https://docs.docker.com/docker-for-windows/install/)\n\n* Install compose: [https://docs.docker.com/compose/install/](https://docs.docker.com/compose/install/)\n\n## Create development environment\nFirst, clone this repository into your local environment. Run followed command in your terminal.\n\n```bash\n  git clone https://github.com/kyhsa93/nestjs-rest-cqrs-example.git\n```\n\nSecond step, install package that needed in this project.\n\nIf your node.js environment is successly downloaded, you can use node package manager.\n\nRun followed command in your terminal.\n\n```bash\n  npm install\n```\n\nNext up, generate mysql and redis.\n\nIf you already have mysql, redis in your development environment, you can use that.\n\nBut if you don't have one or both, try this process.\n\nInstall docker for your OS from link in top of this documentation.\n\nAnd run followed command.\n\nIf your docker is successfully installed, you can use docker cli.\n\n```bash\n  docker run --name nest -d -p 3306:3306 -e MYSQL_DATABASE=nest -e MYSQL_ROOT_PASSWORD=test -v ~/database/nest:/var/lib/mysql mysql:5.7\n  docker run --name redis -d -p 6379:6379 redis:alpine\n\n  OR\n\n  docker-compose -f docker-compose.development.yml up -d # create mysql, redis container for development environment\n  docker-compose -f docker-compose.development.yml down  # remove created containers\n```\n\nAnd then, you can connect mysql in http://localhost:3306, user name 'root' and password is 'test'.\n\nFinaly, your develop environment is created.\n\nYou can start api with followed command.\n\n```bash\n  npm start\n```\n\nAnd if you modify code and save, you can see the process detect code changes and restart it self.\n\n## Start with docker\nIf you can use docker cli, you can build docker image.\n\n```bash\n  docker build -t nest-sample\n  docker images # list up docker images\n```\n\nAnd then you can create and run docker container using builded image.\n\n```bash\n  docker run -d -p 5000:5000 nest-sample\n  docker ps # list up running container\n```\n\nand now you can connect api through http://localhost:5000.\n\n## Start with docker compose\nDocker compose in this project is include api redis and mysql 5.7 for database.\n\nRun followed command in project directory.\n\n```bash\n  docker-compose up -d # build images, create and run containers in background\n```\n\nIf container is created, you can access api on http://localhost:5000.\n\nAnd you can access database through http://localhost:3306.\n\nDefault database user is root and password is test.\n\nIf you want apply your modified code into the running container, you can add build option.\n\n```bash\n  docker-compose up -d --build # if source code is changed, rebuild image, recreate and start container\n```\n\nAfter use compose, you have to stop and remove containers.\n\n```bash\n  docker-compose down # stop and remove container in compose\n```\n\n## Start with kubernetes\nIf you want to use kubernetes, you can use manifest.yaml for apply to your kubernetes cluster.\n\nUse minikube for create kubernetes locally or use your own kubernetes. (docker for desktop can be enable local cluster)\n\nMinikube: [https://kubernetes.io/docs/setup/learning-environment/minikube/](https://kubernetes.io/docs/setup/learning-environment/minikube/)\n\n```bash\n  kubectl apply -f manifest.yaml  # create kubernetes resource in your kubernetes using manifest.yaml file\n  kubectl delete -f manifest.yaml # delete kubernetes resource using manifest.yaml file\n```\n\nWhen all Deployment, Service and Pods is created, you can use api through http://localhost.\n\nAlso swagger ui address is http://localhost/api.\n\nIf you want see all container in deployment, you can use kubectl.\n\n```bash\n  kubectl get all # print all kubernetes default namespace\n  kubectl logs deployment.apps/nestjs-rest-cqrs-example --all-containers=true\n  kubectl logs deployment.apps/nestjs-rest-cqrs-example --all-containers=true -f\n```\n\nAbout kubernetes: [https://kubernetes.io/](https://kubernetes.io/)\n\n### Start with helm\n\nHelm can help you to manage kubernetes applications.\n\nHelm Charts help you define, install, and upgrade even the most complex Kubernetes application.\n\n```bash\n  helm install --name \u003creleasename\u003e helm # create helm chart\n  helm delete --purge \u003creleasename\u003e # delete helm chart\n```\n\nAbout helm: [https://helm.sh/](https://helm.sh/)\n\n## Configuration\nAll configuration is in [src/app.config.ts](https://github.com/kyhsa93/nestJS-sample/blob/main/src/Config.ts)\n\nMost default configuration can use through you environment values.\n\nAnd also you can modify configurations.\n\n## Documentation\nDocumentaion about this project is made swagger.\n\nStart this api and connect http://localhost:5000/api in your browser.\n\n## Scripts\n```bash\n  git clone https://github.com/kyhsa93/nestjs-rest-cqrs-example.git # clone this project\n\n  # run mysql database container\n  docker run --name nest -d -p 3306:3306 -e MYSQL_DATABASE=nest -e MYSQL_ROOT_PASSWORD=test -v ~/database/nest:/var/lib/mysql mysql:5.7\n\n  docker build -t nest-sample # build docker image named nest-sample\n\n  docker images # list up docker images\n\n  docker run -d -p 5000:5000 nest-sample # run docker container using image nameed nest-sample (host port 5000 is mapped container port 5000 and container run background process)\n\n  docker ps # list up running container\n\n  docker-compose up -d # build images, create and start containers in background\n\n  docker-compose up -d --build # if container image's change exists, rebuild image, recreate and restart container\n\n  docker-compose down # stop and remove container in compose\n\n  npm install   # install packges\n  npm test      # run test\n  npm run build # transpile typescript\n  npm start     # run sample code\n\n  docker-compose up # use compose\n  \n  kubectl apply -f manifest.yaml  # create kubernetes resource in your kubernetes using manifest.yaml file\n  kubectl delete -f manifest.yaml # delete kubernetes resource using manifest.yaml file\n\n  kubectl get all # print all kubernetes default namespace\n  kubectl logs deployment.apps/nestjs-rest-cqrs-example --all-containers=true\n  kubectl logs deployment.apps/nestjs-rest-cqrs-example --all-containers=true -f\n\n  helm install --name \u003creleasename\u003e helm # create helm chart\n  helm delete --purge \u003creleasename\u003e # delete helm chart\n```\n\n## Links\nGithub: [https://github.com/kyhsa93/nestjs-rest-cqrs-example](https://github.com/kyhsa93/nestjs-rest-cqrs-example)\n\nDockerhub: [https://cloud.docker.com/repository/docker/kyhsa93/nestjs-rest-cqrs-example/](https://cloud.docker.com/repository/docker/kyhsa93/nestjs-rest-cqrs-example/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyhsa93%2Fnestjs-rest-cqrs-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkyhsa93%2Fnestjs-rest-cqrs-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyhsa93%2Fnestjs-rest-cqrs-example/lists"}