Skip to content

Commit

Permalink
feat(): add ioredis nestjs module
Browse files Browse the repository at this point in the history
  • Loading branch information
juandav committed Apr 7, 2020
0 parents commit fd1acfc
Show file tree
Hide file tree
Showing 25 changed files with 5,825 additions and 0 deletions.
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',
},
};
52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

# compiled output
/dist
/tmp
/out-tsc

# dependencies
/node_modules

# profiling files
chrome-profiler-events.json
speed-measure-plugin.json

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

# misc
/.sass-cache
/connect.lock
/coverage
/libpeerconnection.log
npm-debug.log
yarn-error.log
testem.log
/typings

# System Files
.DS_Store
Thumbs.db

# Doc
lib/core/metadata.js
lib/core/MetadataBlog.js
website/translated_docs
website/build/
website/yarn.lock
website/node_modules
website/i18n/*
21 changes: 21 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# source
lib
tests
sample
index.ts
package-lock.json
yarn.lock
tsconfig.json
.prettierrc
docs
website

# misc
.commitlintrc.json
.release-it.json
.eslintignore
.eslintrc.js
renovate.json
.prettierignore
.prettierrc
publish.sh
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/generated-definitions/*.ts
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
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2020 Nest modules

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<p align="center">
<a href="http://nestjs.com/" target="blank">
<img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" />
</a>
</p>

<p align="center">
A ioredis module for Nest framework (node.js) using <a href="https://github.com/luin/ioredis">ioredis</a> library
</p>

<p align="center">
<a href="https://www.npmjs.com/org/nestjs-modules"><img src="https://img.shields.io/npm/v/@nestjs-modules/ioredis.svg" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/org/nestjs-modules"><img src="https://img.shields.io/npm/l/@nestjs-modules/ioredis.svg" alt="Package License" /></a>
<a href="https://www.npmjs.com/org/nestjs-modules"><img src="https://img.shields.io/npm/dm/@nestjs-modules/ioredis.svg" alt="NPM Downloads" /></a>
</p>

### Installation

#### with npm
```sh
npm install --save @nestjs-modules/ioredis ioredis
```

#### with yarn
```sh
yarn add @nestjs-modules/ioredis ioredis
```

### How to use?

#### RedisModule.forRoot(options, connection?)

```ts
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-modules/ioredis';
import { AppController } from './app.controller';

@Module({
imports: [
RedisModule.forRoot({
config: {
url: 'redis://localhost:6379',
},
}),
],
controllers: [AppController],
})
export class AppModule {}
```

#### RedisModule.forRootAsync(options, connection?)

```ts
import { Module } from '@nestjs/common';
import { RedisModule } from '@nestjs-modules/ioredis';
import { AppController } from './app.controller';

@Module({
imports: [
RedisModule.forRootAsync({
useFactory: () => ({
config: {
url: 'redis://localhost:6379',
},
}),
}),
],
controllers: [AppController],
})
export class AppModule {}
```

#### InjectRedis(connection?)

```ts
import { Controller, Get, } from '@nestjs/common';
import { InjectRedis, Redis } from '@nestjs-modules/ioredis';

@Controller()
export class AppController {
constructor(
@InjectRedis() private readonly redis: Redis,
) {}

@Get()
async getHello() {
await this.redis.set('key', 'Redis data!');
const redisData = await this.redis.get("key");
return { redisData };
}
}
```

## License

MIT
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist';
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
exports.__esModule = true;
__export(require("./dist"));
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dist';
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './redis.module';
export * from './redis.decorators';
export * from './redis.interfaces';
export * from './redis.utils';
3 changes: 3 additions & 0 deletions lib/redis.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const REDIS_MODULE_CONNECTION = 'default';
export const REDIS_MODULE_CONNECTION_TOKEN = 'IORedisModuleConnectionToken';
export const REDIS_MODULE_OPTIONS_TOKEN = 'IORedisModuleOptionsToken';
96 changes: 96 additions & 0 deletions lib/redis.core-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { DynamicModule, Module, Global, Provider } from '@nestjs/common';
import { RedisModuleAsyncOptions, RedisModuleOptions, RedisModuleOptionsFactory } from './redis.interfaces';
import { createRedisConnection, getRedisOptionsToken, getRedisConnectionToken } from './redis.utils'

@Global()
@Module({})
export class RedisCoreModule {

/* forRoot */
static forRoot(options: RedisModuleOptions, connection?: string): DynamicModule {

const redisOptionsProvider: Provider = {
provide: getRedisOptionsToken(connection),
useValue: options,
};

const redisConnectionProvider: Provider = {
provide: getRedisConnectionToken(connection),
useValue: createRedisConnection(options),
};

return {
module: RedisCoreModule,
providers: [
redisOptionsProvider,
redisConnectionProvider,
],
exports: [
redisOptionsProvider,
redisConnectionProvider,
],
};
}

/* forRootAsync */
public static forRootAsync(options: RedisModuleAsyncOptions, connection: string): DynamicModule {

const redisConnectionProvider: Provider = {
provide: getRedisConnectionToken(connection),
useFactory(options: RedisModuleOptions) {
return createRedisConnection(options)
},
inject: [getRedisOptionsToken(connection)],
};

return {
module: RedisCoreModule,
imports: options.imports,
providers: [...this.createAsyncProviders(options, connection), redisConnectionProvider],
exports: [redisConnectionProvider],
};
}

/* createAsyncProviders */
public static createAsyncProviders(options: RedisModuleAsyncOptions, connection?: string): Provider[] {

if(!(options.useExisting || options.useFactory || options.useClass)) {
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
}

if (options.useExisting || options.useFactory) {
return [
this.createAsyncOptionsProvider(options, connection)
];
}

return [
this.createAsyncOptionsProvider(options, connection),
{ provide: options.useClass, useClass: options.useClass },
];
}

/* createAsyncOptionsProvider */
public static createAsyncOptionsProvider(options: RedisModuleAsyncOptions, connection?: string): Provider {

if(!(options.useExisting || options.useFactory || options.useClass)) {
throw new Error('Invalid configuration. Must provide useFactory, useClass or useExisting');
}

if (options.useFactory) {
return {
provide: getRedisOptionsToken(connection),
useFactory: options.useFactory,
inject: options.inject || [],
};
}

return {
provide: getRedisOptionsToken(connection),
async useFactory(optionsFactory: RedisModuleOptionsFactory): Promise<RedisModuleOptions> {
return await optionsFactory.createRedisModuleOptions();
},
inject: [options.useClass || options.useExisting],
};
}
}
6 changes: 6 additions & 0 deletions lib/redis.decorators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Inject } from '@nestjs/common';
import { getRedisConnectionToken } from './redis.utils';

export const InjectRedis = (connection?: string) => {
return Inject(getRedisConnectionToken(connection));
};
Loading

0 comments on commit fd1acfc

Please sign in to comment.