Use pg-boss in your NestJS app
npm install @apricote/nest-pg-boss@apricote/nest-pg-boss
Use pg-boss in your Nest.js service!
``bash`
npm install @apricote/nest-pg-boss
To begin using @apricote/nest-pg-boss, initialize the root module:
`ts
import { PGBossModule } from "@apricote/nest-pg-boss";
// app.module.ts
@Module({
imports: [
PGBossModule.forRootAsync({
application_name: "default",
useFactory: (config: ConfigService) => ({
// Connection details
host: config.get
user: config.get
password: config.get
database: config.get
schema: "public",
max: config.get
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
`
For a list of available settings, check out the pg-boss docs.
`typescript
// jobs.ts
import { createJob } from "@apricote/nest-pg-boss"
interface IFoobarJobData {
foo: string
bar: boolean
}
const FoobarJob = createJob
`
#### Create new Jobs
`typescript
// module.ts
import { PGBossModule } from "@apricote/nest-pg-boss";
import { FoobarService } from "./service.ts";
@Module({
imports: PGBossModule.forJobs([FoobarJob]),
providers: [FoobarService]
})
class FoobarModule {}
`
`typescript
// service.ts
import { JobService } from "@apricote/nest-pg-boss";
import { FoobarJob, IFoobarJobData } from "./jobs.ts";
@Injectable()
class FoobarService {
constructor(
@FoobarJob.Inject()
private readonly foobarJobService: JobService
) {}
async sendJob() {
await this.foobarJobService.send({ foo: "oof", bar: true }, {});
}
}
`
#### Process Jobs
Jobs can be processed by using the @FoobarJob.Handle() decorator.
`typescript
// service.ts
@Injectable()
class FoobarService {
/ ... /
@FoobarJob.Handle()
async handleJob(job: Job
// do something
}
}
`
You can optionally pass an object with WorkOptions to .Handle():
`typescript`
@FoobarJob.Handle({ teamSize: 10, teamConcurrency: 2 })
`bashunit tests
$ npm run test
License
@apricote/nest-pg-boss` is MIT licensed.