Backend
Overview
This is the backend for an IoT application built using NestJS. It interfaces with the Tuya IoT cloud platform to manage homes, rooms, devices, ...etc. This is the backend APIs project, developed with NestJS for Syncrow IOT Project.
Database Model
The database uses PostgreSQL and TypeORM. Below is an entity relationship diagram:
The main entities are:
User - Stores user account information Home - Represents a home/space Room - Represents a room/sub-space Device - Represents a connected device Product - Stores metadata about device products Other Entities - sessions, OTPs, etc.
The entities have a one-to-many relationship - a user has multiple homes, a home has multiple rooms, and a room has multiple devices.
Architecture
The application is deployed on Azure App Service using Docker containers. There are separate deployment slots for development, staging, and production environments.
Installation
First, ensure that you have Node.js v20.11
or newer (LTS ONLY) installed on your system.
To install the project dependencies, run the following command in the project root directory:
$ npm install
Running the app
# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod
Test
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:cov
ERD Diagram
Architecture
+----------------------------------+
| |
| Applications |
| |
+-----------------------------+-------------+--------------------+
| | |
| | API Calls |
| | |
| v |
| +---------------------+------------------------+ |
| | | | |
| | Dev Slot | Staging Slot | |
| | | | |
| +---------------------+------------------------+ |
| | | |
| | | |
| | | |
| +------------------+ +---------------------+ |
| | Dev Database | | Staging Database | |
| +------------------+ +---------------------+ |
| |
| +-----------------------------------------+ |
| | | |
| | Production | |
| | | |
| +-----------------------------------------+ |
| | | |
| | | |
| | | |
| +------------------+ | |
| | Production DB | | |
| | Highly Available | | |
| | Cluster | | |
| +------------------+----------------+ |
| | Production DB | | |
| | Standby Node | | |
| +------------------+ | |
| | Production DB | | |
| | Standby Node | | |
| +------------------+ | |
| | Production DB | | |
| | Standby Node | | |
| +------------------+----------------+ |
+-----------------------------------------------------------------+
CDK Deployment
• Bootstrap CDK (first time only): npx cdk bootstrap aws://482311766496/me-central-1 • List available stacks: npx cdk list • Deploy infrastructure: npx cdk deploy --require-approval never • View changes before deploy: npx cdk diff • Generate CloudFormation template: npx cdk synth • Destroy infrastructure: npx cdk destroy • Environment variables are configured in infrastructure/stack.ts • After code changes: build Docker image, push to ECR, force ECS deployment • Database seeding happens automatically on first deployment with DB_SYNC=true • Admin credentials: admin@syncrow.ae / YourSecureAdminPassword123! • Production API: https://api.syncrow.me • Health check: https://api.syncrow.me/health
GitHub Actions Deployment
• Automatic deployment on push to master/main branch • Configure GitHub Secrets (see GITHUB_SETUP.md for complete list) • Required secrets: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, JWT_SECRET, JWT_SECRET_REFRESH • Workflow builds Docker image, pushes to ECR, and deploys CDK stack • Environment variables are passed securely via GitHub Secrets • Manual deployment: Go to Actions tab and run "Deploy Backend to AWS" workflow • Check deployment status in GitHub Actions tab • Logs available in CloudWatch under /ecs/syncrow-backend log group