npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nestjs-enhanced/pg-boss

v1.2.4

Published

A nestjs toolkit for interacting with integrate with [pg-boss](https://github.com/timgit/pg-boss/blob/master/docs/readme.md), a job queue system for PostgreSQL. It offers a more 'nest' style way to register workers and schedule work.

Downloads

196

Readme

pg-boss

A nestjs toolkit for interacting with integrate with pg-boss, a job queue system for PostgreSQL. It offers a more 'nest' style way to register workers and schedule work.

Install:

npm install pg-boss @nestjs-enhanced/pg-boss

Usage:

Background work:

import { ProcessQueue } from '@nestjs-enhanced/pg-boss';

@Injectable()
export class MyWorkerService {
  @ProcessQueue('some-queue-name')
  processSomeQueue(job: Job) {
    // do work with job
  }
}

import { PgBoss } from '@nestjs-enhanced/pg-boss';

@Injectable()
export class MyOtherService {
  constructor (
    private pgBoss: PgBoss
  )

  doWork() {
    this.pgBoss.send('some-queue-name', {
      // data can be passed through the job
      id: someRecord.id
    });
  }
}

Cron style work:

import { PgBoss } from '@nestjs-enhanced/pg-boss';

@Injectable()
export class MyOtherService {
  constructor (
    private pgBoss: PgBoss
  )

  @ScheduledQueue('* * * * *')
  doWork() {
    // do work every minute of every day
  }
}

QueueModule

register:

import { QueueModule } from 'path-to-queue-module';

@Module({
  imports: [
    QueueModule.register({
      database: 'your-database-connection-string-or-config'
      // ... other PgBoss options
    })
  ]
})
export class AppModule {}

registerAsync:

import { QueueModule } from 'path-to-queue-module';

@Module({
  imports: [
    QueueModule.registerAsync({
      useFactory: async (configService) => {
        return {
          database: configService.get('DATABASE_URL')
          // ... other PgBoss options
        };
      },
      inject: [ConfigService],
    })
  ]
})
export class AppModule {}

Decorators

ProcessQueue

The ProcessQueue decorator calls the work method of pg-boss and can perform a job on a specific queue. It follows the work method of pg-boss.

It takes a queue name and an options object to control how the work is done.

ScheduledQueue

The ScheduledQueue decorator calls the schedule method of pg-boss and can perform a job on a schedule. It is useful for running work on a schedule where only one worker should be processing it at a time.

It takes a string of the cron schedule and an optional options object to control how the job is scheduled

QueueMiddlewareService

The QueueMiddlewareService provides a way to register middlewares that get called for each job coming through the queue. These middlewares follow the express pattern, taking in a job and a next function, and can be used to execute any pre/post-processing steps on jobs like logging or cleanup.

Usage:

To use the middleware service, you first need to inject it into your service or controller:

import { QueueMiddlewareService } from 'path-to-queue-middleware-service';

@Injectable()
export class YourService {
  constructor(private queueMiddlewareService: QueueMiddlewareService) {}

  yourMethod() {
    this.queueMiddlewareService.register((job, next) => {
      // Your middleware logic here
      next();
    });
  }
}

Here, job represents the current job being processed in the queue, and next is a callback function that, when called, moves on to the next middleware in the queue (or finalizes the job if there are no more middlewares).

Future:

  • Wrap pg-boss into a SchedulingService to avoid direct interaction and to simplify interactions with pg-boss
  • Create new queue package that hooks into pg-boss or other types of queueing services