I have to keep track of three collection names for three MongoDB collection. One to insert a json data from an API. The second to consume the data in my own API (this is necessary because I have to avoid the calls restrictions to the API provider). And the third to drop the "old" collection. And this will be repeating every 5 seconds endless. If the following code stay together in one file is no problem, but I have to separate them in 3 different files/modules. My question is what approach is the right one to make it work in NodeJS and to keep the names consistently? At this point, when I run the code the getLetter() it just start over and return the same object every iteration. Here the most important part of code as pseudo code. If you have some experience please help. Thank you!
getLetter.js utils file
// return each time the next object with three different letters as value:
const XYZ = { I: 'X', II: 'Y', III: 'Z' };
const YZX = { I: 'Y', II: 'Z', III: 'X' };
const ZXY = { I: 'Z', II: 'X', III: 'Y' };
let nr = 1;
function getLetter() {
let obj;
if (nr == 1) {
obj = XYZ;
nr++;
return obj;
} else if (nr == 2) {
obj = YZX;
nr++;
return obj;
} else if (nr == 3) {
obj = ZXY;
nr = 1;
return obj;
}
}
server.js file
function intervalFunction() {
insertCollection();
consumeCollection();
dropCollection();
}
if (isInserted && isDropped) {
setInterval(intervalFunction, 5000);
}
insertCollection.js Pseudo code to insert the "new" collection in MongoDB
let newCollectionName = `collection_${getLetter().III}`;
if (isNewCollectionNameInserted) isInserted = true;
consumeCollection.js Pseudo code to deliver the "actual" collection (after aggregation) in an API endpoint
let actualCollection = `collection_${getLetter().II}`;
dropCollection.js Pseudo code to drop the "old" collection from MongoDB
let oldCollection = `collection_${getLetter().I}`;
if (isOldCollectionDropped) isDropped = true;
from How to share a specific utils function between different files in nodeJS to keep track of 3 MongoDB collection names
No comments:
Post a Comment