1.0.2 • Published 3 years ago

@suaveslow/nest-agenda v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Credit

All credit goes to John Biundo and the amazing NestJS framework

About

This module provides a thin wrapper around Agenda. Agenda is a NodeJS job scheduler that works with MongoDB.

The module was generated using @nestjsplus/dyn-schematics, a schematics package for NestJS that generates dynamic modules using the pattern described here. There's a complete tutorial on using the custom schematics here.

Installation

npm i @suaveslow/nest-agenda

Quick Start

To configure Agenda, import the AgendaModule module using the familiar register() / registerAsync() pattern.

Once configured, inject the SINGLETON agenda api interface object into any service using the AGENDA_INSTANCE injection token.

// src/app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { NestAgendaModule } from 'nest-agenda';

@Module({
  imports: [
    NestAgendaModule.registerAsync({
      useFactory: async() => ({
        db: { address: 'mongoConnectionString' }
      })
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Now you have access to a AGENDA_INSTANCE token that is associated with the Agenda API, which you can inject into any provider, and use the resulting Agenda API object directly. For example, you might do this:

// src/app.service.ts
import { Inject, Injectable } from '@nestjs/common';
import { AGENDA_INSTANCE } from 'nest-agenda';

@Injectable()
export class AppService {
  constructor(@Inject(AGENDA_INSTANCE) private readonly agenda) {}

  async index() {
    this.agenda.define("say hello", (job) => console.log('hello there!'));
    await this.agenda.every("5 seconds", "say hello");
  }
  ...

Here, you've injected the Agenda instance as a local property of the service class, and can access any of the Agenda API through that property.

You can use any of the following methods to provide the NestAgendaOptions to the module. These follow the usual patterns for custom providers:

  • register(): pass a plain JavaScript object
  • registerAsync(): pass a dynamic object via:
    • useFactory: supply a factory function to return the object; the factory should implement the appropriate options factory interface
    • useClass: bind to a provider/service that supplies the object; that service should implement the appropriate options factory interface
    • useExisting: bind to an existing (provided elsewhere) provider/service to supply the object; that service should implement the appropriate options factory interface

About @nestjsplus/dyn-schematics

Nest Dynamic Package Generator Schematics generates a starter template for building NestJS dynamic packages. It uses the @nestjs/cli core package, and provides customized schematics for generating modular NestJS applications. See here for the full set of available schematics, and documentation. Read these articles for more background:

License

Licensed under the MIT License - see the LICENSE file for details.

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago