Implemented MongoDB example db, updated documentation

This commit is contained in:
Alex Holliday committed 2024-05-13 14:21:27 -07:00
1 parent 8d6529d497
commit 8cc92e6c40
6 files changed
+154 -30

No files matched your search

+33 -5
View File
@@ -1,7 +1,23 @@
// **************************
// FakeDB is used to simulate the database
// If we standarize the DB methods we can swap out databases easily
// Too bad JS doesn't have interfaces, that would be nice. TypeScript anyone?
// The idea here is to provide a layer of abstraction between the database and whoever is using it.
// Instead of directly calling mongoose methods, we can call the methods on the DB object.
// If this were Typescript or Java or Golang an interface would be implemented to ensure the methods are available.
// But we do the best we can with Javascript.
//
// If the methods are consistent all we have to do to swap out one DB for another is simply change the import.
//
// Example:
// We start with the fake DB:
//
// const db = require("../db/FakeDb");
// const monitors = await db.getAllMonitors();
//
// And when we want to swtich to a real DB, all we have to do is swap the import
//
// const db = require("../db/MongoDb");
// const monitors = await db.getAllMonitors();
//
// The rest of the code is the same, as all the `db` methods are standardized.
// **************************
const Monitor = require("../models/Monitor");
@@ -23,6 +39,14 @@ for (let i = 0; i < 10; i++) {
);
}
const connect = async () => {
try {
await console.log("Connected to FakeDB");
} catch (error) {
console.error(error);
}
};
const getAllMonitors = async () => {
return FAKE_MONITOR_DATA;
};
@@ -49,5 +73,9 @@ const getMonitorsByUserId = async (userId) => {
return userMonitors;
};
// Conisder standardizing these methods so we can swap databases easily
module.exports = { getAllMonitors, getMonitorById, getMonitorsByUserId };
module.exports = {
connect,
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
};
+80
View File
@@ -0,0 +1,80 @@
const Monitor = require("../models/Monitor");
const mongoose = require("mongoose");
const seedDatabase = async () => {
const FAKE_MONITOR_DATA = [];
for (let i = 0; i < 10; i++) {
FAKE_MONITOR_DATA.push(
new Monitor({
userId: i % 2 === 0 ? 1 : 2,
name: `Monitor ${i}`,
description: `Description for Monitor ${i}`,
url: `https://monitor${i}.com`,
isActive: true,
interval: 60000,
updated_at: new Date(),
created_at: new Date(),
})
);
}
for (const monitor of FAKE_MONITOR_DATA) {
await monitor.save();
}
};
const connect = async () => {
try {
await mongoose.connect(process.env.DB_CONNECTION_STRING);
console.log("Connected to MongoDB");
const monitors = await Monitor.find();
if (monitors.length === 0) {
console.log("Seeding database with fake data");
await seedDatabase();
} else {
console.log("Database already seeded");
}
} catch (error) {
console.error("Failed to connect to MongoDB");
throw error;
}
};
// Gets all monitors
const getAllMonitors = async (req, res) => {
try {
const monitors = await Monitor.find();
return monitors;
} catch (error) {
throw error;
}
};
// Get a monitor by ID
const getMonitorById = async (req, res) => {
try {
const monitor = await Monitor.findById(req.params.monitorId);
return monitor;
} catch (error) {
throw error;
}
};
// Gets a monitor by user ID
const getMonitorsByUserId = async (req, res) => {
try {
const monitors = await Monitor.find({ userId: req.params.userId });
return monitors;
} catch (error) {
throw error;
}
};
module.exports = {
connect,
getAllMonitors,
getMonitorById,
getMonitorsByUserId,
};