-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fa5bacd
commit 8488f87
Showing
1 changed file
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,104 @@ | ||
# nestjs-google-pubsub | ||
# NestJS Google Pub/Sub module | ||
|
||
[![p-fedyukovich](https://circleci.com/gh/p-fedyukovich/nestjs-google-pubsub.svg?style=svg)](https://circleci.com/gh/p-fedyukovich/nestjs-google-pubsub) | ||
|
||
A Google Cloud Pub/Sub library for the [NestJS](https://github.com/nestjs/nest) framework. | ||
|
||
## Features | ||
|
||
- Pub/Sub configuration via several ways (sync/async/factory/existing) | ||
- Decorators for injecting clients and topics | ||
- Support multiple projects | ||
|
||
## Installation | ||
|
||
**Yarn** | ||
|
||
```bash | ||
yarn add nestjs-google-pubsub-module | ||
``` | ||
|
||
**NPM** | ||
|
||
```bash | ||
npm install nestjs-google-pubsub-module | ||
``` | ||
|
||
## Usage | ||
|
||
Once the installation process is complete, we can import the `GooglePubSubModule` into the root `AppModule`. | ||
|
||
```ts | ||
import { Module } from '@nestjs/common'; | ||
import { GooglePubSubModule } from 'nestjs-google-pubsub-module'; | ||
|
||
@Module({ | ||
imports: [ | ||
GooglePubSubModule.forRoot({ | ||
projectName: 'gcp-project' | ||
}), | ||
], | ||
}) | ||
export class AppModule {} | ||
``` | ||
|
||
The `forRoot()` method supports all the configuration properties exposed by the PubSub constructor. | ||
In addition, there is `projectName` configuration property which is used as alias name for a project. | ||
|
||
**Important**: Default project name will be used it's not provided. | ||
|
||
Once this is done, the PubSub object will be available to inject across the entire project (without needing to import any modules), for example: | ||
|
||
```ts | ||
import { Injectable } from '@nestjs/common'; | ||
import { PubSub } from '@google-cloud/pubsub'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
constructor(private pubsub: PubSub) {} | ||
} | ||
``` | ||
|
||
### Topics | ||
|
||
Once PubSub module is configured, we can set a topic up. | ||
|
||
```ts | ||
import { Module } from '@nestjs/common'; | ||
import { GooglePubSubModule } from 'nestjs-google-pubsub-module'; | ||
|
||
@Module({ | ||
imports: [ | ||
GooglePubSubModule.forRoot({ | ||
projectName: 'gcp-project' | ||
}), | ||
GooglePubSubModule.forFeature('event_topic', { | ||
projectName: 'gcp-project', | ||
}), | ||
], | ||
}) | ||
export class AppModule {} | ||
``` | ||
|
||
The `forFeature` method applies two arguments. The first is a topic name, and the second - option which supports all | ||
the configuration properties exposed by Topic publish options. | ||
In addition, there are extra configuration properties described below. | ||
|
||
* `projectName` - name of the project for which topic will be configured (default: `default_project`) | ||
|
||
Once this is done, as well as the PubSub object the Topic object will be available to inject across the entire project | ||
(without needing to import any modules), for example: | ||
|
||
```ts | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectTopic } from 'nestjs-google-pubsub-module'; | ||
import { Topic } from '@google-cloud/pubsub'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
constructor( | ||
@InjectTopic('event_topic') | ||
private readonly eventTopic: Topic | ||
) {} | ||
} | ||
``` |