Skip to content

Commit

Permalink
Fixed server connection
Browse files Browse the repository at this point in the history
  • Loading branch information
OlliSchu committed Sep 28, 2020
1 parent a63ac61 commit 618c067
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 48 deletions.
26 changes: 18 additions & 8 deletions api/2.1/Comments/Controller/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ const jwt = require("jsonwebtoken");

var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
return connection;

};

function checkCache(id) {
// check if Connection is already defined
if(cache==undefined){
// if not create a connection to the database and save the connection to the cache variable, so that we only have one connection per database + collection
cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
console.log("cached")
cache = setConnection(id);
return cache
} else {
console.log("Test")
return cache
// check if we are connected to the right server, if not change connection
if(cache.name == id){
return cache;
} else {
cache = setConnection(id);
return cache;
}
}
};

Expand Down
33 changes: 23 additions & 10 deletions api/2.1/Documents/Controller/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,38 @@ const { countDocuments } = require('../../User/Models/user');

var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
return connection;

};

function checkCache(id) {
// check if Connection is already defined
console.log
if(cache==undefined){
// if not create a connection to the database and save the connection to the cache variable, so that we only have one connection per database + collection
cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
console.log("cached")
cache = setConnection(id);
return cache
} else {

return cache
// check if we are connected to the right server, if not change connection
if(cache.name == id){
return cache;
} else {
cache = setConnection(id);
return cache;
}
}
};

exports.documents_get = (req, res, next) => {

const id = req.params.projectId;

//console.log(id)
const conn = checkCache(id)
//console.log(conn)

Documents = conn.model("Documents", require("../Models/documents"));
module.exports = conn;
Expand All @@ -36,6 +46,7 @@ exports.documents_get = (req, res, next) => {
.select("-file -_id -__v")
.exec()
.then(doc => {
//console.log(doc);
res.status(200).json(doc);
})
.catch(err => {
Expand Down Expand Up @@ -101,6 +112,7 @@ exports.documents_post = (req, res, next) => {
_id: new mongoose.Types.ObjectId(),
guid: uuid.v4(),
filename: req.header("Content-Disposition").split("=")[1].slice(1, -1),
description: req.header("Content-Description"),
file: data
});

Expand All @@ -111,6 +123,7 @@ exports.documents_post = (req, res, next) => {
res.status(201).json({
guid: result.guid,
filename: result.filename,
description: result.description
});
})
.catch(err => {
Expand Down
4 changes: 4 additions & 0 deletions api/2.1/Documents/Models/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const documentSchema = new Schema({
file: {
type: Buffer,
required: true
},
description: {
type: String,
required: false
}
});

Expand Down
27 changes: 18 additions & 9 deletions api/2.1/Projects/Controller/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@ const mongoose = require('mongoose');

var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
return connection;

};

function checkCache(id) {
// check if Connection is already defined
if(cache==undefined){
// if not create a connection to the database and save the connection to the cache variable, so that we only have one connection per database + collection
cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
console.log("cached")
cache = setConnection(id);
return cache
} else {

return cache
// check if we are connected to the right server, if not change connection
if(cache.name == id){
return cache;
} else {
cache = setConnection(id);
return cache;
}
}
};


exports.projects_get_all = (req, res, next) => {
Projects.find()
.select('project_id name')
Expand Down
36 changes: 23 additions & 13 deletions api/2.1/Topics/Controller/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ const jwt = require("jsonwebtoken");

var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
return connection;

};

function checkCache(id) {
// check if Connection is already defined
console.log
if(cache==undefined){
// if not create a connection to the database and save the connection to the cache variable, so that we only have one connection per database + collection
cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
console.log("cached")
cache = setConnection(id);
return cache
} else {

return cache
// check if we are connected to the right server, if not change connection
if(cache.name == id){
return cache;
} else {
cache = setConnection(id);
return cache;
}
}
};

Expand Down Expand Up @@ -182,14 +191,15 @@ exports.documentreferences_post = (req, res, next) => {

DocumentReference = conn.model("DocumentReferences", require("../Models/documentreference"));
module.exports = conn;

console.log("References")

const documentReference = new DocumentReference({
_id: new mongoose.Types.ObjectId(),
guid: uuid.v4(),
url: req.body.url,
document_guid: req.body.document_guid,
topic_guid: topicId,
description: req.body.description,
//description: req.body.description,
topic_guid: topicId
});

documentReference
Expand All @@ -199,7 +209,7 @@ exports.documentreferences_post = (req, res, next) => {
guid: result.guid,
url: result.url,
document_guid: result.document_guid,
description: result.description
//description: result.description
});
})
.catch(err => {
Expand Down
26 changes: 18 additions & 8 deletions api/2.1/Viewpoints/Controller/viewpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,29 @@ const fs = require("fs");

var cache = undefined;

function setConnection(id) {

connection = cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
return connection;

};

function checkCache(id) {
// check if Connection is already defined
if(cache==undefined){
// if not create a connection to the database and save the connection to the cache variable, so that we only have one connection per database + collection
cache = mongoose.createConnection(process.env.MONGO_ATLAS_URL + id + '?retryWrites=true&w=majority', {
useNewUrlParser: true,
useUnifiedTopology:true
});
console.log("cached")
cache = setConnection(id);
return cache
} else {

return cache
// check if we are connected to the right server, if not change connection
if(cache.name == id){
return cache;
} else {
cache = setConnection(id);
return cache;
}
}
};

Expand Down

0 comments on commit 618c067

Please sign in to comment.