From 8fb6309e8d96aefa1f2b2e6c9961d10cc136ae06 Mon Sep 17 00:00:00 2001 From: Phips Peter Date: Fri, 30 May 2014 12:34:46 -0700 Subject: [PATCH] Adding type definitions for pg This is heavily based on the definitions from sqlite3. The Connection class is not defined because pg says that it is private for most use cases. I left that work for people who need it. --- pg/pg-tests.ts | 36 +++++++++++++++++++++ pg/pg.d.ts | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 pg/pg-tests.ts create mode 100644 pg/pg.d.ts diff --git a/pg/pg-tests.ts b/pg/pg-tests.ts new file mode 100644 index 0000000000..dd9c75378d --- /dev/null +++ b/pg/pg-tests.ts @@ -0,0 +1,36 @@ +/// +import pg = require("pg"); +var conString = "postgres://username:password@localhost/database"; + +// Client pooling +pg.connect(conString, (err, client, done) => { + if (err) { + return console.error("Error fetching client from pool", err); + } + client.query("SELECT $1::int AS number", ["1"], (err, result) => { + done(); + if (err) { + return console.error("Error running query", err); + } + console.log(result.rows[0]["number"]); + return null; + }); + return null; +}); + +// Simple +var client = new pg.Client(conString); +client.connect((err) => { + if (err) { + return console.error("Could not connect to postgres", err); + } + client.query("SELECT NOW() AS 'theTime'", (err, result) => { + if (err) { + return console.error("Error running query", err); + } + console.log(result.rows[0]["theTime"]); + client.end(); + return null; + }); + return null; +}); \ No newline at end of file diff --git a/pg/pg.d.ts b/pg/pg.d.ts new file mode 100644 index 0000000000..efdda0e4a4 --- /dev/null +++ b/pg/pg.d.ts @@ -0,0 +1,88 @@ +// Type definitions for pg +// Project: https://github.com/brianc/node-postgres +// Definitions by: Phips Peter +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "pg" { + import events = require("events"); + import stream = require("stream"); + + export function connect(connection: string, callback: (err: Error, client: Client, done: () => void) => void): void; + export function connect(config: ClientConfig, callback: (err: Error, client: Client, done: () => void) => void): void; + export function end(): void; + + export interface ConnectionConfig { + user?: string; + database?: string; + password?: string; + port?: number; + host?: string; + } + + export interface Defaults extends ConnectionConfig { + poolSize?: number; + poolIdleTimeout?: number; + reapIntervalMillis?: number; + binary?: boolean; + parseInt8?: boolean; + } + + export interface ClientConfig extends ConnectionConfig { + ssl?: boolean; + } + + export interface QueryConfig { + name?: string; + text: string; + values?: any[]; + } + + export interface QueryResult { + rows: any[]; + } + + export interface ResultBuilder extends QueryResult { + command: string; + rowCount: number; + oid: number; + addRow(row: any): void; + } + + export class Client extends events.EventEmitter { + constructor(connection: string); + constructor(config: ClientConfig); + + connect(callback?: (err:Error) => void): void; + end(): void; + + query(queryText: string, callback?: (err: Error, result: QueryResult) => void): Query; + query(config: QueryConfig, callback?: (err: Error, result: QueryResult) => void): Query; + query(queryText: string, values: any[], callback?: (err: Error, result: QueryResult) => void): Query; + + copyFrom(queryText: string): stream.Writable; + copyTo(queryText: string): stream.Readable; + + pauseDrain(): void; + resumeDrain(): void; + + public on(event: "drain", listener: () => void): Client; + public on(event: "error", listener: (err: Error) => void): Client; + public on(event: "notification", listener: (message: any) => void): Client; + public on(event: "notice", listener: (message: any) => void): Client; + public on(event: string, listener: Function): Client; + } + + export class Query extends events.EventEmitter { + public on(event: "row", listener: (row: any, result?: ResultBuilder) => void): Query; + public on(event: "error", listener: (err: Error) => void): Query; + public on(event: "end", listener: (result: ResultBuilder) => void): Query; + public on(event: string, listener: Function): Query; + } + + export class Events extends events.EventEmitter { + public on(event: "error", listener: (err: Error, client: Client) => void): Events; + public on(event: string, listener: Function): Events; + } +} \ No newline at end of file