-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb.js
198 lines (174 loc) · 7.45 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// database adapter (mysql)
// (c)copyright 2015 by Gerald Wodni <[email protected]>
"use strict";
var mysql = require("mysql");
var _ = require("underscore");
module.exports = function _db( k ) {
var pools = {};
function add( website, config ) {
console.log( "MySql-Pool for ".bold.green, website );
/* apply custom format */
var debugLog = config.debugLog || false;
config.queryFormat = function _queryFormatter( query, values, timeZone ) {
return queryFormatter.call( this, query, values, timeZone, debugLog );
}
if( process.env.MYSQL_HOST ) {
config.host = process.env.MYSQL_HOST;
console.log( "Mysql-host-env:".bold.magenta, config.host );
}
if( process.env.MYSQL_PORT ) {
config.port = process.env.MYSQL_PORT;
console.log( "Mysql-port-env:".bold.magenta, config.port );
}
if( process.env.MYSQL_DATABASE ) {
config.database = process.env.MYSQL_DATABASE;
console.log( "Mysql-host-env:".bold.magenta, config.database );
}
if( process.env.MYSQL_USER ) {
config.user = process.env.MYSQL_USER;
console.log( "Mysql-user-env:".bold.magenta, config.user );
}
if( process.env.MYSQL_PASSWORD ) {
config.password = process.env.MYSQL_PASSWORD;
console.log( "Mysql-password-env:".bold.magenta, config.password.replace(/./g, '*') );
}
const pool = mysql.createPool( config );
pool.pQuery = function() {
const args = Array.from( arguments );
return new Promise( (fulfill, reject) => {
args.push( ( err, data ) => {
if( err ) return reject( err );
fulfill( data );
});
pool.query.apply( pool, args );
});
}
pool.pTransaction = function() {
return new Promise( ( fulfill, reject ) => {
pool.getConnection( function( err, connection ) {
if( err ) {
if( connection && connection.release )
connection.release();
return reject( err );
}
var aborted = false;
function abortTransaction() {
if( aborted ) /* avoid double-abort */
return Promise.resolve();
aborted = true;
return new Promise( ( fulfillAbort, rejectAbort ) => {
connection.rollback( () => {
connection.release();
fulfillAbort();
});
});
}
function commitTransaction() {
return new Promise( (fulfill, reject) => {
connection.commit( err => {
if( err ) return abortTransaction().then( () => reject( err ) );
connection.release();
fulfill();
});
});
}
connection.beginTransaction( err => {
if( err ) {
connection.release();
return reject( err );
}
fulfill({
abort: abortTransaction, /* expose to abort for non sql releated errors */
commit: commitTransaction,
connection,
pQuery: function() { /* modified pQuery which automatically rolls back */
const args = Array.from( arguments );
return new Promise( (fulfill, reject) => {
args.push( ( err, data ) => {
if( err ) return abortTransaction().then( reject( err ) );
fulfill( data );
});
connection.query.apply( connection, args );
});
},
});
})
});
});
}
pools[ website ] = pool;
}
function get( website, nullOnError = false ) {
if( !(website in pools ) )
if( nullOnError )
return null;
else
throw new Error( "No MySql connection for website '" + website + "'" );
return pools[ website ];
}
/* custom query formatting */
function queryFormatter( query, values, timeZone, debugLog ) {
if( values == null ) /* nothing to replace */
return query;
var indexedValues = true; /* find indexedValues if any */
var obj = null; /* initially empty lookup object */
if( !_.isArray( values ) ) {
obj = values;
if( _.has( values, "values" ) )
values = obj.values;
else
indexedValues = false;
}
if( debugLog )
console.log( "Q:".bold.red, query, values );
var regex = /{#?([$_.a-zA-Z0-9]+)}|\?\??/g;
var chunkIndex = 0;
var valueIndex = 0;
var result = '';
var match;
while( match = regex.exec( query ) ) {
var value = '#UNKNOWN_VALUE#';
/* indexed attribute(s) */
if( match[0][0] == '?' ) {
if( !indexedValues ) {
/* pass single object directly */
if( valueIndex++ == 0 )
value = this.escape( values, this.config.stringifyObjects, timeZone );
else
throw "queryFormatter: Indexed values must be served either a values array or the base object must contain a key named values";
}
else if( valueIndex >= values.length )
throw "queryFormatter: Indexed values out of bounds!";
/* indexed key(s) */
else if( match[0] == '??' )
value = mysql.escapeId( values[ valueIndex++ ] );
/* indexed value(s) */
else if( match[0] == '?' )
value = this.escape( values[ valueIndex++ ], this.config.stringifyObjects, timeZone );
}
else {
value = k.rdb.getField( obj, match[1] );
/* object as key */
if( match[0][1] == '#' )
value = mysql.escapeId( value );
/* object as value */
else
value = this.escape( value, this.config.stringifyObjects, timeZone );
}
/* add value to result */
result += query.slice( chunkIndex, match.index ) + value;
chunkIndex = regex.lastIndex;
}
if( chunkIndex == 0 ) /* nothing has been replaced */
return query;
if( chunkIndex < query.length ) /* remaining chunk */
result += query.slice( chunkIndex );
if( debugLog )
console.log( "A:".bold.green, result );
return result;
}
return {
add: add,
get: get
}
}