index.js 1.5 KB

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