Giter Site home page Giter Site logo

nestjs-google-pubsub's Introduction

NestJS Google Pub/Sub module

p-fedyukovich

A Google Cloud Pub/Sub library for the NestJS framework.

Features

  • Pub/Sub configuration via several ways (sync/async/factory/existing)
  • Decorators for injecting clients and topics
  • Support multiple projects

Installation

Yarn

yarn add nestjs-google-pubsub-module

NPM

npm install nestjs-google-pubsub-module

Usage

Once the installation process is complete, we can import the GooglePubSubModule into the root AppModule.

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:

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.

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:

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
  ) {}
}

nestjs-google-pubsub's People

Contributors

p-fedyukovich avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

ruthwt

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.