diff options
author | LarsVomMars | 2020-10-01 18:11:21 +0200 |
---|---|---|
committer | LarsVomMars | 2020-10-01 18:11:36 +0200 |
commit | 1d898a3add62d201c3ce324f1feecd411d06d324 (patch) | |
tree | e88b07a9c2859eb3b0d2e29acea6a5645c8bdfb3 | |
parent | 589c21ef7c1fef178eb7e8047d71f91df61fbe93 (diff) |
Not working
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | db.js | 6 |
2 files changed, 7 insertions, 0 deletions
@@ -3,3 +3,4 @@ node_* *.env *.csv +users.json @@ -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); }); } |