Skip to content

Types: Actually add "pg" property to FastifyInstance (semi-breaking) #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,6 @@ Install the compiler and typings for pg module:
npm install --save-dev typescript @types/pg
```

Add the `pg` property to `FastifyInstance`. Like this if you use an unnamed instance:

```typescript
import type { PostgresDb } from 'fastify-postgres';

declare module 'fastify' {
export interface FastifyInstance {
pg: PostgresDb;
}
}
```

More examples in the [examples/typescript](./examples/typescript) directory.

## Development and Testing
Expand Down
10 changes: 0 additions & 10 deletions examples/typescript/multiple-db/fastify.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions examples/typescript/single-db/fastify.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions examples/typescript/transactions/fastify.d.ts

This file was deleted.

6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ type PostgresPluginOptions = {

declare const fastifyPostgres: FastifyPluginCallback<PostgresPluginOptions>;

declare module 'fastify' {
export interface FastifyInstance {
pg: PostgresDb & Record<string, PostgresDb>;
}
}

export { fastifyPostgres, PostgresDb, PostgresPluginOptions };
export default fastifyPostgres;
9 changes: 8 additions & 1 deletion test/types/initialization.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import fastify from 'fastify';
import * as pg from 'pg';
import { expectAssignable, expectType } from 'tsd';

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

const app = fastify();

Expand Down Expand Up @@ -30,3 +31,9 @@ app.register(fastifyPostgres, {
app.register(fastifyPostgres, {
connectionString: 'postgres://user:password@host:port/db',
});

// Plugin property available
app.after(() => {
expectAssignable<PostgresDb>(app.pg);
expectType<PostgresDb>(app.pg.users);
});
10 changes: 2 additions & 8 deletions test/types/query.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastify from 'fastify';
import { Client, Pool, PoolClient, QueryResult } from 'pg';
import { expectType } from 'tsd';
import { expectAssignable, expectType } from 'tsd';

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

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

declare module 'fastify' {
export interface FastifyInstance {
pg: PostgresDb;
}
}

app.get('/calc', async () => {
expectType<PostgresDb>(app.pg);
expectAssignable<PostgresDb>(app.pg);

expectType<Pool>(app.pg.pool);
expectType<Client>(app.pg.Client);
Expand Down
6 changes: 0 additions & 6 deletions test/types/transaction.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ app.register(fastifyPostgres, {
connectionString: 'postgres://user:password@host:port/db',
});

declare module 'fastify' {
export interface FastifyInstance {
pg: PostgresDb;
}
}

app.post('/insert-async', async () => {
const insertQuery = `
INSERT INTO routes(name)
Expand Down