index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict'
  2. var Client = require('./client')
  3. var defaults = require('./defaults')
  4. var Connection = require('./connection')
  5. var Pool = require('pg-pool')
  6. const { DatabaseError } = require('pg-protocol')
  7. const poolFactory = (Client) => {
  8. return class BoundPool extends Pool {
  9. constructor(options) {
  10. super(options, Client)
  11. }
  12. }
  13. }
  14. var PG = function (clientConstructor) {
  15. this.defaults = defaults
  16. this.Client = clientConstructor
  17. this.Query = this.Client.Query
  18. this.Pool = poolFactory(this.Client)
  19. this._pools = []
  20. this.Connection = Connection
  21. this.types = require('pg-types')
  22. this.DatabaseError = DatabaseError
  23. }
  24. if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
  25. module.exports = new PG(require('./native'))
  26. } else {
  27. module.exports = new PG(Client)
  28. // lazy require native module...the native module may not have installed
  29. Object.defineProperty(module.exports, 'native', {
  30. configurable: true,
  31. enumerable: false,
  32. get() {
  33. var native = null
  34. try {
  35. native = new PG(require('./native'))
  36. } catch (err) {
  37. if (err.code !== 'MODULE_NOT_FOUND') {
  38. throw err
  39. }
  40. }
  41. // overwrite module.exports.native so that getter is never called again
  42. Object.defineProperty(module.exports, 'native', {
  43. value: native,
  44. })
  45. return native
  46. },
  47. })
  48. }