From a1a1c69bcdf64e234a1b5a904e7a43d7c46175d9 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 22 Aug 2020 02:14:46 +0300 Subject: [PATCH 01/13] feat: add typescript typings --- README.md | 10 ++++ examples/typescript/multiple-db/app.ts | 37 ++++++++++++++ examples/typescript/multiple-db/fastify.d.ts | 10 ++++ examples/typescript/multiple-db/tsconfig.json | 22 ++++++++ examples/typescript/single-db/app.ts | 23 +++++++++ examples/typescript/single-db/fastify.d.ts | 7 +++ examples/typescript/single-db/tsconfig.json | 22 ++++++++ examples/typescript/transactions/app.ts | 51 +++++++++++++++++++ examples/typescript/transactions/fastify.d.ts | 7 +++ .../typescript/transactions/tsconfig.json | 22 ++++++++ index.d.ts | 51 +++++++++++++++++++ index.js | 2 + package.json | 12 ++++- tools/build-examples.sh | 7 +++ tsconfig.json | 14 +++++ 15 files changed, 295 insertions(+), 2 deletions(-) create mode 100644 examples/typescript/multiple-db/app.ts create mode 100644 examples/typescript/multiple-db/fastify.d.ts create mode 100644 examples/typescript/multiple-db/tsconfig.json create mode 100644 examples/typescript/single-db/app.ts create mode 100644 examples/typescript/single-db/fastify.d.ts create mode 100644 examples/typescript/single-db/tsconfig.json create mode 100644 examples/typescript/transactions/app.ts create mode 100644 examples/typescript/transactions/fastify.d.ts create mode 100644 examples/typescript/transactions/tsconfig.json create mode 100644 index.d.ts create mode 100755 tools/build-examples.sh create mode 100644 tsconfig.json diff --git a/README.md b/README.md index 231d1bc..d95d644 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,16 @@ fastify.listen(3000, err => { }) ``` +## TypeScript Usage + +Install the compiler and typings for pg module: + +```shell script +npm install --save-dev typescript @types/pg +``` + +You can find examples in the [examples/typescript](./examples/typescript) directory. + ## Development and Testing First, start postgres with: diff --git a/examples/typescript/multiple-db/app.ts b/examples/typescript/multiple-db/app.ts new file mode 100644 index 0000000..8b5b2ab --- /dev/null +++ b/examples/typescript/multiple-db/app.ts @@ -0,0 +1,37 @@ +import fastify from 'fastify'; + +import fastifyPostgres from '../../../index'; + +const app = fastify(); + +app.register(fastifyPostgres, { + name: 'sum', + connectionString: 'postgres://user:password@host:port/sub-db', +}); + +app.register(fastifyPostgres, { + name: 'sub', + connectionString: 'postgres://user:password@host:port/sub-db', +}); + +app.get('/calc', async () => { + const sumClient = await app.pg.sum.connect(); + const subClient = await app.pg.sub.connect(); + + const sumResult = await sumClient.query<{ sum: number }>( + 'SELECT 2 + 2 as sum' + ); + const subResult = await subClient.query<{ sub: number }>( + 'SELECT 6 - 3 as sub' + ); + + sumClient.release(); + subClient.release(); + + return { + sum: sumResult.rows, + sub: subResult.rows, + }; +}); + +export { app }; diff --git a/examples/typescript/multiple-db/fastify.d.ts b/examples/typescript/multiple-db/fastify.d.ts new file mode 100644 index 0000000..444524d --- /dev/null +++ b/examples/typescript/multiple-db/fastify.d.ts @@ -0,0 +1,10 @@ +import type { PostgresDb } from '../../../index'; + +declare module 'fastify' { + export interface FastifyInstance { + pg: { + sum: PostgresDb; + sub: PostgresDb; + }; + } +} diff --git a/examples/typescript/multiple-db/tsconfig.json b/examples/typescript/multiple-db/tsconfig.json new file mode 100644 index 0000000..e84c55b --- /dev/null +++ b/examples/typescript/multiple-db/tsconfig.json @@ -0,0 +1,22 @@ +{ + "include": ["."], + "compilerOptions": { + /* Basic Options */ + "noEmit": true, + "lib": ["ES2020"], + + /* Module Resolution Options */ + "esModuleInterop": true, + "resolveJsonModule": true, + "moduleResolution": "Node", + + /* Strict Type-Checking Options */ + "strict": true, + + /* Additional Checks */ + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + } +} diff --git a/examples/typescript/single-db/app.ts b/examples/typescript/single-db/app.ts new file mode 100644 index 0000000..b6940f9 --- /dev/null +++ b/examples/typescript/single-db/app.ts @@ -0,0 +1,23 @@ +import fastify from 'fastify'; + +import fastifyPostgres from '../../../index'; + +const app = fastify(); + +app.register(fastifyPostgres, { + connectionString: 'postgres://user:password@host:port/db', +}); + +app.get('/calc', async () => { + const client = await app.pg.connect(); + + const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum'); + + client.release(); + + return { + sum: sumResult.rows, + }; +}); + +export { app }; diff --git a/examples/typescript/single-db/fastify.d.ts b/examples/typescript/single-db/fastify.d.ts new file mode 100644 index 0000000..8c72cc7 --- /dev/null +++ b/examples/typescript/single-db/fastify.d.ts @@ -0,0 +1,7 @@ +import type { PostgresDb } from '../../../index'; + +declare module 'fastify' { + export interface FastifyInstance { + pg: PostgresDb; + } +} diff --git a/examples/typescript/single-db/tsconfig.json b/examples/typescript/single-db/tsconfig.json new file mode 100644 index 0000000..e84c55b --- /dev/null +++ b/examples/typescript/single-db/tsconfig.json @@ -0,0 +1,22 @@ +{ + "include": ["."], + "compilerOptions": { + /* Basic Options */ + "noEmit": true, + "lib": ["ES2020"], + + /* Module Resolution Options */ + "esModuleInterop": true, + "resolveJsonModule": true, + "moduleResolution": "Node", + + /* Strict Type-Checking Options */ + "strict": true, + + /* Additional Checks */ + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + } +} diff --git a/examples/typescript/transactions/app.ts b/examples/typescript/transactions/app.ts new file mode 100644 index 0000000..5690483 --- /dev/null +++ b/examples/typescript/transactions/app.ts @@ -0,0 +1,51 @@ +import fastify from 'fastify'; + +import fastifyPostgres from '../../../index'; + +const app = fastify(); + +app.register(fastifyPostgres, { + connectionString: 'postgres://user:password@host:port/db', +}); + +app.post('/init-async', async () => { + const createTableQuery = ` + CREATE TABLE routes ( + id bigserial primary key, + name varchar(80) NOT NULL, + created_at timestamp default NULL + ); + `; + + return app.pg.transact(async (client) => { + const result = await client.query(createTableQuery); + + return result; + }); +}); + +app.post('/init-cb', (_req, reply) => { + const createTableQuery = ` + CREATE TABLE routes ( + id bigserial primary key, + name varchar(80) NOT NULL, + created_at timestamp default NULL + ); + `; + + app.pg.transact( + (client) => { + return client.query(createTableQuery); + }, + (error, result) => { + if (error) { + reply.status(500).send(error); + return; + } + + reply.status(200).send(result); + } + ); +}); + +export { app }; diff --git a/examples/typescript/transactions/fastify.d.ts b/examples/typescript/transactions/fastify.d.ts new file mode 100644 index 0000000..8c72cc7 --- /dev/null +++ b/examples/typescript/transactions/fastify.d.ts @@ -0,0 +1,7 @@ +import type { PostgresDb } from '../../../index'; + +declare module 'fastify' { + export interface FastifyInstance { + pg: PostgresDb; + } +} diff --git a/examples/typescript/transactions/tsconfig.json b/examples/typescript/transactions/tsconfig.json new file mode 100644 index 0000000..e84c55b --- /dev/null +++ b/examples/typescript/transactions/tsconfig.json @@ -0,0 +1,22 @@ +{ + "include": ["."], + "compilerOptions": { + /* Basic Options */ + "noEmit": true, + "lib": ["ES2020"], + + /* Module Resolution Options */ + "esModuleInterop": true, + "resolveJsonModule": true, + "moduleResolution": "Node", + + /* Strict Type-Checking Options */ + "strict": true, + + /* Additional Checks */ + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + } +} diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..8c277ca --- /dev/null +++ b/index.d.ts @@ -0,0 +1,51 @@ +import type { FastifyPluginCallback } from 'fastify'; +import type PgAdapter from 'pg'; +import type { + Client as PgClient, + Pool as PgPool, + PoolClient as PgPoolClient, + PoolConfig as PgPoolConfig, +} from 'pg'; + +type CallbackArgs = + | [error: null, result: TResult] + | [error: unknown, result: undefined]; + +declare function transact( + fn: (client: PgPoolClient) => Promise, +): Promise; + +declare function transact( + fn: (client: PgPoolClient) => Promise, + cb: (...args: CallbackArgs) => void, +): void; + +type PostgresDb = { + pool: PgPool; + Client: PgClient; + query: PgPool['query']; + connect: PgPool['connect']; + transact: typeof transact; +}; + +type Options = { + /** + * Custom pg adapter + */ + pg?: typeof PgAdapter; + + /** + * Use pg-native + */ + native?: boolean; + + /** + * Instance name of fastify-postgres + */ + name?: string; +}; + +declare const PostgresPlugin: FastifyPluginCallback; + +export type { PostgresDb }; +export default PostgresPlugin; diff --git a/index.js b/index.js index bc79d75..a6dda6e 100644 --- a/index.js +++ b/index.js @@ -54,9 +54,11 @@ function transact (fn, cb) { function fastifyPostgres (fastify, options, next) { let pg = defaultPg + if (options.pg) { pg = options.pg } + if (options.native) { delete options.native if (!pg.native) { diff --git a/package.json b/package.json index 8c80f12..25ebcdf 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,10 @@ "version": "3.0.1", "description": "Fastify PostgreSQL connection plugin", "main": "index.js", + "types": "index.d.ts", "scripts": { - "test": "standard && tap -J test/*.test.js", + "test": "standard && tap -J test/*.test.js && npm run test:types", + "test:types": "sh tools/build-examples.sh", "test:report": "standard && tap -J --coverage-report=html test/*.test.js", "test:verbose": "standard && tap -J test/*.test.js -Rspec", "postgres": "docker run -p 5432:5432 --name fastify-postgres -e POSTGRES_PASSWORD=postgres -d postgres:11-alpine", @@ -32,13 +34,19 @@ "fastify-plugin": "^2.0.0" }, "devDependencies": { + "@types/pg": "^7.14.4", "fastify": "^3.0.0", "pg": "^8.2.1", "pg-native": "^3.0.0", "standard": "^14.0.0", - "tap": "^14.10.7" + "tap": "^14.10.7", + "typescript": "^4.0.0" }, "peerDependencies": { "pg": ">=6.0.0" + }, + "optionalDependencies": { + "@types/pg": ">=6.0.0", + "typescript": ">=4.0.0" } } diff --git a/tools/build-examples.sh b/tools/build-examples.sh new file mode 100755 index 0000000..eada33b --- /dev/null +++ b/tools/build-examples.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +set -ex + +npx tsc --project ./examples/typescript/single-db/tsconfig.json +npx tsc --project ./examples/typescript/multiple-db/tsconfig.json +npx tsc --project ./examples/typescript/transactions/tsconfig.json diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..b34f04d --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,14 @@ +{ + "include": ["./index.d.ts"], + + "compilerOptions": { + "noEmit": true, + "esModuleInterop": true, + + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + }, +} From 0da50e201e00d393f86787114575d0ca3340f282 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 22 Aug 2020 20:12:19 +0300 Subject: [PATCH 02/13] fix: remove unnecessary named tuple --- index.d.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8c277ca..9878f1c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,17 +7,13 @@ import type { PoolConfig as PgPoolConfig, } from 'pg'; -type CallbackArgs = - | [error: null, result: TResult] - | [error: unknown, result: undefined]; - declare function transact( fn: (client: PgPoolClient) => Promise, ): Promise; declare function transact( fn: (client: PgPoolClient) => Promise, - cb: (...args: CallbackArgs) => void, + cb: (error: Error | null, result?: TResult) => void, ): void; type PostgresDb = { From f2a868e91275732b7cefb5740bfef93e47ac8151 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 22 Aug 2020 20:15:36 +0300 Subject: [PATCH 03/13] fix: export plugin options --- index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 9878f1c..70ea6ac 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,12 +8,12 @@ import type { } from 'pg'; declare function transact( - fn: (client: PgPoolClient) => Promise, + fn: (client: PgPoolClient) => Promise ): Promise; declare function transact( fn: (client: PgPoolClient) => Promise, - cb: (error: Error | null, result?: TResult) => void, + cb: (error: Error | null, result?: TResult) => void ): void; type PostgresDb = { @@ -24,7 +24,7 @@ type PostgresDb = { transact: typeof transact; }; -type Options = { +type PostgresPluginOptions = { /** * Custom pg adapter */ @@ -39,9 +39,9 @@ type Options = { * Instance name of fastify-postgres */ name?: string; -}; +} & PgPoolConfig; -declare const PostgresPlugin: FastifyPluginCallback; +declare const PostgresPlugin: FastifyPluginCallback; -export type { PostgresDb }; +export type { PostgresDb, PostgresPluginOptions }; export default PostgresPlugin; From a2ed13f1fcbcad3369e9d909af05bda5ab87edc6 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sun, 23 Aug 2020 00:40:21 +0300 Subject: [PATCH 04/13] fix: sync typescript configuration with fastify --- index.d.ts | 6 +++--- package.json | 9 +++------ tsconfig.json | 13 +------------ 3 files changed, 7 insertions(+), 21 deletions(-) diff --git a/index.d.ts b/index.d.ts index 70ea6ac..d9b2f99 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,6 +1,6 @@ -import type { FastifyPluginCallback } from 'fastify'; -import type PgAdapter from 'pg'; -import type { +import { FastifyPluginCallback } from 'fastify'; +import * as PgAdapter from 'pg'; +import { Client as PgClient, Pool as PgPool, PoolClient as PgPoolClient, diff --git a/package.json b/package.json index 25ebcdf..9ed44b6 100644 --- a/package.json +++ b/package.json @@ -40,13 +40,10 @@ "pg-native": "^3.0.0", "standard": "^14.0.0", "tap": "^14.10.7", - "typescript": "^4.0.0" + "typescript": "^4.0.2" }, "peerDependencies": { - "pg": ">=6.0.0" - }, - "optionalDependencies": { - "@types/pg": ">=6.0.0", - "typescript": ">=4.0.0" + "pg": ">=6.0.0", + "@types/pg": ">=6.0.0" } } diff --git a/tsconfig.json b/tsconfig.json index b34f04d..534ed56 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,14 +1,3 @@ { - "include": ["./index.d.ts"], - - "compilerOptions": { - "noEmit": true, - "esModuleInterop": true, - - "strict": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - }, + "extends": "fastify/types/tsconfig.json" } From 9e1ca07599e05763c01533d39fd90c985135643e Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sun, 23 Aug 2020 01:07:36 +0300 Subject: [PATCH 05/13] fix: make type checking faster and more portable --- package.json | 4 ++-- tools/build-examples.sh | 7 ------- 2 files changed, 2 insertions(+), 9 deletions(-) delete mode 100755 tools/build-examples.sh diff --git a/package.json b/package.json index 9ed44b6..d446417 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "main": "index.js", "types": "index.d.ts", "scripts": { - "test": "standard && tap -J test/*.test.js && npm run test:types", - "test:types": "sh tools/build-examples.sh", + "test": "standard && tap -J test/*.test.js && npm run test:typescript", + "test:typescript": "tsc --build examples/typescript/*", "test:report": "standard && tap -J --coverage-report=html test/*.test.js", "test:verbose": "standard && tap -J test/*.test.js -Rspec", "postgres": "docker run -p 5432:5432 --name fastify-postgres -e POSTGRES_PASSWORD=postgres -d postgres:11-alpine", diff --git a/tools/build-examples.sh b/tools/build-examples.sh deleted file mode 100755 index eada33b..0000000 --- a/tools/build-examples.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env sh - -set -ex - -npx tsc --project ./examples/typescript/single-db/tsconfig.json -npx tsc --project ./examples/typescript/multiple-db/tsconfig.json -npx tsc --project ./examples/typescript/transactions/tsconfig.json From 4851fbd33abf743443579371e5e8920b5a84a917 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sun, 23 Aug 2020 01:10:54 +0300 Subject: [PATCH 06/13] fix: downgrade typescript target to node 10 in examples --- examples/typescript/multiple-db/tsconfig.json | 4 +++- examples/typescript/single-db/tsconfig.json | 4 +++- examples/typescript/transactions/tsconfig.json | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/examples/typescript/multiple-db/tsconfig.json b/examples/typescript/multiple-db/tsconfig.json index e84c55b..68458c7 100644 --- a/examples/typescript/multiple-db/tsconfig.json +++ b/examples/typescript/multiple-db/tsconfig.json @@ -3,7 +3,9 @@ "compilerOptions": { /* Basic Options */ "noEmit": true, - "lib": ["ES2020"], + "lib": ["ES2018"], + "target": "ES2018", + "module": "CommonJS", /* Module Resolution Options */ "esModuleInterop": true, diff --git a/examples/typescript/single-db/tsconfig.json b/examples/typescript/single-db/tsconfig.json index e84c55b..68458c7 100644 --- a/examples/typescript/single-db/tsconfig.json +++ b/examples/typescript/single-db/tsconfig.json @@ -3,7 +3,9 @@ "compilerOptions": { /* Basic Options */ "noEmit": true, - "lib": ["ES2020"], + "lib": ["ES2018"], + "target": "ES2018", + "module": "CommonJS", /* Module Resolution Options */ "esModuleInterop": true, diff --git a/examples/typescript/transactions/tsconfig.json b/examples/typescript/transactions/tsconfig.json index e84c55b..68458c7 100644 --- a/examples/typescript/transactions/tsconfig.json +++ b/examples/typescript/transactions/tsconfig.json @@ -3,7 +3,9 @@ "compilerOptions": { /* Basic Options */ "noEmit": true, - "lib": ["ES2020"], + "lib": ["ES2018"], + "target": "ES2018", + "module": "CommonJS", /* Module Resolution Options */ "esModuleInterop": true, From 10eb8d5c2aeaef432215ad876b36b9ad2ac2f1d9 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sun, 23 Aug 2020 03:35:29 +0300 Subject: [PATCH 07/13] test: add tests for typings --- package.json | 6 ++- tsconfig.json => test/tsconfig.json | 0 test/types/initialization.test-d.ts | 32 +++++++++++++++ test/types/query.test-d.ts | 36 +++++++++++++++++ test/types/transaction.test-d.ts | 62 +++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 1 deletion(-) rename tsconfig.json => test/tsconfig.json (100%) create mode 100644 test/types/initialization.test-d.ts create mode 100644 test/types/query.test-d.ts create mode 100644 test/types/transaction.test-d.ts diff --git a/package.json b/package.json index d446417..dd2222e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "types": "index.d.ts", "scripts": { "test": "standard && tap -J test/*.test.js && npm run test:typescript", - "test:typescript": "tsc --build examples/typescript/*", + "test:typescript": "tsd && tsc --build examples/typescript/*", "test:report": "standard && tap -J --coverage-report=html test/*.test.js", "test:verbose": "standard && tap -J test/*.test.js -Rspec", "postgres": "docker run -p 5432:5432 --name fastify-postgres -e POSTGRES_PASSWORD=postgres -d postgres:11-alpine", @@ -40,10 +40,14 @@ "pg-native": "^3.0.0", "standard": "^14.0.0", "tap": "^14.10.7", + "tsd": "^0.13.1", "typescript": "^4.0.2" }, "peerDependencies": { "pg": ">=6.0.0", "@types/pg": ">=6.0.0" + }, + "tsd": { + "directory": "test/types" } } diff --git a/tsconfig.json b/test/tsconfig.json similarity index 100% rename from tsconfig.json rename to test/tsconfig.json diff --git a/test/types/initialization.test-d.ts b/test/types/initialization.test-d.ts new file mode 100644 index 0000000..0c3dc36 --- /dev/null +++ b/test/types/initialization.test-d.ts @@ -0,0 +1,32 @@ +import fastify from 'fastify'; +import * as pg from 'pg'; + +import fastifyPostgres from '../../index'; + +const app = fastify(); + +// Without parameters +app.register(fastifyPostgres); +app.register(fastifyPostgres, {}); + +// Own pg adapter +app.register(fastifyPostgres, { pg }); + +// Native libpq wrapper +app.register(fastifyPostgres, { native: true }); + +// Multiple databases +app.register(fastifyPostgres, { name: 'users' }); +app.register(fastifyPostgres, { name: 'posts' }); + +// Pool options +app.register(fastifyPostgres, { + user: 'dbuser', + host: 'database.server.com', + database: 'mydb', + password: 'secretpassword', + port: 3211, +}); +app.register(fastifyPostgres, { + connectionString: 'postgres://user:password@host:port/db', +}); diff --git a/test/types/query.test-d.ts b/test/types/query.test-d.ts new file mode 100644 index 0000000..abcad5a --- /dev/null +++ b/test/types/query.test-d.ts @@ -0,0 +1,36 @@ +import fastify from 'fastify'; +import { Client, Pool, PoolClient, QueryResult } from 'pg'; +import { expectType } from 'tsd'; + +import fastifyPostgres, { PostgresDb } from '../../index'; + +const app = fastify(); + +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); + + expectType(app.pg.pool); + expectType(app.pg.Client); + + const client = await app.pg.connect(); + expectType(client); + + const sumResult = await client.query<{ sum: number }>('SELECT 2 + 2 as sum'); + expectType>(sumResult); + + client.release(); + + return { + sum: sumResult.rows, + }; +}); diff --git a/test/types/transaction.test-d.ts b/test/types/transaction.test-d.ts new file mode 100644 index 0000000..3bc6ccf --- /dev/null +++ b/test/types/transaction.test-d.ts @@ -0,0 +1,62 @@ +import fastify from 'fastify'; +import { PoolClient, QueryResult } from 'pg'; +import { expectType } from 'tsd'; + +import fastifyPostgres, { PostgresDb } from '../../index'; + +const app = fastify(); + +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) + VALUES ('ochakovo') + RETURNING 1 + 1 as sum; + `; + + const transactionResult = await app.pg.transact((client) => { + expectType(client); + + return client.query<{ sum: number }>(insertQuery); + }); + + expectType>(transactionResult); + + return transactionResult; +}); + +app.post('/insert-cb', (_req, reply) => { + const insertQuery = ` + INSERT INTO routes(name) + VALUES ('ochakovo') + RETURNING 1 + 1 as sum; + `; + + app.pg.transact( + (client) => { + expectType(client); + + return client.query<{ sum: number }>(insertQuery); + }, + (error, result) => { + expectType(error); + expectType | undefined>(result); + + if (error) { + reply.status(500).send(error); + return; + } + + reply.status(200).send(result); + } + ); +}); From f13afb824d954eaff72be678cf99721cceea510e Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sun, 23 Aug 2020 03:39:37 +0300 Subject: [PATCH 08/13] chore: sync tsconfig placement with fastify --- {test => types}/tsconfig.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {test => types}/tsconfig.json (100%) diff --git a/test/tsconfig.json b/types/tsconfig.json similarity index 100% rename from test/tsconfig.json rename to types/tsconfig.json From 9bbd477ecf0e9d6576ae13342414faa6ec332e53 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 29 Aug 2020 14:52:05 +0300 Subject: [PATCH 09/13] fix: use base tsconfig files for examples --- examples/typescript/multiple-db/tsconfig.json | 20 +------------------ examples/typescript/single-db/tsconfig.json | 20 +------------------ .../typescript/transactions/tsconfig.json | 20 +------------------ package.json | 1 + 4 files changed, 4 insertions(+), 57 deletions(-) diff --git a/examples/typescript/multiple-db/tsconfig.json b/examples/typescript/multiple-db/tsconfig.json index 68458c7..97c2b2f 100644 --- a/examples/typescript/multiple-db/tsconfig.json +++ b/examples/typescript/multiple-db/tsconfig.json @@ -1,24 +1,6 @@ { - "include": ["."], + "extends": "@tsconfig/node10/tsconfig.json", "compilerOptions": { - /* Basic Options */ "noEmit": true, - "lib": ["ES2018"], - "target": "ES2018", - "module": "CommonJS", - - /* Module Resolution Options */ - "esModuleInterop": true, - "resolveJsonModule": true, - "moduleResolution": "Node", - - /* Strict Type-Checking Options */ - "strict": true, - - /* Additional Checks */ - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true } } diff --git a/examples/typescript/single-db/tsconfig.json b/examples/typescript/single-db/tsconfig.json index 68458c7..97c2b2f 100644 --- a/examples/typescript/single-db/tsconfig.json +++ b/examples/typescript/single-db/tsconfig.json @@ -1,24 +1,6 @@ { - "include": ["."], + "extends": "@tsconfig/node10/tsconfig.json", "compilerOptions": { - /* Basic Options */ "noEmit": true, - "lib": ["ES2018"], - "target": "ES2018", - "module": "CommonJS", - - /* Module Resolution Options */ - "esModuleInterop": true, - "resolveJsonModule": true, - "moduleResolution": "Node", - - /* Strict Type-Checking Options */ - "strict": true, - - /* Additional Checks */ - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true } } diff --git a/examples/typescript/transactions/tsconfig.json b/examples/typescript/transactions/tsconfig.json index 68458c7..97c2b2f 100644 --- a/examples/typescript/transactions/tsconfig.json +++ b/examples/typescript/transactions/tsconfig.json @@ -1,24 +1,6 @@ { - "include": ["."], + "extends": "@tsconfig/node10/tsconfig.json", "compilerOptions": { - /* Basic Options */ "noEmit": true, - "lib": ["ES2018"], - "target": "ES2018", - "module": "CommonJS", - - /* Module Resolution Options */ - "esModuleInterop": true, - "resolveJsonModule": true, - "moduleResolution": "Node", - - /* Strict Type-Checking Options */ - "strict": true, - - /* Additional Checks */ - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true } } diff --git a/package.json b/package.json index dd2222e..41b1248 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "fastify-plugin": "^2.0.0" }, "devDependencies": { + "@tsconfig/node10": "^1.0.7", "@types/pg": "^7.14.4", "fastify": "^3.0.0", "pg": "^8.2.1", From b3f2c3777f05f8077cefa1efe1cfaa6afcba7c3b Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 29 Aug 2020 14:55:18 +0300 Subject: [PATCH 10/13] fix: add named export for PostgresPlugin --- examples/typescript/multiple-db/app.ts | 2 +- examples/typescript/single-db/app.ts | 2 +- examples/typescript/transactions/app.ts | 2 +- index.d.ts | 6 +++--- test/types/imports.test-d.ts | 6 ++++++ 5 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 test/types/imports.test-d.ts diff --git a/examples/typescript/multiple-db/app.ts b/examples/typescript/multiple-db/app.ts index 8b5b2ab..4556843 100644 --- a/examples/typescript/multiple-db/app.ts +++ b/examples/typescript/multiple-db/app.ts @@ -1,6 +1,6 @@ import fastify from 'fastify'; -import fastifyPostgres from '../../../index'; +import { fastifyPostgres } from '../../../index'; const app = fastify(); diff --git a/examples/typescript/single-db/app.ts b/examples/typescript/single-db/app.ts index b6940f9..a93182a 100644 --- a/examples/typescript/single-db/app.ts +++ b/examples/typescript/single-db/app.ts @@ -1,6 +1,6 @@ import fastify from 'fastify'; -import fastifyPostgres from '../../../index'; +import { fastifyPostgres } from '../../../index'; const app = fastify(); diff --git a/examples/typescript/transactions/app.ts b/examples/typescript/transactions/app.ts index 5690483..636ec59 100644 --- a/examples/typescript/transactions/app.ts +++ b/examples/typescript/transactions/app.ts @@ -1,6 +1,6 @@ import fastify from 'fastify'; -import fastifyPostgres from '../../../index'; +import { fastifyPostgres } from '../../../index'; const app = fastify(); diff --git a/index.d.ts b/index.d.ts index d9b2f99..09b763d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -41,7 +41,7 @@ type PostgresPluginOptions = { name?: string; } & PgPoolConfig; -declare const PostgresPlugin: FastifyPluginCallback; +declare const fastifyPostgres: FastifyPluginCallback; -export type { PostgresDb, PostgresPluginOptions }; -export default PostgresPlugin; +export { fastifyPostgres, PostgresDb, PostgresPluginOptions }; +export default fastifyPostgres; diff --git a/test/types/imports.test-d.ts b/test/types/imports.test-d.ts new file mode 100644 index 0000000..ca66dbe --- /dev/null +++ b/test/types/imports.test-d.ts @@ -0,0 +1,6 @@ +import defaultPluginImport from '../../index'; +import { + fastifyPostgres as namedPluginImport, + PostgresDb, + PostgresPluginOptions, +} from '../../index'; From 97deecf499dc32b36f96cd5e1d71a0b9c42e4cde Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 29 Aug 2020 16:29:57 +0300 Subject: [PATCH 11/13] fix: use types from pg namespace --- index.d.ts | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/index.d.ts b/index.d.ts index 09b763d..174af26 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,34 +1,28 @@ import { FastifyPluginCallback } from 'fastify'; -import * as PgAdapter from 'pg'; -import { - Client as PgClient, - Pool as PgPool, - PoolClient as PgPoolClient, - PoolConfig as PgPoolConfig, -} from 'pg'; +import * as Pg from 'pg'; declare function transact( - fn: (client: PgPoolClient) => Promise + fn: (client: Pg.PoolClient) => Promise ): Promise; declare function transact( - fn: (client: PgPoolClient) => Promise, + fn: (client: Pg.PoolClient) => Promise, cb: (error: Error | null, result?: TResult) => void ): void; type PostgresDb = { - pool: PgPool; - Client: PgClient; - query: PgPool['query']; - connect: PgPool['connect']; + pool: Pg.Pool; + Client: Pg.Client; + query: Pg.Pool['query']; + connect: Pg.Pool['connect']; transact: typeof transact; }; type PostgresPluginOptions = { /** - * Custom pg adapter + * Custom pg */ - pg?: typeof PgAdapter; + pg?: typeof Pg; /** * Use pg-native @@ -39,7 +33,7 @@ type PostgresPluginOptions = { * Instance name of fastify-postgres */ name?: string; -} & PgPoolConfig; +} & Pg.PoolConfig; declare const fastifyPostgres: FastifyPluginCallback; From 4c9b6948292fa31d8a3fe63d15ee59722ae3138b Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 29 Aug 2020 16:31:40 +0300 Subject: [PATCH 12/13] fix: remove pg typings from peerDeps --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index 41b1248..c3cde36 100644 --- a/package.json +++ b/package.json @@ -45,8 +45,7 @@ "typescript": "^4.0.2" }, "peerDependencies": { - "pg": ">=6.0.0", - "@types/pg": ">=6.0.0" + "pg": ">=6.0.0" }, "tsd": { "directory": "test/types" From db10fe8ba9232e08ac256d308408515f21186747 Mon Sep 17 00:00:00 2001 From: Evgeny Markov Date: Sat, 29 Aug 2020 16:35:18 +0300 Subject: [PATCH 13/13] fix: move examples typechecking to separated script --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index c3cde36..81bd945 100644 --- a/package.json +++ b/package.json @@ -6,9 +6,10 @@ "types": "index.d.ts", "scripts": { "test": "standard && tap -J test/*.test.js && npm run test:typescript", - "test:typescript": "tsd && tsc --build examples/typescript/*", + "test:typescript": "tsd", "test:report": "standard && tap -J --coverage-report=html test/*.test.js", "test:verbose": "standard && tap -J test/*.test.js -Rspec", + "check-examples": "tsc --build examples/typescript/*", "postgres": "docker run -p 5432:5432 --name fastify-postgres -e POSTGRES_PASSWORD=postgres -d postgres:11-alpine", "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" },