fp/services/capture/src/fastify-graphile-worker-plu...

22 lines
736 B
TypeScript
Raw Normal View History

2024-07-23 02:59:41 +00:00
import { type FastifyInstance } from 'fastify'
import fp from 'fastify-plugin'
2024-07-28 00:42:09 +00:00
import { type WorkerUtils, makeWorkerUtils } from 'graphile-worker'
2024-07-23 02:59:41 +00:00
type Options = {
connectionString: string;
}
export interface ExtendedFastifyInstance extends FastifyInstance {
2024-07-28 00:42:09 +00:00
graphile?: WorkerUtils
2024-07-23 02:59:41 +00:00
}
async function graphileWorkerPlugin (fastify: ExtendedFastifyInstance, opts: Options) {
if (!fastify.graphile) {
if (!opts.connectionString) throw new Error('graphileWorkerPlugin requires connectionString passed in options argument, but it was missing');
const workerUtils = await makeWorkerUtils({ connectionString: opts.connectionString })
2024-07-28 00:42:09 +00:00
fastify.decorate('graphile', workerUtils)
2024-07-23 02:59:41 +00:00
}
}
export default fp(graphileWorkerPlugin)