UNPKG

opinionated-machine

Version:

Very opinionated DI framework for fastify, built on top of awilix

90 lines 2.84 kB
import { asClass, asFunction } from 'awilix'; import { isEnqueuedJobWorkersEnabled, isJobQueueEnabled, isMessageQueueConsumerEnabled, isPeriodicJobEnabled, } from './diConfigUtils.js'; export function asSingletonClass(Type, opts) { return asClass(Type, { ...opts, lifetime: 'SINGLETON', }); } export function asSingletonFunction(fn, opts) { return asFunction(fn, { ...opts, lifetime: 'SINGLETON', }); } export function asServiceClass(Type, opts) { return asClass(Type, { public: true, ...opts, lifetime: 'SINGLETON', }); } export function asUseCaseClass(Type, opts) { return asClass(Type, { public: true, ...opts, lifetime: 'SINGLETON', }); } export function asRepositoryClass(Type, opts) { return asClass(Type, { public: false, ...opts, lifetime: 'SINGLETON', }); } export function asControllerClass(Type, opts) { return asClass(Type, { public: false, ...opts, lifetime: 'SINGLETON', }); } export function asMessageQueueHandlerClass(Type, mqOptions, opts) { return asClass(Type, { // these follow message-queue-toolkit conventions asyncInit: 'start', asyncDispose: 'close', asyncDisposePriority: 10, enabled: isMessageQueueConsumerEnabled(mqOptions.diOptions.messageQueueConsumersEnabled, mqOptions.queueName), lifetime: 'SINGLETON', public: false, ...opts, }); } export function asEnqueuedJobWorkerClass(Type, workerOptions, opts) { return asClass(Type, { // these follow background-jobs-common conventions asyncInit: 'start', asyncDispose: 'dispose', asyncDisposePriority: 15, public: false, enabled: isEnqueuedJobWorkersEnabled(workerOptions.diOptions.enqueuedJobWorkersEnabled, workerOptions.queueName), lifetime: 'SINGLETON', ...opts, }); } export function asPeriodicJobClass(Type, workerOptions, opts) { return asClass(Type, { // this follows background-jobs-common conventions eagerInject: 'register', asyncDispose: 'dispose', public: false, enabled: isPeriodicJobEnabled(workerOptions.diOptions.periodicJobsEnabled, workerOptions.jobName), lifetime: 'SINGLETON', ...opts, }); } export function asJobQueueClass(Type, queueOptions, opts) { return asClass(Type, { // these follow background-jobs-common conventions asyncInit: 'start', asyncDispose: 'dispose', asyncDisposePriority: 20, public: true, enabled: isJobQueueEnabled(queueOptions.diOptions.jobQueuesEnabled, queueOptions.queueName), lifetime: 'SINGLETON', ...opts, }); } //# sourceMappingURL=resolverFunctions.js.map