Skip to content

Commit a41e7f9

Browse files
committed
Add function that reads from a database
1 parent 03fffbd commit a41e7f9

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// file containing function to read from database
2+
const { readFile } = require('fs');
3+
4+
module.exports = function readDatabase(filePath) {
5+
const students = {};
6+
return new Promise((resolve, reject) => {
7+
readFile(filePath, (err, data) => {
8+
if (err) {
9+
reject(err);
10+
} else {
11+
const lines = data.toString().split('\n');
12+
const noHeader = lines.slice(1);
13+
for (let i = 0; i < noHeader.length; i += 1) {
14+
if (noHeader[i]) {
15+
const field = noHeader[i].toString().split(',');
16+
if (Object.prototype.hasOwnProperty.call(students, field[3])) {
17+
students[field[3]].push(field[0]);
18+
} else {
19+
students[field[3]] = [field[0]];
20+
}
21+
}
22+
}
23+
resolve(students);
24+
}
25+
});
26+
});
27+
};

0 commit comments

Comments
 (0)