Skip to content
This repository was archived by the owner on Jun 18, 2025. It is now read-only.

Commit 14cd0e3

Browse files
authored
fix: add DatabaseService provider to PulseModule (#1)
* fix: add DatabaseService provider to PulseModule * refactor: naming
1 parent 1c1c177 commit 14cd0e3

File tree

8 files changed

+52
-80
lines changed

8 files changed

+52
-80
lines changed

src/constants.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export const JOB_NAME = Symbol('AGENDA_MODULE:JOB_NAME');
2-
export const AGENDA_JOB_OPTIONS = Symbol('AGENDA_JOB_OPTIONS');
3-
export const JOB_PROCESSOR_TYPE = Symbol('AGENDA_MODULE:JOB_PROCESSOR_TYPE');
4-
export const AGENDA_MODULE_CONFIG = Symbol('AGENDA_MODULE_CONFIG');
5-
export const AGENDA_QUEUE_EVENT = Symbol('AGENDA_QUEUE_EVENT');
6-
export const AGENDA_MODULE_QUEUE = Symbol('AGENDA_MODULE_QUEUE');
7-
export const ON_QUEUE_EVENT = Symbol('AGENDA_MODULE:ON_QUEUE_EVENT');
8-
export const QUEUE_CONFIG = Symbol('AGENDA_MODULE:QUEUE_CONFIG');
9-
export const DATABASE_CONNECTION = Symbol('AGENDA_MODULE:DATABASE_CONNECTION');
1+
export const JOB_NAME = Symbol('PULSE_MODULE:JOB_NAME');
2+
export const PULSE_JOB_OPTIONS = Symbol('PULSE_JOB_OPTIONS');
3+
export const JOB_PROCESSOR_TYPE = Symbol('PULSE_MODULE:JOB_PROCESSOR_TYPE');
4+
export const PULSE_MODULE_CONFIG = Symbol('PULSE_MODULE_CONFIG');
5+
export const PULSE_QUEUE_EVENT = Symbol('PULSE_QUEUE_EVENT');
6+
export const PULSE_MODULE_QUEUE = Symbol('PULSE_MODULE_QUEUE');
7+
export const ON_QUEUE_EVENT = Symbol('PULSE_MODULE:ON_QUEUE_EVENT');
8+
export const QUEUE_CONFIG = Symbol('PULSE_MODULE:QUEUE_CONFIG');
9+
export const DATABASE_CONNECTION = Symbol('PULSE_MODULE:DATABASE_CONNECTION');

src/decorators/define.decorator.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
import { applyDecorators, SetMetadata } from '@nestjs/common';
22
import { DefineOptions } from '@pulsecron/pulse';
3-
import { JOB_PROCESSOR_TYPE, AGENDA_JOB_OPTIONS } from '../constants';
3+
import { JOB_PROCESSOR_TYPE, PULSE_JOB_OPTIONS } from '../constants';
44
import { JobProcessorType } from '../enums';
55

66
type NameAndDefineOptions = DefineOptions & Record<'name', string>;
77

88
export function Define(name?: string): MethodDecorator;
99
export function Define(options?: NameAndDefineOptions): MethodDecorator;
10-
export function Define(
11-
nameOrOptions?: string | NameAndDefineOptions,
12-
): MethodDecorator {
10+
export function Define(nameOrOptions?: string | NameAndDefineOptions): MethodDecorator {
1311
let options = {};
1412

1513
if (nameOrOptions) {
16-
options =
17-
typeof nameOrOptions === 'string'
18-
? { name: nameOrOptions }
19-
: nameOrOptions;
14+
options = typeof nameOrOptions === 'string' ? { name: nameOrOptions } : nameOrOptions;
2015
}
2116

2217
return applyDecorators(
23-
SetMetadata(AGENDA_JOB_OPTIONS, options),
24-
SetMetadata(JOB_PROCESSOR_TYPE, JobProcessorType.DEFINE),
18+
SetMetadata(PULSE_JOB_OPTIONS, options),
19+
SetMetadata(JOB_PROCESSOR_TYPE, JobProcessorType.DEFINE)
2520
);
2621
}

src/decorators/queue.decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SetMetadata, Type } from '@nestjs/common';
22
import { PulseQueueConfig } from '../interfaces';
3-
import { AGENDA_MODULE_QUEUE } from '../constants';
3+
import { PULSE_MODULE_QUEUE } from '../constants';
44

55
export function Queue(): ClassDecorator;
66
export function Queue(name: string): ClassDecorator;
@@ -13,6 +13,6 @@ export function Queue(nameOrConfig?: string | PulseQueueConfig): ClassDecorator
1313
: {};
1414

1515
return (target: Type<any> | Function) => {
16-
SetMetadata(AGENDA_MODULE_QUEUE, pulseConfig)(target);
16+
SetMetadata(PULSE_MODULE_QUEUE, pulseConfig)(target);
1717
};
1818
}
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
1-
import { applyDecorators, SetMetadata } from "@nestjs/common";
2-
import { JOB_PROCESSOR_TYPE, AGENDA_JOB_OPTIONS } from "../constants";
3-
import { JobProcessorType } from "../enums";
4-
import { JobOptions } from "../interfaces/job-options.interface";
1+
import { applyDecorators, SetMetadata } from '@nestjs/common';
2+
import { JOB_PROCESSOR_TYPE, PULSE_JOB_OPTIONS } from '../constants';
3+
import { JobProcessorType } from '../enums';
4+
import { JobOptions } from '../interfaces/job-options.interface';
55

6-
export type RepeatableJobOptions = JobOptions & Record<"interval", string>;
6+
export type RepeatableJobOptions = JobOptions & Record<'interval', string>;
77

8-
export type NonRepeatableJobOptions = JobOptions &
9-
Record<"when", string | Date>;
8+
export type NonRepeatableJobOptions = JobOptions & Record<'when', string | Date>;
109

11-
export type PulseModuleJobOptions =
12-
| RepeatableJobOptions
13-
| NonRepeatableJobOptions;
10+
export type PulseModuleJobOptions = RepeatableJobOptions | NonRepeatableJobOptions;
1411

1512
export function Every(interval: string): MethodDecorator;
1613
export function Every(options: RepeatableJobOptions): MethodDecorator;
17-
export function Every(
18-
intervalOrOptions: string | RepeatableJobOptions
19-
): MethodDecorator {
20-
const options =
21-
typeof intervalOrOptions === "string"
22-
? { interval: intervalOrOptions }
23-
: intervalOrOptions;
14+
export function Every(intervalOrOptions: string | RepeatableJobOptions): MethodDecorator {
15+
const options = typeof intervalOrOptions === 'string' ? { interval: intervalOrOptions } : intervalOrOptions;
2416

2517
return applyDecorators(
26-
SetMetadata(AGENDA_JOB_OPTIONS, options),
18+
SetMetadata(PULSE_JOB_OPTIONS, options),
2719
SetMetadata(JOB_PROCESSOR_TYPE, JobProcessorType.EVERY)
2820
);
2921
}
3022

3123
export function Schedule(when: string): MethodDecorator;
3224
export function Schedule(options: NonRepeatableJobOptions): MethodDecorator;
3325
export function Schedule(whenOrOptions: string | NonRepeatableJobOptions) {
34-
const options =
35-
typeof whenOrOptions === "string" ? { when: whenOrOptions } : whenOrOptions;
26+
const options = typeof whenOrOptions === 'string' ? { when: whenOrOptions } : whenOrOptions;
3627

3728
return applyDecorators(
38-
SetMetadata(AGENDA_JOB_OPTIONS, options),
29+
SetMetadata(PULSE_JOB_OPTIONS, options),
3930
SetMetadata(JOB_PROCESSOR_TYPE, JobProcessorType.SCHEDULE)
4031
);
4132
}
@@ -44,7 +35,7 @@ export function Now(name?: string): MethodDecorator {
4435
const options = { name };
4536

4637
return applyDecorators(
47-
SetMetadata(AGENDA_JOB_OPTIONS, options),
38+
SetMetadata(PULSE_JOB_OPTIONS, options),
4839
SetMetadata(JOB_PROCESSOR_TYPE, JobProcessorType.NOW)
4940
);
5041
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from "./decorators";
2-
export * from "./pulse.module";
1+
export * from './decorators';
2+
export * from './pulse.module';

src/providers/database.service.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
import { Db, MongoClient } from "mongodb";
2-
import { Inject, Injectable } from "@nestjs/common";
3-
import { PulseConfig } from "@pulsecron/pulse";
1+
import { Db, MongoClient } from 'mongodb';
2+
import { Inject, Injectable } from '@nestjs/common';
3+
import { PulseConfig } from '@pulsecron/pulse';
44

5-
import { AGENDA_MODULE_CONFIG } from "../constants";
5+
import { PULSE_MODULE_CONFIG } from '../constants';
66

77
@Injectable()
88
export class DatabaseService {
99
private connection!: Db;
1010
private client?: MongoClient;
1111

12-
constructor(
13-
@Inject(AGENDA_MODULE_CONFIG) private readonly config: PulseConfig
14-
) {
12+
constructor(@Inject(PULSE_MODULE_CONFIG) private readonly config: PulseConfig) {
1513
if (config.mongo) {
1614
this.connection = config.mongo;
1715
} else {
18-
this.client = new MongoClient(
19-
config.db?.address as string,
20-
config.db?.options
21-
);
16+
this.client = new MongoClient(config.db?.address as string, config.db?.options);
2217
}
2318
}
2419

2520
async connect() {
2621
if (!this.connection) {
27-
this.client = new MongoClient(
28-
this.config.db?.address as string,
29-
this.config.db?.options
30-
);
22+
this.client = new MongoClient(this.config.db?.address as string, this.config.db?.options);
3123

3224
await this.client.connect();
3325

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
import { Injectable, Inject, Type } from "@nestjs/common";
2-
import { Reflector } from "@nestjs/core";
3-
import {
4-
JOB_PROCESSOR_TYPE,
5-
JOB_NAME,
6-
AGENDA_JOB_OPTIONS,
7-
AGENDA_MODULE_QUEUE,
8-
ON_QUEUE_EVENT,
9-
} from "../constants";
10-
import { PulseModuleJobOptions } from "../decorators";
11-
import { JobProcessorType } from "../enums";
1+
import { Injectable, Inject, Type } from '@nestjs/common';
2+
import { Reflector } from '@nestjs/core';
3+
import { JOB_PROCESSOR_TYPE, JOB_NAME, PULSE_JOB_OPTIONS, PULSE_MODULE_QUEUE, ON_QUEUE_EVENT } from '../constants';
4+
import { PulseModuleJobOptions } from '../decorators';
5+
import { JobProcessorType } from '../enums';
126

137
@Injectable()
148
export class PulseMetadataAccessor {
159
constructor(@Inject(Reflector.name) private readonly reflector: Reflector) {}
1610

1711
isQueue(target: Type<any> | Function): boolean {
18-
return !!this.reflector.get(AGENDA_MODULE_QUEUE, target);
12+
return !!this.reflector.get(PULSE_MODULE_QUEUE, target);
1913
}
2014

2115
isEventListener(target: Type<any> | Function): boolean {
@@ -31,7 +25,7 @@ export class PulseMetadataAccessor {
3125
}
3226

3327
getQueueMetadata(target: Type<any> | Function): any {
34-
return this.reflector.get(AGENDA_MODULE_QUEUE, target);
28+
return this.reflector.get(PULSE_MODULE_QUEUE, target);
3529
}
3630

3731
getJobProcessorType(target: Function): JobProcessorType {
@@ -43,6 +37,6 @@ export class PulseMetadataAccessor {
4337
}
4438

4539
getJobProcessorMetadata(target: Type<any> | Function): PulseModuleJobOptions {
46-
return this.reflector.get(AGENDA_JOB_OPTIONS, target);
40+
return this.reflector.get(PULSE_JOB_OPTIONS, target);
4741
}
4842
}

src/pulse.module.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DynamicModule, InjectionToken, Module, Provider, Type, forwardRef } from '@nestjs/common';
22
import { DiscoveryModule, ModuleRef } from '@nestjs/core';
3-
import { AGENDA_MODULE_CONFIG } from './constants';
3+
import { PULSE_MODULE_CONFIG } from './constants';
44
import { pulseFactory } from './factories';
55
import { PulseConfigFactory, PulseModuleAsyncConfig, PulseModuleConfig, PulseQueueConfig } from './interfaces';
66
import { PulseExplorer, PulseMetadataAccessor } from './providers';
@@ -10,14 +10,14 @@ import { getQueueConfigToken, getQueueToken } from './utils';
1010

1111
@Module({
1212
imports: [DiscoveryModule],
13-
providers: [PulseOrchestrator],
13+
providers: [PulseOrchestrator, DatabaseService],
1414
exports: [PulseOrchestrator],
1515
})
1616
export class PulseModule {
1717
static forRoot(config: PulseModuleConfig): DynamicModule {
1818
const configProviders: Provider[] = [
1919
{
20-
provide: AGENDA_MODULE_CONFIG,
20+
provide: PULSE_MODULE_CONFIG,
2121
useValue: config,
2222
},
2323
DatabaseService,
@@ -35,7 +35,7 @@ export class PulseModule {
3535
}
3636

3737
static forRootAsync(config: PulseModuleAsyncConfig<PulseModuleConfig>): DynamicModule {
38-
const providers = this.createAsyncProviders<PulseModuleConfig>(AGENDA_MODULE_CONFIG, config);
38+
const providers = this.createAsyncProviders<PulseModuleConfig>(PULSE_MODULE_CONFIG, config);
3939

4040
return {
4141
global: true,
@@ -64,7 +64,7 @@ export class PulseModule {
6464
{
6565
provide: getQueueToken(name),
6666
useFactory: pulseFactory,
67-
inject: [queueConfigToken, AGENDA_MODULE_CONFIG],
67+
inject: [queueConfigToken, PULSE_MODULE_CONFIG],
6868
},
6969
];
7070

@@ -82,7 +82,7 @@ export class PulseModule {
8282
{
8383
provide: getQueueToken(name),
8484
useFactory: pulseFactory,
85-
inject: [queueConfigToken, AGENDA_MODULE_CONFIG],
85+
inject: [queueConfigToken, PULSE_MODULE_CONFIG],
8686
},
8787
...this.createAsyncProviders<PulseQueueConfig>(queueConfigToken, config),
8888
];

0 commit comments

Comments
 (0)