pg#PoolConfig TypeScript Examples

The following examples show how to use pg#PoolConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From posthog-foss with MIT License 6 votes vote down vote up
export function createPostgresPool(
    configOrDatabaseUrl: PluginsServerConfig | string,
    onError?: (error: Error) => any
): Pool {
    if (typeof configOrDatabaseUrl !== 'string') {
        if (!configOrDatabaseUrl.DATABASE_URL && !configOrDatabaseUrl.POSTHOG_DB_NAME) {
            throw new Error('Invalid configuration for Postgres: either DATABASE_URL or POSTHOG_DB_NAME required')
        }
    }
    const credentials: Partial<PoolConfig> =
        typeof configOrDatabaseUrl === 'string'
            ? {
                  connectionString: configOrDatabaseUrl,
              }
            : configOrDatabaseUrl.DATABASE_URL
            ? {
                  connectionString: configOrDatabaseUrl.DATABASE_URL,
              }
            : {
                  database: configOrDatabaseUrl.POSTHOG_DB_NAME ?? undefined,
                  user: configOrDatabaseUrl.POSTHOG_DB_USER,
                  password: configOrDatabaseUrl.POSTHOG_DB_PASSWORD,
                  host: configOrDatabaseUrl.POSTHOG_POSTGRES_HOST,
                  port: configOrDatabaseUrl.POSTHOG_POSTGRES_PORT,
              }

    const pgPool = new Pool({
        ...credentials,
        idleTimeoutMillis: 500,
        max: 10,
        ssl: process.env.DYNO // Means we are on Heroku
            ? {
                  rejectUnauthorized: false,
              }
            : undefined,
    })

    const handleError =
        onError ||
        ((error) => {
            Sentry.captureException(error)
            status.error('?', 'PostgreSQL error encountered!\n', error)
        })

    pgPool.on('error', handleError)

    return pgPool
}
Example #2
Source File: config.ts    From cloud-pricing-api with Apache License 2.0 6 votes vote down vote up
async function pg(): Promise<Pool> {
  if (!pgPool) {
    let poolConfig: PoolConfig = {
      user: process.env.POSTGRES_USER || 'postgres',
      database: process.env.POSTGRES_DB || 'cloud_pricing',
      password: process.env.POSTGRES_PASSWORD || '',
      port: Number(process.env.POSTGRES_PORT) || 5432,
      host: process.env.POSTGRES_HOST || 'localhost',
      max: Number(process.env.POSTGRES_MAX_CLIENTS) || 10,
    };

    if (process.env.POSTGRES_URI) {
      poolConfig = {
        connectionString:
          process.env.POSTGRES_URI ||
          'postgresql://postgres:@localhost:5432/cloud_pricing',
      };
    }

    pgPool = new Pool(poolConfig);
  }
  return pgPool;
}
Example #3
Source File: graphile-worker.microservice.ts    From ironfish-api with Mozilla Public License 2.0 6 votes vote down vote up
private getPostgresPoolConfig(): PoolConfig {
    return {
      connectionString: this.config.get<string>('DATABASE_URL'),
      ssl:
        this.config.get<string>('NODE_ENV') !== 'development'
          ? { rejectUnauthorized: false }
          : undefined,
    };
  }
Example #4
Source File: graphile-worker.service.ts    From ironfish-api with Mozilla Public License 2.0 6 votes vote down vote up
private getPostgresPoolConfig(): PoolConfig {
    return {
      connectionString: this.config.get<string>('DATABASE_URL'),
      ssl:
        this.config.get<string>('NODE_ENV') !== 'development'
          ? { rejectUnauthorized: false }
          : undefined,
    };
  }
Example #5
Source File: db.ts    From squid with GNU General Public License v3.0 6 votes vote down vote up
export function createPoolConfig(): PoolConfig {
    return {
        host: process.env.DB_HOST || 'localhost',
        port: process.env.DB_PORT ? parseInt(process.env.DB_PORT) : 5432,
        database: process.env.DB_NAME || 'postgres',
        user: process.env.DB_USER || 'postgres',
        password: process.env.DB_PASS || 'postgres'
    }
}
Example #6
Source File: PostgresMeta.ts    From postgres-meta with Apache License 2.0 6 votes vote down vote up
constructor(config: PoolConfig) {
    const { query, end } = init(config)
    this.query = query
    this.end = end
    this.columns = new PostgresMetaColumns(this.query)
    this.config = new PostgresMetaConfig(this.query)
    this.extensions = new PostgresMetaExtensions(this.query)
    this.functions = new PostgresMetaFunctions(this.query)
    this.policies = new PostgresMetaPolicies(this.query)
    this.publications = new PostgresMetaPublications(this.query)
    this.roles = new PostgresMetaRoles(this.query)
    this.schemas = new PostgresMetaSchemas(this.query)
    this.tables = new PostgresMetaTables(this.query)
    this.triggers = new PostgresMetaTriggers(this.query)
    this.types = new PostgresMetaTypes(this.query)
    this.version = new PostgresMetaVersion(this.query)
    this.views = new PostgresMetaViews(this.query)
  }
Example #7
Source File: db.ts    From postgres-meta with Apache License 2.0 6 votes vote down vote up
init: (config: PoolConfig) => {
  query: (sql: string) => Promise<PostgresMetaResult<any>>
  end: () => Promise<void>
} = (config) => {
  // NOTE: Race condition could happen here: one async task may be doing
  // `pool.end()` which invalidates the pool and subsequently all existing
  // handles to `query`. Normally you might only deal with one DB so you don't
  // need to call `pool.end()`, but since the server needs this, we make a
  // compromise: if we run `query` after `pool.end()` is called (i.e. pool is
  // `null`), we temporarily create a pool and close it right after.
  let pool: Pool | null = new Pool(config)
  return {
    async query(sql) {
      try {
        if (!pool) {
          const pool = new Pool(config)
          const { rows } = await pool.query(sql)
          await pool.end()
          return { data: rows, error: null }
        }

        const { rows } = await pool.query(sql)
        return { data: rows, error: null }
      } catch (e: any) {
        return { data: null, error: { message: e.message } }
      }
    },

    async end() {
      const _pool = pool
      pool = null
      // Gracefully wait for active connections to be idle, then close all
      // connections in the pool.
      if (_pool) await _pool.end()
    },
  }
}
Example #8
Source File: postgres.ts    From eosio-contract-api with GNU Affero General Public License v3.0 5 votes vote down vote up
private readonly args: PoolConfig;