CRUD Golang API Multitenancy MongoDB Fiber
Requirements
- Go installed in the system
- A running instance of postgres db
- Update the
.env
file with the postgres path, db and credentials
Run the following command:
go run main.go
Technologies used
App Flow
- App creates Server.
- Server create Middeware and routes.
- Server receive request.
- Middleware find or create tenant dependencies (Service, Repository, Database).
- Middleware attach tenant dependecies at the request.
- Controller get tenant dependecies.
- Controller use Service dependency.
- Service use Repository dependency.
- Repository use Database repository.
- Database use tenant database.
UML diagrams
Components
graph LR
Serv(Server) -->
M(Middlewares) -->
DI(Dependencies) -->
C(Controllers) -->
Svc(Services) -->
R(Repository) -->
D(Database)
Sequence
sequenceDiagram
Server ->> Middleware:
Middleware ->> Dependencies:
Dependencies -->> Dependencies:
Note right of Dependencies: Find or Create <br/> a tenant dependecies <br/> only if configured
Dependencies ->> Middleware:
Middleware ->> Controller:
Controller ->> Service:
Service ->> Repository:
Repository ->> Database:
How Use
Configure the configurations.json
file to connect to differents database per tenant.
Use the request.http
to know how send request to api.
The data
field can contains any schema of JSON, the api save and get without problems.