Skip to content

Commit bdb7c1b

Browse files
cleanup and lint connectable interface
1 parent 15c5574 commit bdb7c1b

File tree

4 files changed

+134
-105
lines changed

4 files changed

+134
-105
lines changed

machines/create-manager.js

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ module.exports = {
88

99

1010
extendedDescription:
11-
'The `manager` instance returned by this method contains any configuration that is necessary '+
12-
'for communicating with the database and establishing connections (e.g. host, user, password) '+
13-
'as well as any other relevant metadata. The manager will often also contain a reference '+
14-
'to some kind of native container (e.g. a connection pool).\n'+
15-
'\n'+
16-
'Note that a manager instance does not necessarily need to correspond with a pool though--'+
17-
'it might simply be a container for storing config, or it might refer to multiple pools '+
11+
'The `manager` instance returned by this method contains any configuration that is necessary ' +
12+
'for communicating with the database and establishing connections (e.g. host, user, password) ' +
13+
'as well as any other relevant metadata. The manager will often also contain a reference ' +
14+
'to some kind of native container (e.g. a connection pool).\n' +
15+
'\n' +
16+
'Note that a manager instance does not necessarily need to correspond with a pool though--' +
17+
'it might simply be a container for storing config, or it might refer to multiple pools ' +
1818
'(e.g. a PoolCluster from felixge\'s `mysql` package).',
1919

2020

@@ -34,14 +34,14 @@ module.exports = {
3434
onUnexpectedFailure: {
3535
description: 'A function to call any time an unexpected error event is received from this manager or any of its connections.',
3636
extendedDescription:
37-
'This can be used for anything you like, whether that\'s sending an email to devops, '+
38-
'or something as simple as logging a warning to the console.\n'+
39-
'\n'+
40-
'For example:\n'+
41-
'```\n'+
42-
'onUnexpectedFailure: function (err) {\n'+
43-
' console.warn(\'Unexpected failure in database manager:\',err);\n'+
44-
'}\n'+
37+
'This can be used for anything you like, whether that\'s sending an email to devops, ' +
38+
'or something as simple as logging a warning to the console.\n' +
39+
'\n' +
40+
'For example:\n' +
41+
'```\n' +
42+
'onUnexpectedFailure: function (err) {\n' +
43+
' console.warn(\'Unexpected failure in database manager:\',err);\n' +
44+
'}\n' +
4545
'```',
4646
example: '->'
4747
},
@@ -62,10 +62,10 @@ module.exports = {
6262
success: {
6363
description: 'The manager was successfully created.',
6464
extendedDescription:
65-
'The new manager should be passed in to `getConnection()`.'+
66-
'Note that _no matter what_, this manager must be capable of '+
67-
'spawning an infinite number of connections (i.e. via `getConnection()`). '+
68-
'The implementation of how exactly it does this varies on a driver-by-driver '+
65+
'The new manager should be passed in to `getConnection()`.' +
66+
'Note that _no matter what_, this manager must be capable of ' +
67+
'spawning an infinite number of connections (i.e. via `getConnection()`). ' +
68+
'The implementation of how exactly it does this varies on a driver-by-driver ' +
6969
'basis; and it may also vary based on the configuration passed into the `meta` input.',
7070
outputVariableName: 'report',
7171
outputDescription: 'The `manager` property is a manager instance that will be passed into `getConnection()`. The `meta` property is reserved for custom driver-specific extensions.',
@@ -88,19 +88,19 @@ module.exports = {
8888
failed: {
8989
description: 'Could not create a connection manager for this database using the specified connection string.',
9090
extendedDescription:
91-
'If this exit is called, it might mean any of the following:\n'+
92-
' + the credentials encoded in the connection string are incorrect\n'+
93-
' + there is no database server running at the provided host (i.e. even if it is just that the database process needs to be started)\n'+
94-
' + there is no software "database" with the specified name running on the server\n'+
95-
' + the provided connection string does not have necessary access rights for the specified software "database"\n'+
96-
' + this Node.js process could not connect to the database, perhaps because of firewall/proxy settings\n'+
97-
' + any other miscellaneous connection error\n'+
98-
'\n'+
99-
'Note that even if the database is unreachable, bad credentials are being used, etc, '+
100-
'this exit will not necessarily be called-- that depends on the implementation of the driver '+
101-
'and any special configuration passed to the `meta` input. e.g. if a pool is being used that spins up '+
102-
'multiple connections immediately when the manager is created, then this exit will be called if any of '+
103-
'those initial attempts fail. On the other hand, if the manager is designed to produce adhoc connections, '+
91+
'If this exit is called, it might mean any of the following:\n' +
92+
' + the credentials encoded in the connection string are incorrect\n' +
93+
' + there is no database server running at the provided host (i.e. even if it is just that the database process needs to be started)\n' +
94+
' + there is no software "database" with the specified name running on the server\n' +
95+
' + the provided connection string does not have necessary access rights for the specified software "database"\n' +
96+
' + this Node.js process could not connect to the database, perhaps because of firewall/proxy settings\n' +
97+
' + any other miscellaneous connection error\n' +
98+
'\n' +
99+
'Note that even if the database is unreachable, bad credentials are being used, etc, ' +
100+
'this exit will not necessarily be called-- that depends on the implementation of the driver ' +
101+
'and any special configuration passed to the `meta` input. e.g. if a pool is being used that spins up ' +
102+
'multiple connections immediately when the manager is created, then this exit will be called if any of ' +
103+
'those initial attempts fail. On the other hand, if the manager is designed to produce adhoc connections, ' +
104104
'any errors related to bad credentials, connectivity, etc. will not be caught until `getConnection()` is called.',
105105
outputVariableName: 'report',
106106
outputDescription: 'The `error` property is a JavaScript Error instance with more information and a stack trace. The `meta` property is reserved for custom driver-specific extensions.',
@@ -113,9 +113,10 @@ module.exports = {
113113
},
114114

115115

116-
fn: function (inputs, exits){
116+
fn: function createManager(inputs, exits) {
117117
var util = require('util');
118118
var Url = require('url');
119+
var _ = require('lodash');
119120
var felix = require('mysql');
120121

121122

@@ -133,7 +134,6 @@ module.exports = {
133134
// contributions to the core driver in this area are welcome and greatly appreciated!
134135

135136

136-
137137
// Build a local variable (`_mysqlClientConfig`) to house a dictionary
138138
// of additional MySQL options that will be passed into `.createPool()`
139139
// (Note that these could also be used with `.connect()` or `.createPoolCluster()`)
@@ -148,11 +148,9 @@ module.exports = {
148148
var _mysqlClientConfig = {};
149149

150150

151-
152-
153151
// Validate and parse `meta` (if specified).
154-
if ( !util.isUndefined(inputs.meta) ) {
155-
if ( !util.isObject(inputs.meta) ) {
152+
if (!_.isUndefined(inputs.meta)) {
153+
if (!_.isObject(inputs.meta)) {
156154
return exits.error('If provided, `meta` must be a dictionary.');
157155
}
158156

@@ -175,8 +173,8 @@ module.exports = {
175173
// Pool-specific:
176174
'acquireTimeout', 'waitForConnections', 'connectionLimit', 'queueLimit',
177175

178-
].forEach(function (mysqlClientConfKeyName){
179-
if ( !util.isUndefined(inputs.meta[mysqlClientConfKeyName]) ) {
176+
].forEach(function processKey(mysqlClientConfKeyName) {
177+
if (!_.isUndefined(inputs.meta[mysqlClientConfKeyName])) {
180178
_mysqlClientConfig[mysqlClientConfKeyName] = inputs.meta[mysqlClientConfKeyName];
181179
}
182180
});
@@ -207,20 +205,27 @@ module.exports = {
207205

208206
// Validate that a protocol was found before other pieces
209207
// (otherwise other parsed info will be very weird and wrong)
210-
if ( !parsedConnectionStr.protocol ) {
208+
if (!parsedConnectionStr.protocol || parsedConnectionStr.protocol !== 'mysql:') {
211209
throw new Error('Protocol (i.e. `mysql://`) is required in connection string.');
212210
}
213211

214212
// Parse port & host
215213
var DEFAULT_HOST = 'localhost';
216214
var DEFAULT_PORT = 3306;
217-
if (parsedConnectionStr.port) { _mysqlClientConfig.port = +parsedConnectionStr.port; }
218-
else { _mysqlClientConfig.port = DEFAULT_PORT; }
219-
if (parsedConnectionStr.hostname) { _mysqlClientConfig.host = parsedConnectionStr.hostname; }
220-
else { _mysqlClientConfig.host = DEFAULT_HOST; }
215+
if (parsedConnectionStr.port) {
216+
_mysqlClientConfig.port = +parsedConnectionStr.port;
217+
} else {
218+
_mysqlClientConfig.port = DEFAULT_PORT;
219+
}
220+
221+
if (parsedConnectionStr.hostname) {
222+
_mysqlClientConfig.host = parsedConnectionStr.hostname;
223+
} else {
224+
_mysqlClientConfig.host = DEFAULT_HOST;
225+
}
221226

222227
// Parse user & password
223-
if ( parsedConnectionStr.auth && util.isString(parsedConnectionStr.auth) ) {
228+
if (parsedConnectionStr.auth && _.isString(parsedConnectionStr.auth)) {
224229
var authPieces = parsedConnectionStr.auth.split(/:/);
225230
if (authPieces[0]) {
226231
_mysqlClientConfig.user = authPieces[0];
@@ -231,21 +236,21 @@ module.exports = {
231236
}
232237

233238
// Parse database name
234-
if (util.isString(parsedConnectionStr.pathname) ) {
239+
if (_.isString(parsedConnectionStr.pathname)) {
235240
var _databaseName = parsedConnectionStr.pathname;
236241
// Trim leading and trailing slashes
237242
_databaseName = _databaseName.replace(/^\/+/, '');
238243
_databaseName = _databaseName.replace(/\/+$/, '');
239244
// If anything is left, use it as the database name.
240-
if ( _databaseName ) {
245+
if (_databaseName) {
241246
_mysqlClientConfig.database = _databaseName;
242247
}
243248
}
244-
}
245-
catch (_e) {
246-
_e.message = util.format('Provided value (`%s`) is not a valid MySQL connection string.',inputs.connectionString) + ' Error details: '+ _e.message;
249+
} catch (_e) {
250+
_e.message = util.format('Provided value (`%s`) is not a valid MySQL connection string.', inputs.connectionString) + ' Error details: ' + _e.message;
247251
return exits.malformed({
248-
error: _e
252+
error: _e,
253+
meta: inputs.meta
249254
});
250255
}
251256

@@ -261,24 +266,23 @@ module.exports = {
261266
//
262267
// For more background, see:
263268
// • https://github.com/felixge/node-mysql/blob/v2.10.2/Readme.md#error-handling
264-
pool.on('error', function (err){
269+
pool.on('error', function err(err) {
265270
// When/if something goes wrong in this pool, call the `onUnexpectedFailure` notifier
266271
// (if one was provided)
267-
if (!util.isUndefined(onUnexpectedFailure)) {
268-
onUnexpectedFailure(err || new Error('One or more pooled connections to MySQL database were lost. Did the database server go offline?'));
272+
if (!_.isUndefined(inputs.onUnexpectedFailure)) {
273+
inputs.onUnexpectedFailure(err || new Error('One or more pooled connections to MySQL database were lost. Did the database server go offline?'));
269274
}
270275
});
271276

272277
// Finally, build and return the manager.
273278
var mgr = {
274279
pool: pool,
275-
meta: inputs.meta,
276280
connectionString: inputs.connectionString
277281
};
278282
return exits.success({
279-
manager: mgr
283+
manager: mgr,
284+
meta: inputs.meta,
280285
});
281-
282286
}
283287

284288

machines/destroy-manager.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ module.exports = {
1717
required: true
1818
},
1919

20-
meta:
21-
require('../constants/meta.input')
20+
meta: {
21+
friendlyName: 'Meta (custom)',
22+
description: 'Additional stuff to pass to the driver.',
23+
extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
24+
example: '==='
25+
}
2226

2327
},
2428

@@ -38,8 +42,8 @@ module.exports = {
3842
friendlyName: 'Failed',
3943
description: 'Could not destroy the provided connection manager.',
4044
extendedDescription:
41-
'Usually, this means the manager has already been destroyed. But depending on the driver '+
42-
'it could also mean that database cannot be accessed. In production, this can mean that the database '+
45+
'Usually, this means the manager has already been destroyed. But depending on the driver ' +
46+
'it could also mean that database cannot be accessed. In production, this can mean that the database ' +
4347
'server(s) became overwhelemed or were shut off while some business logic was in progress.',
4448
outputVariableName: 'report',
4549
outputDescription: 'The `error` property is a JavaScript Error instance with more information and a stack trace. The `meta` property is reserved for custom driver-specific extensions.',
@@ -52,9 +56,7 @@ module.exports = {
5256
},
5357

5458

55-
fn: function (inputs, exits){
56-
var util = require('util');
57-
59+
fn: function destroyManager(inputs, exits) {
5860
// Note that if this driver is adapted to support managers which spawn
5961
// ad-hoc connections or manage multiple pools/replicas using PoolCluster,
6062
// then relevant settings would need to be included in the manager instance
@@ -66,14 +68,17 @@ module.exports = {
6668
//
6769
// For more info, see:
6870
// • https://github.com/felixge/node-mysql/blob/v2.10.2/Readme.md#closing-all-the-connections-in-a-pool
69-
inputs.manager.pool.end(function (err) {
71+
inputs.manager.pool.end(function end(err) {
7072
if (err) {
7173
return exits.failed({
72-
error: new Error('Failed to destroy the MySQL connection pool and/or gracefully end all connections in the pool. Details:\n=== === ===\n'+err.stack)
74+
error: new Error('Failed to destroy the MySQL connection pool and/or gracefully end all connections in the pool. Details:\n=== === ===\n' + err.stack),
75+
meta: inputs.meta
7376
});
7477
}
7578
// All connections in the pool have ended.
76-
return exits.success();
79+
return exits.success({
80+
meta: inputs.meta
81+
});
7782
});
7883
}
7984

machines/get-connection.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ module.exports = {
1313
friendlyName: 'Manager',
1414
description: 'The connection manager instance to acquire the connection from.',
1515
extendedDescription:
16-
'Only managers built using the `createManager()` method of this driver are supported. '+
17-
'Also, the database connection manager instance provided must not have been destroyed--'+
18-
'i.e. once `destroyManager()` is called on a manager, no more connections can be acquired '+
19-
'from it (also note that all existing connections become inactive-- see `destroyManager()` '+
16+
'Only managers built using the `createManager()` method of this driver are supported. ' +
17+
'Also, the database connection manager instance provided must not have been destroyed--' +
18+
'i.e. once `destroyManager()` is called on a manager, no more connections can be acquired ' +
19+
'from it (also note that all existing connections become inactive-- see `destroyManager()` ' +
2020
'for more on that).',
2121
example: '===',
2222
required: true
2323
},
2424

25-
meta:
26-
require('../constants/meta.input')
25+
meta: {
26+
friendlyName: 'Meta (custom)',
27+
description: 'Additional stuff to pass to the driver.',
28+
extendedDescription: 'This is reserved for custom driver-specific extensions. Please refer to the documentation for the driver you are using for more specific information.',
29+
example: '==='
30+
}
2731

2832
},
2933

@@ -43,12 +47,12 @@ module.exports = {
4347

4448
failed: {
4549
description: 'Could not acquire a connection to the database using the specified manager.',
46-
extendedDescription: 'This might mean any of the following:\n'+
47-
' + the credentials encoded in the connection string are incorrect\n'+
48-
' + there is no database server running at the provided host (i.e. even if it is just that the database process needs to be started)\n'+
49-
' + there is no software "database" with the specified name running on the server\n'+
50-
' + the provided connection string does not have necessary access rights for the specified software "database"\n'+
51-
' + this Node.js process could not connect to the database, perhaps because of firewall/proxy settings\n'+
50+
extendedDescription: 'This might mean any of the following:\n' +
51+
' + the credentials encoded in the connection string are incorrect\n' +
52+
' + there is no database server running at the provided host (i.e. even if it is just that the database process needs to be started)\n' +
53+
' + there is no software "database" with the specified name running on the server\n' +
54+
' + the provided connection string does not have necessary access rights for the specified software "database"\n' +
55+
' + this Node.js process could not connect to the database, perhaps because of firewall/proxy settings\n' +
5256
' + any other miscellaneous connection error',
5357
outputVariableName: 'report',
5458
outputDescription: 'The `error` property is a JavaScript Error instance explaining that a connection could not be made. The `meta` property is reserved for custom driver-specific extensions.',
@@ -61,9 +65,7 @@ module.exports = {
6165
},
6266

6367

64-
fn: function (inputs, exits) {
65-
var util = require('util');
66-
68+
fn: function getConnection(inputs, exits) {
6769
// Note that if this driver is adapted to support managers which spawn
6870
// ad-hoc connections or manage multiple pools/replicas using PoolCluster,
6971
// then relevant settings would need to be included in the manager instance
@@ -74,16 +76,17 @@ module.exports = {
7476
inputs.manager.pool.getConnection(function _gotConnection(err, connection) {
7577
if (err) {
7678
return exits.failed({
77-
error: err
79+
error: err,
80+
meta: inputs.meta
7881
});
7982
}
8083

8184
// Now pass back the connection so it can be provided
8285
// to other methods in this driver.
8386
return exits.success({
84-
connection: connection
87+
connection: connection,
88+
meta: inputs.meta
8589
});
86-
8790
});
8891
}
8992

0 commit comments

Comments
 (0)