- Yarn (https://classic.yarnpkg.com/en/docs/install/)
- Postgres (https://www.postgresql.org/download/)
- when installing, use the default password "postgres"
- for macOS users, suggested installation is through homebrew
- TypeORM Global Installation (
yarn add typeorm -g
)- this will install the TypeORM CLI as well
- Redis (https://redis.io/download)
- this is used for local development without Docker
- Run
yarn install
to install all dependencies
Make a copy of sample.env
, rename it to .env
, and fill out the environment variables respectively.
Likely:
DB_HOST=localhost
REDIS_HOST=localhost
You will need two databases, foodies
and foodies_test
.
Ensure you have a postgres
user setup, and it is your current username.
If you do not have this setup, please execute the following command. Otherwise, go to Step 1.
CREATE ROLE postgres WITH LOGIN PASSWORD 'postgres'
- Launch
psql
- Connect to localhost and select everything to default. The password should be the same as the one during installation, "postgres".
- Once logged in, execute the following commands. Important: don't forget the semicolons.
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgres;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO postgres;
ALTER USER postgres CREATEDB;
CREATE DATABASE foodies;
GRANT ALL PRIVILEGES ON DATABASE foodies TO postgres;
CREATE DATABASE foodies_test;
GRANT ALL PRIVILEGES ON DATABASE foodies_test TO postgres;
Currently, all tests drop the schema after each test. Therefore, do not design tests to be reliant on data from previous tests.
If you want to run all tests in IntelliJ, add this new configuration. It's very important you specify the working directory as specified because TypeORM config needs the correct directory to find all the entities, migrations, subcribers etc.
yarn run test
yarn run dropSchema
This will setup the schema, without any data.
yarn run syncSchema
This will run any migrations in the /migrations
folder. Currently ormconfig.ts
is configured to run migrations when the application starts.
yarn run run_migration
The following steps assume that you already have Docker installed in your local environment. If you don't have Docker installed, follow these steps before doing anything else.
If this is your first time using Docker, run the following command at the top-level of this repository to build a docker image and activate the containers. Or if there have been any changes to the compose file or Docker file, this command will rebuild the images
docker-compose up -d --build
If you already have a Docker image, run the following command instead:
docker-compose up -d
Load the back-end in a container:
docker-compose exec web bash
Stop docker compose without removing containers
docker-compose stop
Stops containers and removes containers, networks, volumes, and images created by up. See https://docs.docker.com/compose/reference/down/
docker-compose down
Removes everything and may help to fix misc Docker errors. WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all images without at least one container associated to them
- all build cache
docker system prune -a --volumes
Make sure you created the databases first.
IntelliJ can show you your databases and tables. Go to the Database
tab and add a new data source.
Fill out with the username and password respectively
You should now be able to see all the databases and tables.
To import data from init_data.sql
, run this command inside a command prompt. Make sure the schema is synced, the migrations are ran, and that the tables are empty.
psql
should prompt you to enter your password.
E.g.
yarn run dropSchema
yarn run syncSchema
yarn run run_migration
yarn run loadSqlData
If this command does not work, clear everything inside the tables (delete all tuples in user_profile
table and cascade delete should remove everything else).
Then open init_data.sql
inside Intellij, select all the lines, and execute (Ctrl + Enter or Command + Enter).
psql -d foodies -f src/main/resources/init_data.sql --username=postgres
or
yarn run loadSqlData
In ormconfig.ts
, if you set:
synchronize: true,
dropSchema: true
Then TypeORM will automatically create the schema drop the schema on every application launch.
Make sure this line is uncommented in App.ts
for TypeORM to save the data when the application starts. After data is loaded,
it is recommended to set dropSchema: false
.
await loadSampleDBData();
Please note that you must shut down the Docker containers if you intend on testing locally without Docker afterwards.
Execute these commands at the directory containing the docker-compose file <roodDir>/foodies
These steps help open the psql cli from the postgres container
docker-compose exec db bash // db is the name of the service we specified in the docker-compose.yml file
psql -U postgres -d foodies // now you should be able to use the psql cli for the postgres container
These steps help open the redis-cli from the redis container. Use the list of commands here https://redis.io/commands
docker-compose exec redis-server redis-cli // redis-server is the name of the service we specified in the docker-compose.yml file
Note we currently don't support this because we have not created the foodies_test
database in docker-compose. But it should be fairly straightforward to do so
docker exec -it foodies_web_1 yarn run test
// or
docker-compose exec web bash
yarn run test
Inspect file system of docker image (https://stackoverflow.com/a/44769468)
docker run -it image_name sh
// or if you prefer bash
docker run -it image_name bash
- make sure
main/resources/Data.ts
is updated accordingly. - generate a new
init_data.sql
script.- Load all data using
Data.ts
into the tables. - Select all tables, right click, and select the following:
- Make note of the location you save these files to and click
Export to File
- Copy all the insert statements into
init_data.sql
. Make sure the insertion order is correct (users, promotions, discounts, savedPromotions). This is critical as new IDs are created each time.
- Delete the generated files.
- Load all data using
To get a local server setup, please refer to the docs here https://redis.io/topics/quickstart.
If you are interested in using a local Redis server, go to your .env
and modify the REDIS_HOST field to localhost
.
If you would like to connect to the Redis server associated with Docker, modify the REDIS_HOST field to redis-server
.