-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.js
More file actions
111 lines (87 loc) · 2.92 KB
/
Copy pathdatabase.js
File metadata and controls
111 lines (87 loc) · 2.92 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
let mysql = require('mysql')
let connection = mysql.createConnection({
host : 'localhost',
user : 'nodejs',
password : '123456789first',
database : 'gene_bank'
});
const createTable = (table_name, obj) => {
// Fields of the table.
let fields = ""
// Column name of the table.
let keys = Object.keys(obj)
keys.map(key => {
fields += key.trim() + " " + obj[key].trim() + ","
})
fields = fields.slice(0, -1)
const createStatement = `CREATE TABLE IF NOT EXISTS ${table_name} (
${fields})`
connection.query(createStatement, [],(error, results, fields) => {
if (error) return console.log(error)
}
)
}
const insertData = (table_name, data) => {
// Values of the table.
let values = ""
let columnNames = ""
// Column name of the table.
let keys = Object.keys(data)
// Gets values of the table from the object.
keys.map(key => {
columnNames += "'" + key.trim() + "',"
values += "'" + data[key].trim() + "',"
})
values = values.slice(0, -1)
columnNames = columnNames.slice(0, -1)
let insertStatement = "INSERT INTO `" + table_name + "` " + `(${columnNames}) VALUES (${values})`
connection.query(insertStatement, [],(error, results, fields) => {
if (error) return console.log(error)
})
}
const insertDataBulk = (table_name, datas) => {
let values = ""
let bulkValue = ""
let insertStatement = "";
const firstData = datas[0];
let columnNames = ""
let keys = Object.keys(firstData)
// Gets values of the table from the object.
keys.map(key => {
columnNames += key.trim() + ","
})
columnNames = columnNames.slice(0, -1)
insertStatement = "INSERT INTO " + table_name + "(" + columnNames + ") VALUES"
datas.map((data, index) => {
let _keys = Object.keys(data)
_keys.map(key => {
values += "'" + data[key].trim() + "',"
})
values = values.slice(0, -1)
bulkValue += " (" + values + "), "
values = "";
if (index % 1000 === 0) {
let query = insertStatement + bulkValue
query = query.slice(0, -2)
connection.query(query, [],(error, results, fields) => {
if (error) return console.log(error)
})
// console.log(query)
bulkValue = ""
}
})
//console.log(insertStatement);
}
const dropTable = (table_name) => {
const dropQuery = "DROP TABLE IF EXISTS " + table_name + ";"
connection.query(dropQuery, [],(error, results, fields) => {
if (error) return console.log(error)
})
}
const deleteTable = (table_name) => {
const deleteQuery = "DELETE FROM " + table_name + ";"
connection.query(deleteQuery, [],(error, results, fields) => {
if (error) return console.log(error)
})
}
module.exports = {createTable, insertData, insertDataBulk, dropTable, deleteTable}