Skip to content

Commit 86d2601

Browse files
samuelim01McNaBry
andauthored
Database per service pattern (#85)
* Remove use of username and password The username and password will be embedded within the URI * Remove use of DB The database will be specified in the URI * Standardise env files * Edit .env.sample * Fix typo Co-authored-by: McNaBry <[email protected]> --------- Co-authored-by: McNaBry <[email protected]>
1 parent 9ff60da commit 86d2601

File tree

16 files changed

+24
-87
lines changed

16 files changed

+24
-87
lines changed

.env.sample

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1+
# This is a sample environment configuration file.
2+
# Copy this file to .env and replace the placeholder values with your own.
3+
14
# Question Service
25
QUESTION_DB_CLOUD_URI=<FILL-THIS-IN>
36
QUESTION_DB_LOCAL_URI=mongodb://question-db:27017/question
4-
QUESTION_DB_USERNAME=user
5-
QUESTION_DB_PASSWORD=password
67

78
# User Service
89
USER_DB_CLOUD_URI=<FILL-THIS-IN>
910
USER_DB_LOCAL_URI=mongodb://user-db:27017/user
10-
USER_DB_USERNAME=user
11-
USER_DB_PASSWORD=password
1211

1312
# Match Service
1413
MATCH_DB_CLOUD_URI=<FILL-THIS-IN>
1514
MATCH_DB_LOCAL_URI=mongodb://match-db:27017/match
16-
MATCH_DB_USERNAME=user
17-
MATCH_DB_PASSWORD=password
18-
19-
# Room Service
20-
COLLAB_DB_CLOUD_URI=mongodb+srv://<username>:<password>@cluster0.h5ukw.mongodb.net/collaboration-service?retryWrites=true&w=majority&appName=Cluster0
21-
COLLAB_DB_LOCAL_URI=mongodb://collaboration-db:27017/collaboration-service
2215

23-
# Collaboration Service (Yjs Documents)
24-
YJS_DB_CLOUD_URI=mongodb+srv://<username>:<password>@cluster0.h5ukw.mongodb.net/yjs-documents?retryWrites=true&w=majority&appName=Cluster0
25-
YJS_DB_LOCAL_URI=mongodb://collaboration-db:27017/yjs-documents
16+
# Collaboration Service
17+
COLLAB_DB_CLOUD_URI=<FILL-THIS-IN>
18+
COLLAB_DB_LOCAL_URI=mongodb://collaboration-db:27017/collaboration
19+
YJS_DB_CLOUD_URI=<FILL-THIS-IN>
20+
YJS_DB_LOCAL_URI=mongodb://collaboration-db:27017/room
2621

2722
# History Service
2823
HISTORY_DB_CLOUD_URI=<FILL-THIS-IN>

compose.yml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ services:
5050
environment:
5151
DB_CLOUD_URI: ${QUESTION_DB_CLOUD_URI}
5252
DB_LOCAL_URI: ${QUESTION_DB_LOCAL_URI}
53-
DB_USERNAME: ${QUESTION_DB_USERNAME}
54-
DB_PASSWORD: ${QUESTION_DB_PASSWORD}
5553
BROKER_URL: ${BROKER_URL}
5654
JWT_SECRET: ${JWT_SECRET}
5755
depends_on:
@@ -65,12 +63,8 @@ services:
6563
question-db:
6664
container_name: question-db
6765
image: mongo:7.0.14
68-
environment:
69-
MONGO_INITDB_ROOT_USERNAME: ${QUESTION_DB_USERNAME}
70-
MONGO_INITDB_ROOT_PASSWORD: ${QUESTION_DB_PASSWORD}
7166
volumes:
7267
- question-db:/data/db
73-
- ./services/question/init-mongo/init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js
7468
networks:
7569
- question-db-network
7670
command: --quiet
@@ -85,8 +79,6 @@ services:
8579
environment:
8680
DB_CLOUD_URI: ${USER_DB_CLOUD_URI}
8781
DB_LOCAL_URI: ${USER_DB_LOCAL_URI}
88-
DB_USERNAME: ${USER_DB_USERNAME}
89-
DB_PASSWORD: ${USER_DB_PASSWORD}
9082
JWT_SECRET: ${JWT_SECRET}
9183
networks:
9284
- gateway-network
@@ -96,9 +88,6 @@ services:
9688
user-db:
9789
container_name: user-db
9890
image: mongo:7.0.14
99-
environment:
100-
MONGO_INITDB_ROOT_USERNAME: ${USER_DB_USERNAME}
101-
MONGO_INITDB_ROOT_PASSWORD: ${USER_DB_PASSWORD}
10291
volumes:
10392
- user-db:/data/db
10493
networks:
@@ -115,8 +104,6 @@ services:
115104
environment:
116105
DB_CLOUD_URI: ${MATCH_DB_CLOUD_URI}
117106
DB_LOCAL_URI: ${MATCH_DB_LOCAL_URI}
118-
DB_USERNAME: ${MATCH_DB_USERNAME}
119-
DB_PASSWORD: ${MATCH_DB_PASSWORD}
120107
JWT_SECRET: ${JWT_SECRET}
121108
BROKER_URL: ${BROKER_URL}
122109
depends_on:
@@ -130,9 +117,6 @@ services:
130117
match-db:
131118
container_name: match-db
132119
image: mongo:7.0.14
133-
environment:
134-
MONGO_INITDB_ROOT_USERNAME: ${MATCH_DB_USERNAME}
135-
MONGO_INITDB_ROOT_PASSWORD: ${MATCH_DB_PASSWORD}
136120
volumes:
137121
- match-db:/data/db
138122
networks:

services/collaboration/.env.sample

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
# MongoDB Cloud URI for the room service. Replace the placeholders with your MongoDB username, password, and cluster details.
2-
COLLAB_DB_CLOUD_URI=mongodb+srv://<username>:<password>@cluster0.h5ukw.mongodb.net/collaboration-service?retryWrites=true&w=majority&appName=Cluster0
3-
COLLAB_DB_LOCAL_URI=mongodb://collaboration-db:27017/collaboration-service
4-
5-
# MongoDB Cloud URI for Yjs documents. Replace the placeholders with your MongoDB username, password, and cluster details.
6-
YJS_DB_CLOUD_URI=mongodb+srv://<username>:<password>@cluster0.h5ukw.mongodb.net/yjs-documents?retryWrites=true&w=majority&appName=Cluster0
7-
YJS_DB_LOCAL_URI=mongodb://collaboration-db:27017/yjs-documents
8-
9-
# Broker Service
1+
# This is a sample environment configuration file.
2+
# Copy this file to .env and replace the placeholder values with your own.
3+
COLLAB_DB_CLOUD_URI=<FILL-THIS-IN>
4+
COLLAB_DB_LOCAL_URI=mongodb://collaboration-db:27017/collaboration
5+
YJS_DB_CLOUD_URI=<FILL-THIS-IN>
6+
YJS_DB_LOCAL_URI=mongodb://collaboration-db:27017/room
107
BROKER_URL=amqp://broker:5672
11-
12-
# CORS origin configuration
138
CORS_ORIGIN=*
14-
15-
# Port
169
PORT=8084
17-
18-
# Secret for creating JWT signature
1910
JWT_SECRET=you-can-replace-this-with-your-own-secret
20-
21-
# Node environment
22-
ENV=development
11+
NODE_ENV=development

services/collaboration/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ Here are the key environment variables used in the `.env` file:
6565
| `COLLAB_LOCAL_MONGO_URI` | URI for connecting to the local MongoDB database for the collaboration service (room) |
6666
| `YJS_CLOUD_MONGO_URI` | URI for connecting to the MongoDB Atlas database for Yjs document persistence |
6767
| `YJS_LOCAL_MONGO_URI` | URI for connecting to the local MongoDB database for Yjs document persistence |
68-
| `DB_USERNAME` | Username for the MongoDB databases (for both cloud and local environments) |
69-
| `DB_PASSWORD` | Password for the MongoDB databases (for both cloud and local environments) |
7068
| `CORS_ORIGIN` | Allowed origins for CORS (default: * to allow all origins) |
7169
| `PORT` | Port for the Room and Collaboration Service (default: 8084) |
7270
| `ENV` | Environment setting (`development` or `production`) |

services/collaboration/src/services/mongodbService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const connectToRoomDB = async (): Promise<Db> => {
1818
if (!roomDb) {
1919
const client = new MongoClient(config.COLLAB_DB_URI);
2020
await client.connect();
21-
roomDb = client.db('collaboration-service');
21+
roomDb = client.db();
2222
console.log('Connected to the collaboration-service (room) database');
2323
}
2424
return roomDb;
@@ -41,7 +41,7 @@ const connectToYJSDB = async (): Promise<Db> => {
4141

4242
const client = new MongoClient(config.YJS_DB_URI);
4343
await client.connect();
44-
yjsDb = client.db('yjs-documents');
44+
yjsDb = client.db();
4545
console.log('Connected to the YJS database');
4646
}
4747
return yjsDb;

services/history/.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Copy this file to .env and replace the placeholder values with your own.
33
DB_CLOUD_URI=<FILL-THIS-IN>
44
DB_LOCAL_URI=mongodb://history-db:27017/history
5-
JWT_SECRET=you-can-replace-this-with-your-own-secret
65
BROKER_URL=amqp://broker:5672
76
CORS_ORIGIN=*
87
PORT=8086
8+
JWT_SECRET=you-can-replace-this-with-your-own-secret
99
NODE_ENV=development

services/match/.env.sample

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
# Copy this file to .env and replace the placeholder values with your own.
33
DB_CLOUD_URI=<FILL-THIS-IN>
44
DB_LOCAL_URI=mongodb://match-db:27017/match
5-
DB_USERNAME=user
6-
DB_PASSWORD=password
75
BROKER_URL=amqp://broker:5672
8-
JWT_SECRET=you-can-replace-this-with-your-own-secret
6+
CORS_ORIGIN=*
97
PORT=8083
8+
JWT_SECRET=you-can-replace-this-with-your-own-secret
109
NODE_ENV=development

services/match/src/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { z } from 'zod';
22

33
const envSchema = z
44
.object({
5-
DB_USERNAME: z.string().trim().min(1),
6-
DB_PASSWORD: z.string().trim().min(1),
75
DB_CLOUD_URI: z.string().trim().optional(),
86
DB_LOCAL_URI: z.string().trim().optional(),
97
BROKER_URL: z.string().url(),

services/match/src/models/repository.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import config from '../config';
55
import { IdType } from '../types/request';
66

77
export async function connectToDB() {
8-
await mongoose.connect(config.DB_URI, {
9-
authSource: 'admin',
10-
user: config.DB_USERNAME,
11-
pass: config.DB_PASSWORD,
12-
});
8+
await mongoose.connect(config.DB_URI);
139
}
1410

1511
export async function createMatchRequest(userId: IdType, username: string, topics: string[], difficulty: Difficulty) {

services/question/.env.sample

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
# Copy this file to .env and replace the placeholder values with your own.
33
DB_CLOUD_URI=<FILL-THIS-IN>
44
DB_LOCAL_URI=mongodb://question-db:27017/question
5-
DB_USERNAME=user
6-
DB_PASSWORD=password
75
BROKER_URL=amqp://broker:5672
8-
JWT_SECRET=you-can-replace-this-with-your-own-secret
96
CORS_ORIGIN=*
107
PORT=8081
8+
JWT_SECRET=you-can-replace-this-with-your-own-secret
119
NODE_ENV=development

0 commit comments

Comments
 (0)