Skip to content

Commit 9fbdc74

Browse files
bdun94mcollinaUzlopak
authored
Adding types to declaration merge for route transact option in typesc… (#142)
* Adding types to declaration merge for route transact option in typescript * added typescript definition tests, added string as transact option type * adding examples with transact route, minor test fix * fixing quote styles * Update index.d.ts Co-authored-by: Uzlopak <[email protected]> Co-authored-by: Matteo Collina <[email protected]> Co-authored-by: Uzlopak <[email protected]>
1 parent 4287f94 commit 9fbdc74

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

examples/typescript/transactions/app.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,32 @@ app.post('/init-cb', (_req, reply) => {
4848
);
4949
});
5050

51+
app.post('/transact-route', { pg: { transact: true } }, async (req, _reply) => {
52+
const createTableQuery = `
53+
CREATE TABLE routes (
54+
id bigserial primary key,
55+
name varchar(80) NOT NULL,
56+
created_at timestamp default NULL
57+
);
58+
`;
59+
60+
return req.pg?.query(createTableQuery);
61+
});
62+
63+
app.post(
64+
'/transact-route-alternate',
65+
{ pg: { transact: 'primary' } },
66+
async (req, _reply) => {
67+
const createTableQuery = `
68+
CREATE TABLE routes (
69+
id bigserial primary key,
70+
name varchar(80) NOT NULL,
71+
created_at timestamp default NULL
72+
);
73+
`;
74+
75+
return req.pg?.query(createTableQuery);
76+
}
77+
);
78+
5179
export { app };

index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type PostgresDb = {
1818
transact: typeof transact;
1919
};
2020

21+
type FastifyPostgresRouteOptions = {
22+
transact: boolean | string;
23+
};
24+
2125
type PostgresPluginOptions = {
2226
/**
2327
* Custom pg
@@ -41,6 +45,14 @@ declare module 'fastify' {
4145
export interface FastifyInstance {
4246
pg: PostgresDb & Record<string, PostgresDb>;
4347
}
48+
49+
export interface FastifyRequest {
50+
pg?: Pg.PoolClient;
51+
}
52+
53+
export interface RouteShorthandOptions {
54+
pg?: FastifyPostgresRouteOptions;
55+
}
4456
}
4557

4658
export { fastifyPostgres, PostgresDb, PostgresPluginOptions };

test/types/transaction.test-d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,27 @@ app.post('/insert-cb', (_req, reply) => {
5454
}
5555
);
5656
});
57+
58+
app.post('/transact-route', { pg: { transact: true } }, async (req, _reply) => {
59+
const insertQuery = `
60+
INSERT INTO routes(name)
61+
VALUES ('ochakovo')
62+
RETURNING 1 + 1 as sum;
63+
`;
64+
65+
return req.pg?.query(insertQuery);
66+
});
67+
68+
app.post(
69+
'/transact-route-alternate',
70+
{ pg: { transact: 'primary' } },
71+
async (req, _reply) => {
72+
const insertQuery = `
73+
INSERT INTO routes(name)
74+
VALUES ('ochakovo')
75+
RETURNING 1 + 1 as sum;
76+
`;
77+
78+
return req.pg?.query(insertQuery);
79+
}
80+
);

0 commit comments

Comments
 (0)