Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoDB-Update #503

Merged
merged 5 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
309 changes: 237 additions & 72 deletions backend/package-lock.json

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "seed-test-backend",
"version": "1.6.2",
"engines": {
"node": "18.13.0"
"node": "^18.13.0"
},
"scripts": {
"start": "tsc && node src/server.js",
Expand Down Expand Up @@ -32,7 +32,7 @@
"express-session": "^1.17.2",
"geckodriver": "^3.0.1",
"moment": "^2.29.4",
"mongodb": "^3.7.3",
"mongodb": "^6.2.0",
"node-fetch": "^2.6.7",
"nodemailer": "^6.7.5",
"passport": "^0.6.0",
Expand Down Expand Up @@ -69,6 +69,8 @@
"<rootDir>/spec/serverHelper.spec.js",
"<rootDir>/node_modules/"
],
"setupFiles": ["./spec/setupTest.js"]
"setupFiles": [
"./spec/setupTest.js"
]
}
}
26 changes: 14 additions & 12 deletions backend/src/database/DbConnector.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const { MongoClient } = require('mongodb');
const { exit } = require('process');
const { setTimeout } = require('timers/promises');

const uri = process.env.DATABASE_URI || 'mongodb://SeedAdmin:SeedTest@seedmongodb:27017';

let connection = [];
// Create the database connection
async function establishConnection(callback) {
// eslint-disable-next-line max-len
MongoClient.connect(uri, { poolSize: 20, useNewUrlParser: true, useUnifiedTopology: true }, async (err, db) => {
if (err) {
console.log('DB_ERROR: Can`t connect to DB. The Project may not be set up correctly. For more information read the README');
exit(-1);
}
connection = db.db('Seed');
if (typeof callback === 'function' && callback()) callback(connection);
console.log('Established Database Connection!');
});
async function establishConnection(attempt) {
const attempts = attempt || 1;
if (attempt > 3) throw new Error('\x1b[31mFailed to connect to the database after multiple retries.\x1b[0m');

try {
const client = await MongoClient.connect(uri, { maxPoolSize: 20 });
connection = client.db('Seed');
return client;
} catch (err) {
console.log(`\x1b[38;5;208mConnection failed! Retrying... ${attempts}\x1b[0m`);
await setTimeout(3000);
return establishConnection(attempts + 1);
}
}

function getConnection() {
Expand Down
296 changes: 144 additions & 152 deletions backend/src/database/DbServices.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions backend/src/database/installDatabase.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ require('dotenv').config();

const uri = process.env.DATABASE_URI || 'mongodb://SeedAdmin:SeedTest@localhost:27017';

async function checkConnection() {
let fails = 1;
while (fails <= 3) try {
const client = await MongoClient.connect(uri, { poolSize: 20, useNewUrlParser: true, useUnifiedTopology: true });
async function getConnection(attempt) {
const attempts = attempt || 1;
if (attempt > 3) throw new Error('\x1b[31mFailed to connect to the database after multiple retries.\x1b[0m');

try {
const client = await MongoClient.connect(uri, { maxPoolSize: 20 });
return client;
} catch (err) {
console.log(`Connection failed! Retrying... ${fails}`);
fails++;
console.log(`\x1b[38;5;208mConnection failed! Retrying... ${attempts}\x1b[0m`);
return getConnection(attempts + 1);
}

throw new Error('Failed to connect to the database after multiple retries.');
}

async function makeCollection(dbo, name) {
Expand All @@ -41,7 +41,7 @@ async function makeCollection(dbo, name) {

async function installDatabase() {
console.log(`\x1b[33m Setting Up DB in: ${uri}\n\x1b[0m`);
const client = await checkConnection();
const client = await getConnection();
const dbo = client.db('Seed');

console.log('Starting: steps');
Expand Down
43 changes: 21 additions & 22 deletions backend/src/database/mongoDB_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mongo = require('./DbServices');
const stepTypes = require('./stepTypes');
require('dotenv').config();

const uri = process.env.DATABASE_URI || "mongodb://SeedAdmin:SeedTest@seedmongodb:27017";
const uri = process.env.DATABASE_URI || 'mongodb://SeedAdmin:SeedTest@seedmongodb:27017';
const dbName = 'Seed';

// /////////////////////////////////////////// ADMIN METHODS ////////////////////////////////////////////
Expand Down Expand Up @@ -83,7 +83,7 @@ async function createContent() {

// show all Collections
function getCollections() {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.listCollections().toArray((error, result) => {
Expand All @@ -94,18 +94,10 @@ function getCollections() {
});
}

async function installDatabase() {
console.log (`Setting Up DB in: ${uri}`);
await makeCollection('stepTypes');
await makeCollection('Stories');
await makeCollection('User');
await insertMore('stepTypes', stepTypes());
}

// create Collection
async function makeCollection(name) {
let connection = [];
await MongoClient.connect(uri, { poolSize: 20, useNewUrlParser: true, useUnifiedTopology: true }, async (err, dbo) => {
await MongoClient.connect(uri, { maxPoolSize: 20 }, async (err, dbo) => {
if (err) throw err;
connection = dbo.db('Seed');
});
Expand All @@ -120,7 +112,7 @@ async function makeCollection(name) {

// insert One document (collectionname, {document})
function insertOne(collection, content) {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection(collection).insertOne(content, (error) => {
Expand Down Expand Up @@ -156,7 +148,7 @@ async function backupScenarios() {

// insert Many documents ("collectionname", [{documents},{...}] )
function insertMore(name, content) {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection(name).insertMany(content, (error, res) => {
Expand All @@ -168,7 +160,7 @@ function insertMore(name, content) {
}

function update(story_id, updatedStuff) {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection(collection).updateOne({ story_id }, { $set: updatedStuff }, (error, res) => {
Expand All @@ -180,7 +172,7 @@ function update(story_id, updatedStuff) {

// doesnt work yet
function eraseAllStories() {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection(collection).deleteOne({}, (error) => {
Expand All @@ -192,7 +184,7 @@ function eraseAllStories() {

// shows single story
function showStory(story_id) {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
const myObjt = { story_id };
Expand All @@ -206,7 +198,7 @@ function showStory(story_id) {

// delete collection
function dropCollection() {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection(collection).drop((error, delOK) => {
Expand All @@ -217,9 +209,8 @@ function dropCollection() {
});
}


function deleteOldReports() {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection('TestReport').deleteMany({ reportTime: { $lt: 1622505600000 } });
Expand All @@ -229,22 +220,30 @@ function deleteOldReports() {
}

function fixOldReports() {
MongoClient.connect(uri, { useNewUrlParser: true }, (err, db) => {
MongoClient.connect(uri, (err, db) => {
if (err) throw err;
const dbo = db.db(dbName);
dbo.collection('TestReport')
// use updateMany for all reports
.updateOne({}, {
$rename: {
'testStatus': 'overallTestStatus',
'jsonReport': 'json'
testStatus: 'overallTestStatus',
jsonReport: 'json'
}
});
console.log('Updated Something');
db.close();
});
}

async function installDatabase() {
console.log(`Setting Up DB in: ${uri}`);
await makeCollection('stepTypes');
await makeCollection('Stories');
await makeCollection('User');
await insertMore('stepTypes', stepTypes());
}

module.exports = {
installDatabase
};
Loading
Loading