diff --git a/code/backend/package.json b/code/backend/package.json index 524cb7d..c31cc10 100644 --- a/code/backend/package.json +++ b/code/backend/package.json @@ -32,7 +32,11 @@ "typescript": "^5.8.3" }, "scripts": { - "start": "nodemon src/server.ts", + "start-containers": "docker-compose -f ../db/docker-compose.yaml up -d", + "stop-containers": "docker-compose down", + "start": "yarn start-containers && nodemon src/server.ts", + "start-no-docker": "nodemon src/server.ts", + "install-script": "ts-node scripts/install.ts", "build": "tsc", "dev": "ts-node-dev --respawn src/server.ts" } diff --git a/code/backend/scripts/install.json b/code/backend/scripts/install.json new file mode 100644 index 0000000..6e82cce --- /dev/null +++ b/code/backend/scripts/install.json @@ -0,0 +1,14 @@ +{ + "commands": [ + "echo Starting installation...", + "echo installing node_modules", + "yarn install", + "echo starting docker container", + "yarn start-containers", + "echo generating prisma", + "yarn prisma generate", + "echo migrating database", + "yarn prisma migrate dev --name init" + ], + "installed": true +} diff --git a/code/backend/scripts/install.ts b/code/backend/scripts/install.ts new file mode 100644 index 0000000..17642c0 --- /dev/null +++ b/code/backend/scripts/install.ts @@ -0,0 +1,37 @@ +import { exec } from "child_process"; +import util from "util"; +const execPromise = util.promisify(exec); +import fs from "fs"; + +const json_path: string = "scripts/install.json"; // Path to the JSON file +const raw = fs.readFileSync(json_path, "utf8"); +const config = JSON.parse(raw); // Parse the JSON file + +if (config.installed) { + // Check if the script has already been run + console.log("Already installed"); + process.exit(0); +} +(async () => { + for (const command of config.commands) { + try { + const { stdout, stderr } = await execPromise(command); // Wait for the command to finish + if (stderr) { + console.log(`stderr: ${stderr}`); + } + console.log(stdout); // Print the output of the command`); + } catch (error: any) { + console.log(`error: ${error.message}`); + break; // Stop execution if an error occurs + } + } + config.installed = true; // Set the installed flag to true + fs.writeFile(json_path, JSON.stringify(config), (err) => { + if (err) { + console.error("Error writing to file:", err); + } else { + console.log("File has been updated successfully."); + } + }); + console.log("Installation complete"); +})(); diff --git a/code/backend/tsconfig.json b/code/backend/tsconfig.json index f3dc2bd..4671c02 100644 --- a/code/backend/tsconfig.json +++ b/code/backend/tsconfig.json @@ -6,7 +6,8 @@ "esModuleInterop": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, - "outDir": "./dist" + "outDir": "./dist", + "resolveJsonModule": true // Allow importing JSON files }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] diff --git a/code/db/.env.example b/code/db/.env.example new file mode 100644 index 0000000..5274de1 --- /dev/null +++ b/code/db/.env.example @@ -0,0 +1,4 @@ +# This is an example of a .env file for the docker-compose setup. +# You can copy this file to .env and fill in the values.) +DB_USER=postgres +DB_PASSWORD=securePassword1234 \ No newline at end of file diff --git a/code/db/docker-compose.yaml b/code/db/docker-compose.yaml new file mode 100644 index 0000000..37833cd --- /dev/null +++ b/code/db/docker-compose.yaml @@ -0,0 +1,15 @@ +services: + db: + image: postgres:15 + container_name: postgres-DB + ports: + - "5432:5432" + environment: + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_DB: prisma + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: