| [62b2964] | 1 | 'use strict'
|
|---|
| 2 |
|
|---|
| 3 | const Client = require('./client')
|
|---|
| 4 | const defaults = require('./defaults')
|
|---|
| 5 | const Connection = require('./connection')
|
|---|
| 6 | const Result = require('./result')
|
|---|
| 7 | const utils = require('./utils')
|
|---|
| 8 | const Pool = require('pg-pool')
|
|---|
| 9 | const TypeOverrides = require('./type-overrides')
|
|---|
| 10 | const { DatabaseError } = require('pg-protocol')
|
|---|
| 11 | const { escapeIdentifier, escapeLiteral } = require('./utils')
|
|---|
| 12 |
|
|---|
| 13 | const poolFactory = (Client) => {
|
|---|
| 14 | return class BoundPool extends Pool {
|
|---|
| 15 | constructor(options) {
|
|---|
| 16 | super(options, Client)
|
|---|
| 17 | }
|
|---|
| 18 | }
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | const PG = function (clientConstructor) {
|
|---|
| 22 | this.defaults = defaults
|
|---|
| 23 | this.Client = clientConstructor
|
|---|
| 24 | this.Query = this.Client.Query
|
|---|
| 25 | this.Pool = poolFactory(this.Client)
|
|---|
| 26 | this._pools = []
|
|---|
| 27 | this.Connection = Connection
|
|---|
| 28 | this.types = require('pg-types')
|
|---|
| 29 | this.DatabaseError = DatabaseError
|
|---|
| 30 | this.TypeOverrides = TypeOverrides
|
|---|
| 31 | this.escapeIdentifier = escapeIdentifier
|
|---|
| 32 | this.escapeLiteral = escapeLiteral
|
|---|
| 33 | this.Result = Result
|
|---|
| 34 | this.utils = utils
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | let clientConstructor = Client
|
|---|
| 38 |
|
|---|
| 39 | let forceNative = false
|
|---|
| 40 | try {
|
|---|
| 41 | forceNative = !!process.env.NODE_PG_FORCE_NATIVE
|
|---|
| 42 | } catch {
|
|---|
| 43 | // ignore, e.g., Deno without --allow-env
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | if (forceNative) {
|
|---|
| 47 | clientConstructor = require('./native')
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | module.exports = new PG(clientConstructor)
|
|---|
| 51 |
|
|---|
| 52 | // lazy require native module...the native module may not have installed
|
|---|
| 53 | Object.defineProperty(module.exports, 'native', {
|
|---|
| 54 | configurable: true,
|
|---|
| 55 | enumerable: false,
|
|---|
| 56 | get() {
|
|---|
| 57 | let native = null
|
|---|
| 58 | try {
|
|---|
| 59 | native = new PG(require('./native'))
|
|---|
| 60 | } catch (err) {
|
|---|
| 61 | if (err.code !== 'MODULE_NOT_FOUND') {
|
|---|
| 62 | throw err
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // overwrite module.exports.native so that getter is never called again
|
|---|
| 67 | Object.defineProperty(module.exports, 'native', {
|
|---|
| 68 | value: native,
|
|---|
| 69 | })
|
|---|
| 70 |
|
|---|
| 71 | return native
|
|---|
| 72 | },
|
|---|
| 73 | })
|
|---|