-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathindex.js
58 lines (53 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict'
/**
* Copyright (c) 2010-2017 Brian Carlson ([email protected])
* 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.
*/
const Client = require('./client')
const defaults = require('./defaults')
const Connection = require('./connection')
const Pool = require('pg-pool')
const poolFactory = (Client) => {
return class BoundPool extends Pool {
constructor(options) {
const config = Object.assign({ Client: Client }, options)
super(config)
}
}
}
class PG {
constructor(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
let native = null
try {
native = new PG(require('./native'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
/* eslint-disable no-console */
console.error(err.message)
/* eslint-enable no-console */
}
module.exports.native = native
return native
})
}