|
| 1 | +# Template Service |
| 2 | + |
| 3 | +This directory contains the code for the Template |
| 4 | +Service. |
| 5 | + |
| 6 | +## Database |
| 7 | + |
| 8 | +We use: |
| 9 | + |
| 10 | +- PostgreSQL 16 for the database. To run it, we use: |
| 11 | + - Docker to run the database, as well as inject any user-defined |
| 12 | + configurations or SQL files into the Docker image. |
| 13 | + - Docker-Compose to run the database, as well as any other |
| 14 | + services this API microservice may depend on. |
| 15 | +- [**Drizzle**](https://orm.drizzle.team/) for the ORM. |
| 16 | + |
| 17 | +Follow the instructions below for the setup, as well as to learn how to work with the database. |
| 18 | + |
| 19 | +### Setup |
| 20 | + |
| 21 | +1. Install Docker Desktop on your device. Launch it. |
| 22 | + |
| 23 | +2. To verify that it is launched and installed correctly, run the |
| 24 | + following in your terminal: |
| 25 | + |
| 26 | + ```bash |
| 27 | + docker --version |
| 28 | + ``` |
| 29 | + |
| 30 | + If the command does not error, and outputs a version, proceed to |
| 31 | + the next step. |
| 32 | + |
| 33 | +3. Inspect the `docker-compose.yml` file. It |
| 34 | + should look like this: |
| 35 | + |
| 36 | + ```yml |
| 37 | + services: |
| 38 | + # ... |
| 39 | + postgres: |
| 40 | + # ... |
| 41 | + volumes: |
| 42 | + - "template-db-docker:/data/template-db" |
| 43 | + # - ./init.sql:/docker-entrypoint-initdb.d/init.sql |
| 44 | + ports: |
| 45 | + - "5431:5432" |
| 46 | + restart: unless-stopped |
| 47 | + |
| 48 | + volumes: |
| 49 | + template-db-docker: |
| 50 | + external: true |
| 51 | + ``` |
| 52 | + |
| 53 | + We observe that this Database relies on a |
| 54 | + Docker Volume. Replace all instances of |
| 55 | + `template-db-docker` with your desired |
| 56 | + volume name. |
| 57 | + |
| 58 | +4. Then, create the Docker Volume with |
| 59 | + the following command: |
| 60 | + |
| 61 | + ```bash |
| 62 | + # in this case, the command is |
| 63 | + # docker volume create template-db-docker |
| 64 | + docker volume create <volume-name> |
| 65 | + ``` |
| 66 | +5. Finally, create the Database Container: |
| 67 | + |
| 68 | + ```bash |
| 69 | + docker-compose up -d |
| 70 | + ``` |
| 71 | + |
| 72 | +6. To bring it down, run this command: |
| 73 | + |
| 74 | + ```bash |
| 75 | + docker-compose down |
| 76 | + ``` |
| 77 | + |
| 78 | +### Schema |
| 79 | + |
| 80 | +We maintain the schema in the `src/lib/db/schema.ts` file. |
| 81 | + |
| 82 | +Refer to the Drizzle documentation to learn how |
| 83 | +to properly define schemas. Then, insert your |
| 84 | +schemas into the file. |
| 85 | + |
| 86 | +### Migration |
| 87 | + |
| 88 | +After you have created/updated your schemas in |
| 89 | +the file, persist them to the Database with |
| 90 | +Migrations. |
| 91 | + |
| 92 | +1. Configure your credentials (port, |
| 93 | + password, ...) in: |
| 94 | + |
| 95 | + - `drizzle.config.ts` |
| 96 | + - `drizzle.migrate.mts`. |
| 97 | + - `src/lib/db/index.ts`. |
| 98 | + |
| 99 | + In the future, we may wish to migrate these |
| 100 | + credentials to environment variables. |
| 101 | + |
| 102 | +2. Run the `npm run db:generate` command to |
| 103 | +generate your `.sql` Migration Files under the |
| 104 | +`drizzle` folder. |
| 105 | + |
| 106 | +3. Rename your |
| 107 | + `<migration_num>_<random_name>.sql` file |
| 108 | + to `<migration_num>_<meaningful_name>.sql`. |
| 109 | + |
| 110 | + For example: |
| 111 | + - Generated: `0000_dazzling_squirrel.sql` |
| 112 | + - Renamed: `0000_initial_schema.sql`. |
| 113 | + |
| 114 | + Then, rename the |
| 115 | + `meta/_journal.json` tag from |
| 116 | + `0000_dazzling_squirrel` to |
| 117 | + `0000_initial_schema` as well. Replace the |
| 118 | + migration number and name with the one you |
| 119 | + used. |
| 120 | + |
| 121 | +4. Finally, run the migration with this: |
| 122 | + |
| 123 | + ```bash |
| 124 | + npm run db:migrate |
| 125 | + ``` |
| 126 | + |
| 127 | +### Connecting with the DB |
| 128 | + |
| 129 | +1. Import the `db` instance from `lib/db`. |
| 130 | +2. Use the Drizzle APIs and the tables defined in |
| 131 | + `src/lib/schema.ts` to interact with the |
| 132 | + tables. |
| 133 | + |
| 134 | + ```ts |
| 135 | + import { db, tableName } from '../lib/db'; |
| 136 | +
|
| 137 | + const route = async (req, res) => { |
| 138 | + await db.select().from(tableName); //... |
| 139 | + } |
| 140 | + ``` |
0 commit comments