Skip to content

Commit

Permalink
Merge pull request #2 from p-fedyukovich/development
Browse files Browse the repository at this point in the history
Implemented Google PubSub module
  • Loading branch information
p-fedyukovich authored Jan 15, 2021
2 parents 2a9e999 + 8488f87 commit 34e9cd6
Show file tree
Hide file tree
Showing 44 changed files with 8,614 additions and 12 deletions.
91 changes: 80 additions & 11 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,82 @@
# Use the latest 2.1 version of CircleCI pipeline process engine. See: https://circleci.com/docs/2.0/configuration-reference
version: 2.1
# Use a package of configuration called an orb.
orbs:
# Declare a dependency on the welcome-orb
welcome: circleci/welcome-orb@0.4.1
# Orchestrate or schedule a set of jobs
version: 2

aliases:
- &restore-cache
restore_cache:
key: dependency-cache-{{ checksum "package.json" }}
- &install-deps
run:
name: Install dependencies
command: yarn install --frozen-lockfile
- &build-packages
run:
name: Build
command: yarn build

jobs:
build:
working_directory: ~/nest
docker:
- image: circleci/node:12
steps:
- checkout
- run:
name: Update NPM version
command: 'sudo npm install -g npm@latest'
- *restore-cache
- *install-deps
- save_cache:
key: dependency-cache-{{ checksum "package.json" }}
paths:
- ./node_modules
- *build-packages

integration_tests:
working_directory: ~/nest
machine: true
environment:
PUBSUB_EMULATOR_HOST: localhost:8681
steps:
- checkout
- run:
name: Prepare nvm
command: |
echo 'export NVM_DIR="/opt/circleci/.nvm"' >> $BASH_ENV
echo ' [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> $BASH_ENV
- run:
name: Upgrade Node.js
command: |
nvm install v12
node -v
nvm alias default v12
- run:
name: Install Yarn
command: npm install yarn -g
- run:
name: Install Docker Compose
command: |
curl -L https://github.com/docker/compose/releases/download/1.19.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
chmod +x ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
- *restore-cache
- *install-deps
- run:
name: Prepare
command: |
docker-compose up -d
sleep 10
- run:
name: List containers
command: docker ps
- run:
name: e2e tests
command: yarn test:e2e

workflows:
# Name the workflow "welcome"
welcome:
# Run the welcome/run job in its own container
version: 2
build-and-test:
jobs:
- welcome/run
- build
- integration_tests:
requires:
- build
28 changes: 28 additions & 0 deletions .commitlintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"extends": ["@commitlint/config-angular"],
"rules": {
"subject-case": [
2,
"always",
["sentence-case", "start-case", "pascal-case", "upper-case", "lower-case"]
],
"type-enum": [
2,
"always",
[
"build",
"chore",
"ci",
"docs",
"feat",
"fix",
"perf",
"refactor",
"revert",
"style",
"test",
"sample"
]
]
}
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/**
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': 'off',
'@typescript-eslint/no-non-null-assertion': 'off',
},
};
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# dependencies
/node_modules

# IDE
/.idea
/.awcache
/.vscode

# misc
npm-debug.log
.DS_Store

# tests
/coverage
/.nyc_output

# dist
dist
13 changes: 13 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# source
lib
tests
index.ts
package-lock.json
tslint.json
tsconfig.json
.prettierrc

# github
.github
CONTRIBUTING.md
renovate.json
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"trailingComma": "all",
"singleQuote": true
}
8 changes: 8 additions & 0 deletions .release-it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"git": {
"commitMessage": "chore(): release v${version}"
},
"github": {
"release": true
}
}
105 changes: 104 additions & 1 deletion README.md
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
) {}
}
```
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3"
services:
pubsub:
image: messagebird/gcloud-pubsub-emulator:latest
ports:
- '8681:8681'
environment:
- PUBSUB_PROJECT1=default_project,event
- PUBSUB_PROJECT2=project2,message
restart: always
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist';
15 changes: 15 additions & 0 deletions lib/common/google-pubsub.decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Inject } from '@nestjs/common';
import { getProjectNameToken, getTopicNameToken } from './google-pubsub.utils';
import { DEFAULT_PROJECT_NAME } from '../google-pubsub.constants';

export const InjectPubSub: (projectName: string) => ParameterDecorator = (
projectName: string = DEFAULT_PROJECT_NAME,
) => Inject(getProjectNameToken(projectName));

export const InjectTopic: (
topicName: string,
projectName?: string,
) => ParameterDecorator = (
topicName: string,
projectName: string = DEFAULT_PROJECT_NAME,
) => Inject(getTopicNameToken(topicName, projectName));
Loading

0 comments on commit 34e9cd6

Please sign in to comment.