Skip to content

Commit b98bee8

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

File tree

18 files changed

+357
-2
lines changed

18 files changed

+357
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
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:
Lines changed: 37 additions & 0 deletions
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 };
Lines changed: 10 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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+
}

examples/typescript/single-db/app.ts

Lines changed: 23 additions & 0 deletions
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 };
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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+
}
Lines changed: 51 additions & 0 deletions
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 };
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 6 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)