Skip to content

Commit

Permalink
updating the TypeScripts
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly-t committed Jul 10, 2017
1 parent e28b027 commit 85ac602
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "pg-promise",
"version": "6.3.2",
"version": "6.3.3",
"description": "Promises interface for PostgreSQL",
"main": "lib/index.js",
"typings": "typescript/pg-promise.d.ts",
Expand Down Expand Up @@ -50,7 +50,7 @@
"JSONStream": "1.3",
"bluebird": "3.5",
"coveralls": "2.11",
"eslint": "^4.1.1",
"eslint": "^4.2.0",
"istanbul": "0.4",
"jasmine-node": "1.14",
"jsdoc": "3.4",
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/connection.ts
Expand Up @@ -3,7 +3,7 @@ import * as pgPromise from '../../typescript/pg-promise';
const pgp: pgPromise.IMain = pgPromise();
const db = pgp('connection');

type t = pgPromise.IConfig;
type t = pgPromise.TConfig;
let r: t;
r.application_name = 'hello';
r.ssl = {
Expand Down
14 changes: 10 additions & 4 deletions test/typescript/init.ts
@@ -1,13 +1,13 @@
import * as pgPromise from '../../typescript/pg-promise';
import {IConnectionParameters} from "../../typescript/pg-subset";
import {TConnectionParameters} from "../../typescript/pg-subset";

const pgp: pgPromise.IMain = pgPromise({
capSQL: true,
pgFormatting: true,
pgNative: true
});

let c: pgPromise.IConfig;
let c: pgPromise.TConfig;
c.binary = true;

const spex = pgp.spex;
Expand All @@ -18,10 +18,16 @@ interface Test {
hello: string;
}

const db = <pgPromise.IDatabase<Test> & Test>pgp('connection');
const db = <pgPromise.IDatabase<Test> & Test>pgp({
isDomainSocket: true,
poolSize: 20,
min: 0,
max: 20,
application_name: 'my-app'
});

const connection1: string = <string>db.$cn;
const connection2: IConnectionParameters = <IConnectionParameters>db.$cn;
const connection2: TConnectionParameters = <TConnectionParameters>db.$cn;

const context: any = db.$dc;
const pool: any = db.$pool;
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/types.ts
Expand Up @@ -23,7 +23,7 @@ import {
QueryFile,
PromiseAdapter,
IDatabase,
IConfig,
TConfig,
IMain,
ITask,
IBaseProtocol,
Expand Down
30 changes: 16 additions & 14 deletions typescript/README.MD
@@ -1,6 +1,6 @@
## TypeScript for pg-promise

Complete TypeScript 2.0 declarations for the [pg-promise] module.
Complete TypeScript 2.x declarations for the [pg-promise] module.

### Inclusion

Expand All @@ -12,13 +12,13 @@ Typescript should be able to pick up the definitions without any manual configur
import {IMain, IDatabase} from 'pg-promise';
import * as pgPromise from 'pg-promise';

var pgp:IMain = pgPromise({
const pgp:IMain = pgPromise({
// Initialization Options
});

var cn:string = 'postgres://username:password@host:port/database';
const cn:string = 'postgres://username:password@host:port/database';

var db:IDatabase<any> = pgp(cn);
const db:IDatabase<any> = pgp(cn);
```

Example of compiling into JavaScript:
Expand Down Expand Up @@ -47,40 +47,42 @@ interface IExtensions {
}

// pg-promise initialization options:
var options = {
const options = {
extend: obj => {
obj.findUser = userId => {
return obj.one('SELECT * FROM Users WHERE id=$1', userId);
return obj.one('SELECT * FROM Users WHERE id = $1', userId);
}
}
};

var cn = 'postgres://username:password@host:port/database';
const cn = 'postgres://username:password@host:port/database';

// initializing the library:
var pgp: IMain = pgPromise(options);
const pgp: IMain = pgPromise(options);

// database object with extensions:
var db = <IDatabase<IExtensions>&IExtensions>pgp(cn);
const db = <IDatabase<IExtensions>&IExtensions>pgp(cn);

// now you can use the extensions everywhere (including tasks and transactions):
db.findUser(123).then(...);
```

Alternatively, you can create the extended `db` object like this:

```ts
var db = pgp<IExtensions>(cn);
const db = pgp<IExtensions>(cn);
```

The result is identical, but some IntelliSense implementations may struggle to inspect such type correctly.

And if you want strict initialization options, with inline type casting, you can do this:

```ts
// extended initialization:
var pgp = pgPromise<IExtensions>({
extend: obj=> {
obj.findUser = userId=> {
return obj.one('SELECT * FROM Users WHERE id=$1', userId);
const pgp = pgPromise<IExtensions>({
extend: obj => {
obj.findUser = userId => {
return obj.one('SELECT * FROM Users WHERE id = $1', userId);
}
}
});
Expand Down
10 changes: 5 additions & 5 deletions typescript/pg-promise.d.ts
@@ -1,5 +1,5 @@
////////////////////////////////////////
// Requires pg-promise v6.2.1 or later.
// Requires pg-promise v6.3.3 or later.
////////////////////////////////////////

import * as XPromise from './ext-promise'; // External Promise Provider
Expand Down Expand Up @@ -321,7 +321,7 @@ declare namespace pgPromise {
readonly $config: ILibConfig<Ext>

// API: http://vitaly-t.github.io/pg-promise/Database.html#$cn
readonly $cn: string | IConfig
readonly $cn: string | TConfig

// API: http://vitaly-t.github.io/pg-promise/Database.html#$dc
readonly $dc: any
Expand All @@ -331,14 +331,14 @@ declare namespace pgPromise {
readonly $pool: any
}

type IConfig = pg.IConnectionParameters
type TConfig = pg.TConnectionParameters

// Post-initialization interface;
// API: http://vitaly-t.github.io/pg-promise/module-pg-promise.html
interface IMain {
(cn: string | IConfig, dc?: any): IDatabase<IEmptyExt>
(cn: string | TConfig, dc?: any): IDatabase<IEmptyExt>

<T>(cn: string | IConfig, dc?: any): IDatabase<T> & T
<T>(cn: string | TConfig, dc?: any): IDatabase<T> & T

readonly PromiseAdapter: typeof PromiseAdapter
readonly PreparedStatement: typeof PreparedStatement
Expand Down
30 changes: 21 additions & 9 deletions typescript/pg-subset.d.ts
Expand Up @@ -4,7 +4,7 @@
// Calling it 'pg-subset' to avoid a conflict in case the application also
// includes the official 'pg' typings.
//
// Supported version of pg: 4.3.0 and later.
// Supported version of pg: 6.3.0 and later.
//
// pg: https://github.com/brianc/node-postgres
//////////////////////////////////////////////////////////////////////////////
Expand All @@ -18,7 +18,7 @@ declare namespace pg {
name: string,
dataTypeID: number,

// properties below are not available within Native Bindings:
// NOTE: properties below are not available within Native Bindings:

tableID: number,
columnID: number,
Expand All @@ -43,7 +43,7 @@ declare namespace pg {
// SSL configuration;
// For property types and documentation see:
// http://nodejs.org/api/tls.html#tls_tls_connect_options_callback
interface ISSLConfig {
type TSSLConfig = {
ca?: string | string[] | Buffer | Buffer[];
pfx?: string | Buffer;
cert?: string | string[] | Buffer | Buffer[];
Expand All @@ -53,18 +53,30 @@ declare namespace pg {
NPNProtocols?: string[] | Buffer;
}

interface IConnectionParameters {
// See:
// 1) https://github.com/brianc/node-postgres/blob/master/lib/defaults.js
// 2) https://github.com/brianc/node-pg-pool
type TConnectionParameters = {
database?: string;
user?: string;
password?: string;
port?: number;
host?: string;
ssl?: boolean | ISSLConfig;
ssl?: boolean | TSSLConfig;
binary?: boolean;
client_encoding?: string;
application_name?: string;
fallback_application_name?: string;
isDomainSocket?: boolean;
poolSize?: number; // is the same as `max` below
max?: number; // replaces `poolSize`
min?: number;
poolIdleTimeout?: number;
reapIntervalMillis?: number;
returnToHead?: boolean;
poolLog?: boolean | (() => void);
parseInputDatesAsUTC?: boolean;
rows?: number;
}

// Interface of 'pg-types' module;
Expand Down Expand Up @@ -115,7 +127,7 @@ declare namespace pg {

client_encoding: string,

ssl: boolean | ISSLConfig,
ssl: boolean | TSSLConfig,

application_name?: string,

Expand All @@ -134,7 +146,7 @@ declare namespace pg {

class Client extends EventEmitter {

constructor(cn: string | IConnectionParameters);
constructor(cn: string | TConnectionParameters);

query: (config: any, values: any, callback: (err: Error, result: IResult) => void) => Query;

Expand All @@ -144,7 +156,7 @@ declare namespace pg {
on(event: 'notice', listener: (message: any) => void): this;
on(event: string, listener: Function): this;

connectionParameters: IConnectionParameters;
connectionParameters: TConnectionParameters;
database: string;
user: string;
password: string;
Expand All @@ -155,7 +167,7 @@ declare namespace pg {

queryQueue: Array<Query>;
binary: boolean;
ssl: boolean | ISSLConfig;
ssl: boolean | TSSLConfig;
secretKey: number;
processID: number;
encoding: string;
Expand Down

0 comments on commit 85ac602

Please sign in to comment.