|
1 | 1 | # [Graphile Worker](https://github.com/graphile/worker) for [Nest](https://github.com/nestjs/nest)
|
2 | 2 |
|
| 3 | +This is wrapper for Nest.js and [Graphile Worker](https://github.com/graphile/worker). |
| 4 | + |
| 5 | +What is Graphile worker ? |
| 6 | + |
3 | 7 | > Job queue for PostgreSQL running on Node.js - allows you to run jobs (e.g. sending emails, performing calculations, generating PDFs, etc) "in the background" so that your HTTP response/application code is not held up. Can be used with any PostgreSQL-backed application. Pairs beautifully with [PostGraphile](https://www.graphile.org/postgraphile/) or [PostgREST](http://postgrest.org/).
|
4 | 8 |
|
5 |
| -## Description |
| 9 | +Why you should prefer Graphile Worker instead of [Bull](https://github.com/nestjs/bull) ? |
6 | 10 |
|
7 |
| -framework TypeScript starter repository. |
| 11 | +1. You already have a PostgreSQL in your stack (and you don't want to add a Redis server) |
8 | 12 |
|
9 | 13 | ## Installation
|
10 | 14 |
|
11 | 15 | ```bash
|
12 |
| -$ npm install |
| 16 | +$ npm install nest-graphile-worker |
13 | 17 | ```
|
14 | 18 |
|
15 |
| -## Running the app |
| 19 | +Then import module |
| 20 | + |
| 21 | +```ts |
| 22 | +// src/app.module.ts |
| 23 | +import { GraphileWorkerModule } from 'nest-graphile-worker'; |
| 24 | +import { Module } from '@nestjs/common'; |
| 25 | +import { AppController } from './app.controller'; |
| 26 | + |
| 27 | +@Module({ |
| 28 | + imports: [ |
| 29 | + GraphileWorkerModule.forRoot({ |
| 30 | + connectionString: 'postgres://example:password@postgres/example', |
| 31 | + }), |
| 32 | + ], |
| 33 | + controllers: [AppController], |
| 34 | + providers: [], |
| 35 | +}) |
| 36 | +export class AppModule {} |
| 37 | +``` |
16 | 38 |
|
17 |
| -```bash |
18 |
| -# development |
19 |
| -$ npm run start |
| 39 | +Now you can add jobs using `GraphileWorkerService` |
| 40 | + |
| 41 | +```ts |
| 42 | +import { GraphileWorkerService } from 'graphile-worker'; |
| 43 | +import { Controller, HttpCode, Post } from '@nestjs/common'; |
| 44 | + |
| 45 | +@Controller() |
| 46 | +export class AppController { |
| 47 | + constructor(private readonly graphileWorker: GraphileWorkerService) {} |
| 48 | + |
| 49 | + @Post() |
| 50 | + @HttpCode(201) |
| 51 | + async addJob() { |
| 52 | + await this.graphileWorker.quickAddJob('test', { hello: 'world' }); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Also you can run worker in bacground in `main.ts` file: |
20 | 58 |
|
21 |
| -# watch mode |
22 |
| -$ npm run start:dev |
| 59 | +```ts |
| 60 | +import { GraphileWorkerService } from '@app/graphile-worker'; |
| 61 | +import { NestFactory } from '@nestjs/core'; |
| 62 | +import { AppModule } from './app.module'; |
23 | 63 |
|
24 |
| -# production mode |
25 |
| -$ npm run start:prod |
| 64 | +async function bootstrap() { |
| 65 | + const app = await NestFactory.create(AppModule); |
| 66 | + app.get(GraphileWorkerService).run(); |
| 67 | + await app.listen(3000); |
| 68 | +} |
| 69 | +bootstrap(); |
26 | 70 | ```
|
27 | 71 |
|
28 | 72 | ## Test
|
|
0 commit comments