Skip to content

Commit b8497fb

Browse files
committed
Fix 'query: { pool: false }' in connection object.
1 parent 1af9b1f commit b8497fb

File tree

2 files changed

+88
-25
lines changed

2 files changed

+88
-25
lines changed

lib/ORM.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@ exports.connect = function (opts, cb) {
7171
return ORM_Error(new ORMError("CONNECTION_URL_EMPTY", 'PARAM_MISMATCH'), cb);
7272
}
7373
opts = url.parse(opts, true);
74-
for(var k in opts.query) {
75-
opts[k] = opts.query[k];
76-
}
7774
} else if (typeof opts == 'object') {
7875
opts = _.cloneDeep(opts);
7976
}
77+
78+
opts.query = opts.query || {};
79+
80+
for(var k in opts.query) {
81+
opts.query[k] = queryParamCast(opts.query[k]);
82+
opts[k] = opts.query[k];
83+
}
84+
8085
if (!opts.database) {
8186
// if (!opts.pathname) {
8287
// return cb(new Error("CONNECTION_URL_NO_DATABASE"));
@@ -112,11 +117,9 @@ exports.connect = function (opts, cb) {
112117
try {
113118
var Driver = adapters.get(proto);
114119
var settings = new Settings.Container(exports.settings.get('*'));
115-
var debug = extractOption(opts, "debug");
116-
var pool = extractOption(opts, "pool");
117120
var driver = new Driver(opts, null, {
118-
debug : (debug !== null ? ((debug === "false" || debug === "0") ? false : true) : settings.get("connection.debug")),
119-
pool : (pool !== null ? ((pool === "false" || pool === "0") ? false : true) : settings.get("connection.pool")),
121+
debug : 'debug' in opts.query ? opts.query.debug : settings.get("connection.debug"),
122+
pool : 'pool' in opts.query ? opts.query.pool : settings.get("connection.pool"),
120123
settings : settings
121124
});
122125

@@ -392,19 +395,16 @@ function ORM_Error(err, cb) {
392395
return Emitter;
393396
}
394397

395-
function extractOption(opts, key) {
396-
if (!opts.query || !opts.query.hasOwnProperty(key)) {
397-
return null;
398-
}
399-
400-
var opt = opts.query[key];
401-
402-
delete opts.query[key];
403-
if (opts.href) {
404-
opts.href = opts.href.replace(new RegExp(key + "=[^&]+&?"), "");
405-
}
406-
if (opts.search) {
407-
opts.search = opts.search.replace(new RegExp(key + "=[^&]+&?"), "");
398+
function queryParamCast (val) {
399+
if (typeof val == 'string') {
400+
switch (val) {
401+
case '1':
402+
case 'true':
403+
return true;
404+
case '0':
405+
case 'false':
406+
return false;
407+
}
408408
}
409-
return opt;
409+
return val;
410410
}

test/integration/orm-exports.js

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,75 @@ describe("ORM.connect()", function () {
238238
return done();
239239
});
240240
});
241+
});
241242

242-
it("should allow pool and debug settings to be false", function(done) {
243+
describe("query options", function () {
244+
it("should understand pool `'false'` from query string", function (done) {
243245
var connString = common.getConnectionString() + "debug=false&pool=false";
244-
ORM.connect(connString, function(err, db) {
245-
db.driver.opts.pool.should.equal(false);
246-
db.driver.opts.debug.should.equal(false);
246+
ORM.connect(connString, function (err, db) {
247+
should.not.exist(err);
248+
should.strictEqual(db.driver.opts.pool, false);
249+
should.strictEqual(db.driver.opts.debug, false);
250+
done();
251+
});
252+
});
253+
254+
it("should understand pool `'0'` from query string", function (done) {
255+
var connString = common.getConnectionString() + "debug=0&pool=0";
256+
ORM.connect(connString, function (err, db) {
257+
should.not.exist(err);
258+
should.strictEqual(db.driver.opts.pool, false);
259+
should.strictEqual(db.driver.opts.debug, false);
260+
done();
261+
});
262+
});
263+
264+
it("should understand pool `'true'` from query string", function (done) {
265+
var connString = common.getConnectionString() + "debug=true&pool=true";
266+
ORM.connect(connString, function (err, db) {
267+
should.not.exist(err);
268+
should.strictEqual(db.driver.opts.pool, true);
269+
should.strictEqual(db.driver.opts.debug, true);
270+
done();
271+
});
272+
});
273+
274+
it("should understand pool `'1'` from query string", function (done) {
275+
var connString = common.getConnectionString() + "debug=1&pool=1";
276+
ORM.connect(connString, function (err, db) {
277+
should.not.exist(err);
278+
should.strictEqual(db.driver.opts.pool, true);
279+
should.strictEqual(db.driver.opts.debug, true);
280+
done();
281+
});
282+
});
283+
284+
it("should understand pool `false` from query options", function (done) {
285+
var connOpts = _.extend(common.getConfig(), {
286+
protocol: common.protocol(),
287+
query: {
288+
pool: false, debug: false
289+
}
290+
});
291+
ORM.connect(connOpts, function (err, db) {
292+
should.not.exist(err);
293+
should.strictEqual(db.driver.opts.pool, false);
294+
should.strictEqual(db.driver.opts.debug, false);
295+
done();
296+
});
297+
});
298+
299+
it("should understand pool `true` from query options", function (done) {
300+
var connOpts = _.extend(common.getConfig(), {
301+
protocol: common.protocol(),
302+
query: {
303+
pool: true, debug: true
304+
}
305+
});
306+
ORM.connect(connOpts, function (err, db) {
307+
should.not.exist(err);
308+
should.strictEqual(db.driver.opts.pool, true);
309+
should.strictEqual(db.driver.opts.debug, true);
247310
done();
248311
});
249312
});

0 commit comments

Comments
 (0)