From 04dcfd25a5b0420ef2e030b41e1db31ce920f703 Mon Sep 17 00:00:00 2001 From: NecroBread Date: Mon, 8 Jan 2024 10:27:02 +0200 Subject: [PATCH] update config --- .env.example | 4 ++++ .env.local.example | 6 ++++-- docker-compose-debug.yml | 2 +- docker-compose-dev.yml | 1 - docker-compose.yml | 1 - src/config/db.ts | 13 ++++++++----- src/config/get-config.ts | 14 +++++++++++++- src/features/article/article-repository.ts | 2 +- src/features/article/article-service.ts | 16 ++++++++-------- src/features/comment/comment-repository.ts | 2 +- src/features/comment/comment-service.ts | 16 ++++++++-------- src/features/user/user-repository.ts | 2 +- src/features/user/user-service.ts | 18 +++++++++--------- src/index.ts | 7 ------- 14 files changed, 58 insertions(+), 46 deletions(-) diff --git a/.env.example b/.env.example index d5e28d5..200d89e 100644 --- a/.env.example +++ b/.env.example @@ -4,3 +4,7 @@ ALLOW_ORIGIN="*" ALLOW_METHODS="GET,PUT,POST,PATCH,DELETE,HEAD,OPTIONS" ALLOW_HEADERS="Content-Type,Cache-Control,Expires" JWT_SECRET=SUPERSECRET +DB_HOST=host.docker.internal +DB_PORT=6543 +DB_USERNAME=postgres +DB_PASSWORD=123123 \ No newline at end of file diff --git a/.env.local.example b/.env.local.example index 087cdac..200d89e 100644 --- a/.env.local.example +++ b/.env.local.example @@ -4,5 +4,7 @@ ALLOW_ORIGIN="*" ALLOW_METHODS="GET,PUT,POST,PATCH,DELETE,HEAD,OPTIONS" ALLOW_HEADERS="Content-Type,Cache-Control,Expires" JWT_SECRET=SUPERSECRET -CONNECTION_STRING="mongodb://localhost:27017/express-template" # When started locally -# CONNECTION_STRING="mongodb://express-ts-api-mongo-1:27017/express-template" # When started in docker dev mode +DB_HOST=host.docker.internal +DB_PORT=6543 +DB_USERNAME=postgres +DB_PASSWORD=123123 \ No newline at end of file diff --git a/docker-compose-debug.yml b/docker-compose-debug.yml index a034165..bb9f880 100644 --- a/docker-compose-debug.yml +++ b/docker-compose-debug.yml @@ -1,5 +1,5 @@ # Overlay configuration to enable debuggers -version: "3.9" +version: "3" services: app: command: diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml index 14bfc45..055c2b6 100644 --- a/docker-compose-dev.yml +++ b/docker-compose-dev.yml @@ -25,7 +25,6 @@ services: postgres_db: image: postgres:latest - container_name: postgres_db environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=123123 diff --git a/docker-compose.yml b/docker-compose.yml index f253d31..4ba47da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,7 +20,6 @@ services: postgres_db: image: postgres:latest - container_name: postgres_db environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=123123 diff --git a/src/config/db.ts b/src/config/db.ts index a26ffda..2f66a66 100644 --- a/src/config/db.ts +++ b/src/config/db.ts @@ -3,15 +3,18 @@ import "reflect-metadata"; import { Article } from "../features/article/article-entity"; import { Comment } from "../features/comment/comment-entity"; import { User } from "../features/user/user-entity"; +import getConfig from "./get-config"; + +const config = getConfig(); export const AppDataSource = new DataSource({ type: "postgres", - host: "localhost", - port: 6543, - password: "123123", - username: "postgres", + host: config.DB_HOST, + port: parseInt(config.DB_PORT), + username: config.DB_USERNAME, + password: config.DB_PASSWORD, database: "express_app", - synchronize: true, + synchronize: config.NODE_ENV === "production" ? false : true, logging: true, entities: [Article, Comment, User], }); diff --git a/src/config/get-config.ts b/src/config/get-config.ts index b3746cc..2807b39 100644 --- a/src/config/get-config.ts +++ b/src/config/get-config.ts @@ -1,11 +1,23 @@ +import dotenv from "dotenv"; + +/** + * Load enviroment variables + */ +dotenv.config(); +dotenv.config({ path: `.env.local`, override: true }); + export default function getConfig() { + console.log(process.env); return { NODE_ENV: process.env.NODE_ENV, PORT: process.env.PORT, ALLOW_ORIGIN: process.env.ALLOW_ORIGIN, ALLOW_METHODS: process.env.ALLOW_METHODS, ALLOW_HEADERS: process.env.ALLOW_HEADERS, - CONNECTION_STRING: process.env.CONNECTION_STRING, JWT_SECRET: process.env.JWT_SECRET, + DB_HOST: process.env.DB_HOST, + DB_PORT: process.env.DB_PORT, + DB_PASSWORD: process.env.DB_PASSWORD, + DB_USERNAME: process.env.DB_USERNAME, }; } diff --git a/src/features/article/article-repository.ts b/src/features/article/article-repository.ts index 9b464c3..1bce69c 100644 --- a/src/features/article/article-repository.ts +++ b/src/features/article/article-repository.ts @@ -1,4 +1,4 @@ import { AppDataSource } from "../../config/db"; import { Article } from "./article-entity"; -export const articleRepository = () => AppDataSource.getRepository(Article); +export const getArticleRepository = () => AppDataSource.getRepository(Article); diff --git a/src/features/article/article-service.ts b/src/features/article/article-service.ts index 44dabe2..bb25579 100644 --- a/src/features/article/article-service.ts +++ b/src/features/article/article-service.ts @@ -1,11 +1,11 @@ import { NotFoundError, UnauthorizedError } from "../../utils/app-error"; import { IFullQuery, buildQuery } from "../../utils/build-query"; import { Article } from "./article-entity"; -import { articleRepository } from "./article-repository"; +import { getArticleRepository } from "./article-repository"; import { ArticleDto, ICreateArticleData, IPutArticleData } from "./article-types"; export async function getAll(query: IFullQuery): Promise { - const articles = await buildQuery(articleRepository(), query); + const articles = await buildQuery(getArticleRepository(), query); if (!articles) { return []; @@ -15,7 +15,7 @@ export async function getAll(query: IFullQuery): Promise { } export async function getOne(id: string, query: any): Promise { - let queryBuilder = articleRepository().createQueryBuilder("entity"); + let queryBuilder = getArticleRepository().createQueryBuilder("entity"); queryBuilder = queryBuilder.where("entity.id = :id", { id }); @@ -40,7 +40,7 @@ export async function create(data: ICreateArticleData & { owner: string }): Prom article.created_at = new Date(); article.updated_at = new Date(); - await articleRepository().save(article); + await getArticleRepository().save(article); return new ArticleDto(article); } @@ -48,7 +48,7 @@ export async function create(data: ICreateArticleData & { owner: string }): Prom const ALLOWED_UPDATE_FIELDS = ["title", "content"]; export async function update(id: string, userId: string, data: IPutArticleData): Promise { - const article = await articleRepository().findOneBy({ id }); + const article = await getArticleRepository().findOneBy({ id }); if (article === null) throw new NotFoundError(); if (article.owner.toString() !== userId) throw new UnauthorizedError(); @@ -61,16 +61,16 @@ export async function update(id: string, userId: string, data: IPutArticleData): article.updated_at = new Date(); - await articleRepository().save(article); + await getArticleRepository().save(article); return new ArticleDto(article); } export async function remove(id: string, userId: string): Promise { - const article = await articleRepository().findOneBy({ id }); + const article = await getArticleRepository().findOneBy({ id }); if (!article) throw new NotFoundError(); if (article.owner !== userId) throw new UnauthorizedError(); - await articleRepository().remove(article); + await getArticleRepository().remove(article); } diff --git a/src/features/comment/comment-repository.ts b/src/features/comment/comment-repository.ts index 75b1700..8118845 100644 --- a/src/features/comment/comment-repository.ts +++ b/src/features/comment/comment-repository.ts @@ -1,4 +1,4 @@ import { AppDataSource } from "../../config/db"; import { Comment } from "./comment-entity"; -export const commentRepository = () => AppDataSource.getRepository(Comment); +export const getCommentRepository = () => AppDataSource.getRepository(Comment); diff --git a/src/features/comment/comment-service.ts b/src/features/comment/comment-service.ts index 33c656c..d9c3332 100644 --- a/src/features/comment/comment-service.ts +++ b/src/features/comment/comment-service.ts @@ -1,11 +1,11 @@ import { NotFoundError, UnauthorizedError } from "../../utils/app-error"; import { IFullQuery, buildQuery } from "../../utils/build-query"; import { Comment } from "./comment-entity"; -import { commentRepository } from "./comment-repository"; +import { getCommentRepository } from "./comment-repository"; import { CommentDto, ICreateCommentData, IPutCommentData } from "./comment-types"; export async function getAllComments(query: IFullQuery): Promise { - const comments = await buildQuery(commentRepository(), query); + const comments = await buildQuery(getCommentRepository(), query); if (!comments) { return []; @@ -15,7 +15,7 @@ export async function getAllComments(query: IFullQuery): Promise { - let queryBuilder = commentRepository().createQueryBuilder("entity"); + let queryBuilder = getCommentRepository().createQueryBuilder("entity"); queryBuilder = queryBuilder.where("entity.id = :id", { id }); @@ -41,7 +41,7 @@ export async function createComment(data: ICreateCommentData & { owner: string } comment.updated_at = new Date(); comment.owner = data.owner; - await commentRepository().save(comment); + await getCommentRepository().save(comment); return new CommentDto(comment); } @@ -49,7 +49,7 @@ export async function createComment(data: ICreateCommentData & { owner: string } const ALLOWED_UPDATE_FIELDS = ["content"]; export async function updateComment(id: string, userId: string, data: IPutCommentData): Promise { - const comment = await commentRepository().findOneBy({ id }); + const comment = await getCommentRepository().findOneBy({ id }); if (comment === null) throw new NotFoundError(); if (comment.owner !== userId) throw new UnauthorizedError(); @@ -62,16 +62,16 @@ export async function updateComment(id: string, userId: string, data: IPutCommen comment.updated_at = new Date(); - await commentRepository().save(comment); + await getCommentRepository().save(comment); return new CommentDto(comment); } export async function deleteComment(id: string, userId: string): Promise { - const comment = await commentRepository().findOneBy({ id }); + const comment = await getCommentRepository().findOneBy({ id }); if (!comment) throw new NotFoundError(); if (comment.owner !== userId) throw new UnauthorizedError(); - await commentRepository().remove(comment); + await getCommentRepository().remove(comment); } diff --git a/src/features/user/user-repository.ts b/src/features/user/user-repository.ts index a21240e..f4b2534 100644 --- a/src/features/user/user-repository.ts +++ b/src/features/user/user-repository.ts @@ -1,4 +1,4 @@ import { AppDataSource } from "../../config/db"; import { User } from "./user-entity"; -export const userRepository = () => AppDataSource.getRepository(User); +export const getUserRepository = () => AppDataSource.getRepository(User); diff --git a/src/features/user/user-service.ts b/src/features/user/user-service.ts index 233564f..0ea8c2e 100644 --- a/src/features/user/user-service.ts +++ b/src/features/user/user-service.ts @@ -3,14 +3,14 @@ import getConfig from "../../config/get-config"; import { IUserLocal, ISignUpUserData, UserDto, IUpdateUserData } from "./user-types"; import { BadRequestError, InternalServerError, NotFoundError, UnauthorizedError } from "../../utils/app-error"; import { User } from "./user-entity"; -import { userRepository } from "./user-repository"; +import { getUserRepository } from "./user-repository"; export async function signUp(userData: ISignUpUserData): Promise<[string, UserDto]> { if (!userData.username) { throw new BadRequestError(); } - const existing = await userRepository().findOneBy({ + const existing = await getUserRepository().findOneBy({ username: userData.username.toLowerCase(), }); @@ -26,7 +26,7 @@ export async function signUp(userData: ISignUpUserData): Promise<[string, UserDt user.created_at = new Date(); user.updated_at = new Date(); - await userRepository().save(user); + await getUserRepository().save(user); const token = await createToken(user.id); @@ -38,7 +38,7 @@ export async function signIn(username: string, password: string): Promise<[strin throw new BadRequestError(); } - const user = await userRepository().findOneBy({ + const user = await getUserRepository().findOneBy({ username: username.toLowerCase(), }); @@ -70,7 +70,7 @@ async function createToken(id: string) { } export async function getUser(userData: IUserLocal): Promise { - const user = await userRepository().findOneBy({ id: userData.id }); + const user = await getUserRepository().findOneBy({ id: userData.id }); if (!user) { throw new NotFoundError(); } @@ -81,7 +81,7 @@ export async function getUser(userData: IUserLocal): Promise { const ALLOWED_UPDATE_FIELDS = ["firstName", "lastName"]; export async function updateUser(userId: string, userData: Partial): Promise { - const user = await userRepository().findOneBy({ id: userId }); + const user = await getUserRepository().findOneBy({ id: userId }); if (!user || user.id !== userId) { throw new UnauthorizedError(); @@ -95,13 +95,13 @@ export async function updateUser(userId: string, userData: Partial