Skip to content

Commit 5dae2b2

Browse files
grncdrbrianc
authored andcommitted
Extract query config normalization into utils
1 parent 102a069 commit 5dae2b2

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/query.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,7 @@ var Query = function(config, values, callback) {
99
// use of "new" optional
1010
if (!(this instanceof Query)) return new Query(config, values, callback);
1111

12-
//can take in strings or config objects
13-
config = (typeof(config) == 'string') ? { text: config } : config;
14-
if(values) {
15-
if(typeof values === 'function') {
16-
callback = values;
17-
} else {
18-
config.values = values;
19-
}
20-
}
21-
config.callback = callback;
12+
config = utils.normalizeQueryConfig(config, values, callback);
2213

2314
this.text = config.text;
2415
this.values = config.values;

lib/utils.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,29 @@ var prepareValue = function(val) {
105105
return val === null ? null : val.toString();
106106
}
107107

108+
function normalizeQueryConfig (config, values, callback) {
109+
//can take in strings or config objects
110+
config = (typeof(config) == 'string') ? { text: config } : config;
111+
if(values) {
112+
if(typeof values === 'function') {
113+
config.callback = values;
114+
} else {
115+
config.values = values;
116+
}
117+
}
118+
if (callback) {
119+
config.callback = callback;
120+
}
121+
return config;
122+
}
123+
108124
module.exports = {
109125
normalizeConnectionInfo: normalizeConnectionInfo,
110126
//only exported here to make testing of this method possible
111127
//since it contains quite a bit of logic and testing for
112128
//each connection scenario in an integration test is impractical
113129
buildLibpqConnectionString: getLibpgConString,
114130
parseConnectionString: parseConnectionString,
115-
prepareValue: prepareValue
131+
prepareValue: prepareValue,
132+
normalizeQueryConfig: normalizeQueryConfig
116133
}

0 commit comments

Comments
 (0)