mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Copyright (c) 2010-2017 Brian Carlson (brian.m.carlson@gmail.com)
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* README.md file in the root directory of this source tree.
|
|
*/
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
var util = require('util');
|
|
var Client = require('./client');
|
|
var defaults = require('./defaults');
|
|
var Connection = require('./connection');
|
|
var ConnectionParameters = require('./connection-parameters');
|
|
var Pool = require('pg-pool');
|
|
|
|
const poolFactory = (Client) => {
|
|
var BoundPool = function(options) {
|
|
var config = { Client: Client };
|
|
for (var key in options) {
|
|
config[key] = options[key];
|
|
}
|
|
Pool.call(this, config);
|
|
};
|
|
|
|
util.inherits(BoundPool, Pool);
|
|
|
|
return BoundPool;
|
|
};
|
|
|
|
|
|
var PG = function(clientConstructor) {
|
|
this.defaults = defaults;
|
|
this.Client = clientConstructor;
|
|
this.Query = this.Client.Query;
|
|
this.Pool = poolFactory(this.Client);
|
|
this._pools = [];
|
|
this.Connection = Connection;
|
|
this.types = require('pg-types');
|
|
};
|
|
|
|
if(typeof process.env.NODE_PG_FORCE_NATIVE != 'undefined') {
|
|
module.exports = new PG(require('./native'));
|
|
} else {
|
|
module.exports = new PG(Client);
|
|
|
|
//lazy require native module...the native module may not have installed
|
|
module.exports.__defineGetter__("native", function() {
|
|
delete module.exports.native;
|
|
var native = null;
|
|
try {
|
|
native = new PG(require('./native'));
|
|
} catch (err) {
|
|
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
throw err;
|
|
}
|
|
console.error(err.message);
|
|
}
|
|
module.exports.native = native;
|
|
return native;
|
|
});
|
|
}
|