Skip to content

Commit

Permalink
Added sync directory to database function to excelreader that execute…
Browse files Browse the repository at this point in the history
…s on startup, and added delete all systems function to dbtools.
  • Loading branch information
underdorff18 committed Jun 12, 2023
1 parent 5cc6a9a commit b2607ad
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
Binary file removed JS/1234.xlsx
Binary file not shown.
File renamed without changes.
Binary file added JS/Inventory/1001.xlsx
Binary file not shown.
Binary file added JS/Inventory/1234.xlsx
Binary file not shown.
4 changes: 4 additions & 0 deletions JS/dbtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const dbtools = {
const result = await this.collection.deleteOne({ serialnum: serial});
console.log(`${result.deletedCount} document(s) was/were deleted.`);
},
deleteAllSystems: async function () {
const result = await this.collection.deleteMany({});
console.log(`${result.deletedCount} document(s) was/were deleted.`);
},
getAllSystems: async function () {
cursor = await this.collection.find();
let allSystems = []
Expand Down
30 changes: 29 additions & 1 deletion JS/excelreader.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const { all } = require('express/lib/application');

//const myfilepath = Path.join(__dirname, 'Inventory', 'samplepc1234.xlsx');

function readInventoryFile(filepath) {
//given a full path to a file, this function will produce a system object or an object with an error key
//if formatted incorrectly
function readInventoryFile(filepath) {
//Checking for correct file type
const fileinfo = Path.parse(filepath);
if (fileinfo.ext !== ".xlsx") {
Expand Down Expand Up @@ -46,6 +48,30 @@ function readInventoryFile(filepath) {
return system;
}

//This function reads all files in the given directory, and updates the database to match
async function syncDirToDatabase(directory) {
dbtools.deleteAllSystems();
let files = fs.readdirSync(directory);
let systemsArr = [];
files.forEach((val, index) => {
let result = readInventoryFile(Path.join(directory, val))
if (result.error) {
console.log(`syncDirToDatabase: error with ${val}: ${result.error}`);
}
else {
console.log('here');
console.log(result);
console.log(val);
systemsArr.push(val);
dbtools.insertSystem(result);
}
})

console.log("Database synced. Current contents: ");
console.log(systemsArr);

}


var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), {
ignored: /(^|[\/\\])\../,
Expand All @@ -55,6 +81,8 @@ var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), {
});
console.log(`Watching directory for changes...` )

syncDirToDatabase(Path.join(__dirname, "Inventory"));

inventoryWatcher
.on('add', function(filepath) {
const system = readInventoryFile(filepath);
Expand Down

0 comments on commit b2607ad

Please sign in to comment.