Skip to content

Commit 6ba665e

Browse files
feat: nest redis
1 parent 4c2080f commit 6ba665e

20 files changed

+377
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@
5151
[typeorm 多对多映射](./typeorm-relation-mapping3)
5252

5353
[Nest 集成 typeorm](./nest-typeorm)
54+
55+
[Node 操作 redis](./redis-node-test)
56+
57+
[Nest 操作 redis](./nest-redis)

nest-redis/.eslintrc.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
tsconfigRootDir: __dirname,
6+
sourceType: 'module',
7+
},
8+
plugins: ['@typescript-eslint/eslint-plugin'],
9+
extends: [
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:prettier/recommended',
12+
],
13+
root: true,
14+
env: {
15+
node: true,
16+
jest: true,
17+
},
18+
ignorePatterns: ['.eslintrc.js'],
19+
rules: {
20+
'@typescript-eslint/interface-name-prefix': 'off',
21+
'@typescript-eslint/explicit-function-return-type': 'off',
22+
'@typescript-eslint/explicit-module-boundary-types': 'off',
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
},
25+
};

nest-redis/.gitignore

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
pnpm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
14+
# OS
15+
.DS_Store
16+
17+
# Tests
18+
/coverage
19+
/.nyc_output
20+
21+
# IDEs and editors
22+
/.idea
23+
.project
24+
.classpath
25+
.c9/
26+
*.launch
27+
.settings/
28+
*.sublime-workspace
29+
30+
# IDE - VSCode
31+
.vscode/*
32+
!.vscode/settings.json
33+
!.vscode/tasks.json
34+
!.vscode/launch.json
35+
!.vscode/extensions.json

nest-redis/.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

nest-redis/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# nest + redis
2+
3+
npm install
4+
5+
修改 redis 连接配置
6+
7+
npm run start:dev

nest-redis/nest-cli.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

nest-redis/package.json

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "nest-redis",
3+
"version": "0.0.1",
4+
"description": "",
5+
"author": "",
6+
"private": true,
7+
"license": "UNLICENSED",
8+
"scripts": {
9+
"build": "nest build",
10+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
11+
"start": "nest start",
12+
"start:dev": "nest start --watch",
13+
"start:debug": "nest start --debug --watch",
14+
"start:prod": "node dist/main",
15+
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
16+
"test": "jest",
17+
"test:watch": "jest --watch",
18+
"test:cov": "jest --coverage",
19+
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
20+
"test:e2e": "jest --config ./test/jest-e2e.json"
21+
},
22+
"dependencies": {
23+
"@nestjs/common": "^9.0.0",
24+
"@nestjs/core": "^9.0.0",
25+
"@nestjs/platform-express": "^9.0.0",
26+
"redis": "^4.6.7",
27+
"reflect-metadata": "^0.1.13",
28+
"rxjs": "^7.2.0"
29+
},
30+
"devDependencies": {
31+
"@nestjs/cli": "^9.0.0",
32+
"@nestjs/schematics": "^9.0.0",
33+
"@nestjs/testing": "^9.0.0",
34+
"@types/express": "^4.17.13",
35+
"@types/jest": "29.5.0",
36+
"@types/node": "18.15.11",
37+
"@types/supertest": "^2.0.11",
38+
"@typescript-eslint/eslint-plugin": "^5.0.0",
39+
"@typescript-eslint/parser": "^5.0.0",
40+
"eslint": "^8.0.1",
41+
"eslint-config-prettier": "^8.3.0",
42+
"eslint-plugin-prettier": "^4.0.0",
43+
"jest": "29.5.0",
44+
"prettier": "^2.3.2",
45+
"source-map-support": "^0.5.20",
46+
"supertest": "^6.1.3",
47+
"ts-jest": "29.0.5",
48+
"ts-loader": "^9.2.3",
49+
"ts-node": "^10.0.0",
50+
"tsconfig-paths": "4.2.0",
51+
"typescript": "^4.7.4"
52+
},
53+
"jest": {
54+
"moduleFileExtensions": [
55+
"js",
56+
"json",
57+
"ts"
58+
],
59+
"rootDir": "src",
60+
"testRegex": ".*\\.spec\\.ts$",
61+
"transform": {
62+
"^.+\\.(t|j)s$": "ts-jest"
63+
},
64+
"collectCoverageFrom": [
65+
"**/*.(t|j)s"
66+
],
67+
"coverageDirectory": "../coverage",
68+
"testEnvironment": "node"
69+
}
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { AppController } from './app.controller';
3+
import { AppService } from './app.service';
4+
5+
describe('AppController', () => {
6+
let appController: AppController;
7+
8+
beforeEach(async () => {
9+
const app: TestingModule = await Test.createTestingModule({
10+
controllers: [AppController],
11+
providers: [AppService],
12+
}).compile();
13+
14+
appController = app.get<AppController>(AppController);
15+
});
16+
17+
describe('root', () => {
18+
it('should return "Hello World!"', () => {
19+
expect(appController.getHello()).toBe('Hello World!');
20+
});
21+
});
22+
});

nest-redis/src/app.controller.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { AppService } from './app.service';
3+
4+
@Controller()
5+
export class AppController {
6+
constructor(private readonly appService: AppService) {}
7+
8+
@Get()
9+
async getHello() {
10+
return await this.appService.getHello();
11+
}
12+
}

nest-redis/src/app.module.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Module } from '@nestjs/common';
2+
import { AppController } from './app.controller';
3+
import { AppService } from './app.service';
4+
import { createClient } from 'redis';
5+
6+
@Module({
7+
imports: [],
8+
controllers: [AppController],
9+
providers: [
10+
AppService,
11+
{
12+
provide: 'REDIS_CLIENT',
13+
async useFactory() {
14+
const client = createClient({
15+
socket: {
16+
host: 'localhost',
17+
port: 6379
18+
}
19+
});
20+
await client.connect();
21+
return client;
22+
}
23+
}
24+
],
25+
})
26+
export class AppModule {}

0 commit comments

Comments
 (0)