当前位置: 首页>>代码示例>>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;未经允许,请勿转载。