aboutsummaryrefslogtreecommitdiff
path: root/db.js
diff options
context:
space:
mode:
authorLarsVomMars2020-10-01 18:11:21 +0200
committerLarsVomMars2020-10-01 18:11:36 +0200
commit1d898a3add62d201c3ce324f1feecd411d06d324 (patch)
treee88b07a9c2859eb3b0d2e29acea6a5645c8bdfb3 /db.js
parent589c21ef7c1fef178eb7e8047d71f91df61fbe93 (diff)
Not working
Diffstat (limited to 'db.js')
-rw-r--r--db.js6
1 files changed, 6 insertions, 0 deletions
diff --git a/db.js b/db.js
index f2cf13d..dbef8f4 100644
--- a/db.js
+++ b/db.js
@@ -28,6 +28,9 @@ class DB {
queries.pop();
for (const query of queries) await this.query(query);
console.log("Tables created!");
+
+ const res = await this.query("SELECT * FROM users");
+ if (res.length === 0) this.initValues();
});
}
@@ -35,6 +38,7 @@ class DB {
fs.readFile(__dirname + "/names.csv", "utf8", (err, data) => {
if (err) throw err;
const classes = data.split("--");
+ const userPasswords = [];
classes.forEach((clazz, classIndex) => {
const students = clazz.split("\n");
students.forEach(async (student) => {
@@ -48,6 +52,7 @@ class DB {
username += names[0].toLowerCase().slice(0, 2);
const pwd = nanoid.nanoid(8);
const password = await bcrypt.hash(pwd, 12);
+ userPasswords.push({ username, pwd });
await this.query(
"INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)",
[username, names[0].replace("\r", ""), middlename, surname, password, classIndex + 1, 2]
@@ -55,6 +60,7 @@ class DB {
}
});
});
+ fs.writeFile(__dirname + "/users.json", JSON.stringify(userPasswords), console.error);
});
}