Skip to content

Commit

Permalink
Updated prettier options and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bashleigh committed Sep 3, 2018
1 parent 43246bc commit ca3c21c
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 62 deletions.
11 changes: 9 additions & 2 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"singleQuote": true,
"trailingComma": "all"
"singleQuote": true,
"printWidth": 80,
"proseWrap": "always",
"tabWidth": 2,
"useTabs": false,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"semi": true
}
157 changes: 106 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,112 @@ An amqp connection service for nestjs.
$ yarn add nestjs-amqp
```

## Basic usage

```ts
import {Module} from '@nestjs/common';
import {AmqpModule} from 'nestjs-amqp';

@Module({
imports: [AmqpModule.forRoot({
name: 'rabbitmq',
host: 'localhost',
port: 5672,
username: 'test',
password: 'test',
})],
})
export default AppModule {}

```

### Usage with nestjs-config

```ts
import {Module} from '@nestjs/common';
import {AmqpModule} from 'nestjs-amqp';
import {ConfigModule, ConfigService} from 'nestjs-config';
import * as path from 'path';

@Module({
imports: [
ConfigModule.load(path.resolve(__dirname, 'config', '**/*.ts')),
AmqpModule.forRootAsync({
useFactory: (config: ConfigService) => config.get('aqmp'),
inject: [ConfigService],
}),
],
})
export default AppModule {}

//src/config/amqp.ts
export default {
name: 'rabbitmq',
host: process.env.AMQP_HOST,
port: process.env.AMQP_PORT,
username: process.env.USERNAME,
password: process.env.PASSWORD,
}

//alternatively you can use an array
export default [
{
name: 'rabbitmq',
host: process.env.AMQP_HOST,
port: process.env.AMQP_PORT,
username: process.env.USERNAME,
password: process.env.PASSWORD,
},
{
name: 'other_connection',
host: process.env.ANOTHER_CONNECTION,
port: process.env.ANOTHER_PORT,
},
];
```

## tests
In order to test first you need to start the rabbitmq container. We've provided a `docker-compose` file to make this easier.

```bash
$ docker-compose up -d
$ yarn test
```
> Navigate to localhost:15672 for rabbitmq manager, username and password are both `guest`
```javascript
import {
Module,
} from '@nestjs/common';
import {ConfigModule} from 'nestjs-config';
```ts
import {Module} from '@nestjs/common';
import {AmqpModule} from 'nestjs-amqp';

@module({
imports: [ConfigModule, AmqpModule.forRoot([
{
host: 'amqp://test:test@localhost',
},
{
username: 'test',
password: 'test',
host: 'localhost',
port: 5672,
ssl: true,
name: 'test',
}
])],
imports: [AmqpModule.forRoot([
{
host: 'amqp://test:test@localhost',
},
{
username: 'test',
password: 'test',
host: 'localhost',
port: 5672,
ssl: true,
name: 'test',
}
])],
})
export default class ExecutionModule {
}
```
> Alternatively use the env method `AMQP_URL=amqp://test@test:localhost:5672`

```javascript
import {
Injectable,
} from '@nestjs/common';
import {
InjectAmqpConnection,
} from 'nestjs-amqp';
```ts
import {Injectable} from '@nestjs/common';
import {InjectAmqpConnection} from 'nestjs-amqp';

@Injectable()
export default TestService {
constructor(
@InjectAmqpConnection('test') private readonly connectionTest, //gets connection with name 'test' defined in module
@InjectAmqpConnection(0) private readonly connection0, //gets first defined connection without a name
) {}
constructor(
@InjectAmqpConnection('test') private readonly connectionTest, //gets connection with name 'test' defined in module
@InjectAmqpConnection(0) private readonly connection0, //gets first defined connection without a name
) {}
}
```
> Use InjectAmqpConnection without a parameter for default connection
Expand All @@ -73,34 +130,32 @@ So far this package manages multiple AMQP connections using the nestjs container
Alternatively I'd like to implement something like this:

```javascript
import {Injectable} from '@nestjs/common';
import {
Injectable,
} from '@nestjs/common';
import {
AmqpConnection,
Consume,
Publish,
Message,
AmqpConnection,
Consume,
Publish,
Message,
} from 'nestjs-amqp';

@Injectable()
@AmqpConnection()
export default class MyAmqpService {

@Consume("queue_name", {
noAck: true,
})
async listen(@Message message) {
console.log('Message received', message);
//send a message back
this.publish();
}

@Publish("queue_name")
async publish() {
return "Send this to 'queue queue_name'";
}
@Consume("queue_name", {
noAck: true,
})
async listen(@Message message) {
console.log('Message received', message);

//send a message back
this.publish();
}

@Publish("queue_name")
async publish() {
return "Send this to 'queue queue_name'";
}
}
```

Expand Down
6 changes: 3 additions & 3 deletions src/amqp.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ export default class AMQPModule {
options?: AmqpConnectionOptions[] | AmqpConnectionOptions
): DynamicModule {

const providersOptions = this.createOptionsProviders(options typeof Array ? options : [options]);

const providersOptions = this.createOptionsProviders(Array.isArray(options) ? options : [options]);
const providers = this.createConnectionProviders(Array.isArray(options) ? options : [options]);

return {
module: AMQPModule,
providers: [
...providersOptions,
...providers,
],
exports: [providers],
exports: providers,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/amqp/createOptionsToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AMQP_CONNECTION_OPTIONS} from './../amqp.constants';
import { AMQP_CONNECTION_OPTIONS } from './../amqp.constants';

export default function createOptionsToken(name: string): string {
return `${AMQP_CONNECTION_OPTIONS}_${name}`;
}
}
4 changes: 2 additions & 2 deletions src/amqp/createProviderToken.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AMQP_PROVIDER } from "../amqp.constants";
import { AMQP_PROVIDER } from '../amqp.constants';

export default function createProviderToken(name: string): string {
return `${AMQP_PROVIDER}_${name}`;
}
}
2 changes: 1 addition & 1 deletion src/decorators/injectampqconnection.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import createProviderToken from '../amqp/createProviderToken';

export default function InjectAmqpConnection(
connectionName: string = 'default',
){
) {
return Inject(createProviderToken(connectionName));
}
4 changes: 3 additions & 1 deletion src/interfaces/amqpconnection-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export interface AmqpConnectionOptions {

export interface AmqpConnectionAsyncOptions {
inject?: any[];
useFactory?: (...args: any[]) => Promise<AmqpConnectionOptions|AmqpConnectionAsyncOptions[]>;
useFactory?: (
...args: any[]
) => Promise<AmqpConnectionOptions | AmqpConnectionAsyncOptions[]>;
}

0 comments on commit ca3c21c

Please sign in to comment.