diff options
author | Lars Krönner | 2020-10-01 16:49:45 +0200 |
---|---|---|
committer | GitHub | 2020-10-01 16:49:45 +0200 |
commit | 590723afb4178e070a372d6e6054fe4d8549a4b0 (patch) | |
tree | 85485162adf07781b8eb0c19b37a9f34ec4e7320 /db.js | |
parent | ce6a00fa25bd12774fa86ee384e840565ecf965b (diff) | |
parent | c0469923b81368d087746a0789fec416ecdec43c (diff) |
Merge pull request #1 from marvinborner/users
Users
Diffstat (limited to 'db.js')
-rw-r--r-- | db.js | 60 |
1 files changed, 58 insertions, 2 deletions
@@ -1,4 +1,6 @@ const mariadb = require("mariadb"); +const bcrypt = require("bcrypt"); +const nanoid = require("nanoid"); const fs = require("fs"); class DB { @@ -10,6 +12,9 @@ class DB { database: process.env.DBName, }); this.init(); + this.query("SELECT * FROM users").then((res) => { + if (res.length === 0) this.initValues(); + }); } connect() { @@ -21,10 +26,61 @@ class DB { if (err) throw err; const queries = data.split(";"); queries.pop(); - const conn = await this.connect(); - for (const query of queries) await conn.query(query); + for (const query of queries) await this.query(query); console.log("Tables created!"); }); + + fs.readFile(__dirname + "/names.csv", "utf8", (err, data) => { + if (err) throw err; + const classes = data.split("--"); + classes.forEach((clazz, classIndex) => { + const students = clazz.split("\n"); + students.forEach(async (student) => { + // Fix undefined + if (student && student.length > 3) { + const [_, surname, name] = student.split(","); + const names = name.split(" "); + const middlename = names.length > 1 && names[1] ? names.slice(1).join(" ") : null; + let username = surname.toLowerCase().slice(0, 6); + if (middlename) username += middlename[0].toLowerCase(); + username += names[0].toLowerCase().slice(0, 2); + const pwd = nanoid.nanoid(8); + const password = await bcrypt.hash(pwd, 12); + await this.query( + "INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)", + [username, names[0], middlename, surname, password, classIndex + 1, 2] + ); + } + }); + }); + }); + } + + initValues() { + fs.readFile(__dirname + "/names.csv", "utf8", (err, data) => { + if (err) throw err; + const classes = data.split("--"); + classes.forEach((clazz, classIndex) => { + const students = clazz.split("\n"); + students.forEach(async (student) => { + // Fix undefined + if (student && student.length > 3) { + const [_, surname, name] = student.split(","); + const names = name.split(" "); + const middlename = names.length > 1 && names[1] ? names.slice(1).join(" ") : null; + let username = surname.toLowerCase().slice(0, 6); + if (middlename) username += middlename[0].toLowerCase(); + username += names[0].toLowerCase().slice(0, 2); + const pwd = nanoid.nanoid(8); + const password = await bcrypt.hash(pwd, 12); + await this.query( + "INSERT INTO users (username, name, middlename, surname, password, class_id, type_id) VALUE (?,?,?,?,?,?,?)", + [username, names[0], middlename, surname, password, classIndex + 1, 2] + ); + } + }); + }); + }); } async query(query, params) { |