|
1 |
| -# Template Service |
| 1 | +# User Service |
2 | 2 |
|
3 |
| -This directory contains the code for the Template |
4 |
| -Service. |
| 3 | +## Running with Docker (Standalone) |
5 | 4 |
|
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 |
| 5 | +1. Run this command to build: |
| 6 | + ```sh |
| 7 | + docker build \ |
| 8 | + -t user-express-local \ |
| 9 | + --build-arg port=9001 \ |
| 10 | + -f express.Dockerfile . |
70 | 11 | ```
|
71 |
| - |
72 |
| -6. To bring it down, run this command: |
73 |
| - |
74 |
| - ```bash |
75 |
| - docker-compose down |
| 12 | +2. Run this command, from the root folder: |
| 13 | + ```sh |
| 14 | + make db-up |
76 | 15 | ```
|
77 | 16 |
|
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`. |
| 17 | +3. Run the necessary migrate and seed commands, if you haven't yet. |
98 | 18 |
|
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 |
| 19 | +4. Run this command to expose the container: |
| 20 | + ```sh |
| 21 | + docker run -p 9001:9001 --env-file ./.env.docker user-express-local |
125 | 22 | ```
|
126 | 23 |
|
127 |
| -### Connecting with the DB |
| 24 | +## Running with Docker-Compose (Main config) |
128 | 25 |
|
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. |
| 26 | +Edit the variables in the `.env.compose` file and run `make up` from the root folder. |
133 | 27 |
|
134 |
| - ```ts |
135 |
| - import { db, tableName } from '../lib/db'; |
136 |
| -
|
137 |
| - const route = async (req, res) => { |
138 |
| - await db.select().from(tableName); //... |
139 |
| - } |
140 |
| - ``` |
| 28 | +Any startup instructions will be run from `entrypoint.sh` instead. |
0 commit comments