|
1 |
| -import { MongoClient } from "mongodb"; |
2 |
| -import { MongoDBOrderRepository } from "./adapters/order.repo"; |
3 |
| -import { MongoDBProductRepository } from "./adapters/product.repo"; |
| 1 | +import {MongoClient} from "mongodb"; |
| 2 | +import {MongoDBOrderRepository} from "./adapters/order.repo"; |
| 3 | +import {MongoDBProductRepository} from "./adapters/product.repo"; |
4 | 4 | import {NestFactory} from "@nestjs/core";
|
5 | 5 | import {AppModuleInversionOfControl} from "./app.module.ioc";
|
6 | 6 | import {AppModuleOverrides} from "./app.module.overrides";
|
7 | 7 | import {AppModuleWithRegister} from "./app.module.register";
|
8 | 8 | import {MongoDBModule} from "./adapters/mongodb.module";
|
| 9 | +import {z} from "zod"; |
| 10 | + |
| 11 | +const EnvConfig = z.object({ |
| 12 | + MONGO_URI: z.string().default('mongodb://root:[email protected]'), |
| 13 | + MONGO_DB: z.string().default('store'), |
| 14 | + MONGO_CONNECT_TIMEOUT: z.number().default(100), |
| 15 | + MONGO_SOCKET_TIMEOUT: z.number().default(100), |
| 16 | + MONGO_SERVER_SELECTION_TIMEOUT: z.number().default(100), |
| 17 | +}).transform((input) => ({ |
| 18 | + uri: input.MONGO_URI, |
| 19 | + dbName: input.MONGO_DB, |
| 20 | + connectTimeoutMS: input.MONGO_CONNECT_TIMEOUT, |
| 21 | + socketTimeoutMS: input.MONGO_SOCKET_TIMEOUT, |
| 22 | + serverSelectionTimeoutMS: input.MONGO_SERVER_SELECTION_TIMEOUT, |
| 23 | +})); |
9 | 24 |
|
10 | 25 | // @ts-ignore
|
11 | 26 | async function startServerIoC() {
|
12 |
| - const mongo = await new MongoClient( |
13 |
| - `mongodb://root:[email protected]?retryWrites=true&writeConcern=majority` |
14 |
| - ).connect(); |
| 27 | + const mongo = await new MongoClient( |
| 28 | + `mongodb://root:[email protected]?retryWrites=true&writeConcern=majority` |
| 29 | + ).connect(); |
15 | 30 |
|
16 |
| - const db = mongo.db("store"); |
17 |
| - const productRepo = new MongoDBProductRepository(db); |
18 |
| - const orderRepo = new MongoDBOrderRepository(db); |
| 31 | + const db = mongo.db("store"); |
| 32 | + const productRepo = new MongoDBProductRepository(db); |
| 33 | + const orderRepo = new MongoDBOrderRepository(db); |
19 | 34 |
|
20 |
| - const app = await NestFactory.create(AppModuleInversionOfControl.register(productRepo, orderRepo)) |
21 |
| - app.enableCors({origin: "*"}); |
22 |
| - await app.listen(8080); |
| 35 | + const app = await NestFactory.create(AppModuleInversionOfControl.register(productRepo, orderRepo)) |
| 36 | + app.enableCors({origin: "*"}); |
| 37 | + await app.listen(8080); |
23 | 38 | }
|
24 | 39 |
|
25 | 40 | // @ts-ignore
|
26 | 41 | async function startServerOverrides() {
|
27 |
| - const app = await NestFactory.create(AppModuleOverrides) |
28 |
| - app.enableCors({origin: "*"}); |
29 |
| - await app.listen(8080); |
| 42 | + const app = await NestFactory.create(AppModuleOverrides) |
| 43 | + app.enableCors({origin: "*"}); |
| 44 | + await app.listen(8080); |
30 | 45 | }
|
31 | 46 |
|
32 | 47 | async function startServerRegister() {
|
33 |
| - const app = await NestFactory.create(AppModuleWithRegister.register( |
34 |
| - MongoDBModule.forRoot(`mongodb://root:[email protected]`) |
35 |
| - )); |
36 |
| - app.enableCors({origin: "*"}); |
37 |
| - await app.listen(8080); |
| 48 | + const config = EnvConfig.parse(process.env); |
| 49 | + const app = await NestFactory.create(AppModuleWithRegister.register( |
| 50 | + MongoDBModule.forRoot(config) |
| 51 | + )); |
| 52 | + app.enableCors({origin: "*"}); |
| 53 | + await app.listen(8080); |
38 | 54 | }
|
39 | 55 |
|
40 | 56 | void startServerRegister();
|
|
0 commit comments