Onboarding Guide: treetracker-api (TreeTracker API)
Generated from the Understand-Anything knowledge graph. Source commit 85258c8.
Project Overview
The TreeTracker API is a Greenstand Node.js/Express microservice. It exposes a REST API over PostgreSQL (via Knex), validates requests with Joi and express-validator, serves Swagger docs, and exchanges domain events over RabbitMQ (rascal/amqplib). It stores grower and capture images in AWS S3.
- Languages: JavaScript, SQL, JSON, YAML, Dockerfile, shell, Markdown
- Frameworks: Express, Knex, Joi, Mocha, Docker, GitHub Actions
- Entry points:
server/server.js(runtime boot) andserver/app.js(Express factory)
The code follows a layered, MVC-like flow: router → handler → service → model → repository → database, with messaging and object storage as side integrations.
Architecture Layers
- API Layer
Files: 22
What it holds: Express routers and handlers for captures, trees, tags, grower accounts; Joi request schemas; Swagger/OpenAPI doc generation. - Service Layer
Files: 11
What it holds: Business logic and orchestration (capture, tree, tag, grower account, S3, queue, legacy API, domain events). Coordinates repositories and external systems. - Domain Models & Data Access
Files: 18
What it holds: Domain model constructors and Knex repositories (includingBaseRepository) that read/write PostgreSQL entities. - Platform Integration
Files: 5
What it holds: Adapters to external systems: Postgres session/Knex, RabbitMQ client + config, AWS S3 client. - Application Core & Utilities
Files: 8
What it holds: Express app assembly, server bootstrap, setup,knexfile.js, shared utilities (HttpError, enums, helpers). - Database Schema & Data
Files: 168
What it holds: Knex/db-migrate SQL and JS migrations, seed data + seed CLI, ETL, and generated table schema nodes. - Testing
Files: 22
What it holds: Mocha integration specs, mocks, and inline unit specs. - Infrastructure & CI/CD
Files: 43
What it holds: Dockerfile, Kustomize overlays (base/dev/prod/test) for Kubernetes, GitHub Actions workflows. - Project Config & Documentation
Files: 19
What it holds: Repo config (package.json, lint/prettier/husky/release, CODEOWNERS), docs (README, CHANGELOG, ADRs, API spec), assets, scripts.
Key Concepts
- Single database session per request. Services run over one
Session(server/infra/database/Session.js) that owns the transaction lifecycle (begin/commit/rollback). Handlers stay free of transaction concerns. HttpErrordrives rollback. The custom error type carries a status code and arollbackflag. The global error middleware (server/utils/utils.js) uses it to unwind the transaction and map errors to HTTP responses.- Repository inheritance. Every repository extends
BaseRepository(server/repositories/BaseRepository.js), which suppliesgetById, filtered queries, counting, create, and update primitives over Knex. - Event backbone. Domain events (
capture-created,tree-created, ...) are published and consumed through RabbitMQ.QueueServiceowns publish/subscribe;RabbitMQ.jswraps the rascal Broker;EventHandlerServicedispatches messages;DomainEvent.jspersists the records. - Geospatial matching. Captures are matched to trees using PostGIS spatial queries (
st_dwithin, GiST geography indexes). Test helpers convert POINT strings into PostGIS geometry. - Docs stay near the routes. Each handler ships a
docs.js(Swagger paths) andschemas.js(Joi), assembled byswaggerDoc.jsinto the single served OpenAPI spec.
Guided Tour
Follow these steps in order to learn the codebase end to end:
- Project Overview: Read the
READMEfor purpose, architecture, and setup/migration/seeding workflows. - Bootstrap and Express Wiring:
server/server.jsboots;server/app.jswires middleware, Swagger UI, the router, error handler, and event handlers. - Routing and API Surface:
routes/index.jsaggregates feature routers;treeRoutes.jsis a representative router;swaggerDoc.jsassembles the OpenAPI spec. - Request Handlers and Validation:
treeHandler/index.jshandles tree CRUD/matching/tags;schemas.js(Joi) guards inputs;docs.jssupplies Swagger paths. - The Service Layer:
TreeService.jsorchestrates over one session;HttpError.jscarries status + rollback flag. - Domain Models and Repositories:
models/tree.jsholds domain logic;TreeRepository.jsextendsBaseRepository.js. - Database Connection and Transactions:
knex.jsconfigures the shared connection;Session.jsmanages the transaction lifecycle. - Database Schema and Seed Data: SQL migrations create the
treetable (capture ref, GPS, species, status, geo indexes); seed fixtures make a fresh DB usable. - Asynchronous Messaging:
QueueService.js,RabbitMQ.js,EventHandlerService.js,DomainEvent.jsimplement publish/subscribe over rascal. - Image Upload to S3:
s3.jsbuilds the client;S3Service.jsexposesuploadImage;GrowerAccountImageService.jsuploads + persists in one transaction. - Testing Approach:
tree.spec.jsexercises endpoints end-to-end;__tests__/utils.jsseeds/cleans data and builds PostGIS geometry. - Containerization, CI, and Deployment:
Dockerfile(node:16-alpine,node .); PR CI runs lint + migrations + tests against Postgres/RabbitMQ service containers; Kustomize deploys 2 replicas with sealed secrets.
File Map (by layer)
- API Layer:
server/routes/index.js,server/routes/treeRoutes.js(+ capture/tag/grower routers),server/handlers/*/index.js,server/handlers/*/schemas.js,server/handlers/*/docs.js,server/handlers/swaggerDoc.js - Service Layer:
server/services/TreeService.js,CaptureService.js,TagService.js,GrowerAccountService.js,GrowerAccountImageService.js,S3Service.js,QueueService.js,EventHandlerService.js - Domain Models & Data Access:
server/models/*.js,server/repositories/*Repository.js,server/repositories/BaseRepository.js - Platform Integration:
server/infra/database/knex.js,Session.js,server/infra/messaging/RabbitMQ.js,server/infra/s3.js - Application Core & Utilities:
server/app.js,server/server.js,server/setup.js,knexfile.js,server/utils/*.js,server/infra/HttpError.js - Database:
database/migrations/**,database/seeds/** - Infrastructure & CI/CD:
Dockerfile,deployment/**(Kustomize),.github/workflows/**
Complexity Hotspots
Approach these areas carefully: they carry the most logic or the largest surface (56 files rated moderate+):
server/models/Capture.js(complex): capture query/create/update logic plus domain-event emission.server/handlers/treeHandler/docs.jsandcaptureHandler/docs.js(complex): the largest Swagger definition files.server/services/TreeService.js,GrowerAccountService.js(moderate): transaction + queue orchestration.server/handlers/*/index.js(moderate): the request handlers where HTTP meets the domain.server/utils/utils.js(moderate): async handler wrapper + global error middleware.__tests__/integration/tree/tree.spec.js,capture/capture.spec.js(complex): full-stack integration suites; read them to see real end-to-end behavior.docs/api/spec/treetracker.v1.yaml(complex): the OpenAPI contract; large but authoritative.database/seeds/data/*.json(complex): large seed fixtures; big but low-risk data files.
Regenerate with /understand then /understand-onboard. See docs/regenerating.md in the docs repo.