Backend Documentation - OtagoPolytechnic/Air-Quality-Monitoring-System GitHub Wiki

Project Setup

To setup locally, from the project root.

From the bash terminal run the following commands:

cd backend
npm install
touch .env

Open docker desktop and open the terminal: image

Run the following command:

docker run -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d postgres

IMPORTANT If you already have a .env and for some reason you have used production API URL, please remove it.

Paste in your created .env file:

DATABASE_URL=postgresql://postgres:mysecretpassword@localhost:5432/postgres

You will also need to attach the Firebase Admin SDK keys to the .env. These are found in the fresh air keepass under firebase .env info in the notes They will like something like this:

DATABASE_URL=<SENSITIVE>
FIREBASE_TYPE=<SENSITIVE>
FIREBASE_PROJECT_ID=<SENSITIVE>
etc

Once the above is done, run the following from the terminal to get started

npm run generate  
npm run migrate **This may show some errors, it is okay to ignore it for now and continue**    
npm run seed
npm run dev

From here, move into the frontend directory

cd ../frontend
npm install
touch .env

Add the following to your frontend .env

VITE_BACKEND_API_KEY='http://localhost:3000'

then run

npm run dev

Docker Cleanup

NEED TO CHECK WITH ROB IF THIS STILL APPLIES When finished with local environment, delete containers, images, volumes from docker desktop.
Remove DATABASE_URL from your .env to prevent any potential mishaps. Or delete your .env.

Testing

From the project root

cd backend
npm install
npm run test

Jest testing uses the Babel module plugin to allow ES6 imports via the .babelrc file

{
    "env": {
      "test": {
        "plugins": ["@babel/plugin-transform-modules-commonjs"]
      }
    }
  }

Database Backups

The database backups are done all automatically using crontab. The crontab runs three bash scripts called BackupScript.sh, CopyBackup.sh and RemoveoldBackups.sh. These three scripts run at 12:01am, 12:03am and 12:02am respectively. The scripts are located in a folder called backup on the home directory of the live VM and the RemoveOldBackups.sh also lives on the database backup VM in the same directory structure as well. You can find the backup scripts in the folder called Bash Scripts in the Fresh Air repo.

⚠️ **GitHub.com Fallback** ⚠️