Template repository for creating a dockerized TypeScript Express server with Postgres
Template repository for creating a Node.js + Express server with TypeScript
For this project, were are using docker to contain the application.
# Build the docker container for the first time
docker-compose up --build
# Run the existed docker container
docker-compose up
# Tear down the docker container
docker-compose down
The express server expects a few environment variables to be included via a dev.env
file.
Create this file in the root of your project with the following keys:
SERVER_PORT=8081
DATABASE_HOST="localhost"
DATABASE_USER="starter"
DATABASE_PASSWORD="starter-dev"
DATABASE_NAME="express-starter-db"
DATABASE_PORT=5432
NOTE: The variables above are what this starter expects out of the box.
This project is setup with a Docker Postgres image and uses pg for database migrations.
All files for this database are inside of the ./postgres
folder.
To set up your database tables, add your sql scripts inside of the ./postgres/tables
folder. Once your files are in there, make sure to import them all in the ./posgres/deploy_tables.sql
script.
All migrations scripts can be found inside of the ./postgres/migrations
folder.
In the package.json
there is a set of scripts for creating and running database migrations:
# Create a new migration file
yarn db:migrate-create <name_of_new_migration>
# Run all pending 'up' migrations
yarn db:migrate-up
# Run a single 'down' migration
yarn db:migrate-down
# Run N 'up' migrations
yarn db:migrate-up <number_to_run>
# Run N 'down' migrations
yarn db:migrate-down <number_to_run>
NOTE: these scripts must be run inside of the docker image's bash shell while it's running and can be accessed with the following command:
docker exec -it express-starter-api bash
psql
terminalTo make use of the psql terminal run the following command while the postgres container is running:
docker exec -it express-starter-postgres psql -U starter -d express-starter-db
GET /v1/health - returns that the api is running