-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockdb.js
76 lines (72 loc) · 2.11 KB
/
mockdb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const fs = require("fs");
const { parse } = require("csv-parse");
const { stringify } = require("csv-stringify");
const userColumns = [
"user",
"password",
"email"
];
const benchmarkColumns = [
"user",
"timestamp",
"op",
"l",
"r",
"elapsed",
"attempts"
];
class MockDB {
constructor() {
let users = {}
this.users = users;
let benchmarks = {}
this.benchmarks = benchmarks;
fs.createReadStream("./users.csv")
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", function (row) {
users[row[0]] = {userid: row[0], password: row[1], email: row[2]};
})
fs.createReadStream("./benchmarks.csv")
.pipe(parse({ delimiter: ",", from_line: 2 }))
.on("data", function (row) {
if (row[0] in benchmarks) {
benchmarks[row[0]].push(row);
} else {
benchmarks[row[0]] = [row];
}
})
}
getUser(userid) {
if (userid in this.users) {
return this.users[userid];
} else {
return null;
}
}
createUser(userid, password, email) {
this.users[userid] = {userid: userid, password: password, email: email};
const writableStream = fs.createWriteStream("users.csv");
const stringifier = stringify({ header: true, columns: userColumns });
Object.entries(this.users).forEach(([key, value]) => {
stringifier.write([value.userid, value.password, value.email])
});
stringifier.pipe(writableStream);
return this.users[userid];
}
createBenchmark(userid, timestamp, op, l, r, elapsed, attempts) {
if (!(userid in this.benchmarks)) {
this.benchmarks[userid] = [];
}
this.benchmarks[userid].push([userid, timestamp, op, l, r, elapsed, attempts]);
const writableStream = fs.createWriteStream("benchmarks.csv");
const stringifier = stringify({ header: true, columns: benchmarkColumns });
Object.entries(this.benchmarks).forEach(([key, value]) => {
value.forEach((row) => {
stringifier.write(row);
});
});
stringifier.pipe(writableStream);
return [userid, timestamp, op, l, r, elapsed, attempts];
}
}
module.exports = MockDB;