From aa073574a3c547dba7544d80ff318d73ebfa7c65 Mon Sep 17 00:00:00 2001 From: James Bracy Date: Wed, 19 Oct 2016 05:26:52 -0700 Subject: [PATCH] Adding pg-types (#12037) --- pg-types/pg-types-tests.ts | 18 ++++++++++++++++++ pg-types/pg-types.d.ts | 23 +++++++++++++++++++++++ pg/pg.d.ts | 23 ++++++++++++----------- 3 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 pg-types/pg-types-tests.ts create mode 100644 pg-types/pg-types.d.ts diff --git a/pg-types/pg-types-tests.ts b/pg-types/pg-types-tests.ts new file mode 100644 index 0000000000..bcabaa68d9 --- /dev/null +++ b/pg-types/pg-types-tests.ts @@ -0,0 +1,18 @@ +/// +/// +import * as types from "pg-types"; + +types.getTypeParser(1184, 'text'); + +types.setTypeParser(1184, (value) => value === null ? null : value); +types.setTypeParser(1186, 'text', (value) => value === null ? null : value); +types.setTypeParser(1186, 'binary', (value) => value.toISOString()); +types.setTypeParser(1185, (value) => types.arrayParser.create(value, (x) => x).parse()); + +var TIMESTAMPTZ_OID = 1184 +var TIMESTAMP_OID = 1114 +var parseFn = function(val: any) { + return val === null ? null : moment(val) +} +types.setTypeParser(TIMESTAMPTZ_OID, parseFn) +types.setTypeParser(TIMESTAMP_OID, parseFn) diff --git a/pg-types/pg-types.d.ts b/pg-types/pg-types.d.ts new file mode 100644 index 0000000000..9775bef483 --- /dev/null +++ b/pg-types/pg-types.d.ts @@ -0,0 +1,23 @@ +// Type definitions for pg-types 1.11.0 +// Project: https://github.com/brianc/node-pg-types +// Definitions by: James Bracy +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module "pg-types" { + + interface TypeParser { + (value: any): any; + } + + export function getTypeParser(oid: number, format: string): TypeParser; + + export function setTypeParser(oid: number, format: string, parseFn: TypeParser): void; + export function setTypeParser(oid: number, parseFn: TypeParser): void; + + export namespace arrayParser { + + export function create(source: any, transform: TypeParser): { parse: () => any[] }; + + } + +} diff --git a/pg/pg.d.ts b/pg/pg.d.ts index bd15066beb..c30fd46603 100644 --- a/pg/pg.d.ts +++ b/pg/pg.d.ts @@ -4,10 +4,12 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// declare module "pg" { import events = require("events"); import stream = require("stream"); + import pgTypes = require("pg-types"); 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; @@ -34,13 +36,13 @@ declare module "pg" { } export interface PoolConfig extends ClientConfig { - // properties from module 'node-pool' - max?: number; - min?: number; - refreshIdle?: boolean; - idleTimeoutMillis?: number; - reapIntervalMillis?: number; - returnToHead?: boolean; + // properties from module 'node-pool' + max?: number; + min?: number; + refreshIdle?: boolean; + idleTimeoutMillis?: number; + reapIntervalMillis?: number; + returnToHead?: boolean; } export interface QueryConfig { @@ -90,7 +92,7 @@ declare module "pg" { constructor(connection: string); constructor(config: ClientConfig); - connect(callback?: (err:Error) => void): void; + connect(callback?: (err: Error) => void): void; end(): void; release(): void; @@ -126,7 +128,6 @@ declare module "pg" { public on(event: string, listener: Function): this; } - export namespace types { - function setTypeParser(typeId: number, parser: (value: string) => T): void; - } + export const types: typeof pgTypes; + }