typescript-express-starter 10.0.0 → 10.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. package/CONTRIBUTORS.md +8 -0
  2. package/lib/default/Makefile +1 -1
  3. package/lib/graphql/Makefile +1 -1
  4. package/lib/graphql/src/app.ts +2 -3
  5. package/lib/graphql/src/database/index.ts +23 -19
  6. package/lib/knex/Makefile +2 -2
  7. package/lib/knex/src/app.ts +3 -4
  8. package/lib/knex/src/database/index.ts +20 -17
  9. package/lib/mikro-orm/Makefile +2 -2
  10. package/lib/mikro-orm/src/app.ts +2 -1
  11. package/lib/mongoose/Makefile +2 -2
  12. package/lib/mongoose/src/app.ts +2 -7
  13. package/lib/mongoose/src/database/index.ts +17 -8
  14. package/lib/node-postgres/.env.development.local +7 -0
  15. package/lib/node-postgres/.env.production.local +7 -0
  16. package/lib/node-postgres/.env.test.local +7 -0
  17. package/lib/node-postgres/.swcrc +0 -1
  18. package/lib/node-postgres/Makefile +1 -1
  19. package/lib/node-postgres/src/app.ts +0 -6
  20. package/lib/node-postgres/src/config/index.ts +1 -1
  21. package/lib/node-postgres/src/database/index.ts +6 -2
  22. package/lib/node-postgres/src/database/init.sql +11 -108
  23. package/lib/node-postgres/src/middlewares/auth.middleware.ts +12 -4
  24. package/lib/node-postgres/src/routes/auth.route.ts +2 -2
  25. package/lib/node-postgres/src/routes/users.route.ts +2 -2
  26. package/lib/node-postgres/src/services/auth.service.ts +66 -15
  27. package/lib/node-postgres/src/services/users.service.ts +103 -21
  28. package/lib/node-postgres/src/test/auth.test.ts +8 -3
  29. package/lib/node-postgres/src/test/users.test.ts +16 -15
  30. package/lib/node-postgres/tsconfig.json +0 -1
  31. package/lib/prisma/Makefile +2 -2
  32. package/lib/prisma/src/routes/users.route.ts +1 -1
  33. package/lib/routing-controllers/Makefile +1 -1
  34. package/lib/sequelize/.sequelizerc +8 -0
  35. package/lib/sequelize/Makefile +2 -2
  36. package/lib/sequelize/package.json +3 -1
  37. package/lib/sequelize/src/app.ts +2 -2
  38. package/lib/sequelize/src/config/sequelize-cli.js +15 -0
  39. package/lib/typegoose/Makefile +2 -2
  40. package/lib/typegoose/src/app.ts +4 -8
  41. package/lib/typegoose/src/database/index.ts +12 -5
  42. package/lib/typeorm/.env.development.local +5 -5
  43. package/lib/typeorm/.env.production.local +5 -5
  44. package/lib/typeorm/.env.test.local +5 -5
  45. package/lib/typeorm/Makefile +1 -1
  46. package/lib/typeorm/docker-compose.yml +8 -8
  47. package/lib/typeorm/src/app.ts +2 -3
  48. package/lib/typeorm/src/config/index.ts +1 -1
  49. package/lib/typeorm/src/database/index.ts +23 -19
  50. package/lib/typeorm/src/test/auth.test.ts +1 -1
  51. package/lib/typeorm/src/test/users.test.ts +1 -1
  52. package/package.json +1 -1
  53. package/.vscode/configurationCache.log +0 -1
  54. package/.vscode/dryrun.log +0 -6
  55. package/.vscode/settings.json +0 -3
  56. package/.vscode/targets.log +0 -239
  57. package/lib/node-postgres/src/models/users.model.ts +0 -9
  58. /package/lib/default/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  59. /package/lib/graphql/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  60. /package/lib/knex/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  61. /package/lib/mikro-orm/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  62. /package/lib/mongoose/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  63. /package/lib/prisma/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  64. /package/lib/routing-controllers/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  65. /package/lib/sequelize/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  66. /package/lib/typegoose/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
  67. /package/lib/typeorm/src/exceptions/{httpException.ts → HttpException.ts} +0 -0
@@ -2,10 +2,10 @@ import { hash, compare } from 'bcrypt';
2
2
  import { sign } from 'jsonwebtoken';
3
3
  import { Service } from 'typedi';
4
4
  import { SECRET_KEY } from '@config';
5
+ import pg from '@database';
5
6
  import { HttpException } from '@exceptions/httpException';
6
7
  import { DataStoredInToken, TokenData } from '@interfaces/auth.interface';
7
8
  import { User } from '@interfaces/users.interface';
8
- import { UserModel } from '@models/users.model';
9
9
 
10
10
  const createToken = (user: User): TokenData => {
11
11
  const dataStoredInToken: DataStoredInToken = { id: user.id };
@@ -21,32 +21,83 @@ const createCookie = (tokenData: TokenData): string => {
21
21
  @Service()
22
22
  export class AuthService {
23
23
  public async signup(userData: User): Promise<User> {
24
- const findUser: User = UserModel.find(user => user.email === userData.email);
25
- if (findUser) throw new HttpException(409, `This email ${userData.email} already exists`);
24
+ const { email, password } = userData;
26
25
 
27
- const hashedPassword = await hash(userData.password, 10);
28
- const createUserData: User = { id: UserModel.length + 1, ...userData, password: hashedPassword };
26
+ const { rows: findUser } = await pg.query(
27
+ `
28
+ SELECT EXISTS(
29
+ SELECT
30
+ "email"
31
+ FROM
32
+ users
33
+ WHERE
34
+ "email" = $1
35
+ )`,
36
+ [email],
37
+ );
38
+ if (findUser[0].exists) throw new HttpException(409, `This email ${userData.email} already exists`);
29
39
 
30
- return createUserData;
40
+ const hashedPassword = await hash(password, 10);
41
+ const { rows: signUpUserData } = await pg.query(
42
+ `
43
+ INSERT INTO
44
+ users(
45
+ "email",
46
+ "password"
47
+ )
48
+ VALUES ($1, $2)
49
+ RETURNING "email", "password"
50
+ `,
51
+ [email, hashedPassword],
52
+ );
53
+
54
+ return signUpUserData[0];
31
55
  }
32
56
 
33
57
  public async login(userData: User): Promise<{ cookie: string; findUser: User }> {
34
- const findUser: User = UserModel.find(user => user.email === userData.email);
35
- if (!findUser) throw new HttpException(409, `This email ${userData.email} was not found`);
58
+ const { email, password } = userData;
59
+
60
+ const { rows, rowCount } = await pg.query(
61
+ `
62
+ SELECT
63
+ "email",
64
+ "password"
65
+ FROM
66
+ users
67
+ WHERE
68
+ "email" = $1
69
+ `,
70
+ [email],
71
+ );
72
+ if (!rowCount) throw new HttpException(409, `This email ${email} was not found`);
36
73
 
37
- const isPasswordMatching: boolean = await compare(userData.password, findUser.password);
74
+ const isPasswordMatching: boolean = await compare(password, rows[0].password);
38
75
  if (!isPasswordMatching) throw new HttpException(409, "You're password not matching");
39
76
 
40
- const tokenData = createToken(findUser);
77
+ const tokenData = createToken(rows[0]);
41
78
  const cookie = createCookie(tokenData);
42
-
43
- return { cookie, findUser };
79
+ return { cookie, findUser: rows[0] };
44
80
  }
45
81
 
46
82
  public async logout(userData: User): Promise<User> {
47
- const findUser: User = UserModel.find(user => user.email === userData.email && user.password === userData.password);
48
- if (!findUser) throw new HttpException(409, "User doesn't exist");
83
+ const { email, password } = userData;
84
+
85
+ const { rows, rowCount } = await pg.query(
86
+ `
87
+ SELECT
88
+ "email",
89
+ "password"
90
+ FROM
91
+ users
92
+ WHERE
93
+ "email" = $1
94
+ AND
95
+ "password" = $2
96
+ `,
97
+ [email, password],
98
+ );
99
+ if (!rowCount) throw new HttpException(409, "User doesn't exist");
49
100
 
50
- return findUser;
101
+ return rows[0];
51
102
  }
52
103
  }
@@ -1,51 +1,133 @@
1
1
  import { hash } from 'bcrypt';
2
2
  import { Service } from 'typedi';
3
+ import pg from '@database';
3
4
  import { HttpException } from '@exceptions/httpException';
4
5
  import { User } from '@interfaces/users.interface';
5
- import { UserModel } from '@models/users.model';
6
6
 
7
7
  @Service()
8
8
  export class UserService {
9
9
  public async findAllUser(): Promise<User[]> {
10
- const users: User[] = UserModel;
11
- return users;
10
+ const { rows } = await pg.query(`
11
+ SELECT
12
+ *
13
+ FROM
14
+ users
15
+ `);
16
+ return rows;
12
17
  }
13
18
 
14
19
  public async findUserById(userId: number): Promise<User> {
15
- const findUser: User = UserModel.find(user => user.id === userId);
16
- if (!findUser) throw new HttpException(409, "User doesn't exist");
20
+ const { rows, rowCount } = await pg.query(
21
+ `
22
+ SELECT
23
+ *
24
+ FROM
25
+ users
26
+ WHERE
27
+ id = $1
28
+ `,
29
+ [userId],
30
+ );
31
+ if (!rowCount) throw new HttpException(409, "User doesn't exist");
17
32
 
18
- return findUser;
33
+ return rows[0];
19
34
  }
20
35
 
21
36
  public async createUser(userData: User): Promise<User> {
22
- const findUser: User = UserModel.find(user => user.email === userData.email);
23
- if (findUser) throw new HttpException(409, `This email ${userData.email} already exists`);
37
+ const { email, password } = userData;
24
38
 
25
- const hashedPassword = await hash(userData.password, 10);
26
- const createUserData: User = { id: UserModel.length + 1, ...userData, password: hashedPassword };
39
+ const { rows } = await pg.query(
40
+ `
41
+ SELECT EXISTS(
42
+ SELECT
43
+ "email"
44
+ FROM
45
+ users
46
+ WHERE
47
+ "email" = $1
48
+ )`,
49
+ [email],
50
+ );
51
+ if (rows[0].exists) throw new HttpException(409, `This email ${email} already exists`);
27
52
 
28
- return createUserData;
53
+ const hashedPassword = await hash(password, 10);
54
+ const { rows: createUserData } = await pg.query(
55
+ `
56
+ INSERT INTO
57
+ users(
58
+ "email",
59
+ "password"
60
+ )
61
+ VALUES ($1, $2)
62
+ RETURNING "email", "password"
63
+ `,
64
+ [email, hashedPassword],
65
+ );
66
+
67
+ return createUserData[0];
29
68
  }
30
69
 
31
70
  public async updateUser(userId: number, userData: User): Promise<User[]> {
32
- const findUser: User = UserModel.find(user => user.id === userId);
33
- if (!findUser) throw new HttpException(409, "User doesn't exist");
71
+ const { rows: findUser } = await pg.query(
72
+ `
73
+ SELECT EXISTS(
74
+ SELECT
75
+ "id"
76
+ FROM
77
+ users
78
+ WHERE
79
+ "id" = $1
80
+ )`,
81
+ [userId],
82
+ );
83
+ if (findUser[0].exists) throw new HttpException(409, "User doesn't exist");
34
84
 
35
- const hashedPassword = await hash(userData.password, 10);
36
- const updateUserData: User[] = UserModel.map((user: User) => {
37
- if (user.id === findUser.id) user = { id: userId, ...userData, password: hashedPassword };
38
- return user;
39
- });
85
+ const { email, password } = userData;
86
+ const hashedPassword = await hash(password, 10);
87
+ const { rows: updateUserData } = await pg.query(
88
+ `
89
+ UPDATE
90
+ users
91
+ SET
92
+ "email" = $2,
93
+ "password" = $3
94
+ WHERE
95
+ "id" = $1
96
+ RETURNING "email", "password"
97
+ `,
98
+ [userId, email, hashedPassword],
99
+ );
40
100
 
41
101
  return updateUserData;
42
102
  }
43
103
 
44
104
  public async deleteUser(userId: number): Promise<User[]> {
45
- const findUser: User = UserModel.find(user => user.id === userId);
46
- if (!findUser) throw new HttpException(409, "User doesn't exist");
105
+ const { rows: findUser } = await pg.query(
106
+ `
107
+ SELECT EXISTS(
108
+ SELECT
109
+ "id"
110
+ FROM
111
+ users
112
+ WHERE
113
+ "id" = $1
114
+ )`,
115
+ [userId],
116
+ );
117
+ if (findUser[0].exists) throw new HttpException(409, "User doesn't exist");
118
+
119
+ const { rows: deleteUserData } = await pg.query(
120
+ `
121
+ DELETE
122
+ FROM
123
+ users
124
+ WHERE
125
+ id = $1
126
+ RETURNING "email", "password"
127
+ `,
128
+ [userId],
129
+ );
47
130
 
48
- const deleteUserData: User[] = UserModel.filter(user => user.id !== findUser.id);
49
131
  return deleteUserData;
50
132
  }
51
133
  }
@@ -1,15 +1,17 @@
1
1
  import request from 'supertest';
2
2
  import { App } from '@/app';
3
+ import pg from '@database';
3
4
  import { CreateUserDto } from '@dtos/users.dto';
4
5
  import { AuthRoute } from '@routes/auth.route';
5
6
 
6
7
  afterAll(async () => {
7
8
  await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
9
+ pg.end();
8
10
  });
9
11
 
10
12
  describe('Testing Auth', () => {
11
13
  describe('[POST] /signup', () => {
12
- it('response should have the Create userData', () => {
14
+ it('response should have the Create userData', async () => {
13
15
  const userData: CreateUserDto = {
14
16
  email: 'example@email.com',
15
17
  password: 'password',
@@ -17,7 +19,10 @@ describe('Testing Auth', () => {
17
19
  const authRoute = new AuthRoute();
18
20
  const app = new App([authRoute]);
19
21
 
20
- return request(app.getServer()).post('/signup').send(userData);
22
+ return await request(app.getServer())
23
+ .post('/signup')
24
+ .send(userData)
25
+ .expect(201);
21
26
  });
22
27
  });
23
28
 
@@ -31,7 +36,7 @@ describe('Testing Auth', () => {
31
36
  const authRoute = new AuthRoute();
32
37
  const app = new App([authRoute]);
33
38
 
34
- return request(app.getServer())
39
+ return await request(app.getServer())
35
40
  .post('/login')
36
41
  .send(userData)
37
42
  .expect('Set-Cookie', /^Authorization=.+/);
@@ -1,33 +1,35 @@
1
1
  import request from 'supertest';
2
- import App from '@/app';
2
+ import { App } from '@/app';
3
+ import pg from '@database';
3
4
  import { CreateUserDto } from '@dtos/users.dto';
4
- import { User } from '@interfaces/users.interface';
5
- import { UserModel } from '@models/users.model';
6
5
  import { UserRoute } from '@routes/users.route';
7
6
 
8
7
  afterAll(async () => {
9
8
  await new Promise<void>(resolve => setTimeout(() => resolve(), 500));
9
+ pg.end();
10
10
  });
11
11
 
12
12
  describe('Testing Users', () => {
13
13
  describe('[GET] /users', () => {
14
- it('response statusCode 200 / findAll', () => {
15
- const findUser: User[] = UserModel;
14
+ it('response statusCode 200 / findAll', async () => {
16
15
  const usersRoute = new UserRoute();
17
16
  const app = new App([usersRoute]);
18
17
 
19
- return request(app.getServer()).get(`${usersRoute.path}`).expect(200, { data: findUser, message: 'findAll' });
18
+ return await request(app.getServer()).get(`${usersRoute.path}`).expect(200);
20
19
  });
21
20
  });
22
21
 
23
22
  describe('[GET] /users/:id', () => {
24
- it('response statusCode 200 / findOne', () => {
25
- const userId = 1;
26
- const findUser: User = UserModel.find(user => user.id === userId);
23
+ it('response statusCode 200 / findOne', async () => {
27
24
  const usersRoute = new UserRoute();
28
25
  const app = new App([usersRoute]);
29
26
 
30
- return request(app.getServer()).get(`${usersRoute.path}/${userId}`).expect(200, { data: findUser, message: 'findOne' });
27
+ return await request(app.getServer())
28
+ .get(`${usersRoute.path}`)
29
+ .query({
30
+ userId: 1,
31
+ })
32
+ .expect(200);
31
33
  });
32
34
  });
33
35
 
@@ -40,7 +42,7 @@ describe('Testing Users', () => {
40
42
  const usersRoute = new UserRoute();
41
43
  const app = new App([usersRoute]);
42
44
 
43
- return request(app.getServer()).post(`${usersRoute.path}`).send(userData).expect(201);
45
+ return await request(app.getServer()).post(`${usersRoute.path}`).send(userData).expect(201);
44
46
  });
45
47
  });
46
48
 
@@ -54,18 +56,17 @@ describe('Testing Users', () => {
54
56
  const usersRoute = new UserRoute();
55
57
  const app = new App([usersRoute]);
56
58
 
57
- return request(app.getServer()).put(`${usersRoute.path}/${userId}`).send(userData).expect(200);
59
+ return await request(app.getServer()).put(`${usersRoute.path}/${userId}`).send(userData).expect(200);
58
60
  });
59
61
  });
60
62
 
61
63
  describe('[DELETE] /users/:id', () => {
62
- it('response statusCode 200 / deleted', () => {
64
+ it('response statusCode 200 / deleted', async () => {
63
65
  const userId = 1;
64
- const deleteUser: User[] = UserModel.filter(user => user.id !== userId);
65
66
  const usersRoute = new UserRoute();
66
67
  const app = new App([usersRoute]);
67
68
 
68
- return request(app.getServer()).delete(`${usersRoute.path}/${userId}`).expect(200, { data: deleteUser, message: 'deleted' });
69
+ return await request(app.getServer()).delete(`${usersRoute.path}/${userId}`).expect(200);
69
70
  });
70
71
  });
71
72
  });
@@ -29,7 +29,6 @@
29
29
  "@exceptions/*": ["exceptions/*"],
30
30
  "@interfaces/*": ["interfaces/*"],
31
31
  "@middlewares/*": ["middlewares/*"],
32
- "@models/*": ["models/*"],
33
32
  "@routes/*": ["routes/*"],
34
33
  "@services/*": ["services/*"],
35
34
  "@utils/*": ["utils/*"]
@@ -25,7 +25,7 @@ build: ## Build the container image - Production
25
25
  docker build -t ${APP_NAME}\
26
26
  -f Dockerfile.prod .
27
27
 
28
- build-dev: ## Build the container image - Dvelopment
28
+ build-dev: ## Build the container image - Development
29
29
  docker build -t ${APP_NAME}\
30
30
  -f Dockerfile.dev .
31
31
 
@@ -43,4 +43,4 @@ remove: ## Remove the volumes
43
43
 
44
44
  #-- Database
45
45
  db: ## Start the local database MySQL
46
- docker-compose up -d mysql
46
+ docker-compose up -d mysql
@@ -7,7 +7,7 @@ import { ValidationMiddleware } from '@middlewares/validation.middleware';
7
7
  export class UserRoute implements Routes {
8
8
  public path = '/users';
9
9
  public router = Router();
10
- public user = new UsersController();
10
+ public user = new UserController();
11
11
 
12
12
  constructor() {
13
13
  this.initializeRoutes();
@@ -25,7 +25,7 @@ build: ## Build the container image - Production
25
25
  docker build -t ${APP_NAME}\
26
26
  -f Dockerfile.prod .
27
27
 
28
- build-dev: ## Build the container image - Dvelopment
28
+ build-dev: ## Build the container image - Development
29
29
  docker build -t ${APP_NAME}\
30
30
  -f Dockerfile.dev .
31
31
 
@@ -0,0 +1,8 @@
1
+ const path = require('path');
2
+
3
+ module.exports = {
4
+ config: path.resolve('src', 'config', 'sequelize-cli.js'),
5
+ 'models-path': path.resolve('src', 'models'),
6
+ 'seeders-path': path.resolve('src', 'database', 'seeders'),
7
+ 'migrations-path': path.resolve('src', 'database', 'migrations'),
8
+ };
@@ -25,7 +25,7 @@ build: ## Build the container image - Production
25
25
  docker build -t ${APP_NAME}\
26
26
  -f Dockerfile.prod .
27
27
 
28
- build-dev: ## Build the container image - Dvelopment
28
+ build-dev: ## Build the container image - Development
29
29
  docker build -t ${APP_NAME}\
30
30
  -f Dockerfile.dev .
31
31
 
@@ -43,4 +43,4 @@ remove: ## Remove the volumes
43
43
 
44
44
  #-- Database
45
45
  db: ## Start the local database MySQL
46
- docker-compose up -d mysql
46
+ docker-compose up -d mysql
@@ -13,7 +13,9 @@
13
13
  "lint": "eslint --ignore-path .gitignore --ext .ts src/",
14
14
  "lint:fix": "npm run lint -- --fix",
15
15
  "deploy:prod": "npm run build && pm2 start ecosystem.config.js --only prod",
16
- "deploy:dev": "pm2 start ecosystem.config.js --only dev"
16
+ "deploy:dev": "pm2 start ecosystem.config.js --only dev",
17
+ "migration:generate": "sequelize migration:generate --name",
18
+ "migration:run": "sequelize db:migrate"
17
19
  },
18
20
  "dependencies": {
19
21
  "bcrypt": "^5.0.1",
@@ -44,8 +44,8 @@ export class App {
44
44
  return this.app;
45
45
  }
46
46
 
47
- private connectToDatabase() {
48
- DB.sequelize.sync({ force: false });
47
+ private async connectToDatabase() {
48
+ await DB.sequelize.sync({ force: false });
49
49
  }
50
50
 
51
51
  private initializeMiddlewares() {
@@ -0,0 +1,15 @@
1
+ const { config } = require("dotenv");
2
+ config({ path: `.env.${process.env.NODE_ENV || "development"}.local` });
3
+
4
+ const { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_DATABASE } = process.env;
5
+
6
+ module.exports = {
7
+ username: DB_USER,
8
+ password: DB_PASSWORD,
9
+ database: DB_DATABASE,
10
+ port: DB_PORT,
11
+ host: DB_HOST,
12
+ dialect: "mysql",
13
+ migrationStorageTableName: "sequelize_migrations",
14
+ seederStorageTableName: "sequelize_seeds",
15
+ };
@@ -25,7 +25,7 @@ build: ## Build the container image - Production
25
25
  docker build -t ${APP_NAME}\
26
26
  -f Dockerfile.prod .
27
27
 
28
- build-dev: ## Build the container image - Dvelopment
28
+ build-dev: ## Build the container image - Development
29
29
  docker build -t ${APP_NAME}\
30
30
  -f Dockerfile.dev .
31
31
 
@@ -43,4 +43,4 @@ remove: ## Remove the volumes
43
43
 
44
44
  #-- Database
45
45
  db: ## Start the local database MongoDB
46
- docker-compose up -d mongo
46
+ docker-compose up -d mongo
@@ -6,11 +6,11 @@ import express from 'express';
6
6
  import helmet from 'helmet';
7
7
  import hpp from 'hpp';
8
8
  import morgan from 'morgan';
9
- import { connect, set } from 'mongoose';
9
+
10
10
  import swaggerJSDoc from 'swagger-jsdoc';
11
11
  import swaggerUi from 'swagger-ui-express';
12
12
  import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
13
- import { dbConnection } from '@databases';
13
+ import { dbConnection } from '@database';
14
14
  import { Routes } from '@interfaces/routes.interface';
15
15
  import { ErrorMiddleware } from '@middlewares/error.middleware';
16
16
  import { logger, stream } from '@utils/logger';
@@ -45,12 +45,8 @@ class App {
45
45
  return this.app;
46
46
  }
47
47
 
48
- private connectToDatabase() {
49
- if (this.env !== 'production') {
50
- set('debug', true);
51
- }
52
-
53
- connect(dbConnection);
48
+ private async connectToDatabase() {
49
+ await dbConnection();
54
50
  }
55
51
 
56
52
  private initializeMiddlewares() {
@@ -1,11 +1,18 @@
1
- import { DB_HOST, DB_PORT, DB_DATABASE } from '@config';
1
+ import { connect, set } from 'mongoose';
2
+ import { NODE_ENV, DB_HOST, DB_PORT, DB_DATABASE } from '@config';
2
3
 
3
- // export const dbConnection = `mongodb://${DB_HOST}:${DB_PORT}/${DB_DATABASE}`;
4
-
5
- export const dbConnection = {
4
+ export const dbConnection = async () => {
5
+ const dbConfing = {
6
6
  url: `mongodb://${DB_HOST}:${DB_PORT}/${DB_DATABASE}`,
7
7
  options: {
8
8
  useNewUrlParser: true,
9
9
  useUnifiedTopology: true
10
10
  },
11
- };
11
+ };
12
+
13
+ if (NODE_ENV !== 'production') {
14
+ set('debug', true);
15
+ }
16
+
17
+ await connect(dbConfing);
18
+ };
@@ -2,11 +2,11 @@
2
2
  PORT = 3000
3
3
 
4
4
  # DATABASE
5
- DB_USER = root
6
- DB_PASSWORD = password
7
- DB_HOST = localhost
8
- DB_PORT = 5432
9
- DB_DATABASE = dev
5
+ POSTGRES_USER = root
6
+ POSTGRES_PASSWORD = password
7
+ POSTGRES_HOST = localhost
8
+ POSTGRES_PORT = 5432
9
+ POSTGRES_DATABASE = dev
10
10
 
11
11
  # TOKEN
12
12
  SECRET_KEY = secretKey
@@ -2,11 +2,11 @@
2
2
  PORT = 3000
3
3
 
4
4
  # DATABASE
5
- DB_USER = root
6
- DB_PASSWORD = password
7
- DB_HOST = localhost
8
- DB_PORT = 5432
9
- DB_DATABASE = dev
5
+ POSTGRES_USER = root
6
+ POSTGRES_PASSWORD = password
7
+ POSTGRES_HOST = localhost
8
+ POSTGRES_PORT = 5432
9
+ POSTGRES_DATABASE = dev
10
10
 
11
11
  # TOKEN
12
12
  SECRET_KEY = secretKey
@@ -2,11 +2,11 @@
2
2
  PORT = 3000
3
3
 
4
4
  # DATABASE
5
- DB_USER = root
6
- DB_PASSWORD = password
7
- DB_HOST = localhost
8
- DB_PORT = 5432
9
- DB_DATABASE = dev
5
+ POSTGRES_USER = root
6
+ POSTGRES_PASSWORD = password
7
+ POSTGRES_HOST = localhost
8
+ POSTGRES_PORT = 5432
9
+ POSTGRES_DATABASE = dev
10
10
 
11
11
  # TOKEN
12
12
  SECRET_KEY = secretKey
@@ -25,7 +25,7 @@ build: ## Build the container image - Production
25
25
  docker build -t ${APP_NAME}\
26
26
  -f Dockerfile.prod .
27
27
 
28
- build-dev: ## Build the container image - Dvelopment
28
+ build-dev: ## Build the container image - Development
29
29
  docker build -t ${APP_NAME}\
30
30
  -f Dockerfile.dev .
31
31
 
@@ -20,11 +20,11 @@ services:
20
20
  ports:
21
21
  - "3000:3000"
22
22
  environment:
23
- DB_USER: root
24
- DB_PASSWORD: password
25
- DB_HOST: pg
26
- DB_PORT: 5432
27
- DB_DATABASE: dev
23
+ POSTGRES_USER: root
24
+ POSTGRES_PASSWORD: password
25
+ POSTGRES_HOST: pg
26
+ POSTGRES_PORT: 5432
27
+ POSTGRES_DATABASE: dev
28
28
  volumes:
29
29
  - ./:/app
30
30
  - /app/node_modules
@@ -40,9 +40,9 @@ services:
40
40
  container_name: pg
41
41
  image: postgres:14.5-alpine
42
42
  environment:
43
- DB_USER: root
44
- DB_PASSWORD: password
45
- DB_DATABASE: dev
43
+ POSTGRES_USER: root
44
+ POSTGRES_PASSWORD: password
45
+ POSTGRES_DB: dev
46
46
  ports:
47
47
  - "5432:5432"
48
48
  networks: