From 337c4c547228390ee2dcb26b6ea74a5a2afc049e Mon Sep 17 00:00:00 2001 From: Erik Date: Sun, 10 Dec 2023 11:27:52 +0100 Subject: [PATCH] Updated for prod and dev testing db. --- .gitignore | 3 ++- package-lock.json | 28 ++++++++++++++++++++++++---- package.json | 3 ++- src/server.ts | 2 ++ src/startup/db.ts | 2 +- test/routes/project.test.ts | 4 ++-- 6 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 76add87..a0d218e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules -dist \ No newline at end of file +dist +.env \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a8af44c..87e8209 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "body-parser": "^1.20.2", "config": "^3.3.9", "cors": "^2.8.5", + "dotenv": "^16.3.1", "express": "^4.18.2", "express-rate-limit": "^7.1.5", "joi": "^17.11.0", @@ -22,7 +23,7 @@ "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/jest": "^29.5.11", - "@types/node": "^20.6.5", + "@types/node": "^20.10.4", "@types/supertest": "^2.0.16", "@typescript-eslint/eslint-plugin": "^6.8.0", "@typescript-eslint/parser": "^6.8.0", @@ -1510,9 +1511,12 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.6.5", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.6.5.tgz", - "integrity": "sha512-2qGq5LAOTh9izcc0+F+dToFigBWiK1phKPt7rNhOqJSr35y8rlIBjDwGtFSgAI6MGIhjwOVNSQZVdJsZJ2uR1w==" + "version": "20.10.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.4.tgz", + "integrity": "sha512-D08YG6rr8X90YB56tSIuBaddy/UXAA9RKJoFvrsnogAum/0pmjkgi4+2nx96A330FmioegBWmEYQ+syqCFaveg==", + "dependencies": { + "undici-types": "~5.26.4" + } }, "node_modules/@types/qs": { "version": "6.9.10", @@ -2900,6 +2904,17 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -7304,6 +7319,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==" + }, "node_modules/unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index ab760ce..351e91a 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "body-parser": "^1.20.2", "config": "^3.3.9", "cors": "^2.8.5", + "dotenv": "^16.3.1", "express": "^4.18.2", "express-rate-limit": "^7.1.5", "joi": "^17.11.0", @@ -24,7 +25,7 @@ "@types/cors": "^2.8.17", "@types/express": "^4.17.21", "@types/jest": "^29.5.11", - "@types/node": "^20.6.5", + "@types/node": "^20.10.4", "@types/supertest": "^2.0.16", "@typescript-eslint/eslint-plugin": "^6.8.0", "@typescript-eslint/parser": "^6.8.0", diff --git a/src/server.ts b/src/server.ts index e562a0a..4144e05 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,6 +4,8 @@ import isOnline from './routes/isOnline'; import project from './routes/project'; import connectToDatabase from './startup/db'; import addRateLimiter from './startup/limitRate'; +import * as dotenv from 'dotenv'; +dotenv.config(); const app: Express = express(); diff --git a/src/startup/db.ts b/src/startup/db.ts index 9987ddb..3203051 100644 --- a/src/startup/db.ts +++ b/src/startup/db.ts @@ -2,7 +2,7 @@ import mongoose from 'mongoose'; const connectToDatabase = async () => { try { - await mongoose.connect('mongodb://127.0.0.1:27017/berlin'); + await mongoose.connect(process.env.db); } catch (error) { console.error('Error connecting to MongoDB:', error.message); throw error; diff --git a/test/routes/project.test.ts b/test/routes/project.test.ts index 1723ec5..c77b2f1 100644 --- a/test/routes/project.test.ts +++ b/test/routes/project.test.ts @@ -15,10 +15,10 @@ describe('Project Routes Tests', () => { }); it('should get projects when GET /:id', async () => { - const response = await app.get('/project/6574e248ed4efbe66d836229'); + const response = await app.get('/project/65750b72771d33966c6da5cc'); expect(response.status).toBe(200); expect(response.body.name).toEqual('GetSpecialId'); - expect(response.body._id).toEqual('6574e248ed4efbe66d836229'); + expect(response.body._id).toEqual('65750b72771d33966c6da5cc'); }); it('should create a project when POST /', async () => {