Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

PCRinus/looties-backend

Repository files navigation

looties-backend

Requirements

  • Node v18
  • Docker

We also use npm instead of yarn or pnpm to be consistent with the frontend.

Running locally

  1. First install the vscode extensions for a better dev experience. You can find them in the .vscode/extensions.json file. To install them, go to the Extensions tab, filter by Recommended and click on the Workspace Recommendations tab. Docs if you need help

  2. Setup the environment variables. There is an .env.example file in the root of the project. Copy it and rename it to .env. Fill in the values.

  3. Install the dependencies with npm install.

  4. After you have Docker installed, run the following command:

docker-compose up -d --build

This will start the Postgres database and the NestJS API in watch mode. Any changes you make to the source code will be reflected in the container using Webpack HMR. It will also run the Prisma migrations and seed the database.

Ports

The only exposed port is 3000.

  • To use the REST APIs, call localhost:3000. There is also a swagger page at localhost:3000/api.
  • To use the Chat websocket, call localhost:3000/chat
  • To use the Live Drops websocket, call localhost:3000/live-drops

Going to localhost:3000/api will also open up the swagger page.

There is also a pgAdmin container running, that you can access at localhost:5050.

Installing new dependencies

When installing new dependencies with npm, you will need to rebuild the Docker container. You can do this by running the following command:

docker-compose up --build -dV (note the capital V)

The -V flag will remove any anonymous volumes that are attached to the container, such as the one were the node_modules are stored.

Working with migrations

Because Prisma is a terrible piece of software, it will crash if you try to do this inside docker:

  • Change the schema file
  • Run prisma generate
  • Restart the container
  • Run prisma migrate from inside the container

This works locally, but not in docker for some reason. So instead, you will need to do the following:

  • Have the container already running
  • Change the schema file
  • Run npx prisma migrate dev --name NameOfMigration fom inside the container with the name of the new migration
  • Restart the container

This will ensure that everything works as expected.

To reset the db, run npx prisma migrate reset inside the docker container terminal.

To seed the db, run npx prisma db seed inside the docker container terminal.

Deploying manually to the DigitalOcean droplet

We already have a droplet setup in DigitalOcean here.

To deploy the docker-compose file manually to the droplet, do the following

  • Setup a docker remote context for the droplet: docker context create remote --docker "host=ssh://[email protected]".
  • Switch to the remote context: docker context use remote.
  • Run docker compose -f docker-compose.yml up --build -dV to deploy the docker-compose file to the droplet.
  • Inspect changes with docker compose ps
  • Change your context back to local with docker context use default

Deploy guide

Running tests

Tests are run with Jest. You can run them with the following command:

npm run test

E2E test are run with:

npm run test:e2e

Troubleshooting

Prisma

Sometimes you might get this message in the docker logs:

Error: Prisma Migrate has detected that the environment is non-interactive, which is not supported.

`prisma migrate dev` is an interactive command designed to create new migrations and evolve the database in development.
To apply existing migrations in deployments, use prisma migrate deploy.
See https://www.prisma.io/docs/reference/api-reference/command-reference#migrate-deploy

This is because prisma thinks the shell it is being run from is non interactive. To fix this, run the docker-compose in interactive mode.

CORS

Be aware that you need 2 cors configurations:

  • One for the NestJS API
  • One for the websockets server

There is a cors middleware in the NestJS API setup, but you also need to configure cors in the websockets server (chat for example).

Digitalocean builds failing because of new Prisma types/properties

DigitalOcean will cache the node_modules folder between builds if the package.json has not changed.

This will sometimes make the build fail, because of new db schema properties introduced in the latest migration. To fix this, clear the deploy cache, and rebuild.

About

Looties backend server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published