# @codahead/nestjs-amqp-connection-manager

> Wrapper around amqp-connection-manager working as a NestJS module

Latest version **0.0.4** (published 2022-04-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install @codahead/nestjs-amqp-connection-manager
pnpm add @codahead/nestjs-amqp-connection-manager
yarn add @codahead/nestjs-amqp-connection-manager
bun add @codahead/nestjs-amqp-connection-manager
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2022-04-28 |
| First published | 2022-04-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 29.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jakub Surdej |
| Maintainers | kuskoman |

## Links

- npm: https://www.npmjs.com/package/@codahead/nestjs-amqp-connection-manager
- npm.io page: https://npm.io/package/@codahead/nestjs-amqp-connection-manager

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Recent versions

- 0.0.4 (latest) — 2022-04-28
- 0.0.3 — 2022-04-27
- 0.0.2 — 2022-04-27
- 0.0.1 — 2022-04-27

## README

# NestJS AMQP Connection Manager

## Description

Project based on [nestjs-amqp](https://github.com/nestjsx/nestjs-amqp), but under the hood
using [amqp-connection-manager](https://www.npmjs.com/package/amqp-connection-manager)
instead of pure [amqplib](https://www.npmjs.com/package/amqplib).

The project provides wrapper on `amqp-connection-manager` allowing to easily use it with
[NestJS framework](https://github.com/nestjs/nest).

## Installation

npm:

```sh
npm i @codahead/nestjs-amqp-connection-manager amqp-connection-manager amqplib
npm i --save-dev @types/amqplib
```

yarn:

```sh
yarn add @codahead/nestjs-amqp-connection-manager amqp-connection-manager amqplib
yarn add -D @types/amqplib
```

## Usage

### Importing module

The package provides four ways to import the module, for creating single connection
and multiple connections using both `forRoot` and `forRootAsync` methods.

#### forRoot method

Creating single connection example:

```ts
import { AmqpConnectionManagerModule } from "@codahead/nestjs-amqp-connection-manager"; 
const CONNECTION_NAME = 'connection1';

@Module({
    // name is optional and defaults to AMQP_CONNECTION_MANAGER_DEFAULT_CONNECTION_NAME constant
    imports: [AmqpConnectionManagerModule.forRoot({ name: CONNECTION_NAME, urls: ['url'] })],
})
class AppModule {}
```

Creating multiple connections example:

```ts
import { AmqpConnectionManagerModule } from "@codahead/nestjs-amqp-connection-manager"; 
const FIRST_CONNECTION_NAME = 'connection1';
const SECOND_CONNECTION_NAME = 'connection2';

@Module({
    // name is optional and defaults to AMQP_CONNECTION_MANAGER_DEFAULT_CONNECTION_NAME constant
    imports: [AmqpConnectionManagerModule.forRoot([{ name: CONNECTION_NAME, urls: ['url'] }, { name: SECOND_CONNECTION_NAME, urls: ['url'] }])],
})
class AppModule {}
```

#### forRootAsync method

Creating single connection example:

```ts
import { AmqpConnectionManagerModule } from "@codahead/nestjs-amqp-connection-manager"; 
const CONNECTION_NAME = 'connection1';

@Injectable()
  class ConfigServiceProvider {
    public getConfig(): AmqpConnectionSingleInstanceOptions {
      return { urls: ['testConfig.brokerUrl'] };
    }
  }

@Module({ providers: [ConfigServiceProvider], exports: [ConfigServiceProvider] })
class ConfigServiceModule {}

@Module({
    imports: [
        AmqpConnectionManagerModule.forRootAsync({
            name: CONNECTION_NAME,
            useFactory: (config: ConfigServiceProvider) => {
                return config.getConfig();
            },
            inject: [ConfigServiceProvider],
            imports: [ConfigServiceModule],
        }),
        ConfigServiceModule,
    ],
    providers: [AmqpService],
})
class AppModule {}
```

Creating multiple connection example:

```ts
// [...]

@Module({
  imports: [
    AmqpConnectionManagerModule.forRootAsync({
      providerOptions: [
        { useFactory: factory, name: FIRST_CONNECTION_NAME, inject: [ConfigServiceProvider] },
        { useFactory: factory, name: SECOND_CONNECTION_NAME, inject: [ConfigServiceProvider] },
      ],
      inject: [ConfigServiceProvider],
      imports: [ConfigServiceModule],
    }),
    ConfigServiceModule,
  ],
  providers: [AmqpService],
})
class AppModule {}
```

### Injecting connection manager

Connection can be injected using `InjectAmqpConnection` decorator.

```ts
import { InjectAmqpManager } from "@codahead/nestjs-amqp-connection-manager"; 

// [...]

@Injectable()
class AmqpService {
    constructor(
        @InjectAmqpManager(FIRST_CONNECTION_NAME) public readonly connection1: IAmqpConnectionManager,
        @InjectAmqpManager(SECOND_CONNECTION_NAME) public readonly connection2: IAmqpConnectionManager,
    ) {}
}
```

Decorator parameter may be omitted if only one connection was created and its name was not overridden.

### Amqplib and amqp-connection-manager

To learn how to use amqplib and amqp-connection-manager you can head to:

- [RabbitMQ documentation](https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html)
- [AMQP connection manager GitHub page](https://github.com/jwalton/node-amqp-connection-manager)

---
_Source: https://npm.io/package/@codahead/nestjs-amqp-connection-manager · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
