forked from remarcmij/database_examples
-
Notifications
You must be signed in to change notification settings - Fork 5
/
4-db-await.js
62 lines (52 loc) · 1.83 KB
/
4-db-await.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
const util = require('util');
const fs = require('fs');
const mysql = require('mysql');
const CONNECTION_CONFIG = {
host: 'localhost',
user: 'hyfuser',
password: 'hyfpassword',
database: 'userdb',
};
const CREATE_STUDENTS_TABLE = `
CREATE TABLE IF NOT EXISTS students (
student_number INT,
student_name VARCHAR(50),
date_of_birth DATE,
grade FLOAT,
gender ENUM('m', 'f')
);`;
const CREATE_TEACHERS_TABLE = `
CREATE TABLE IF NOT EXISTS teachers (
teacher_number INT,
teacher_name VARCHAR(50),
date_of_birth DATE,
subject TEXT,
gender ENUM('m', 'f')
);`;
/*
Promisification : Conversion of a function that accepts a callback
into a function that returns a promise
*/
async function seedDatabase() {
const connection = mysql.createConnection(CONNECTION_CONFIG);
const readFile = util.promisify(fs.readFile);
// Invoking bind() on any function returns a copy of the function where ‘this’ is set to the first argument passed into bind.
// Here bind is invoked in query() function.
// Inside query function "this" is used as the connection object.
// Check the code https://github.com/mysqljs/mysql/blob/master/lib/Connection.js#L182
// apply() or call() is not used because these method not only assign "this" but also run the function immediately
const execQuery = util.promisify(connection.query.bind(connection));
try {
await execQuery(CREATE_STUDENTS_TABLE);
await execQuery(CREATE_TEACHERS_TABLE);
const data = await readFile(__dirname + '/students.json', 'utf8');
const students = JSON.parse(data);
const promises = students.map(student => execQuery('INSERT INTO students SET ?', student));
await Promise.all(promises);
connection.end();
} catch (err) {
console.error(err.message);
connection.end();
}
}
seedDatabase();