diff --git a/README.md b/README.md index 0563cc7f..8c6e06fa 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,6 @@ Chayn's Bloom service offers several key features designed to support individual ## Bloom Backend Technical Documentation -Read our [Bloom Backend Tech Wiki Docs](https://github.com/chaynHQ/bloom-backend/wiki) for overviews of key concepts and data & database architecture. - Technologies Used: - [NestJS](https://nestjs.com/) - NodeJs framework for building scalable and reliable server-side applications @@ -38,7 +36,9 @@ Technologies Used: - [GitHub Actions](https://github.com/features/actions) - CI pipeline - [ESLint](https://eslint.org/) and [Prettier](https://prettier.io/) for linting and formatting. -## Local Development +Read our [Bloom Backend Tech Wiki Docs](https://github.com/chaynHQ/bloom-backend/wiki) for overviews of key concepts and data & database architecture. + +## Local Development Directions: Making an open-source contribution you have agreed to our [Code of Conduct](/CODE_OF_CONDUCT.md). diff --git a/docker-compose.yml b/docker-compose.yml index e0856df9..772bda70 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,10 @@ services: build: context: . dockerfile: Dockerfile + env_file: '.env' environment: + # To connect to local your local psql db, replace DATABASE_URL with: + # postgres://postgres:postgres@host.docker.internal:5432/bloom DATABASE_URL: postgres://postgres:postgres@db:5432/bloom ports: - 35001:35001 @@ -25,3 +28,8 @@ services: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: bloom + volumes: + - postgres-data:/var/lib/postgresql/data # path for named volume + +volumes: + postgres-data: # named volume for persistent postgres data diff --git a/docs/database-guide.md b/docs/database-guide.md index 0deaf1d9..fc32783c 100644 --- a/docs/database-guide.md +++ b/docs/database-guide.md @@ -2,65 +2,92 @@ ## How to Populate the Database -**Prerequisites:** +### Prerequisites -- [Postgres 16](https://www.postgresql.org/download/) \*technically not required if running in Docker -- Running Bloom’s backend +- Bloom's backend is running ### Summary -Most open-source contributions (like running Cypress integration tests from the frontend) require adding test data to your local database. To do this, download Bloom's test data dump file, connect to the database server, then populate the database with the backup data. +Populating your database with test data is essential for a fully functional development environment, making full-stack contributions, and running Cypress integration tests from the frontend. However, it is not necessary for smaller, isolated contributions. + +First, download Bloom's test data dump file. Next, connect to the database server, restore your database with the dump file, then verify with a query. ### Download Test Data Dump File -Download the test data dump file [linked here from our Google Drive](https://drive.google.com/file/d/1y6KJpAlpozEg3GqhK8JRdmK-s7uzJXtB/view?usp=drive_link). +First, download the test data dump file [linked here from our Google Drive](https://drive.google.com/file/d/1y6KJpAlpozEg3GqhK8JRdmK-s7uzJXtB/view?usp=drive_link). Then place this dump file in the project directory. ### Connect to Server and Add Data -There are multiple methods you can use here. For simplicity, these directions assume you are using Docker. +Next, connect to the database server and add test data from the dump file, using the appropriate commands based on how you are running the app - fully containerized, containerized app with local database, or manually without Docker. -Run the following command to restore the database from the dump file using pg_restore: +1. Restore the database from the dump file by running these pg_restore commands. -``` -docker exec -i pg_restore -U -d --clean --if-exists < -``` + **Fully Containerized App Command:** -`container_name`, `username`, and `database_name` are defined in the `docker-compose.yml` file under ‘db’. + ``` + docker exec -i pg_restore -U -d --clean --if-exists < /path/to/dumpfile.dump + ``` -Start the bloom psql database server: + `container_name`, `username`, and `database_name` are defined in the `docker-compose.yml` under the ‘db’ service. Here is the same command with the default values: -``` -docker exec -it psql -U -d -``` + ``` + docker exec -i bloom-local-db pg_restore -U postgres -d bloom --clean --if-exists < /path/to/dumpfile.dump + ``` -This will open the psql server for bloom, where you can run queries to verify the restore. + **Docker with Local DB or Running Manually Command:** -You can verify the restore by running a SQL query to test if one of our test user's data has been properly populated into the database: + ``` + pg_restore -U postgres -d bloom --clean --if-exists /path/to/dumpfile.dump + ``` -``` -SELECT * FROM public."user" users WHERE users."email" = 'tech+cypresspublic@chayn.co'; -``` +2. Next, start the bloom psql database server. -If the user exists, the database has successfully been seeded! + **Fully Containerized App Command:** + + ``` + docker exec -it psql -U -d + + # same command with default values added: + docker exec -it bloom-local-db psql -U postgres -d bloom + ``` + + **Docker with Local DB or Running Manually Command:** + + ``` + psql -U -h localhost -p 5432 -d + ``` + +3. Verify the restore by running queries in the psql server. + + ``` + SELECT \* FROM public."user" users WHERE users."email" = 'tech+cypresspublic@chayn.co'; + ``` + + If the user exists, your database has successfully been populated with test data! ### Troubleshooting +- Persistent storage is configured in the `docker-compose.yml` file using [named volumes](https://docs.docker.com/engine/storage/volumes/). This maintains your data, even if you delete your container. If you have issues with accessing persistent db storage, try replacing the volume path with an absolute path, or update your firewall settings if using WSL (especially if running integration tests). If issues with volumes persist, remove the named volumes from `docker-compose.yml` and populate your database manually as needed. +- Ensure both the 'db' and 'api' containers are running. +- Hard reset Docker containers `docker-compose up -d --force-recreate`. - If you remove **`--clean`** from the restore command but encounter duplicate object errors, the existing schema may conflict with the restore. In that case, clean the specific objects manually or use **`DROP SCHEMA public CASCADE`** before restoring. -- Verify that the dump file is valid by running: `pg_restore --list yourfile.dump` If it fails to list contents, the dump file may be corrupted or incomplete. -- In the psql server, verify the tables and columns exist with `\dt` , `\dt public.*` , and `\d public."user";` +- Verify that the dump file is valid by running: `pg_restore --list yourfile.dump` If it fails to list contents, the dump file may be corrupted or incomplete. Please notify our team if this happens. +- Verify the tables and columns exist within the psql server with `\dt` , `\dt public.*` , and `\d public."user";` - Run a **`DROP SCHEMA`** or truncate tables before running **`pg_restore`:** + ``` DROP SCHEMA public CASCADE; CREATE SCHEMA public; ``` -- Try the following: delete the existing db, create a new db with the same name, and try the restore on this new db. The db drop may throw an error, if so run the following command first. - - `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'bloom';` +- To hard reset the database in the psql server, first delete the existing db, then create a new db with the same name, and try the restore on this new db. The db drop may throw an error, if so run the following command first: + ``` + SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = 'bloom';` + ``` Then drop the database using: - - `DROP DATABASE bloom;` - + ``` + DROP DATABASE bloom; + ``` - If the sql dump file is outdated, you can update it by running `docker compose down` then `docker compose up` again as this is configured to run migrations. ### Chayn Staff - Heroku Directions @@ -72,11 +99,11 @@ Chayn staff with access to Heroku, you also have the option to seed the database 3. Replace with the correct Heroku app name in the `seed-local-db.sh file` 4. Run `chmod +x ./seed-local-db.sh` in your terminal to make the file executable - After the above has been confirmed, run +After the above has been confirmed, run - ```bash - bash seed-local-db.sh - ``` +```bash +bash seed-local-db.sh +``` ## Database Migrations diff --git a/docs/local-development.md b/docs/local-development.md index 11c16a08..61bf2d14 100644 --- a/docs/local-development.md +++ b/docs/local-development.md @@ -2,35 +2,26 @@ ## Summary -**The develop branch is our source of truth.** Fork from develop, create new feature branch, then when your PR is merged, develop will automatically merge into the main branch for deployment to production. - To run Bloom's backend: 1. Install prerequisites 2. Configure environment variables 3. Install dependencies -4. Run in a Dev Container, with Docker, or manually. -5. Populate the database (required for most fullstack contributions and running integration tests from the frontend) +4. Run the app using Docker, Dev Containers, or Manually +5. Populate the database To test the backend: - Run unit tests -- Run e2e integration tests from the frontend for fullstack contributions \*requires populating the database with data first +- Run e2e integration tests from the frontend for full-stack contributions ## Prerequisites - NodeJS v20.x - Yarn v1.x -- Docker -- PostgreSQL 16 - -#### Recommended Minimum System Requirements: +- Docker and / or PostgreSQL -- CPU: Quad-core 2.5 GHz (i5/Ryzen 5) -- Memory: 16 GB RAM -- Storage: 512 GB -- OS: Linux, macOS, Windows, or WSL2 (latest versions) -- Internet Connection: For accessing dependencies and external APIs/services +_Recommended Minimum System Requirements: CPU: Quad-core 2.5 GHz (i5/Ryzen 5), Memory: 16 GB RAM, Storage: 512 GB, OS: Linux, macOS, Windows, or WSL2 (latest versions), Internet Connection: For accessing dependencies and external APIs/services._ ## Configure Environment Variables @@ -46,15 +37,17 @@ yarn There are 3 methods you can use to run Bloom’s backend locally: -1. **Using Docker (recommended)** - runs postgres in a container. -2. **Visual Studio Code Dev Container (recommended for Visual Studio users)** - installs all dependencies and the postgres database container automatically. -3. **Manually** - manage postgres locally. +1. **Using Docker (recommended)** - the backend app is fully containerized, installing PostgreSQL is optional. +2. **Visual Studio Code Dev Container (recommended for Visual Code users)** - installs all dependencies and the PostgreSQL database container automatically. +3. **Manually (recommended for PostgreSQL users)** - run the app with yarn and manage PostgreSQL locally. -### With Docker - Recommended +### Run with Docker - Recommended -Bloom's backend is containerized and can be run solely in Docker - both the PostgreSQL database and NestJS app. This uses the least resources on your computer. To run the backend locally, make sure your system has Docker installed - you may need Docker Desktop if using a Mac or Windows. +Prequisites: Docker (we recommend [Docker Desktop](https://docs.docker.com/desktop/)), PostgreSQL (optional). -First, make sure the Docker app is running (just open the app). Then run +Bloom's backend is fully containerized - both PostgreSQL and NestJS app. This does not require PostgreSQL to be installed locally. To connect to a local PostgreSQL database instead, modify the `DATABASE_URL` in the `docker-compose.yml` file. This will enable communications between Docker and your local database. + +To start the Docker container run: ```bash docker-compose up @@ -66,9 +59,7 @@ You should see this in the shell output: Listening on localhost:35001, CTRL+C to stop ``` -_Note: you can use an application like Postman to test the apis locally_ - -### Run in Dev Container - Recommended for Visual Studio Users +### Run with Dev Container - Recommended for Visual Studio Users This method will automatically install all dependencies, IDE settings, and postgres container in a Dev Container (Docker container) within Visual Studio Code. @@ -88,9 +79,13 @@ The dev Container is configured in the `.devcontainer` directory: See [Visual Studio Code Docs: Developing Inside a Dev Container](https://code.visualstudio.com/docs/devcontainers/containers) for more info. -### Run Manually +### Run Manually - Recommended for PostgreSQL Users -Manage postgres locally to [populate the database](#populate-database), then run: +Prerequisites: PostgreSQL + +Log into PostgreSQL and create a database called "bloom". Ensure it is running on port `35000` (or your desired port). Finally, start the PostgreSQL server on your machine. + +With the psql server running, start the app: ```bash yarn start:dev @@ -148,16 +143,16 @@ See the [database-guide.md](database-guide.md) for instructions. # Git Flow and Deployment -**The develop branch is our source of truth, not main.** +**The develop branch is our source of truth, not main.** Fork from `develop`, create new feature branch, then when your PR is merged, `develop` will automatically merge into the main branch for deployment to production. Keep your branch updated by rebasing and merging feature/bug branches into `develop` as you code. -Create new branches from the `develop` base branch. There is no need to run the build command before pushing changes to GitHub, simply push and create a pull request for the new branch. GitHub Actions will run build and linting tasks automatically. Rebase and merge feature/bug branches into `develop`. - -This will trigger an automatic deployment to the staging app by Heroku. +Once your PR is merged to `develop`, this will trigger an automatic deployment to the staging app by Heroku. When changes have been tested in staging, merge `develop` into `main`. This will trigger an automatic deployment to the production app by Heroku. -# Swagger +# APIs Swagger automatically reflects all of the endpoints in the app, showing their urls and example request and response objects. To access Swagger simply run the project and visit http://localhost:35001/swagger + +For testing APIs, we recommend using tools like Postman. diff --git a/yarn.lock b/yarn.lock index 9be7fc5c..1e430405 100644 --- a/yarn.lock +++ b/yarn.lock @@ -551,14 +551,6 @@ "@firebase/util" "1.10.2" tslib "^2.1.0" -"@firebase/component@0.6.11": - version "0.6.11" - resolved "https://registry.yarnpkg.com/@firebase/component/-/component-0.6.11.tgz#228a2ff5a6b0e5970b84d4dd298bf6ed0483018e" - integrity sha512-eQbeCgPukLgsKD0Kw5wQgsMDX5LeoI1MIrziNDjmc6XDq5ZQnuUymANQgAb2wp1tSF9zDSXyxJmIUXaKgN58Ug== - dependencies: - "@firebase/util" "1.10.2" - tslib "^2.1.0" - "@firebase/data-connect@0.1.3": version "0.1.3" resolved "https://registry.yarnpkg.com/@firebase/data-connect/-/data-connect-0.1.3.tgz#a8184a2ece8a78b24240100b91ac4721e622cd4e" @@ -570,30 +562,6 @@ "@firebase/util" "1.10.2" tslib "^2.1.0" -"@firebase/database-compat@2.0.1": - version "2.0.1" - resolved "https://registry.yarnpkg.com/@firebase/database-compat/-/database-compat-2.0.1.tgz#063c4bff74782337117280fbf5b73b463a9a0638" - integrity sha512-IsFivOjdE1GrjTeKoBU/ZMenESKDXidFDzZzHBPQ/4P20ptGdrl3oLlWrV/QJqJ9lND4IidE3z4Xr5JyfUW1vg== - dependencies: - "@firebase/component" "0.6.11" - "@firebase/database" "1.0.10" - "@firebase/database-types" "1.0.7" - "@firebase/logger" "0.4.4" - "@firebase/util" "1.10.2" - tslib "^2.1.0" - -"@firebase/database-compat@^1.0.2": - version "1.0.5" - resolved "https://registry.yarnpkg.com/@firebase/database-compat/-/database-compat-1.0.5.tgz#18c2314f169942ac315e46b68f86cbe64bafe063" - integrity sha512-NDSMaDjQ+TZEMDMmzJwlTL05kh1+0Y84C+kVMaOmNOzRGRM7VHi29I6YUhCetXH+/b1Wh4ZZRyp1CuWkd8s6hg== - dependencies: - "@firebase/component" "0.6.7" - "@firebase/database" "1.0.5" - "@firebase/database-types" "1.0.3" - "@firebase/logger" "0.4.2" - "@firebase/util" "1.9.6" - tslib "^2.1.0" - "@firebase/database-compat@2.0.1", "@firebase/database-compat@^2.0.0": version "2.0.1" resolved "https://registry.yarnpkg.com/@firebase/database-compat/-/database-compat-2.0.1.tgz#063c4bff74782337117280fbf5b73b463a9a0638" @@ -627,19 +595,6 @@ faye-websocket "0.11.4" tslib "^2.1.0" -"@firebase/database@1.0.5": - version "1.0.5" - resolved "https://registry.yarnpkg.com/@firebase/database/-/database-1.0.5.tgz#09d7162b7dbc4533f17498ac6a76d5e757ab45be" - integrity sha512-cAfwBqMQuW6HbhwI3Cb/gDqZg7aR0OmaJ85WUxlnoYW2Tm4eR0hFl5FEijI3/gYPUiUcUPQvTkGV222VkT7KPw== - dependencies: - "@firebase/app-check-interop-types" "0.3.2" - "@firebase/auth-interop-types" "0.2.3" - "@firebase/component" "0.6.7" - "@firebase/logger" "0.4.2" - "@firebase/util" "1.9.6" - faye-websocket "0.11.4" - tslib "^2.1.0" - "@firebase/firestore-compat@0.3.40": version "0.3.40" resolved "https://registry.yarnpkg.com/@firebase/firestore-compat/-/firestore-compat-0.3.40.tgz#3f4cfc2d25d2f25d9925cdf5903c0b49bfdaeebc" @@ -740,13 +695,6 @@ "@firebase/util" "1.10.2" tslib "^2.1.0" -"@firebase/logger@0.4.4": - version "0.4.4" - resolved "https://registry.yarnpkg.com/@firebase/logger/-/logger-0.4.4.tgz#29e8379d20fd1149349a195ee6deee4573a86f48" - integrity sha512-mH0PEh1zoXGnaR8gD1DeGeNZtWFKbnz9hDO91dIml3iou1gpOnLqXQ2dJfB71dj6dpmUjcQ6phY3ZZJbjErr9g== - dependencies: - tslib "^2.1.0" - "@firebase/messaging-interop-types@0.2.3": version "0.2.3" resolved "https://registry.yarnpkg.com/@firebase/messaging-interop-types/-/messaging-interop-types-0.2.3.tgz#e647c9cd1beecfe6a6e82018a6eec37555e4da3e" @@ -840,9 +788,6 @@ version "0.13.4" resolved "https://registry.yarnpkg.com/@firebase/storage/-/storage-0.13.4.tgz#83a3b638ffb8dbb8cb4f58d25c0961dfebc7cd9d" integrity sha512-b1KaTTRiMupFurIhpGIbReaWev0k5O3ouTHkAPcEssT+FvU3q/1JwzvkX4+ZdB60Fc43Mbp8qQ1gWfT0Z2FP9Q== - dependencies: - "@firebase/component" "0.6.11" - "@firebase/util" "1.10.2" dependencies: "@firebase/component" "0.6.11" "@firebase/util" "1.10.2" @@ -855,13 +800,6 @@ dependencies: tslib "^2.1.0" -"@firebase/util@1.10.2": - version "1.10.2" - resolved "https://registry.yarnpkg.com/@firebase/util/-/util-1.10.2.tgz#4dbb565cfbdf51b4fb2081c5093dba3037d49a35" - integrity sha512-qnSHIoE9FK+HYnNhTI8q14evyqbc/vHRivfB4TgCIUOl4tosmKSQlp7ltymOlMP4xVIJTg5wrkfcZ60X4nUf7Q== - dependencies: - tslib "^2.1.0" - "@firebase/vertexai@1.0.2": version "1.0.2" resolved "https://registry.yarnpkg.com/@firebase/vertexai/-/vertexai-1.0.2.tgz#e9104361e88531d9f6f7c9f25ea64287f0a061b9" @@ -7137,11 +7075,6 @@ uid@2.0.2: dependencies: "@lukeed/csprng" "^1.0.0" -undici-types@~6.19.8: - version "6.19.8" - resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.19.8.tgz#35111c9d1437ab83a7cdc0abae2f26d88eda0a02" - integrity sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw== - undici-types@~6.20.0: version "6.20.0" resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.20.0.tgz#8171bf22c1f588d1554d55bf204bc624af388433" @@ -7214,12 +7147,7 @@ uuid@9.0.1, uuid@^9.0.0, uuid@^9.0.1: resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30" integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA== -uuid@^11.0.2: - version "11.0.3" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-11.0.3.tgz#248451cac9d1a4a4128033e765d137e2b2c49a3d" - integrity sha512-d0z310fCWv5dJwnX1Y/MncBAqGMKEzlBb1AOf7z9K8ALnd0utBX/msg/fA0+sbyN1ihbMsLhrBlnl1ak7Wa0rg== - -uuid@^11.0.3: +uuid@^11.0.2, uuid@^11.0.3: version "11.0.3" resolved "https://registry.yarnpkg.com/uuid/-/uuid-11.0.3.tgz#248451cac9d1a4a4128033e765d137e2b2c49a3d" integrity sha512-d0z310fCWv5dJwnX1Y/MncBAqGMKEzlBb1AOf7z9K8ALnd0utBX/msg/fA0+sbyN1ihbMsLhrBlnl1ak7Wa0rg==