Skip to content

Commit a943500

Browse files
committed
Merge pull request brianc#381 from hoegaarden/master
force usage of pg.native via environment variable
2 parents 690f224 + b313a39 commit a943500

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

lib/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,16 @@ PG.prototype.cancel = function(config, client, query) {
5252
cancellingClient.cancel(client, query);
5353
};
5454

55-
module.exports = new PG(Client);
56-
57-
//lazy require native module...the native module may not have installed
58-
module.exports.__defineGetter__("native", function() {
59-
delete module.exports.native;
60-
module.exports.native = new PG(require(__dirname + '/native'));
61-
return module.exports.native;
62-
});
55+
var forceNative = Object.prototype.hasOwnProperty.call(process.env, 'NODE_PG_FORCE_NATIVE');
56+
if (forceNative) {
57+
module.exports = new PG(require(__dirname + '/native'));
58+
} else {
59+
module.exports = new PG(Client);
6360

61+
//lazy require native module...the native module may not have installed
62+
module.exports.__defineGetter__("native", function() {
63+
delete module.exports.native;
64+
module.exports.native = new PG(require(__dirname + '/native'));
65+
return module.exports.native;
66+
});
67+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* helper needs to be loaded for the asserts but it alos proloads
3+
* client which we don't want here
4+
*
5+
*/
6+
var helper = require(__dirname+"/test-helper")
7+
, path = require('path')
8+
;
9+
10+
var paths = {
11+
'pg' : path.join(__dirname, '..', '..', '..', 'lib', 'index.js') ,
12+
'query_js' : path.join(__dirname, '..', '..', '..', 'lib', 'query.js') ,
13+
'query_native' : path.join(__dirname, '..', '..', '..', 'lib', 'native', 'query.js') ,
14+
};
15+
16+
/**
17+
* delete the modules we are concerned about from the
18+
* module cache, so they get loaded cleanly and the env
19+
* var can kick in ...
20+
*/
21+
function emptyCache(){
22+
Object.keys(require.cache).forEach(function(key){
23+
delete require.cache[key];
24+
});
25+
};
26+
27+
emptyCache();
28+
process.env.NODE_PG_FORCE_NATIVE = '1';
29+
30+
var pg = require( paths.pg );
31+
var query_native = require( paths.query_native );
32+
var query_js = require( paths.query_js );
33+
34+
assert.deepEqual(pg.Client.Query, query_native);
35+
assert.notDeepEqual(pg.Client.Query, query_js);
36+
37+
emptyCache();
38+
delete process.env.NODE_PG_FORCE_NATIVE

0 commit comments

Comments
 (0)