Skip to content

Commit e62eb93

Browse files
Troy Kruthoffbrianc
Troy Kruthoff
authored andcommitted
make Query a public api
1 parent 312a3dd commit e62eb93

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

lib/client.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -185,23 +185,12 @@ p._pulseQueryQueue = function() {
185185
};
186186

187187
p.query = function(config, values, callback) {
188-
//can take in strings or config objects
189-
config = (typeof(config) == 'string') ? { text: config } : config;
190-
if (this.binary && !('binary' in config)) {
191-
config.binary = true;
188+
//can take in strings, config object or query object
189+
var query = (config instanceof Query) ? config : new Query(config, values, callback);
190+
if (this.binary && !query.binary) {
191+
query.binary = true;
192192
}
193193

194-
if(values) {
195-
if(typeof values === 'function') {
196-
callback = values;
197-
} else {
198-
config.values = values;
199-
}
200-
}
201-
202-
config.callback = callback;
203-
204-
var query = new Query(config);
205194
this.queryQueue.push(query);
206195
this._pulseQueryQueue();
207196
return query;

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var PG = function(clientConstructor) {
1313
EventEmitter.call(this);
1414
this.Client = clientConstructor;
1515
this.Connection = require(__dirname + '/connection');
16+
this.Query = require(__dirname + '/query');
1617
this.defaults = defaults;
1718
};
1819

lib/query.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,21 @@ var Result = require(__dirname + '/result');
55
var Types = require(__dirname + '/types');
66
var utils = require(__dirname + '/utils');
77

8-
var Query = function(config) {
8+
var Query = function(config, values, callback) {
9+
// use of "new" optional
10+
if (!(this instanceof Query)) return new Query(config, values, callback);
11+
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;
22+
923
this.text = config.text;
1024
this.values = config.values;
1125
this.rows = config.rows;

0 commit comments

Comments
 (0)