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

22 lines
736 B
TypeScript

import { type FastifyInstance } from 'fastify'
import fp from 'fastify-plugin'
import { type WorkerUtils, makeWorkerUtils } from 'graphile-worker'
type Options = {
connectionString: string;
}
export interface ExtendedFastifyInstance extends FastifyInstance {
graphile?: WorkerUtils
}
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 })
fastify.decorate('graphile', workerUtils)
}
}
export default fp(graphileWorkerPlugin)