Giter VIP home page Giter VIP logo

practice-cqrs's Introduction

CQRS and Event Sourcing Practice Project

This project creates a JSON API around patient records. The write API implements event sourcing based on these immutable patient events - admit patient, transfer patient, discharge patient, update patient name and update patient age. These events are aggregated asynchronously to generate a most recent patient record in the database. The read API just queries the database for these generated record(s).

Directory Structure

Directory Description
activities Contains temporal activities
cmd Contains program entrypoint in server subpackage
config Configuration settings for db, temporal and api server
db Database client
ent Ent ORM schema and generated files
eventstore Event store implementation using PostgreSQL
handlers Container HTTP handlers (subpackages seggregate commands and queries handlers)
repositories Contains repositories and models
server API server setup
workflows Contains temporal workflows

Application Configuration (Environment Variables)

Variable Description
PSQL_HOST Host name for PostgreSQL database
PSQL_PORT Port for PostgreSQL database
PSQL_USER PostgreSQL database user name
PSQL_PASSWORD PostgreSQL database user password
PSQL_DB PostgreSQL database name
GIN_MODE debug or release mode for Gin
ENV Application mode: dev or prod
SERVER_PORT Server port to listen on (default: 3001)
TEMPORAL_HOST Temporal host name
TEMPORAL_PORT Temporal port number
TEMPORAL_NAMESPACE Temporal namespace to use

Build/Run (Docker Compose)

Run the following steps to build/run the application -

  1. cp .env.example .env
  2. docker compose up --build -d

Now, docker containers for postgresql server, temporal server and the application server should be up and running and you can play with the API endpoints.

You can also access temporal web ui at this address: http://localhost:8080 and see which workflows are being executed.

Build/Run (Manual)

  1. Run cp .env.example .env command and set the environment variables for postgresql and temporal in the .env file.
  2. Build command - go build -o api ./cmd/server
  3. Run command - ./api

Now, application server should be up and running and you can play with the API endpoints.

Note: This manual build/run requires pre-running instances of Temporal and PostgreSQL. The application server will connect with these instances.

Endpoints

Base URL: http://localhost:3001

The API exposes the following endpoints -

Commands

Admit New Patient

POST /patient/admit

Request Body

{
    "name": "Foo Name",
    "ward": 10,
    "age": 25
}

The response contains id field which is the ID for new patient.

Transfer Patient to Ward

POST /patient/transfer

Request Body

{
    "id": "<patient-id-here>",
    "ward": 11
}

Discharge Patient

POST /patient/discharge

Request Body

{
    "id": "<patient-id-here>",
}

Update Patient Name

POST /patient/updateName

Request Body

{
    "id": "<patient-id-here>",
    "name": "New Foo Name",
}

Update Patient Age

POST /patient/updateAge

Request Body

{
    "id": "<patient-id-here>",
    "age": 30,
}

Queries

Get All Patients

GET /patient/all

Get Patient By ID

GET /patient/id/<patient-id-here>

Get Patient By Name

GET /patient/name/<patient-name>

practice-cqrs's People

Contributors

cod3rboy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.