diff --git a/README.md b/README.md index 6122698..5b5ac1b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/typescript/multiple-db/fastify.d.ts b/examples/typescript/multiple-db/fastify.d.ts deleted file mode 100644 index 444524d..0000000 --- a/examples/typescript/multiple-db/fastify.d.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { PostgresDb } from '../../../index'; - -declare module 'fastify' { - export interface FastifyInstance { - pg: { - sum: PostgresDb; - sub: PostgresDb; - }; - } -} diff --git a/examples/typescript/single-db/fastify.d.ts b/examples/typescript/single-db/fastify.d.ts deleted file mode 100644 index 8c72cc7..0000000 --- a/examples/typescript/single-db/fastify.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { PostgresDb } from '../../../index'; - -declare module 'fastify' { - export interface FastifyInstance { - pg: PostgresDb; - } -} diff --git a/examples/typescript/transactions/fastify.d.ts b/examples/typescript/transactions/fastify.d.ts deleted file mode 100644 index 8c72cc7..0000000 --- a/examples/typescript/transactions/fastify.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { PostgresDb } from '../../../index'; - -declare module 'fastify' { - export interface FastifyInstance { - pg: PostgresDb; - } -} diff --git a/index.d.ts b/index.d.ts index 174af26..8dacfdc 100644 --- a/index.d.ts +++ b/index.d.ts @@ -37,5 +37,11 @@ type PostgresPluginOptions = { declare const fastifyPostgres: FastifyPluginCallback; +declare module 'fastify' { + export interface FastifyInstance { + pg: PostgresDb & Record; + } +} + export { fastifyPostgres, PostgresDb, PostgresPluginOptions }; export default fastifyPostgres; diff --git a/test/types/initialization.test-d.ts b/test/types/initialization.test-d.ts index 0c3dc36..4063a77 100644 --- a/test/types/initialization.test-d.ts +++ b/test/types/initialization.test-d.ts @@ -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(); @@ -30,3 +31,9 @@ app.register(fastifyPostgres, { app.register(fastifyPostgres, { connectionString: 'postgres://user:password@host:port/db', }); + +// Plugin property available +app.after(() => { + expectAssignable(app.pg); + expectType(app.pg.users); +}); diff --git a/test/types/query.test-d.ts b/test/types/query.test-d.ts index abcad5a..d4cc201 100644 --- a/test/types/query.test-d.ts +++ b/test/types/query.test-d.ts @@ -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'; @@ -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(app.pg); + expectAssignable(app.pg); expectType(app.pg.pool); expectType(app.pg.Client); diff --git a/test/types/transaction.test-d.ts b/test/types/transaction.test-d.ts index 3bc6ccf..a48841a 100644 --- a/test/types/transaction.test-d.ts +++ b/test/types/transaction.test-d.ts @@ -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)