當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript pg-connection-string.parse函數代碼示例

本文整理匯總了TypeScript中pg-connection-string.parse函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript parse函數的具體用法?TypeScript parse怎麽用?TypeScript parse使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了parse函數的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: Pool

import { Pool } from 'pg'
import { parse as parsePgConnectionString } from 'pg-connection-string'

const pgUrl = process.env.TEST_PG_URL || 'postgres://localhost:5432/postgraphql_test'

const pgPool = new Pool(Object.assign({}, parsePgConnectionString(pgUrl), {
  max: 15,
  idleTimeoutMillis: 500,
}))

export default pgPool
開發者ID:tim-field,項目名稱:postgraphql,代碼行數:11,代碼來源:pgPool.ts

示例2: postgraphql

export default function postgraphql (
  poolOrConfig?: Pool | PoolConfig | string,
  schemaOrOptions?: string | Array<string> | PostGraphQLOptions,
  maybeOptions?: PostGraphQLOptions,
): HttpRequestHandler {
  let schema: string | Array<string>
  let options: PostGraphQLOptions

  // If the second argument is undefined, use defaults for both `schema` and
  // `options`.
  if (typeof schemaOrOptions === 'undefined') {
    schema = 'public'
    options = {}
  }
  // If the second argument is a string or array, it is the schemas so set the
  // `schema` value and try to use the third argument (or a default) for
  // `options`.
  else if (typeof schemaOrOptions === 'string' || Array.isArray(schemaOrOptions)) {
    schema = schemaOrOptions
    options = maybeOptions || {}
  }
  // Otherwise the second argument is the options so set `schema` to the
  // default and `options` to the second argument.
  else {
    schema = 'public'
    options = schemaOrOptions
  }

  // Creates the Postgres schemas array.
  const pgSchemas: Array<string> = Array.isArray(schema) ? schema : [schema]

  // Do some things with `poolOrConfig` so that in the end, we actually get a
  // Postgres pool.
  const pgPool =
    // If it is already a `Pool`, just use it.
    poolOrConfig instanceof Pool
      ? poolOrConfig
      : new Pool(typeof poolOrConfig === 'string'
        // Otherwise if it is a string, let us parse it to get a config to
        // create a `Pool`.
        ? parsePgConnectionString(poolOrConfig)
        // Finally, it must just be a config itself. If it is undefined, we
        // will just use an empty config and let the defaults take over.
        : poolOrConfig || {},
      )

  // Creates a promise which will resolve to a GraphQL schema. Connects a
  // client from our pool to introspect the database.
  //
  // This is not a constant because when we are in watch mode, we want to swap
  // out the `gqlSchema`.
  let gqlSchema = createGqlSchema()

  const _emitter = new EventEmitter()

  // If the user wants us to watch the schema, execute the following:
  if (options.watchPg) {
    watchPgSchemas({
      pgPool,
      pgSchemas,
      onChange: ({ commands }) => {
        // tslint:disable-next-line no-console
        console.log(`Rebuilding PostGraphQL API after Postgres command(s): ️${commands.map(command => chalk.bold.cyan(command)).join(', ')}`)

        _emitter.emit('schemas:changed')

        // Actually restart the GraphQL schema by creating a new one. Note that
        // `createGqlSchema` returns a promise and we aren’t ‘await’ing it.
        gqlSchema = createGqlSchema()
      },
    })
      // If an error occurs when watching the Postgres schemas, log the error and
      // exit the process.
      .catch(error => {
        // tslint:disable-next-line no-console
        console.error(`${error.stack}\n`)
        process.exit(1)
      })
  }

  // Finally create our Http request handler using our options, the Postgres
  // pool, and GraphQL schema. Return the final result.
  return createPostGraphQLHttpRequestHandler(Object.assign({}, options, {
    getGqlSchema: () => gqlSchema,
    pgPool,
    _emitter,
  }))

  /**
   * Creates a GraphQL schema by connecting a client from our pool which will
   * be used to introspect our Postgres database. If this function fails, we
   * will log the error and exit the process.
   *
   * This may only be executed once, at startup. However, if we are in watch
   * mode this will be updated whenever there is a change in our schema.
   */
  async function createGqlSchema (): Promise<GraphQLSchema> {
    try {
      const pgClient = await pgPool.connect()
      const newGqlSchema = await createPostGraphQLSchema(pgClient, pgSchemas, options)
//.........這裏部分代碼省略.........
開發者ID:tim-field,項目名稱:postgraphql,代碼行數:101,代碼來源:postgraphql.ts

示例3: Pool

import { Pool } from 'pg';
import { parse as parsePgConnectionString } from 'pg-connection-string';

const pgUrl = process.env.TEST_PG_URL || 'postgres://localhost:5432/postgraphile_test';

const pgPool = new Pool({
  ...parsePgConnectionString(pgUrl),
  max: 15,
  idleTimeoutMillis: 500,
});

export default pgPool;
開發者ID:calebmer,項目名稱:postgraphql,代碼行數:12,代碼來源:pgPool.ts

示例4: parse

import { parse } from 'pg-connection-string';

const parts = parse('postgres://user:password@host:port/dbname');

parts.application_name;
parts.client_encoding;
parts.database;
parts.fallback_application_name;
parts.host;
parts.password;
parts.port;
parts.ssl;
parts.user;
開發者ID:AbraaoAlves,項目名稱:DefinitelyTyped,代碼行數:13,代碼來源:pg-connection-string-tests.ts

示例5: parsePgConnectionString

// tslint:disable-next-line no-any
} = program as any

// Add custom logic for getting the schemas from our CLI. If we are in demo
// mode, we want to use the `forum_example` schema. Otherwise the `public`
// schema is what we want.
const schemas: Array<string> = program['schema'] || (isDemo ? ['forum_example'] : ['public'])

// Create our Postgres config.
const pgConfig = Object.assign(
  {},
  // If we have a Postgres connection string, parse it and use that as our
  // config. If we don’t have a connection string use some environment
  // variables or final defaults. Other environment variables should be
  // detected and used by `pg`.
  pgConnectionString || isDemo ? parsePgConnectionString(pgConnectionString || DEMO_PG_URL) : {
    host: process.env.PGHOST || 'localhost',
    port: process.env.PGPORT || 5432,
    database: process.env.PGDATABASE,
  },
  // Add the max pool size to our config.
  { max: maxPoolSize },
)

// Create’s our PostGraphQL server and provides all the appropriate
// configuration options.
const server = createServer(postgraphql(pgConfig, schemas, {
  classicIds,
  dynamicJson,
  disableDefaultMutations,
  graphqlRoute,
開發者ID:tim-field,項目名稱:postgraphql,代碼行數:31,代碼來源:cli.ts

示例6:

import * as winston from 'winston';
import * as dotenv from 'dotenv';
import { createConnection } from 'typeorm';
import 'reflect-metadata';
import * as PostgressConnectionStringParser from 'pg-connection-string';

import { User } from './entity/user';
import { logger } from './logging';
import { config } from './config';
import { router } from './routes';

// Load environment variables from .env file, where API keys and passwords are configured
dotenv.config({ path: '.env' });

// Get DB connection options from env variable
const connectionOptions = PostgressConnectionStringParser.parse(config.databaseUrl);

// create connection with database
// note that its not active database connection
// TypeORM creates you connection pull to uses connections from pull on your requests
createConnection({
    type: 'postgres',
    host: connectionOptions.host,
    port: connectionOptions.port,
    username: connectionOptions.user,
    password: connectionOptions.password,
    database: connectionOptions.database,
    synchronize: true,
    logging: false,
    entities: [
       'dist/entity/**/*.js'
開發者ID:ryanwild,項目名稱:node-typescript-koa-rest,代碼行數:31,代碼來源:server.ts

示例7: coerce

    user: typeof o.user === 'string' ? o.user : undefined,
    database: typeof o.database === 'string' ? o.database : undefined,
    password: typeof o.password === 'string' ? o.password : undefined,
    port: o.port || typeof o.port === 'number' ? o.port : undefined,
    host: typeof o.host === 'string' ? o.host : undefined,
  };
};

// Create our Postgres config.
const pgConfig: PoolConfig = {
  // If we have a Postgres connection string, parse it and use that as our
  // config. If we don’t have a connection string use some environment
  // variables or final defaults. Other environment variables should be
  // detected and used by `pg`.
  ...(pgConnectionString || process.env.DATABASE_URL || isDemo
    ? coerce(parsePgConnectionString(pgConnectionString || process.env.DATABASE_URL || DEMO_PG_URL))
    : {
        host: process.env.PGHOST || 'localhost',
        port: (process.env.PGPORT ? parseInt(process.env.PGPORT, 10) : null) || 5432,
        database: process.env.PGDATABASE,
        user: process.env.PGUSER,
        password: process.env.PGPASSWORD,
      }),
  // Add the max pool size to our config.
  max: maxPoolSize,
};

const loadPlugins = (rawNames: mixed) => {
  if (!rawNames) {
    return undefined;
  }
開發者ID:calebmer,項目名稱:postgraphql,代碼行數:31,代碼來源:cli.ts


注:本文中的pg-connection-string.parse函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。