Skip to content

DailyDesk Backend

This is the backend project for DailyDesk.

Prerequisites

It is recommended to use Node.js v16 for this project.

To manage multiple Node.js versions, we recommend using a version manager:

Database & Cache

  • Redis: It is recommended to run Redis using Docker with the redis-stack image, which includes RedisInsight.

    docker run --name daily-desk-redis -p 6379:6379 -p 8001:8001 -d redis/redis-stack:latest
    
    Is running, you can access RedisInsight at http://localhost:8001. Install Docker Desktop if you haven't already.

  • PostgreSQL: You can install PostgreSQL locally using Postgres.app (macOS) or the Windows Installer. Alternatively, you can run it via Docker.

    You can also connect to the shared development database if credentials are provided.

  • pgAdmin 4: Recommended for managing PostgreSQL databases. Download pgAdmin 4.

Database Connection via SSH Tunnel

If the development database is located in a private network, this project includes an automated script to establish the SSH tunnel when you start the server.

  1. Ensure you have your Bastion SSH key (PEM file) available locally.
  2. In your .env file, configure the following:
    SSH_REMOTE_DB_HOST=<Actual RDS Endpoint>
    BASTION_KEY_PATH=<Absolute Path to PEM file>
    # Optional overrides
    # BASTION_HOST=ec2-98-81-247-161.compute-1.amazonaws.com
    # BASTION_USER=ec2-user
    
  3. Set DB_HOST=localhost in your .env.

When you run npm run start:dev, the script will automatically open the tunnel. Manual SSH connection is no longer required.

Environment Setup

  1. Run the setup script to create your .env file and look up AWS resources:
    npm run setup:env
    

    Note: This script automatically fetches configurations (including database credentials) for the Remote AWS Development Environment.

    If you intend to run the application with a Local Database (via Option 2 below), you must manually update the following variables in your .env file after running the script: * DB_HOST=localhost * DB_PORT=5432 (or your local port) * DB_USERNAME=postgres (or your local user) * DB_PASSWORD=... * DB_NAME=...

  2. Open the newly created .env file.
  3. Replace the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY values with the credentials provided by your administrator.
  4. Verify other variables (Host, Port, Database Credentials, etc.) are correct.

Installation

Install the project dependencies using the --legacy-peer-deps flag:

npm install --legacy-peer-deps

Running the Application

Running the Application

There are two main ways to run the application in development mode, depending on which database you want to connect to.

Option 1: Connect to Remote AWS Dev Database

This option automatically opens an SSH tunnel to the AWS development database via a Bastion host.

Command:

npm run start:dev:remote

What happens: 1. The script (scripts/start-dev-remote.ts) reads your configured BASTION_HOST and BASTION_KEY_PATH. 2. It opens a secure SSH tunnel forwarding your local port (default 5433 or as configured in DB_PORT) to the remote RDS instance. 3. It starts the NestJS server (nest start --watch).

Note: The Bastion host is an EC2 instance. If this instance is rebooted, its public IP address may change. If you cannot connect, verifying the current BASTION_HOST IP address.

Option 2: Connect to Local Database

Use this option if you are running a local PostgreSQL instance and do not need the remote data.

Command:

npm run start:dev

What happens: 1. The application starts immediately (nest start --watch). 2. It attempts to connect to the database configured in your .env (typically localhost on DB_PORT) without establishing a tunnel. 3. Ensure your local PostgreSQL service is running and matches the credentials in .env.

TypeORM Migrations

List all available migration commands:

npm run typeorm:cli

Generate initial migration from TypeORM Entities:

npm run migration:generate -- -n <MIGRATION_NAME>

Create a migration file:

npm run migration:create -- -n <MIGRATION_NAME>

Run all new migrations:

npm run migration:run

License

Nest is MIT licensed.