Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions apps/nest-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,24 @@
"homePage": "https://142vip.cn"
},
"scripts": {
"dev": "nest start --watch",
"dev": "nest start -d -w",
"dev:debug": "nest start --debug --watch",
"dev:prod": "node dist/main",
"build": "nest build",
"start": "nest start"
},
"dependencies": {
"@142vip/nest-redis": "workspace:*",
"@nestjs/common": "11.1.6",
"@nestjs/core": "11.1.6",
"@nestjs/platform-express": "11.1.6",
"reflect-metadata": "0.2.2",
"rxjs": "7.8.1"
},
"devDependencies": {
"@nestjs/cli": "11.0.0",
"@nestjs/schematics": "11.0.0",
"@nestjs/testing": "11.1.6",
"@nestjs/cli": "11.0.10",
"@nestjs/schematics": "11.0.9",
"@nestjs/testing": "11.1.7",
"@types/express": "5.0.0",
"@types/jest": "30.0.0",
"@types/supertest": "6.0.2",
Expand Down
14 changes: 0 additions & 14 deletions apps/nest-demo/src/app.controller.ts

This file was deleted.

13 changes: 9 additions & 4 deletions apps/nest-demo/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { RedisModule } from '@142vip/nest-redis'
import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { RedisExampleModule } from './redis-example/redis-example.module'

@Module({
controllers: [AppController],
providers: [AppService],
imports: [
// 全局模块
RedisModule.register({ url: 'redis://localhost:6379' }),

// 最佳实践
RedisExampleModule,
],
})
export class AppModule {}
8 changes: 0 additions & 8 deletions apps/nest-demo/src/app.service.ts

This file was deleted.

24 changes: 24 additions & 0 deletions apps/nest-demo/src/redis-example/redis-example.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Controller, Delete, Get, Post } from '@nestjs/common'
import { RedisExampleService } from './redis-example.service'

@Controller('redis-example')
export class RedisExampleController {
constructor(
private readonly redisExampleService: RedisExampleService,
) {}

@Post('/')
public async 'Post /'(): Promise<void> {
await this.redisExampleService.setKey()
}

@Get('/')
public async 'Get /'(): Promise<string | null> {
return await this.redisExampleService.getKey()
}

@Delete('/')
public async 'Delete /'(): Promise<void> {
await this.redisExampleService.delKey()
}
}
9 changes: 9 additions & 0 deletions apps/nest-demo/src/redis-example/redis-example.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common'
import { RedisExampleController } from './redis-example.controller'
import { RedisExampleService } from './redis-example.service'

@Module({
controllers: [RedisExampleController],
providers: [RedisExampleService],
})
export class RedisExampleModule {}
23 changes: 23 additions & 0 deletions apps/nest-demo/src/redis-example/redis-example.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisService } from '@142vip/nest-redis'
import { Injectable } from '@nestjs/common'

const storeExample = { key: 'test', value: 'redis-example' }

@Injectable()
export class RedisExampleService {
constructor(
private readonly redisService: RedisService,
) { }

async setKey(): Promise<void> {
await this.redisService.setEx(storeExample.key, storeExample.value, 5)
}

async getKey(): Promise<string | null> {
return await this.redisService.getEx(storeExample.key)
}

async delKey(): Promise<void> {
await this.redisService.del(storeExample.key)
}
}
31 changes: 6 additions & 25 deletions apps/nest-demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
{
"extends": "../../tsconfig.nest-pkg.json",
"compilerOptions": {
"incremental": true,
"target": "ES2023",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"baseUrl": "./",
"module": "nodenext",
"moduleResolution": "nodenext",
"resolvePackageJsonExports": true,
"strictBindCallApply": false,
"strictNullChecks": true,
"noFallthroughCasesInSwitch": false,
"noImplicitAny": false,
"declaration": true,
"outDir": "./dist",
"removeComments": true,
"sourceMap": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"skipLibCheck": true
"declaration": false,
"outDir": "dist"
},
"exclude": [
"node_modules",
"../src/test",
"dist",
"**/*spec.ts"
]
"dist"
],
"compileOnSave": true
}
57 changes: 57 additions & 0 deletions packages/nest-redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,63 @@ npm install @142vip/nest-redis
pnpm i @142vip/nest-redis
```

## 配置

```typescript
import { RedisConfig } from '@142vip/redis'

// 简单配置
const config: RedisConfig = {
url: 'redis://localhost:6379',
}

// 集群配置
const clusterConfig: RedisConfig = {
clusterNodes: [
{
url: 'redis://localhost:6379',
},
{
url: 'redis://localhost:6380',
},
],
}
// ...
```

## 使用

### 模块注入
```typescript
import { RedisModule } from '@142vip/nest-redis'

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

### 使用服务
```typescript
import { RedisService } from '@142vip/nest-redis'

@Injectable()
export class AppService {
constructor(
private readonly redisService: RedisService
) {}
}
```

`RedisService`类实例化后,可以直接使用类对应的方法,代码的最佳实践,可以参考模块:[redis-example](https://github.com/142vip/core-x/apps/nest-demo/src/core/redis-example)

## 参考

- [NPM @142vip/redis](https://www.npmjs.com/package/@142vip/redis)
- [Redis 官网](https://redis.io/)

## 证书

[MIT](https://opensource.org/license/MIT)
Expand Down
13 changes: 0 additions & 13 deletions packages/nest-redis/build.config.ts

This file was deleted.

15 changes: 15 additions & 0 deletions packages/nest-redis/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createDefaultPreset } from 'ts-jest'

const tsJestTransformCfg = createDefaultPreset().transform

/** @type {import("jest").Config} **/
export default {
testEnvironment: 'node',
transform: {
...tsJestTransformCfg,
},
testMatch: [
// "**/__tests__/**/*.[jt]s?(x)",
'**/test/**/?(*.)+(spec|test).[tj]s?(x)',
],
}
22 changes: 15 additions & 7 deletions packages/nest-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
"import": "./dist/index.js",
"require": "./dist/index.js"
}
},
"authorInfo": {
Expand All @@ -33,21 +33,29 @@
"url": "https://github.com/142vip",
"homePage": "https://142vip.cn"
},
"main": "./dist/index.mjs",
"module": "./dist/index.mjs",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"build": "unbuild",
"typecheck": "tsc --noEmit"
"build": "npx nest build",
"typecheck": "tsc --noEmit",
"test": "npx jest",
"test:coverage": "npx jest --coverage"
},
"dependencies": {
"@142vip/utils": "workspace:*"
"@142vip/redis": "workspace:*",
"@nestjs/common": "11.1.7",
"@nestjs/core": "11.1.7"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"devDependencies": {
"@nestjs/cli": "11.0.10",
"@nestjs/testing": "11.1.7"
}
}
4 changes: 4 additions & 0 deletions packages/nest-redis/src/core/redis.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* redis client Token 唯一标记
*/
export const REDIS_CLIENT_TOKEN = Symbol('@142vip/nest-redis#client-token')
9 changes: 9 additions & 0 deletions packages/nest-redis/src/core/redis.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Inject } from '@nestjs/common'
import { REDIS_CLIENT_TOKEN } from './redis.constants'

/**
* redis client 装饰器
*/
export function InjectRedisClient(): PropertyDecorator & ParameterDecorator {
return Inject(REDIS_CLIENT_TOKEN)
}
27 changes: 27 additions & 0 deletions packages/nest-redis/src/core/redis.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { RedisConfig } from '@142vip/redis'
import { DynamicModule, Module } from '@nestjs/common'
import { REDIS_CLIENT_TOKEN } from './redis.constants'
import { RedisService } from './redis.service'

@Module({})
export class RedisModule {
public static register(config: RedisConfig): DynamicModule {
const redisService = new RedisService(config)
const providers = [
{
provide: RedisService,
useValue: redisService,
},
{
provide: REDIS_CLIENT_TOKEN,
useValue: redisService.getClient(),
},
]
return {
module: RedisModule,
providers,
exports: providers,
global: true,
}
}
}
Loading
Loading