-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.js
64 lines (53 loc) · 1.54 KB
/
db.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
63
64
'use strict';
import mysql from 'mysql';
import { config } from './config/config.js';
let debug = new function(){
this.log = console.log;
};
let DB = new function(){
this.connection;
this.init = function(cb){
this.connection = mysql.createConnection(config);
this.connection.connect((err) => {
if (err) throw err;
console.log('Connected!');
if(typeof cb == 'function')
cb();
});
}
this.query = function(query,cb){
this.connection.query(query, function (error, results, fields) {
if (error) cb(error);
else cb(null, results)
console.log('Tables created: ', results[0]);
});
}
this.create_tables = function(cb){
var create_tables = 'CREATE TABLE `sites` (\n'+
' `id` int(11) NOT NULL,\n'+
' `site_url` varchar(1000) NOT NULL,\n'+
' `status` smallint(3) NOT NULL,\n'+
' `date_added` int(11) NOT NULL,\n'+
' `date_crawled` int(11) NOT NULL\n'+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n'+
'ALTER TABLE `sites`\n'+
' ADD PRIMARY KEY (`id`);\n'+
'ALTER TABLE `sites`\n'+
' MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;\n'+
'COMMIT;\n'+
'CREATE TABLE `links` (\n'+
' `in_id` int(11) NOT NULL,\n'+
' `out_id` int(11) NOT NULL\n'+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n'+
'ALTER TABLE `links`\n'+
' ADD PRIMARY KEY (`in_id`,`out_id`);\n'+
'COMMIT;';
this.connection.query(create_tables, function (error, results, fields) {
if (error) throw error;
debug.log('Tables created: ', results[0]);
cb();
});
this.connection.end();
}
}
export const db = DB;