From 4ce23b34dbd3aeb0ab4eb4f60a62c0f248a57e74 Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Fri, 26 Aug 2016 15:12:22 +0800 Subject: [PATCH 1/2] pg 6.1.0 --- pg-pool/pg-pool-tests.ts | 42 +++++++++++++++++++++++++++++++++ pg-pool/pg-pool.d.ts | 50 ++++++++++++++++++++++++++++++++++++++++ pg/pg-tests.ts | 30 ++++++++++++++++++++++++ pg/pg.d.ts | 11 +++++++-- 4 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 pg-pool/pg-pool-tests.ts create mode 100644 pg-pool/pg-pool.d.ts diff --git a/pg-pool/pg-pool-tests.ts b/pg-pool/pg-pool-tests.ts new file mode 100644 index 0000000000..96de93d667 --- /dev/null +++ b/pg-pool/pg-pool-tests.ts @@ -0,0 +1,42 @@ +/// +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(); \ No newline at end of file diff --git a/pg-pool/pg-pool.d.ts b/pg-pool/pg-pool.d.ts new file mode 100644 index 0000000000..7762425963 --- /dev/null +++ b/pg-pool/pg-pool.d.ts @@ -0,0 +1,50 @@ +// Type definitions for pg-pool +// Project: https://github.com/brianc/node-pg-pool +// Definitions by: Leo Liang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// + +declare module "pg-pool" { + import * as events from "events"; + import * as stream from "stream"; + import * as pg from "pg"; + + export interface PoolConfig extends pg.ClientConfig { + // properties from module 'node-pool' + max?: number; + min?: number; + refreshIdle?: boolean; + idleTimeoutMillis?: number; + reapIntervalMillis?: number; + returnToHead?: boolean; + } + + 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; + connect(callback: (err: Error, client: pg.Client, done: () => void) => void): void; + + end(): Promise; + + query(queryText: string): Promise; + query(queryText: string, values: any[]): Promise; + + query(queryText: string, callback: (err: Error, result: pg.QueryResult) => void): void; + query(queryText: string, values: any[], callback: (err: Error, result: pg.QueryResult) => void): void; + + public on(event: "error", listener: (err: Error, client: pg.Client) => void): this; + public on(event: "connect", listener: (client: pg.Client) => void): this; + public on(event: "acquire", listener: (client: pg.Client) => void): this; + public on(event: string, listener: Function): this; + } + +} diff --git a/pg/pg-tests.ts b/pg/pg-tests.ts index 559c6183fc..6c919bd132 100644 --- a/pg/pg-tests.ts +++ b/pg/pg-tests.ts @@ -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) +}) \ No newline at end of file diff --git a/pg/pg.d.ts b/pg/pg.d.ts index 592d782610..4df1e63104 100644 --- a/pg/pg.d.ts +++ b/pg/pg.d.ts @@ -1,14 +1,17 @@ -// Type definitions for pg +// Type definitions for pg 6.1.0 // Project: https://github.com/brianc/node-postgres // Definitions by: Phips Peter // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// declare module "pg" { import events = require("events"); import stream = require("stream"); + export {Pool, PoolConfig} from "pg-pool"; + export function connect(connection: string, callback: (err: Error, client: Client, done: (err?: any) => void) => void): void; export function connect(config: ClientConfig, callback: (err: Error, client: Client, done: (err?: any) => void) => void): void; export function end(): void; @@ -56,6 +59,10 @@ declare module "pg" { connect(callback?: (err:Error) => void): void; end(): void; + release(): void; + + query(queryText: string): Promise; + query(queryText: string, values: any[]): Promise; query(queryText: string, callback?: (err: Error, result: QueryResult) => void): Query; query(config: QueryConfig, callback?: (err: Error, result: QueryResult) => void): Query; @@ -86,7 +93,7 @@ declare module "pg" { public on(event: string, listener: Function): this; } - namespace types { + export namespace types { function setTypeParser(typeId: number, parser: (value: string) => T): void; } } From 67e4127430d1b0fd0d51925b9380206180ce5e23 Mon Sep 17 00:00:00 2001 From: Leo Liang Date: Tue, 30 Aug 2016 20:38:15 +0800 Subject: [PATCH 2/2] Remove circular dependency between modules. --- pg-pool/pg-pool.d.ts | 42 +----------------------------------------- pg/pg.d.ts | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/pg-pool/pg-pool.d.ts b/pg-pool/pg-pool.d.ts index 7762425963..d25f5a9bb6 100644 --- a/pg-pool/pg-pool.d.ts +++ b/pg-pool/pg-pool.d.ts @@ -3,48 +3,8 @@ // Definitions by: Leo Liang // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/// /// declare module "pg-pool" { - import * as events from "events"; - import * as stream from "stream"; - import * as pg from "pg"; - - export interface PoolConfig extends pg.ClientConfig { - // properties from module 'node-pool' - max?: number; - min?: number; - refreshIdle?: boolean; - idleTimeoutMillis?: number; - reapIntervalMillis?: number; - returnToHead?: boolean; - } - - 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; - connect(callback: (err: Error, client: pg.Client, done: () => void) => void): void; - - end(): Promise; - - query(queryText: string): Promise; - query(queryText: string, values: any[]): Promise; - - query(queryText: string, callback: (err: Error, result: pg.QueryResult) => void): void; - query(queryText: string, values: any[], callback: (err: Error, result: pg.QueryResult) => void): void; - - public on(event: "error", listener: (err: Error, client: pg.Client) => void): this; - public on(event: "connect", listener: (client: pg.Client) => void): this; - public on(event: "acquire", listener: (client: pg.Client) => void): this; - public on(event: string, listener: Function): this; - } - + export {Pool, PoolConfig} from "pg"; } diff --git a/pg/pg.d.ts b/pg/pg.d.ts index 4df1e63104..bd15066beb 100644 --- a/pg/pg.d.ts +++ b/pg/pg.d.ts @@ -4,14 +4,11 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// -/// declare module "pg" { import events = require("events"); import stream = require("stream"); - export {Pool, PoolConfig} from "pg-pool"; - export function connect(connection: string, callback: (err: Error, client: Client, done: (err?: any) => void) => void): void; export function connect(config: ClientConfig, callback: (err: Error, client: Client, done: (err?: any) => void) => void): void; export function end(): void; @@ -36,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; @@ -53,6 +60,32 @@ 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; + connect(callback: (err: Error, client: Client, done: () => void) => void): void; + + end(): Promise; + + query(queryText: string): Promise; + query(queryText: string, values: any[]): Promise; + + 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);