From ef73958384ffe79304171dd020215f31f97ebb87 Mon Sep 17 00:00:00 2001 From: Bryan Dun Date: Fri, 12 Aug 2022 00:20:21 -0400 Subject: [PATCH 1/5] Adding types to declaration merge for route transact option in typescript --- index.d.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/index.d.ts b/index.d.ts index 8dacfdc..2731400 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,5 @@ -import { FastifyPluginCallback } from 'fastify'; -import * as Pg from 'pg'; +import { FastifyPluginCallback } from "fastify"; +import * as Pg from "pg"; declare function transact( fn: (client: Pg.PoolClient) => Promise @@ -13,11 +13,15 @@ declare function transact( type PostgresDb = { pool: Pg.Pool; Client: Pg.Client; - query: Pg.Pool['query']; - connect: Pg.Pool['connect']; + query: Pg.Pool["query"]; + connect: Pg.Pool["connect"]; transact: typeof transact; }; +type FastifyPostgresRouteOptions = { + transact: boolean; +}; + type PostgresPluginOptions = { /** * Custom pg @@ -37,10 +41,17 @@ type PostgresPluginOptions = { declare const fastifyPostgres: FastifyPluginCallback; -declare module 'fastify' { +declare module "fastify" { export interface FastifyInstance { pg: PostgresDb & Record; } + + export interface FastifyRequest { + pg?: Pg.PoolClient; + } + export interface RouteShorthandOptions { + pg?: FastifyPostgresRouteOptions; + } } export { fastifyPostgres, PostgresDb, PostgresPluginOptions }; From 35bd02358712fe020af9953ff0fd301219ec8161 Mon Sep 17 00:00:00 2001 From: Bryan Dun Date: Fri, 12 Aug 2022 00:57:45 -0400 Subject: [PATCH 2/5] added typescript definition tests, added string as transact option type --- index.d.ts | 2 +- test/types/transaction.test-d.ts | 38 ++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2731400..f7b177b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -19,7 +19,7 @@ type PostgresDb = { }; type FastifyPostgresRouteOptions = { - transact: boolean; + transact: boolean | string; }; type PostgresPluginOptions = { diff --git a/test/types/transaction.test-d.ts b/test/types/transaction.test-d.ts index a48841a..61ae396 100644 --- a/test/types/transaction.test-d.ts +++ b/test/types/transaction.test-d.ts @@ -1,16 +1,16 @@ -import fastify from 'fastify'; -import { PoolClient, QueryResult } from 'pg'; -import { expectType } from 'tsd'; +import fastify from "fastify"; +import { PoolClient, QueryResult } from "pg"; +import { expectType } from "tsd"; -import fastifyPostgres, { PostgresDb } from '../../index'; +import fastifyPostgres, { PostgresDb } from "../../index"; const app = fastify(); app.register(fastifyPostgres, { - connectionString: 'postgres://user:password@host:port/db', + connectionString: "postgres://user:password@host:port/db", }); -app.post('/insert-async', async () => { +app.post("/insert-async", async () => { const insertQuery = ` INSERT INTO routes(name) VALUES ('ochakovo') @@ -28,7 +28,7 @@ app.post('/insert-async', async () => { return transactionResult; }); -app.post('/insert-cb', (_req, reply) => { +app.post("/insert-cb", (_req, reply) => { const insertQuery = ` INSERT INTO routes(name) VALUES ('ochakovo') @@ -54,3 +54,27 @@ app.post('/insert-cb', (_req, reply) => { } ); }); + +app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { + const insertQuery = ` + INSERT INTO routes(name) + VALUES ('ochakovo') + RETURNING 1 + 1 as sum; + `; + + return req.pg?.query(insertQuery); +}); + +app.post( + "/transact-route", + { pg: { transact: "primary" } }, + async (req, _reply) => { + const insertQuery = ` + INSERT INTO routes(name) + VALUES ('ochakovo') + RETURNING 1 + 1 as sum; + `; + + return req.pg?.query(insertQuery); + } +); From 3590c52e2069cf9cc788baf8f3ce75e03ebf9251 Mon Sep 17 00:00:00 2001 From: Bryan Dun Date: Fri, 12 Aug 2022 01:01:28 -0400 Subject: [PATCH 3/5] adding examples with transact route, minor test fix --- examples/typescript/transactions/app.ts | 38 +++++++++++++++++++++---- test/types/transaction.test-d.ts | 2 +- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/examples/typescript/transactions/app.ts b/examples/typescript/transactions/app.ts index 636ec59..bb6e740 100644 --- a/examples/typescript/transactions/app.ts +++ b/examples/typescript/transactions/app.ts @@ -1,14 +1,14 @@ -import fastify from 'fastify'; +import fastify from "fastify"; -import { fastifyPostgres } from '../../../index'; +import { fastifyPostgres } from "../../../index"; const app = fastify(); app.register(fastifyPostgres, { - connectionString: 'postgres://user:password@host:port/db', + connectionString: "postgres://user:password@host:port/db", }); -app.post('/init-async', async () => { +app.post("/init-async", async () => { const createTableQuery = ` CREATE TABLE routes ( id bigserial primary key, @@ -24,7 +24,7 @@ app.post('/init-async', async () => { }); }); -app.post('/init-cb', (_req, reply) => { +app.post("/init-cb", (_req, reply) => { const createTableQuery = ` CREATE TABLE routes ( id bigserial primary key, @@ -48,4 +48,32 @@ app.post('/init-cb', (_req, reply) => { ); }); +app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { + const createTableQuery = ` + CREATE TABLE routes ( + id bigserial primary key, + name varchar(80) NOT NULL, + created_at timestamp default NULL + ); + `; + + return req.pg?.query(createTableQuery); +}); + +app.post( + "/transact-route-alternate", + { pg: { transact: "primary" } }, + async (req, _reply) => { + const createTableQuery = ` + CREATE TABLE routes ( + id bigserial primary key, + name varchar(80) NOT NULL, + created_at timestamp default NULL + ); + `; + + return req.pg?.query(createTableQuery); + } +); + export { app }; diff --git a/test/types/transaction.test-d.ts b/test/types/transaction.test-d.ts index 61ae396..591157b 100644 --- a/test/types/transaction.test-d.ts +++ b/test/types/transaction.test-d.ts @@ -66,7 +66,7 @@ app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { }); app.post( - "/transact-route", + "/transact-route-alternate", { pg: { transact: "primary" } }, async (req, _reply) => { const insertQuery = ` From 1755ed4741bdfcbcc4ba1aee54aa4f83b23e56ea Mon Sep 17 00:00:00 2001 From: Bryan Dun Date: Mon, 15 Aug 2022 22:23:22 -0400 Subject: [PATCH 4/5] fixing quote styles --- examples/typescript/transactions/app.ts | 16 ++++++++-------- index.d.ts | 10 +++++----- test/types/transaction.test-d.ts | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/examples/typescript/transactions/app.ts b/examples/typescript/transactions/app.ts index bb6e740..0ffe723 100644 --- a/examples/typescript/transactions/app.ts +++ b/examples/typescript/transactions/app.ts @@ -1,14 +1,14 @@ -import fastify from "fastify"; +import fastify from 'fastify'; -import { fastifyPostgres } from "../../../index"; +import { fastifyPostgres } from '../../../index'; const app = fastify(); app.register(fastifyPostgres, { - connectionString: "postgres://user:password@host:port/db", + connectionString: 'postgres://user:password@host:port/db', }); -app.post("/init-async", async () => { +app.post('/init-async', async () => { const createTableQuery = ` CREATE TABLE routes ( id bigserial primary key, @@ -24,7 +24,7 @@ app.post("/init-async", async () => { }); }); -app.post("/init-cb", (_req, reply) => { +app.post('/init-cb', (_req, reply) => { const createTableQuery = ` CREATE TABLE routes ( id bigserial primary key, @@ -48,7 +48,7 @@ app.post("/init-cb", (_req, reply) => { ); }); -app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { +app.post('/transact-route', { pg: { transact: true } }, async (req, _reply) => { const createTableQuery = ` CREATE TABLE routes ( id bigserial primary key, @@ -61,8 +61,8 @@ app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { }); app.post( - "/transact-route-alternate", - { pg: { transact: "primary" } }, + '/transact-route-alternate', + { pg: { transact: 'primary' } }, async (req, _reply) => { const createTableQuery = ` CREATE TABLE routes ( diff --git a/index.d.ts b/index.d.ts index f7b177b..4e80c97 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,5 @@ -import { FastifyPluginCallback } from "fastify"; -import * as Pg from "pg"; +import { FastifyPluginCallback } from 'fastify'; +import * as Pg from 'pg'; declare function transact( fn: (client: Pg.PoolClient) => Promise @@ -13,8 +13,8 @@ declare function transact( type PostgresDb = { pool: Pg.Pool; Client: Pg.Client; - query: Pg.Pool["query"]; - connect: Pg.Pool["connect"]; + query: Pg.Pool['query']; + connect: Pg.Pool['connect']; transact: typeof transact; }; @@ -41,7 +41,7 @@ type PostgresPluginOptions = { declare const fastifyPostgres: FastifyPluginCallback; -declare module "fastify" { +declare module 'fastify' { export interface FastifyInstance { pg: PostgresDb & Record; } diff --git a/test/types/transaction.test-d.ts b/test/types/transaction.test-d.ts index 591157b..f56588b 100644 --- a/test/types/transaction.test-d.ts +++ b/test/types/transaction.test-d.ts @@ -1,16 +1,16 @@ -import fastify from "fastify"; -import { PoolClient, QueryResult } from "pg"; -import { expectType } from "tsd"; +import fastify from 'fastify'; +import { PoolClient, QueryResult } from 'pg'; +import { expectType } from 'tsd'; -import fastifyPostgres, { PostgresDb } from "../../index"; +import fastifyPostgres, { PostgresDb } from '../../index'; const app = fastify(); app.register(fastifyPostgres, { - connectionString: "postgres://user:password@host:port/db", + connectionString: 'postgres://user:password@host:port/db', }); -app.post("/insert-async", async () => { +app.post('/insert-async', async () => { const insertQuery = ` INSERT INTO routes(name) VALUES ('ochakovo') @@ -28,7 +28,7 @@ app.post("/insert-async", async () => { return transactionResult; }); -app.post("/insert-cb", (_req, reply) => { +app.post('/insert-cb', (_req, reply) => { const insertQuery = ` INSERT INTO routes(name) VALUES ('ochakovo') @@ -55,7 +55,7 @@ app.post("/insert-cb", (_req, reply) => { ); }); -app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { +app.post('/transact-route', { pg: { transact: true } }, async (req, _reply) => { const insertQuery = ` INSERT INTO routes(name) VALUES ('ochakovo') @@ -66,8 +66,8 @@ app.post("/transact-route", { pg: { transact: true } }, async (req, _reply) => { }); app.post( - "/transact-route-alternate", - { pg: { transact: "primary" } }, + '/transact-route-alternate', + { pg: { transact: 'primary' } }, async (req, _reply) => { const insertQuery = ` INSERT INTO routes(name) From a1be27b7f7140fa2d67d430c12c2d96398074946 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 16 Aug 2022 09:38:05 +0200 Subject: [PATCH 5/5] Update index.d.ts Co-authored-by: Uzlopak --- index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/index.d.ts b/index.d.ts index 4e80c97..d6ea691 100644 --- a/index.d.ts +++ b/index.d.ts @@ -49,6 +49,7 @@ declare module 'fastify' { export interface FastifyRequest { pg?: Pg.PoolClient; } + export interface RouteShorthandOptions { pg?: FastifyPostgresRouteOptions; }