This repository contains the code for the CodeSupport API. The project is written in TypeScript using the NestJS framework with a postgreSQL database.
The project uses a .env
file for configuration. Copy .env.example
and rename it to .env
. If you're going to run the application in docker, no changes to the configuration are required. If running without docker, change the settings to match your own environment.
For development purposes, this project is configured to make use of Docker for easily running the API and a local postgresql instance with no configuration required. In order to make use of this, Docker and Docker Compose must be installed on the machine. The easiest way to get these components on MacOS, Windows and Linux is to install Docker Desktop.
With docker installed, the application can be started by running the start:docker npm script with no further configuration required.
npm run start:docker
This will automatically set up docker images for postgresql and the NestJS API with hot reloading, meaning the docker container will automatically update when a file is changed.
If any NPM dependencies are installed or uninstalled, docker might not pick up the changes to the NPM dependencies. To fix this, run docker:rebuild
. This will rebuild the image and refresh any cached NPM packages.
The application can also be ran without Docker. In this case, Node.js version 12 or later (tested with 16.13) and a PostgreSQL server instance is required. Make sure you've set the correct settings in the .env
file, then start the app by running the start:debug
script.
npm run start:debug
With both the docker and non-docker approach, the application is automatically set to debug mode. If you're using visual studio code, you can easily attach a debugger by going to the 'Run and Debug' tab and selecting either the debug-docker
or debug-local
profile.
Creating a new migration can be done by running the following command docker compose exec api npm run migration:create -- [name]
.
There are a few requirements before this can be done. First being that both the database and api container are running. And that the synchronize
option is turned to false in the typeorm config, this can be done by adding NODE_ENV=testing
in the .env
file.
Lastly the database should only be filled with tables generated by migrations. To active this use the following commands to get a clean database up. docker compose down postgres
, docker volume prune
and docker compose up
. Now the migrations can be run in order to have the database up to the latest version.
To run migrations make sure both the api and database containers are up, then run the following command: docker compose exec api npm run migration:run
.
In order to generate a seed file run npm run seed:create -- [name]
. This will create a file in /src/seeds
named time-[name].ts
. Note: Typeorm doesn't automatically capitalize the name, make sure you do this in the command.
If you want to run the migrations run the following command: docker compose exec api npm run seed:run
.
Feel free to mention @LamboCreeper#6510 in the CodeSupport Discord.