Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding types to declaration merge for route transact option in typesc… #142

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions examples/typescript/transactions/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
12 changes: 12 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type PostgresDb = {
transact: typeof transact;
};

type FastifyPostgresRouteOptions = {
transact: boolean | string;
};

type PostgresPluginOptions = {
/**
* Custom pg
Expand All @@ -41,6 +45,14 @@ 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
24 changes: 24 additions & 0 deletions test/types/transaction.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);