-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (166 loc) · 3.71 KB
/
index.js
File metadata and controls
180 lines (166 loc) · 3.71 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Builder
*/
'use strict'
/**
* Import dependencies.
*/
const mysql = require('mysql')
const Query = require('./lib/query')
/**
* Builder class.
* @class
*/
class Builder {
/**
* Builder constructor.
* @constructor
*/
constructor (options) {
this._setDefaults()
this._checkRequired(options)
this._setOptions(options)
}
/**
* Check if all required options is specified.
* @param {Object} options
* @private
*/
_checkRequired (options) {
if (!options.host) throw new Error(`Option 'host' is required`)
if (!options.port) throw new Error(`Option 'port' is required`)
if (!options.user) throw new Error(`Option 'user' is required`)
if (!options.password) throw new Error(`Option 'password' is required`)
if (!options.database) throw new Error(`Option 'database' is required`)
return true
}
/**
* Set option values to instance.
* @param {Object} options
* @private
*/
_setOptions (options) {
this._connectionData.host = options.host
this._connectionData.port = options.port
this._connectionData.user = options.user
this._connectionData.password = options.password
this._connectionData.database = options.database
}
/**
* Set default options.
* @private
*/
_setDefaults () {
this._table = null
this._connectionData = {}
}
/**
* Build and call select query.
* @param {String} table
* @returns {SelectBuilder}
* @public
*/
use (table) {
return new Query(this, table)
}
/**
* Call mysql query with created connection.
* @param {String} query
* @param {Array} data
* @returns {Promise}
* @public
* @async
*/
queryAsync (query, data) {
return new Promise((resolve, reject) => {
this.query(query, data, function (err, resultData) {
if (err) return reject(err)
resolve(resultData)
})
})
}
/**
* Call mysql query with created connection.
* @param {String} query
* @param {Array} data
* @param {Function} cb
*/
query (query, data, cb) {
this.connected((err, connection) => {
if (err) return cb(err)
connection.query(query, data, cb)
})
}
/**
* Call callback when connection is active.
* @param {Function} cb
* @public
*/
connected (cb) {
if (Builder._connection) {
cb(null, Builder._connection)
} else {
this._createConnection(cb)
}
}
/**
* Call callback when connection is active.
* @param {Function} cb
* @public
* @async
*/
connectedAsycn () {
return new Promise((resolve, reject) => {
this.connected((err, connection) => {
if (err) return reject(err)
resolve(connection)
})
})
}
/**
* Crete new database connection.
* @param {Function} cb // callback
* @private
*/
_createConnection (cb) {
const connection = mysql.createConnection({
host: this._connectionData.host,
port: this._connectionData.port,
user: this._connectionData.user,
password: this._connectionData.password,
database: this._connectionData.database
})
connection.connect((err) => {
Builder._connection = connection
cb(err, connection)
})
}
/**
* Close database connection.
* @public
*/
closeConnection (cb) {
if (!Builder._connection) {
cb(null)
}
Builder._connection.end((err) => {
Builder._connection = null
cb(err)
})
}
/**
* Close database connection.
* @public
* @async
*/
closeConnectionAsync () {
return new Promise((resolve, reject) => {
this.closeConnection(function (err) {
if (err) return reject(err)
resolve(true)
})
})
}
}
// export dependencies
module.exports = Builder