From 3e03f67efeb5e2fed718617c398c4fe8196817a8 Mon Sep 17 00:00:00 2001 From: Chris Charabaruk Date: Tue, 12 Apr 2016 09:40:51 -0400 Subject: [PATCH] Created definitions for promise-pg (#8888) * Created definitions for promise-pg Dependencies: * node * Q * pg Tests script is cribbed from pg typings and modified for promise-pg. 'promise-pg'.Client does not inherit from 'pg'.Client because the functions redefined to return promises completely replace the originals from 'pg'. * fix: QueryConfig.buffer is optional It's only needed when doing a SELECT anyway, so mark it optional. * Bring promise-pg tests in line with examples from promise-pg readme * Add the promise-pg readme examples as tests --- promise-pg/promise-pg-tests.ts | 95 ++++++++++++++++++++++++++++++++++ promise-pg/promise-pg.d.ts | 61 ++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 promise-pg/promise-pg-tests.ts create mode 100644 promise-pg/promise-pg.d.ts diff --git a/promise-pg/promise-pg-tests.ts b/promise-pg/promise-pg-tests.ts new file mode 100644 index 000000000..6a2e74a6f --- /dev/null +++ b/promise-pg/promise-pg-tests.ts @@ -0,0 +1,95 @@ +/// +/// + +import * as pg from 'promise-pg'; + +var conString = "postgres://username:password@localhost/database"; + +// https://github.com/brianc/node-pg-types +pg.raw.types.setTypeParser(20, (val) => Number(val)); + +// Client pooling +pg.connect(conString) + .spread(function (client: pg.Client, done: () => void) { + client.query("SELECT $1::int AS number", ["1"]).promise + .then(function (result) { + // done + }, function (err) { + console.error("Error running query", err); + }) + .finally(done); + }, function (err) { + return console.error("Error fetching client from pool", err); + }).done(); + +// Simple +var client = new pg.Client(conString); +client.connect() + .spread(function (client: pg.Client, done: () => void) { + client.query("SELECT NOW() AS 'theTime'").promise + .then(function (result) { + console.log(result.rows[0]["theTime"]); + client.end(); + return null; + }, function (err) { + return console.error("Error running query", err); + }); + }, function (err) { + return console.error("Could not connect to postgres", err); + }).done(); + +// Using buffer query option +pg.connect(conString) + .spread(function (client: pg.Client, done: () => void) { + client.query({ + text: "SELECT * FROM users", + buffer: true + }).promise.then( + function (result) { console.log(result.rows.length + " rows returned"); }, + function (err) { console.error("Error running query", err); throw err; }, + function (user: any) {} // called for each returned row + ).finally(done); + }).done(); + +// Transactions +pg.connect(conString) + .spread(function (client: pg.Client, done: () => void) { + var INSERT = "INSERT INTO users(name, t_birth, country) VALUES ($1, $2, $3)"; + + console.log("Transaction I:"); + var trans = client.transaction(function () { + return Q.all([{ + text: INSERT, + values: ["Jake1", "now()", "Oo"] + }, { + text: INSERT, + values: ["Cake2", null, "Küche"] + }, { + text: INSERT, + values: ["Mike3", "now()", null] + }].map(function (q) { return client.query(q).promise; })); + }).then(function() { + console.log("Good - Committed Transaction I."); + }, function (err) { + console.log("Bad - Rolled back Transaction I.", err); + }); + + trans.finally(function () { + client.transaction(function () { + return Q.all([{ + text: INSERT, + values: ["Jake", "now()", "Oo"] + }, { + text: INSERT, // Bad, name is NOT NULL + values: [null, null, "Küche"] + }, { + text: INSERT, + values: ["Mike", "now()", null] + }].map(function (q) { return client.query(q).promise; })); + }).then(function () { + console.log("Bad - Committed Transaction II."); + }, function (err) { + console.log("Good - Rolled back Transaction II.", err.message); + }).finally(done).done(); + }); + }).done(); diff --git a/promise-pg/promise-pg.d.ts b/promise-pg/promise-pg.d.ts new file mode 100644 index 000000000..47321bbf6 --- /dev/null +++ b/promise-pg/promise-pg.d.ts @@ -0,0 +1,61 @@ +// Type definitions for promise-pg +// Project: https://bitbucket.org/lplabs/promise-pg +// Definitions by: Chris Charabaruk +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// +/// + +declare module "promise-pg" { + import * as stream from 'stream'; + import * as pg from 'pg'; + + export {pg as raw}; + + export interface ClientConfig extends pg.ClientConfig {} + + export function connect(connection: string): Q.Promise; + export function connect(connection: pg.ClientConfig): Q.Promise; + + export function end(): Q.Promise; + + export interface QueryConfig extends pg.QueryConfig { + buffer?: boolean; + } + + export class Client { + constructor(connection: string); + constructor(config: ClientConfig); + + raw: pg.Client; + + connect(): Q.Promise; + end(): Q.Promise; + + query(queryText: string): Query; + query(config: QueryConfig): Query; + query(queryText: string, values: any[]): 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; + + transaction(task: () => Q.Promise): Q.Promise; + } + + export interface QueryResult extends pg.QueryResult {} + export interface ResultBuilder extends pg.ResultBuilder {} + + export class Query extends pg.Query { + promise: Q.Promise; + } +}