How abujubair7/medicine-tracker works
1. What this repository is
- A Node.js backend for the Dosely medicine-tracking app.
- It is built with Express, TypeScript, and TypeORM, and persists data in PostgreSQL.
- The backend is organized as a modular API service rather than a monolith with a single large controller.
2. Top-level application shape
- src/index.ts and src/main.ts are the startup path.
- appController.ts and appServices.ts appear to provide root-level application wiring or shared endpoints, while feature logic lives in modules.
- Runtime entry is therefore: bootstrap app → register middleware/routes → connect database → serve HTTP requests.
3. Domain modules are the main architectural boundary
- There are two primary feature modules under src/modules/:
- user/ for authentication and user management
- stock/ for medicine stock management
- Each module follows the same internal pattern:
- DTOs for input shape/validation
- entities for database mapping
- services for business logic
- controllers for HTTP handling
- This makes the request flow and ownership boundaries easy to diagram.
4. Request handling flow
- HTTP request enters Express.
- Global middleware runs first:
- authMiddleware.ts for token-based access control
- validateDTO.ts for request validation
- Controllers translate the request into service calls.
- Services contain the actual business rules and talk to TypeORM repositories/entities.
- Responses are then formatted back by the controller layer.
5. User subsystem
- The user module supports signup, login, and Google login, plus authenticated user lookup endpoints.
- userEntity.ts maps user records to the database.
- createUser.dto.ts and loginUser.dto.ts define the allowed request payloads.
- This module is responsible for identity and authentication-related state, including JWT-based access patterns implied by the middleware and README.
6. Stock subsystem
- The stock module owns medicine containers and medicine records.
- stockEntity.ts and medicineEntity.ts represent the database side of that domain.
- DTOs separate stock container creation/update data from medicine insertion/update data.
- Endpoints in this module cover:
- listing stocks for a user
- creating, renaming, and deleting stocks
- adding, updating, and deleting medicines within a stock
7. Important business logic boundary
- src/helpers/deductionHelper.ts is shared business logic rather than controller logic.
- It is used for automatic medicine deduction behavior when a stock is fetched.
- This is a key domain behavior that sits between the service layer and persistence layer, not in the HTTP layer.
8. Database and migration architecture
- src/database/data-source.ts is the TypeORM configuration boundary between app code and PostgreSQL.
- Active schema evolution happens through src/database/migrations/.
- archived_migrations/ contains older migration history kept for reference, while migrations/ contains the current deployable migration chain.
- The repository therefore relies on migration-driven schema changes rather than sync-on-start behavior.
9. Performance-oriented data access
- The README indicates stock listing is optimized to return a medicine count without loading every medicine row.
- The stock create/insert flows are designed to return small, targeted payloads rather than re-fetching full aggregates.
- That suggests the services are deliberately shaped to minimize database load and keep API responses lightweight.
10. Security and API protection
- Authentication is enforced at the middleware layer, likely using JWTs and request headers.
- Validation is also centralized, so DTOs act as a contract before business logic runs.
- This keeps security and input sanity at the edges of the system, not scattered across services.
11. Testing architecture
- Tests are split into unit/ and integration/.
- Unit tests cover isolated logic such as services, middleware, helpers, and validation.
- Integration tests cover HTTP routes with a lightweight app setup.
- The tests are structured to avoid depending on a real database, which keeps the core architecture service-oriented and mock-friendly.
12. Delivery and operational shape
- The repo includes Docker support via Dockerfile and compose.yml, indicating containerized local/dev/prod execution.
- GitHub Actions CI is present in .github/workflows/ci.yml, so build/test verification is part of the pipeline.
- vercel.json suggests deployment configuration is also tracked at the repo level, even though the app itself is a traditional backend API service.
Main entry points: main.ts main.ts · stockServices.ts stock services · userServices.test.ts unit tests · userRoutes.test.ts integration tests
Components 23
Runtime API
Application bootstrap and request edge
- index.ts · Process entrypoint
src/index.ts - main.ts · Wires app startup
src/main.ts - app controller · Shared application endpoints
src/appController.ts - app services · Common application service layer
src/appServices.ts - auth middleware · Token-based access control
src/middleware/authMiddleware.ts - validate DTO · DTO validation boundary
src/middleware/validateDTO.ts
Domain Modules
Feature boundaries for user and stock logic
- user controller · Auth and user endpoints
src/modules/user/userController.ts - user services · Signup, login, Google login
src/modules/user/userServices.ts - user entity · User persistence model
src/modules/user/entities/userEntity.ts - user DTOs · Create and login payloads
src/modules/user/dto/createUser.dto.ts - stock controller · Stock and medicine endpoints
src/modules/stock/stockController.ts - stock services · Stock and medicine rules
src/modules/stock/stockServices.ts - stock entities · Stock and medicine persistence models
src/modules/stock/entities/stockEntity.ts - stock DTOs · Stock and medicine payloads
src/modules/stock/dto/stock.dto.ts - deduction helper · Automatic medicine deduction logic
src/helpers/deductionHelper.ts
Persistence
Database configuration and schema evolution
- data source · PostgreSQL connection boundary
src/database/data-source.ts - migrations · Current deployable migration chain
src/database/migrations/1769419957484-DeployInit.ts - archived migrations · Older migration chain kept for reference
src/database/archived_migrations/1769068927828-InitialMigration.ts
Delivery
Container and CI/CD support
- Dockerfile · Image build definition
Dockerfile - compose.yml · Container orchestration for local runs
compose.yml - ci workflow · GitHub Actions test/build automation
.github/workflows/ci.yml
Testing
Unit and integration test structure
- unit tests · Isolated logic and middleware tests
src/tests/unit/userServices.test.ts - integration tests · HTTP route coverage
src/tests/integration/userRoutes.test.ts
Connections 31
- index.ts → main.ts: starts · Process entry enters app bootstrap
- main.ts → auth middleware: registers · App wires global request guard
- main.ts → validate DTO: registers · App wires request validation
- main.ts → app controller: mounts · Root endpoints are attached to Express
- main.ts → data source: connects · App connects to PostgreSQL via TypeORM
- main.ts → user controller: routes · User module is mounted into the API
- main.ts → stock controller: routes · Stock module is mounted into the API
- app controller → app services: uses · Root endpoints delegate shared logic
- user controller → validate DTO: validates · DTO validation occurs before service calls
- user controller → user services: calls · HTTP handling delegates to business logic
- user services → user entity: persists · User logic reads/writes user records
- user services → user DTOs: consumes · Service layer consumes shaped inputs
- stock controller → validate DTO: validates · Stock requests are DTO-checked
- stock controller → stock services: calls · HTTP handling delegates to stock logic
- stock services → stock entities: persists · Stock logic reads/writes medicine data
- stock services → stock DTOs: consumes · Service layer consumes stock payloads
- stock services → deduction helper: uses · Automatic deduction is shared domain logic
- stock services → data source: queries · Services use TypeORM-backed database access
- data source → migrations: applies · Active schema changes are driven by migrations
- data source → archived migrations: references · Older migration history is kept separate
- unit tests → user services: covers · Unit tests exercise isolated service logic
- unit tests → stock services: covers · Unit tests exercise stock logic
- unit tests → auth middleware: covers · Middleware behavior is tested in isolation
- unit tests → validate DTO: covers · Validation behavior is tested in isolation
- integration tests → main.ts: boots · Integration tests run against the app setup
- integration tests → user controller: hits · User routes are tested over HTTP
- integration tests → stock controller: hits · Stock routes are tested over HTTP
- ci workflow → unit tests: runs · CI verifies isolated logic
- ci workflow → integration tests: runs · CI verifies route behavior
- Dockerfile → main.ts: packages · Container image runs the API entrypoint
- compose.yml → Dockerfile: orchestrates · Compose coordinates containerized execution