Merge pull request #10839 from aleung/pg

pg 6.1.0
This commit is contained in:
Ryan Cavanaugh
2016-08-30 20:42:04 -07:00
committed by GitHub
4 changed files with 124 additions and 2 deletions

42
pg-pool/pg-pool-tests.ts Normal file
View File

@@ -0,0 +1,42 @@
/// <reference path="pg-pool.d.ts" />
import {Pool} from "pg-pool";
let pool = new Pool()
//you can pass properties to the pool
//these properties are passed unchanged to both the node-postgres Client constructor
//and the node-pool (https://github.com/coopernurse/node-pool) constructor
//allowing you to fully configure the behavior of both
let pool2 = new Pool({
database: 'postgres',
user: 'brianc',
password: 'secret!',
port: 5432,
ssl: true,
max: 20, //set pool max size to 20
min: 4, //set min pool size to 4
idleTimeoutMillis: 1000 //close idle clients after 1 second
})
pool.connect().then(client => {
client.query('select $1::text as name', ['pg-pool']).then(res => {
client.release()
console.log('hello from', res.rows[0].name)
})
.catch(e => {
client.release()
console.error('query error', e.message, e.stack)
})
})
async function helperTest() {
const time = await pool.query('SELECT NOW()');
const name = await pool.query('select $1::text as name', ['brianc']);
console.log(name.rows[0].name, 'says hello at', time.rows[0].name);
}
pool.query('SELECT $1::text as name', ['brianc'], function (err, res) {
console.log(res.rows[0].name) // brianc
})
pool.end();

10
pg-pool/pg-pool.d.ts vendored Normal file
View File

@@ -0,0 +1,10 @@
// Type definitions for pg-pool
// Project: https://github.com/brianc/node-pg-pool
// Definitions by: Leo Liang <https://github.com/aleung>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../pg/pg.d.ts" />
declare module "pg-pool" {
export {Pool, PoolConfig} from "pg";
}

View File

@@ -42,3 +42,33 @@ client.connect((err) => {
});
return null;
});
// client pooling
var config = {
user: 'foo', //env var: PGUSER
database: 'my_db', //env var: PGDATABASE
password: 'secret', //env var: PGPASSWORD
port: 5432, //env var: PGPORT
max: 10, // max number of clients in the pool
idleTimeoutMillis: 30000, // how long a client is allowed to remain idle before being closed
};
var pool = new pg.Pool(config);
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
client.query('SELECT $1::int AS number', ['1'], function(err, result) {
done();
if(err) {
return console.error('error running query', err);
}
console.log(result.rows[0].number);
});
});
pool.on('error', function (err, client) {
console.error('idle client error', err.message, err.stack)
})

44
pg/pg.d.ts vendored
View File

@@ -1,4 +1,4 @@
// Type definitions for pg
// Type definitions for pg 6.1.0
// Project: https://github.com/brianc/node-postgres
// Definitions by: Phips Peter <http://pspeter3.com>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -33,6 +33,16 @@ declare module "pg" {
ssl?: boolean;
}
export interface PoolConfig extends ClientConfig {
// properties from module 'node-pool'
max?: number;
min?: number;
refreshIdle?: boolean;
idleTimeoutMillis?: number;
reapIntervalMillis?: number;
returnToHead?: boolean;
}
export interface QueryConfig {
name?: string;
text: string;
@@ -50,12 +60,42 @@ declare module "pg" {
addRow(row: any): void;
}
export class Pool extends events.EventEmitter {
constructor();
// `new Pool('pg://user@localhost/mydb')` is not allowed.
// But it passes type check because of issue:
// https://github.com/Microsoft/TypeScript/issues/7485
constructor(config: PoolConfig);
connect(): Promise<Client>;
connect(callback: (err: Error, client: Client, done: () => void) => void): void;
end(): Promise<void>;
query(queryText: string): Promise<QueryResult>;
query(queryText: string, values: any[]): Promise<QueryResult>;
query(queryText: string, callback: (err: Error, result: QueryResult) => void): void;
query(queryText: string, values: any[], callback: (err: Error, result: QueryResult) => void): void;
public on(event: "error", listener: (err: Error, client: Client) => void): this;
public on(event: "connect", listener: (client: Client) => void): this;
public on(event: "acquire", listener: (client: Client) => void): this;
public on(event: string, listener: Function): this;
}
export class Client extends events.EventEmitter {
constructor(connection: string);
constructor(config: ClientConfig);
connect(callback?: (err:Error) => void): void;
end(): void;
release(): void;
query(queryText: string): Promise<QueryResult>;
query(queryText: string, values: any[]): Promise<QueryResult>;
query(queryText: string, callback?: (err: Error, result: QueryResult) => void): Query;
query(config: QueryConfig, callback?: (err: Error, result: QueryResult) => void): Query;
@@ -86,7 +126,7 @@ declare module "pg" {
public on(event: string, listener: Function): this;
}
namespace types {
export namespace types {
function setTypeParser<T>(typeId: number, parser: (value: string) => T): void;
}
}