Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
38 changes: 33 additions & 5 deletions examples/typescript/transactions/app.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand All @@ -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 };
21 changes: 16 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<TResult>(
fn: (client: Pg.PoolClient) => Promise<TResult>
Expand All @@ -13,11 +13,15 @@ declare function transact<TResult>(
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 | string;
};

type PostgresPluginOptions = {
/**
* Custom pg
Expand All @@ -37,10 +41,17 @@ type PostgresPluginOptions = {

declare const fastifyPostgres: FastifyPluginCallback<PostgresPluginOptions>;

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

export interface FastifyRequest {
pg?: Pg.PoolClient;
}
export interface RouteShorthandOptions {
pg?: FastifyPostgresRouteOptions;
}
}

export { fastifyPostgres, PostgresDb, PostgresPluginOptions };
Expand Down
38 changes: 31 additions & 7 deletions test/types/transaction.test-d.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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')
Expand All @@ -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-alternate",
{ pg: { transact: "primary" } },
async (req, _reply) => {
const insertQuery = `
INSERT INTO routes(name)
VALUES ('ochakovo')
RETURNING 1 + 1 as sum;
`;

return req.pg?.query(insertQuery);
}
);