Skip to content

Commit 9d501bf

Browse files
committed
Added mongo config + minor changes
1 parent 05fb6cc commit 9d501bf

File tree

12 files changed

+224
-8
lines changed

12 files changed

+224
-8
lines changed

.env.sample

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ export REDIS_RESTART_POLICY='unless-stopped'
5656
export REDIS_STOP_GRACE_PERIOD='1m00s'
5757
export REDIS_HOST_SERVICE_DIR='./services/redis'
5858

59+
# ======================================================================= #
60+
# Mongo #
61+
# ======================================================================= #
62+
export MONGO_CONNECTION_SCHEME='mongodb'
63+
export MONGO_HOST='127.0.0.1'
64+
export MONGO_INITDB_ROOT_USERNAME='mongo'
65+
export MONGO_INITDB_ROOT_PASSWORD='mongo'
66+
export MONGO_INITDB_DATABASE='boilerplate-primary'
67+
68+
# Container-Related
69+
export MONGO_SERVICE_NAME=mongo
70+
export MONGO_RESTART_POLICY='unless-stopped'
71+
export MONGO_SERVICE_PORT_MAP='27017:27017'
72+
export MONGO_SERVICE_PORT_EXPOSE='27017'
73+
export MONGO_STOP_GRACE_PERIOD='3m30s'
74+
export MONGO_SERVICE_DIR_NAME='mongo'
75+
export MONGO_HOST_SERVICE_DIR='./services/mongo'
76+
77+
5978
# ======================================================================= #
6079
# Password Hash: Argon2 #
6180
# ======================================================================= #

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"@nestjs/common": "^10.0.0",
3131
"@nestjs/config": "^3.2.2",
3232
"@nestjs/core": "^10.0.0",
33+
"@nestjs/mongoose": "^10.0.6",
3334
"@nestjs/platform-express": "^10.0.0",
3435
"@nestjs/swagger": "^7.3.1",
3536
"@nestjs/typeorm": "^10.0.2",
@@ -42,6 +43,7 @@
4243
"helmet": "^7.1.0",
4344
"ioredis": "^5.4.1",
4445
"joi": "^17.13.1",
46+
"mongoose": "^8.4.0",
4547
"nestjs-i18n": "^10.4.5",
4648
"pg": "^8.11.5",
4749
"reflect-metadata": "^0.2.0",

src/app.module.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { ClassifiedExceptionFilter } from "@/base/filters/classified.exception.f
77
import { OutInterceptor } from "@/base/interceptors/out.interceptor";
88
import developmentSchema from "@/config/env/schema.development";
99
import productionSchema from "@/config/env/schema.production";
10-
11-
import { PostgresConfigModule } from "./config/postgres/postgres.config.module";
12-
import { RedisConfigModule } from "./config/redis/redis.config.module";
10+
import { MongoConfigModule } from "@/config/mongo/mongo.config.module";
11+
import { PostgresConfigModule } from "@/config/postgres/postgres.config.module";
12+
import { RedisConfigModule } from "@/config/redis/redis.config.module";
1313

1414
@Module({
1515
imports: [
@@ -36,6 +36,7 @@ import { RedisConfigModule } from "./config/redis/redis.config.module";
3636
// ** =============================================================== ** //
3737
PostgresConfigModule,
3838
RedisConfigModule,
39+
MongoConfigModule,
3940
],
4041
controllers: [],
4142
providers: [

src/base/.keep

Whitespace-only changes.

src/config/env/schema.development.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,13 @@ export default Joi.object({
2323
REDIS_CACHE_DB_NUMBER: Joi.number(),
2424
REDIS_USERNAME: Joi.string().allow(""),
2525
REDIS_PASSWORD: Joi.string(),
26+
27+
// ** ============================ Mongo ============================ ** //
28+
MONGO_CONNECTION_SCHEME: Joi.string().valid("mongodb", "mongodb+srv"),
29+
MONGO_HOST: Joi.string().valid("localhost", "127.0.0.1"),
30+
MONGO_INITDB_ROOT_USERNAME: Joi.string(),
31+
MONGO_INITDB_ROOT_PASSWORD: Joi.string(),
32+
MONGO_INITDB_DATABASE: Joi.string(),
33+
MONGO_SERVICE_PORT_MAP: Joi.string().pattern(SERVICE_PORT_MAP_REGEX),
34+
MONGO_SERVICE_PORT_EXPOSE: Joi.number().greater(1023),
2635
});

src/config/mongo/.keep

Whitespace-only changes.

src/config/mongo/environment.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22
namespace NodeJS {
33
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4-
interface ProcessEnv {}
4+
interface ProcessEnv {
5+
MONGO_CONNECTION_SCHEME: string;
6+
MONGO_HOST: string;
7+
MONGO_INITDB_ROOT_USERNAME: string;
8+
MONGO_INITDB_ROOT_PASSWORD: string;
9+
MONGO_INITDB_DATABASE: string;
10+
11+
MONGO_SERVICE_NAME: string;
12+
MONGO_RESTART_POLICY: string;
13+
MONGO_SERVICE_PORT_MAP: string;
14+
MONGO_SERVICE_PORT_EXPOSE: string;
15+
MONGO_SERVICE_DIR_NAME: string;
16+
}
517
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Logger, Module, OnModuleInit } from "@nestjs/common";
2+
import { InjectConnection, MongooseModule } from "@nestjs/mongoose";
3+
import { Connection } from "mongoose";
4+
5+
import {
6+
MONGO_CONFIG_MODULE_CONTEXT,
7+
MongoConfigService,
8+
} from "@/config/mongo/mongo.config.service";
9+
10+
@Module({
11+
imports: [
12+
MongooseModule.forRootAsync({
13+
imports: [MongoConfigModule],
14+
inject: [MongoConfigService],
15+
useFactory: async (config: MongoConfigService) => ({
16+
uri: config.uriWithoutUserPass,
17+
dbName: config.database,
18+
user: config.username,
19+
pass: config.password,
20+
appName: config.database,
21+
autoIndex: process.env.NODE_ENV === "development",
22+
maxPoolSize: 100, // * This is the default
23+
}),
24+
}),
25+
],
26+
providers: [MongoConfigService],
27+
exports: [MongoConfigService],
28+
})
29+
export class MongoConfigModule implements OnModuleInit {
30+
constructor(
31+
@InjectConnection()
32+
private readonly connection: Connection,
33+
) {}
34+
35+
async onModuleInit() {
36+
await this.connection.db.stats();
37+
Logger.log("Connected to Mongo", MONGO_CONFIG_MODULE_CONTEXT);
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Injectable } from "@nestjs/common";
2+
3+
export const MONGO_CONFIG_MODULE_CONTEXT = "Mongo";
4+
5+
@Injectable()
6+
export class MongoConfigService {
7+
public readonly scheme = process.env.MONGO_CONNECTION_SCHEME;
8+
9+
public readonly username = process.env.MONGO_INITDB_ROOT_USERNAME;
10+
public readonly password = process.env.MONGO_INITDB_ROOT_PASSWORD;
11+
12+
public readonly host = process.env.MONGO_HOST;
13+
public readonly port =
14+
process.env.MONGO_SERVICE_PORT_EXPOSE ??
15+
process.env.MONGO_SERVICE_PORT_MAP.split(":")[0];
16+
17+
public readonly database = process.env.MONGO_INITDB_DATABASE;
18+
19+
public readonly uriWithoutUserPass = `${this.scheme}://${this.host}:${this.port}/`;
20+
}

src/config/postgres/postgres.config.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { Logger, Module, OnModuleInit } from "@nestjs/common";
22
import { InjectDataSource, TypeOrmModule } from "@nestjs/typeorm";
33
import { DataSource } from "typeorm";
44

5-
import { PostgresConfigService } from "./postgres.config.service";
5+
import { PostgresConfigService } from "@/config/postgres/postgres.config.service";
66

77
export const POSTGRES_CONFIG_MODULE_CONTEXT = "Postgres";
8+
89
@Module({
910
imports: [
1011
TypeOrmModule.forRootAsync({

0 commit comments

Comments
 (0)