From a11db3d6b027b39cbd9015495fbd083c20f5ec73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Kor=C4=8Dek?= Date: Tue, 1 Jul 2025 13:34:17 +0200 Subject: [PATCH 1/3] TECH-988: setup database --- docker-compose.yml | 17 ++++++++++++++++ src/db/index.ts | 51 +++++++++++++++++++++++++--------------------- 2 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..fa8f6f8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +name: fitness-app + +services: + db: + container_name: fitness-app-database + image: postgres:16 + environment: + POSTGRES_DB: fitness_app + POSTGRES_USER: postgres + POSTGRES_PASSWORD: root + ports: + - '5432:5432' + volumes: + - postgres_data:/var/lib/postgresql/data + +volumes: + postgres_data: diff --git a/src/db/index.ts b/src/db/index.ts index bac1357..4d1f995 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,36 +1,41 @@ -import fs from 'fs' -import { Sequelize } from 'sequelize' +import fs from 'fs'; +import { Sequelize } from 'sequelize'; -import defineExercise from './exercise' -import defineProgram from './program' +import defineExercise from './exercise'; +import defineProgram from './program'; -const sequelize: Sequelize = new Sequelize('postgresql://localhost:5432/fitness_app', { - logging: false -}) +const sequelize: Sequelize = new Sequelize( + 'postgresql://postgres:root@localhost:5432/fitness_app', + { + logging: false, + } +); -sequelize.authenticate().catch((e: any) => console.error(`Unable to connect to the database${e}.`)) +sequelize + .authenticate() + .catch((e: any) => console.error(`Unable to connect to the database${e}.`)); -const Exercise = defineExercise(sequelize, 'exercise') -const Program = defineProgram(sequelize, 'program') +const Exercise = defineExercise(sequelize, 'exercise'); +const Program = defineProgram(sequelize, 'program'); const models = { - Exercise, - Program -} -type Models = typeof models + Exercise, + Program, +}; +type Models = typeof models; // check if every model is imported -const modelsFiles = fs.readdirSync(__dirname) +const modelsFiles = fs.readdirSync(__dirname); // -1 because index.ts can not be counted -if (Object.keys(models).length !== (modelsFiles.length - 1)) { - throw new Error('You probably forgot import database model!') +if (Object.keys(models).length !== modelsFiles.length - 1) { + throw new Error('You probably forgot import database model!'); } Object.values(models).forEach((value: any) => { - if (value.associate) { - value.associate(models) - } -}) + if (value.associate) { + value.associate(models); + } +}); -export { models, sequelize } -export type { Models } +export { models, sequelize }; +export type { Models }; From eb7ce0dd8c542699de8c5e7137cb7f7c2770c99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Kor=C4=8Dek?= Date: Tue, 1 Jul 2025 13:36:37 +0200 Subject: [PATCH 2/3] TECH-988: remove formatting --- src/db/index.ts | 51 ++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/db/index.ts b/src/db/index.ts index 4d1f995..83a9077 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,41 +1,36 @@ -import fs from 'fs'; -import { Sequelize } from 'sequelize'; +import fs from 'fs' +import { Sequelize } from 'sequelize' -import defineExercise from './exercise'; -import defineProgram from './program'; +import defineExercise from './exercise' +import defineProgram from './program' -const sequelize: Sequelize = new Sequelize( - 'postgresql://postgres:root@localhost:5432/fitness_app', - { - logging: false, - } -); +const sequelize: Sequelize = new Sequelize('postgresql://postgres:root@localhost:5432/fitness_app', { + logging: false +}) -sequelize - .authenticate() - .catch((e: any) => console.error(`Unable to connect to the database${e}.`)); +sequelize.authenticate().catch((e: any) => console.error(`Unable to connect to the database${e}.`)) -const Exercise = defineExercise(sequelize, 'exercise'); -const Program = defineProgram(sequelize, 'program'); +const Exercise = defineExercise(sequelize, 'exercise') +const Program = defineProgram(sequelize, 'program') const models = { - Exercise, - Program, -}; -type Models = typeof models; + Exercise, + Program +} +type Models = typeof models // check if every model is imported -const modelsFiles = fs.readdirSync(__dirname); +const modelsFiles = fs.readdirSync(__dirname) // -1 because index.ts can not be counted -if (Object.keys(models).length !== modelsFiles.length - 1) { - throw new Error('You probably forgot import database model!'); +if (Object.keys(models).length !== (modelsFiles.length - 1)) { + throw new Error('You probably forgot import database model!') } Object.values(models).forEach((value: any) => { - if (value.associate) { - value.associate(models); - } -}); + if (value.associate) { + value.associate(models) + } +}) -export { models, sequelize }; -export type { Models }; +export { models, sequelize } +export type { Models } From 76d1afb098beb6a6999d6eeeb836c353f61bed69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1n=20Kor=C4=8Dek?= Date: Tue, 1 Jul 2025 14:09:36 +0200 Subject: [PATCH 3/3] TECH-988: update instructions --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cdadf8e..0c53f93 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - fork or download this repository - install dependencies with `npm i` -- create fitness_app database (application access `postgresql://localhost:5432/fitness_app`, make sure you use correct port and db name ) +- create fitness_app database with `docker compose up -d` or manually (application access `postgresql://postgres:root@localhost:5432/fitness_app`, make sure you use correct port, db name and db credentials ) - create db schema and populate db with `npm run seed` - run express server with `npm start`