Skip to content

Commit b98bee8

Browse files
author
Evgeny Markov
authored
Add TypeScript typings (#63)
* feat: add typescript typings
1 parent 7ad652f commit b98bee8

18 files changed

+357
-2
lines changed

Diff for: README.md

+10
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,16 @@ fastify.listen(3000, err => {
203203
})
204204
```
205205

206+
## TypeScript Usage
207+
208+
Install the compiler and typings for pg module:
209+
210+
```shell script
211+
npm install --save-dev typescript @types/pg
212+
```
213+
214+
You can find examples in the [examples/typescript](./examples/typescript) directory.
215+
206216
## Development and Testing
207217

208218
First, start postgres with:

Diff for: examples/typescript/multiple-db/app.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import fastify from 'fastify';
2+
3+
import { fastifyPostgres } from '../../../index';
4+
5+
const app = fastify();
6+
7+
app.register(fastifyPostgres, {
8+
name: 'sum',
9+
connectionString: 'postgres://user:password@host:port/sub-db',
10+
});
11+
12+
app.register(fastifyPostgres, {
13+
name: 'sub',
14+
connectionString: 'postgres://user:password@host:port/sub-db',
15+
});
16+
17+
app.get('/calc', async () => {
18+
const sumClient = await app.pg.sum.connect();
19+
const subClient = await app.pg.sub.connect();
20+
21+
const sumResult = await sumClient.query<{ sum: number }>(
22+
'SELECT 2 + 2 as sum'
23+
);
24+
const subResult = await subClient.query<{ sub: number }>(
25+
'SELECT 6 - 3 as sub'
26+
);
27+
28+
sumClient.release();
29+
subClient.release();
30+
31+
return {
32+
sum: sumResult.rows,
33+
sub: subResult.rows,
34+
};
35+
});
36+
37+
export { app };

Diff for: examples/typescript/multiple-db/fastify.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { PostgresDb } from '../../../index';
2+
3+
declare module 'fastify' {
4+
export interface FastifyInstance {
5+
pg: {
6+
sum: PostgresDb;
7+
sub: PostgresDb;
8+
};
9+
}
10+
}

Diff for: examples/typescript/multiple-db/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@tsconfig/node10/tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
}
6+
}

Diff for: examples/typescript/single-db/app.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fastify from 'fastify';
2+
3+
import { fastifyPostgres } from '../../../index';
4+
5+
const app = fastify();
6+
7+
app.register(fastifyPostgres, {
8+
connectionString: 'postgres://user:password@host:port/db',
9+
});
10+
11+
app.get('/calc', async () => {
12+
const client = await app.pg.connect();
13+
14+
const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum');
15+
16+
client.release();
17+
18+
return {
19+
sum: sumResult.rows,
20+
};
21+
});
22+
23+
export { app };

Diff for: examples/typescript/single-db/fastify.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { PostgresDb } from '../../../index';
2+
3+
declare module 'fastify' {
4+
export interface FastifyInstance {
5+
pg: PostgresDb;
6+
}
7+
}

Diff for: examples/typescript/single-db/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@tsconfig/node10/tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
}
6+
}

Diff for: examples/typescript/transactions/app.ts

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import fastify from 'fastify';
2+
3+
import { fastifyPostgres } from '../../../index';
4+
5+
const app = fastify();
6+
7+
app.register(fastifyPostgres, {
8+
connectionString: 'postgres://user:password@host:port/db',
9+
});
10+
11+
app.post('/init-async', async () => {
12+
const createTableQuery = `
13+
CREATE TABLE routes (
14+
id bigserial primary key,
15+
name varchar(80) NOT NULL,
16+
created_at timestamp default NULL
17+
);
18+
`;
19+
20+
return app.pg.transact(async (client) => {
21+
const result = await client.query(createTableQuery);
22+
23+
return result;
24+
});
25+
});
26+
27+
app.post('/init-cb', (_req, reply) => {
28+
const createTableQuery = `
29+
CREATE TABLE routes (
30+
id bigserial primary key,
31+
name varchar(80) NOT NULL,
32+
created_at timestamp default NULL
33+
);
34+
`;
35+
36+
app.pg.transact(
37+
(client) => {
38+
return client.query(createTableQuery);
39+
},
40+
(error, result) => {
41+
if (error) {
42+
reply.status(500).send(error);
43+
return;
44+
}
45+
46+
reply.status(200).send(result);
47+
}
48+
);
49+
});
50+
51+
export { app };

Diff for: examples/typescript/transactions/fastify.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { PostgresDb } from '../../../index';
2+
3+
declare module 'fastify' {
4+
export interface FastifyInstance {
5+
pg: PostgresDb;
6+
}
7+
}

Diff for: examples/typescript/transactions/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "@tsconfig/node10/tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
}
6+
}

Diff for: index.d.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { FastifyPluginCallback } from 'fastify';
2+
import * as Pg from 'pg';
3+
4+
declare function transact<TResult>(
5+
fn: (client: Pg.PoolClient) => Promise<TResult>
6+
): Promise<TResult>;
7+
8+
declare function transact<TResult>(
9+
fn: (client: Pg.PoolClient) => Promise<TResult>,
10+
cb: (error: Error | null, result?: TResult) => void
11+
): void;
12+
13+
type PostgresDb = {
14+
pool: Pg.Pool;
15+
Client: Pg.Client;
16+
query: Pg.Pool['query'];
17+
connect: Pg.Pool['connect'];
18+
transact: typeof transact;
19+
};
20+
21+
type PostgresPluginOptions = {
22+
/**
23+
* Custom pg
24+
*/
25+
pg?: typeof Pg;
26+
27+
/**
28+
* Use pg-native
29+
*/
30+
native?: boolean;
31+
32+
/**
33+
* Instance name of fastify-postgres
34+
*/
35+
name?: string;
36+
} & Pg.PoolConfig;
37+
38+
declare const fastifyPostgres: FastifyPluginCallback<PostgresPluginOptions>;
39+
40+
export { fastifyPostgres, PostgresDb, PostgresPluginOptions };
41+
export default fastifyPostgres;

Diff for: index.js

+2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ function transact (fn, cb) {
5454

5555
function fastifyPostgres (fastify, options, next) {
5656
let pg = defaultPg
57+
5758
if (options.pg) {
5859
pg = options.pg
5960
}
61+
6062
if (options.native) {
6163
delete options.native
6264
if (!pg.native) {

Diff for: package.json

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
"version": "3.0.1",
44
"description": "Fastify PostgreSQL connection plugin",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
7-
"test": "standard && tap -J test/*.test.js",
8+
"test": "standard && tap -J test/*.test.js && npm run test:typescript",
9+
"test:typescript": "tsd",
810
"test:report": "standard && tap -J --coverage-report=html test/*.test.js",
911
"test:verbose": "standard && tap -J test/*.test.js -Rspec",
12+
"check-examples": "tsc --build examples/typescript/*",
1013
"postgres": "docker run -p 5432:5432 --name fastify-postgres -e POSTGRES_PASSWORD=postgres -d postgres:11-alpine",
1114
"load-data": "docker exec -it fastify-postgres psql -c 'CREATE TABLE users(id serial PRIMARY KEY, username VARCHAR (50) NOT NULL);' -U postgres -d postgres"
1215
},
@@ -32,13 +35,20 @@
3235
"fastify-plugin": "^2.0.0"
3336
},
3437
"devDependencies": {
38+
"@tsconfig/node10": "^1.0.7",
39+
"@types/pg": "^7.14.4",
3540
"fastify": "^3.0.0",
3641
"pg": "^8.2.1",
3742
"pg-native": "^3.0.0",
3843
"standard": "^14.0.0",
39-
"tap": "^14.10.7"
44+
"tap": "^14.10.7",
45+
"tsd": "^0.13.1",
46+
"typescript": "^4.0.2"
4047
},
4148
"peerDependencies": {
4249
"pg": ">=6.0.0"
50+
},
51+
"tsd": {
52+
"directory": "test/types"
4353
}
4454
}

Diff for: test/types/imports.test-d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import defaultPluginImport from '../../index';
2+
import {
3+
fastifyPostgres as namedPluginImport,
4+
PostgresDb,
5+
PostgresPluginOptions,
6+
} from '../../index';

Diff for: test/types/initialization.test-d.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import fastify from 'fastify';
2+
import * as pg from 'pg';
3+
4+
import fastifyPostgres from '../../index';
5+
6+
const app = fastify();
7+
8+
// Without parameters
9+
app.register(fastifyPostgres);
10+
app.register(fastifyPostgres, {});
11+
12+
// Own pg adapter
13+
app.register(fastifyPostgres, { pg });
14+
15+
// Native libpq wrapper
16+
app.register(fastifyPostgres, { native: true });
17+
18+
// Multiple databases
19+
app.register(fastifyPostgres, { name: 'users' });
20+
app.register(fastifyPostgres, { name: 'posts' });
21+
22+
// Pool options
23+
app.register(fastifyPostgres, {
24+
user: 'dbuser',
25+
host: 'database.server.com',
26+
database: 'mydb',
27+
password: 'secretpassword',
28+
port: 3211,
29+
});
30+
app.register(fastifyPostgres, {
31+
connectionString: 'postgres://user:password@host:port/db',
32+
});

Diff for: test/types/query.test-d.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import fastify from 'fastify';
2+
import { Client, Pool, PoolClient, QueryResult } from 'pg';
3+
import { expectType } from 'tsd';
4+
5+
import fastifyPostgres, { PostgresDb } from '../../index';
6+
7+
const app = fastify();
8+
9+
app.register(fastifyPostgres, {
10+
connectionString: 'postgres://user:password@host:port/db',
11+
});
12+
13+
declare module 'fastify' {
14+
export interface FastifyInstance {
15+
pg: PostgresDb;
16+
}
17+
}
18+
19+
app.get('/calc', async () => {
20+
expectType<PostgresDb>(app.pg);
21+
22+
expectType<Pool>(app.pg.pool);
23+
expectType<Client>(app.pg.Client);
24+
25+
const client = await app.pg.connect();
26+
expectType<PoolClient>(client);
27+
28+
const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum');
29+
expectType<QueryResult<{ sum: number }>>(sumResult);
30+
31+
client.release();
32+
33+
return {
34+
sum: sumResult.rows,
35+
};
36+
});

0 commit comments

Comments
 (0)