This directory contains the code for the Template Service.
-
Run this command to build:
docker build \ -t question-express-local \ --build-arg port=9002 \ -f express.Dockerfile .
-
Run this command, from the root folder:
make db-up
-
Run the necessary migrate and seed commands, if you haven't yet.
-
Run this command to expose the container:
docker run -p 9002:9002 --env-file ./.env.docker question-express-local
Edit the variables in the .env.compose
file and run make up
from the root folder.
Any startup instructions will be run from entrypoint.sh
instead.
We use:
- PostgreSQL 16 for the database. To run it, we use:
- Docker to run the database, as well as inject any user-defined configurations or SQL files into the Docker image.
- Docker-Compose to run the database, as well as any other services this API microservice may depend on.
- Drizzle for the ORM.
Follow the instructions below for the setup, as well as to learn how to work with the database.
-
Install Docker Desktop on your device. Launch it.
-
To verify that it is launched and installed correctly, run the following in your terminal:
docker --version
If the command does not error, and outputs a version, proceed to the next step.
-
Inspect the
docker-compose.yml
file. It should look like this:services: # ... postgres: # ... volumes: - "template-db-docker:/data/template-db" # - ./init.sql:/docker-entrypoint-initdb.d/init.sql ports: - "5431:5432" restart: unless-stopped volumes: template-db-docker: external: true
We observe that this Database relies on a Docker Volume. Replace all instances of
template-db-docker
with your desired volume name. -
Then, create the Docker Volume with the following command:
# in this case, the command is # docker volume create template-db-docker docker volume create <volume-name>
-
Finally, create the Database Container:
docker-compose up -d
-
To bring it down, run this command:
docker-compose down
We maintain the schema in the src/lib/db/schema.ts
file.
Refer to the Drizzle documentation to learn how to properly define schemas. Then, insert your schemas into the file.
After you have created/updated your schemas in the file, persist them to the Database with Migrations.
-
Configure your credentials (port, password, ...) in:
drizzle.config.ts
drizzle.migrate.mts
.src/lib/db/index.ts
.
In the future, we may wish to migrate these credentials to environment variables.
-
Run the
npm run db:generate
command to generate your.sql
Migration Files under thedrizzle
folder. -
Rename your
<migration_num>_<random_name>.sql
file to<migration_num>_<meaningful_name>.sql
.For example:
- Generated:
0000_dazzling_squirrel.sql
- Renamed:
0000_initial_schema.sql
.
Then, rename the
meta/_journal.json
tag from0000_dazzling_squirrel
to0000_initial_schema
as well. Replace the migration number and name with the one you used. - Generated:
-
Finally, run the migration with this:
npm run db:migrate
-
Import the
db
instance fromlib/db
. -
Use the Drizzle APIs and the tables defined in
src/lib/schema.ts
to interact with the tables.import { db, tableName } from '../lib/db'; const route = async (req, res) => { await db.select().from(tableName); //... }