Skip to content

Commit a45dd9e

Browse files
authored
BREAKING: Add "pg" fastify property type (#69)
Follow up to #68
1 parent a01d184 commit a45dd9e

File tree

8 files changed

+16
-51
lines changed

8 files changed

+16
-51
lines changed

README.md

-12
Original file line numberDiff line numberDiff line change
@@ -238,18 +238,6 @@ Install the compiler and typings for pg module:
238238
npm install --save-dev typescript @types/pg
239239
```
240240

241-
Add the `pg` property to `FastifyInstance`. Like this if you use an unnamed instance:
242-
243-
```typescript
244-
import type { PostgresDb } from 'fastify-postgres';
245-
246-
declare module 'fastify' {
247-
export interface FastifyInstance {
248-
pg: PostgresDb;
249-
}
250-
}
251-
```
252-
253241
More examples in the [examples/typescript](./examples/typescript) directory.
254242

255243
## Development and Testing

examples/typescript/multiple-db/fastify.d.ts

-10
This file was deleted.

examples/typescript/single-db/fastify.d.ts

-7
This file was deleted.

examples/typescript/transactions/fastify.d.ts

-7
This file was deleted.

index.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ type PostgresPluginOptions = {
3737

3838
declare const fastifyPostgres: FastifyPluginCallback<PostgresPluginOptions>;
3939

40+
declare module 'fastify' {
41+
export interface FastifyInstance {
42+
pg: PostgresDb & Record<string, PostgresDb>;
43+
}
44+
}
45+
4046
export { fastifyPostgres, PostgresDb, PostgresPluginOptions };
4147
export default fastifyPostgres;

test/types/initialization.test-d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import fastify from 'fastify';
22
import * as pg from 'pg';
3+
import { expectAssignable, expectType } from 'tsd';
34

4-
import fastifyPostgres from '../../index';
5+
import fastifyPostgres, { PostgresDb } from '../../index';
56

67
const app = fastify();
78

@@ -30,3 +31,9 @@ app.register(fastifyPostgres, {
3031
app.register(fastifyPostgres, {
3132
connectionString: 'postgres://user:password@host:port/db',
3233
});
34+
35+
// Plugin property available
36+
app.after(() => {
37+
expectAssignable<PostgresDb>(app.pg);
38+
expectType<PostgresDb>(app.pg.users);
39+
});

test/types/query.test-d.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fastify from 'fastify';
22
import { Client, Pool, PoolClient, QueryResult } from 'pg';
3-
import { expectType } from 'tsd';
3+
import { expectAssignable, expectType } from 'tsd';
44

55
import fastifyPostgres, { PostgresDb } from '../../index';
66

@@ -10,14 +10,8 @@ app.register(fastifyPostgres, {
1010
connectionString: 'postgres://user:password@host:port/db',
1111
});
1212

13-
declare module 'fastify' {
14-
export interface FastifyInstance {
15-
pg: PostgresDb;
16-
}
17-
}
18-
1913
app.get('/calc', async () => {
20-
expectType<PostgresDb>(app.pg);
14+
expectAssignable<PostgresDb>(app.pg);
2115

2216
expectType<Pool>(app.pg.pool);
2317
expectType<Client>(app.pg.Client);

test/types/transaction.test-d.ts

-6
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ app.register(fastifyPostgres, {
1010
connectionString: 'postgres://user:password@host:port/db',
1111
});
1212

13-
declare module 'fastify' {
14-
export interface FastifyInstance {
15-
pg: PostgresDb;
16-
}
17-
}
18-
1913
app.post('/insert-async', async () => {
2014
const insertQuery = `
2115
INSERT INTO routes(name)

0 commit comments

Comments
 (0)