-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy.db.js
93 lines (78 loc) · 2.57 KB
/
lazy.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
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
'use strict'
const mysql = require('mysql2');
const {LazyTypes} = require('./lazy.utils')
const LAZY_DB_DEFAULTS = {
host: "127.0.0.1",
database: "db",
user: "user",
password: "password",
port: 3306
}
//TODO: Improve this, it works but it sould be improved
class LazyDB{
constructor(){
if(!instance){
this.pool = null;
this._isInit = false
this.engine = 'InnoDB'
this.charset = 'utf8'
this.collation = 'utf8_unicode_ci'
instance = this;
}
return instance;
}
async init(config){
if(!this._isInit){
// if(!LazyTypes.isString(config.host)){
// throw new Error(`db's "host" should be a string. got ${LazyTypes.getFullType(config.host)}.`);
// }else if(!LazyTypes.isString(config.host)){
// }else if(!LazyTypes.isString(config.host)){
// }
await this.setupTestEnvironment(config);
this.db = process.env.NODE_ENV == "test" ? "test" : config.db
this.pool = mysql.createPool({
host: config.host,
database: this.db,
user: config.user,
password: config.password,
port: config.port || LAZY_DB_DEFAULTS.port,
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
})
}else{
throw "DB already initialised."
}
}
async setupTestEnvironment(config){
if(process.env.NODE_ENV == "test"){
var con = mysql.createConnection({
host: config.host,
user: config.user,
password: config.password,
});
await con.promise().execute("DROP DATABASE IF EXISTS test;");
await con.promise().execute("CREATE DATABASE IF NOT EXISTS test");
}
}
async cleanupTestEnvironment(){
await this.execute("DROP DATABASE IF EXISTS test;");
}
async execute(query, args){
let [rows] = await this.pool.promise().execute(query, args);
return rows;
}
async query(query, args){
let [rows] = await this.pool.promise().query(query, args);
return rows;
}
//TODO: remove this
// static _sanitize_args(args){
// const sanitize_regex = /?:(?![A-z0-9])./
// return args.map((arg)=>{
// arg.replace(sanitize_regex,'');
// })
// }
}
var instance = new LazyDB();
module.exports = instance;