Skip to content

Commit

Permalink
Added functionality to update files
Browse files Browse the repository at this point in the history
  • Loading branch information
underdorff18 committed Nov 20, 2022
1 parent d90af6b commit e61f36a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Binary file modified JS/1000.xlsx
Binary file not shown.
11 changes: 11 additions & 0 deletions JS/dbtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ const dbtools = {
const result = await this.collection.insertOne(system);
console.log(`Created a system entry with id: ${result.insertedId}`);
},
updateSystem: async function (system) {
console.log(system);
const result = await this.collection.replaceOne({ serialnum: system.serialnum }, {
serialnum: system.serialnum,
model: system.model,
OS: system.OS,
price: system.price,
specs: system.specs
});
console.log(`${result.deletedCount} document(s) was/were updated.`);
},
deleteSystem: async function (serial) {
const result = await this.collection.deleteOne({ serialnum: serial});
console.log(`${result.deletedCount} document(s) was/were deleted.`);
Expand Down
10 changes: 7 additions & 3 deletions JS/excelreader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const Path = require('path');
const { MongoClient, ServerApiVersion } = require('mongodb');
dbtools = require('./dbtools');
const dbtools = require('./dbtools');
const xlsx = require('xlsx');
const chokidar = require('chokidar');
const System = require('./system');
Expand All @@ -25,15 +25,19 @@ function readInventoryFile(filepath) {
}


var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), {ignored: /^\./, persistent: true});
var inventoryWatcher = chokidar.watch(Path.join(__dirname, "Inventory"), {
ignored: /(^|[\/\\])\../,

This comment has been minimized.

Copy link
@underdorff18

underdorff18 Nov 20, 2022

Author Owner

Ignored filter was not working properly. Updated regex to actually filter dot files. This was required to get the update feature working.

persistent: true,
awaitWriteFinish: true,

This comment has been minimized.

Copy link
@underdorff18

underdorff18 Nov 20, 2022

Author Owner

Added awaitWriteFinish to make sure the file is saved before it is read. This was causing issues with updating files.

});
console.log(`Watching directory for changes...` )

inventoryWatcher
.on('add', function(filepath) {
dbtools.insertSystem(readInventoryFile(filepath));
})
.on('change', function(filepath) {
console.log('File', path, 'has been changed');
dbtools.updateSystem(readInventoryFile(filepath));
})
.on('unlink', function(filepath) {
dbtools.deleteSystem(parseInt(Path.basename(filepath, '.xlsx')));
Expand Down

0 comments on commit e61f36a

Please sign in to comment.