Skip to content

Commit 94f51d7

Browse files
committed
feat: initialize NestJS Redis example project
1 parent 95eeb19 commit 94f51d7

File tree

14 files changed

+397
-0
lines changed

14 files changed

+397
-0
lines changed
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+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<p align="center">
2+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a>
3+
</p>
4+
5+
[circleci-image]: https://img.shields.io/circleci/build/github/nestjs/nest/master?token=abc123def456
6+
[circleci-url]: https://circleci.com/gh/nestjs/nest
7+
8+
<p align="center">A progressive <a href="http://nodejs.org" target="_blank">Node.js</a> framework for building efficient and scalable server-side applications.</p>
9+
<p align="center">
10+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a>
11+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a>
12+
<a href="https://www.npmjs.com/~nestjscore" target="_blank"><img src="https://img.shields.io/npm/dm/@nestjs/common.svg" alt="NPM Downloads" /></a>
13+
<a href="https://circleci.com/gh/nestjs/nest" target="_blank"><img src="https://img.shields.io/circleci/build/github/nestjs/nest/master" alt="CircleCI" /></a>
14+
<a href="https://coveralls.io/github/nestjs/nest?branch=master" target="_blank"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#9" alt="Coverage" /></a>
15+
<a href="https://discord.gg/G7Qnnhy" target="_blank"><img src="https://img.shields.io/badge/discord-online-brightgreen.svg" alt="Discord"/></a>
16+
<a href="https://opencollective.com/nest#backer" target="_blank"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a>
17+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a>
18+
<a href="https://paypal.me/kamilmysliwiec" target="_blank"><img src="https://img.shields.io/badge/Donate-PayPal-ff3f59.svg" alt="Donate us"/></a>
19+
<a href="https://opencollective.com/nest#sponsor" target="_blank"><img src="https://img.shields.io/badge/Support%20us-Open%20Collective-41B883.svg" alt="Support us"></a>
20+
<a href="https://twitter.com/nestframework" target="_blank"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow" alt="Follow us on Twitter"></a>
21+
</p>
22+
<!--[![Backers on Open Collective](https://opencollective.com/nest/backers/badge.svg)](https://opencollective.com/nest#backer)
23+
[![Sponsors on Open Collective](https://opencollective.com/nest/sponsors/badge.svg)](https://opencollective.com/nest#sponsor)-->
24+
25+
## Description
26+
27+
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository.
28+
29+
## Project setup
30+
31+
```bash
32+
$ pnpm install
33+
```
34+
35+
## Compile and run the project
36+
37+
```bash
38+
# development
39+
$ pnpm run start
40+
41+
# watch mode
42+
$ pnpm run start:dev
43+
44+
# production mode
45+
$ pnpm run start:prod
46+
```
47+
48+
## Run tests
49+
50+
```bash
51+
# unit tests
52+
$ pnpm run test
53+
54+
# e2e tests
55+
$ pnpm run test:e2e
56+
57+
# test coverage
58+
$ pnpm run test:cov
59+
```
60+
61+
## Deployment
62+
63+
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the [deployment documentation](https://docs.nestjs.com/deployment) for more information.
64+
65+
If you are looking for a cloud-based platform to deploy your NestJS application, check out [Mau](https://mau.nestjs.com), our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
66+
67+
```bash
68+
$ pnpm install -g mau
69+
$ mau deploy
70+
```
71+
72+
With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
73+
74+
## Resources
75+
76+
Check out a few resources that may come in handy when working with NestJS:
77+
78+
- Visit the [NestJS Documentation](https://docs.nestjs.com) to learn more about the framework.
79+
- For questions and support, please visit our [Discord channel](https://discord.gg/G7Qnnhy).
80+
- To dive deeper and get more hands-on experience, check out our official video [courses](https://courses.nestjs.com/).
81+
- Deploy your application to AWS with the help of [NestJS Mau](https://mau.nestjs.com) in just a few clicks.
82+
- Visualize your application graph and interact with the NestJS application in real-time using [NestJS Devtools](https://devtools.nestjs.com).
83+
- Need help with your project (part-time to full-time)? Check out our official [enterprise support](https://enterprise.nestjs.com).
84+
- To stay in the loop and get updates, follow us on [X](https://x.com/nestframework) and [LinkedIn](https://linkedin.com/company/nestjs).
85+
- Looking for a job, or have a job to offer? Check out our official [Jobs board](https://jobs.nestjs.com).
86+
87+
## Support
88+
89+
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
90+
91+
## Stay in touch
92+
93+
- Author - [Kamil Myśliwiec](https://twitter.com/kammysliwiec)
94+
- Website - [https://nestjs.com](https://nestjs.com/)
95+
- Twitter - [@nestframework](https://twitter.com/nestframework)
96+
97+
## License
98+
99+
Nest is [MIT licensed](https://github.com/nestjs/nest/blob/master/LICENSE).
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// @ts-check
2+
import eslint from '@eslint/js';
3+
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
4+
import globals from 'globals';
5+
import tseslint from 'typescript-eslint';
6+
7+
export default tseslint.config(
8+
{
9+
ignores: ['eslint.config.mjs'],
10+
},
11+
eslint.configs.recommended,
12+
...tseslint.configs.recommendedTypeChecked,
13+
eslintPluginPrettierRecommended,
14+
{
15+
languageOptions: {
16+
globals: {
17+
...globals.node,
18+
...globals.jest,
19+
},
20+
ecmaVersion: 5,
21+
sourceType: 'module',
22+
parserOptions: {
23+
projectService: true,
24+
tsconfigRootDir: import.meta.dirname,
25+
},
26+
},
27+
},
28+
{
29+
rules: {
30+
'@typescript-eslint/no-explicit-any': 'off',
31+
'@typescript-eslint/no-floating-promises': 'warn',
32+
'@typescript-eslint/no-unsafe-argument': 'warn',
33+
},
34+
},
35+
);
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+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"name": "nest-redis-example",
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-labs/nestjs-redis": "workspace:^",
24+
"@nestjs/common": "11.0.1",
25+
"@nestjs/core": "11.0.1",
26+
"@nestjs/platform-express": "^11.0.1",
27+
"redis": "^5.6.0",
28+
"reflect-metadata": "^0.2.2",
29+
"rxjs": "^7.8.1"
30+
},
31+
"devDependencies": {
32+
"@eslint/eslintrc": "^3.2.0",
33+
"@eslint/js": "^9.18.0",
34+
"@nestjs/cli": "^11.0.0",
35+
"@nestjs/schematics": "^11.0.0",
36+
"@nestjs/testing": "^11.0.1",
37+
"@swc/cli": "^0.6.0",
38+
"@swc/core": "^1.10.7",
39+
"@types/express": "^5.0.0",
40+
"@types/jest": "^29.5.14",
41+
"@types/node": "^22.10.7",
42+
"@types/supertest": "^6.0.2",
43+
"eslint": "^9.18.0",
44+
"eslint-config-prettier": "^10.0.1",
45+
"eslint-plugin-prettier": "^5.2.2",
46+
"globals": "^16.0.0",
47+
"jest": "^29.7.0",
48+
"prettier": "^3.4.2",
49+
"source-map-support": "^0.5.21",
50+
"supertest": "^7.0.0",
51+
"ts-jest": "^29.2.5",
52+
"ts-loader": "^9.5.2",
53+
"ts-node": "^10.9.2",
54+
"tsconfig-paths": "^4.2.0",
55+
"typescript": "^5.7.3",
56+
"typescript-eslint": "^8.20.0"
57+
},
58+
"jest": {
59+
"moduleFileExtensions": [
60+
"js",
61+
"json",
62+
"ts"
63+
],
64+
"rootDir": "src",
65+
"testRegex": ".*\\.spec\\.ts$",
66+
"transform": {
67+
"^.+\\.(t|j)s$": "ts-jest"
68+
},
69+
"collectCoverageFrom": [
70+
"**/*.(t|j)s"
71+
],
72+
"coverageDirectory": "../coverage",
73+
"testEnvironment": "node"
74+
}
75+
}
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+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Body, Controller, Get, Post, Query } 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('hello')
9+
async getHello() {
10+
return await this.appService.getHello();
11+
}
12+
13+
@Get('redis-info')
14+
async getRedisInfo() {
15+
return await this.appService.getRedisInfo();
16+
}
17+
18+
@Post('set-key')
19+
async setKey(@Body() body: { key: string; value: string }) {
20+
const { key, value } = body;
21+
return await this.appService.setKey(key, value);
22+
}
23+
24+
@Get('get-key')
25+
async getKey(@Query('key') key: string) {
26+
return await this.appService.getKey(key);
27+
}
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Module } from '@nestjs/common';
2+
import { RedisModule } from '@nestjs-labs/nestjs-redis';
3+
4+
import { AppController } from './app.controller';
5+
import { AppService } from './app.service';
6+
7+
@Module({
8+
imports: [
9+
RedisModule.forRoot({
10+
readonly: false,
11+
url: 'redis://localhost:6379',
12+
}),
13+
],
14+
controllers: [AppController],
15+
providers: [AppService],
16+
})
17+
export class AppModule {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { RedisService } from '@nestjs-labs/nestjs-redis';
3+
4+
@Injectable()
5+
export class AppService {
6+
constructor(private readonly redisService: RedisService) {}
7+
8+
async getHello(): Promise<string | null> {
9+
const redis = this.redisService.getClient();
10+
await redis.set('test-key', 'Hello from Redis!');
11+
return await redis.get('test-key');
12+
}
13+
14+
async getRedisInfo() {
15+
const redis = this.redisService.getClient();
16+
const info = await redis.info();
17+
return {
18+
message: 'Redis connection successful',
19+
serverInfo: info.split('\r\n').slice(0, 10), // 只返回前10行信息
20+
};
21+
}
22+
23+
async setKey(key: string, value: string): Promise<string> {
24+
const client = this.redisService.getClient();
25+
await client.set(key, value);
26+
return `Key "${key}" set successfully`;
27+
}
28+
29+
async getKey(key: string): Promise<string | null> {
30+
const client = this.redisService.getClient();
31+
return await client.get(key);
32+
}
33+
34+
async deleteKey(key: string): Promise<string> {
35+
const client = this.redisService.getClient();
36+
const result = await client.del(key);
37+
return result > 0
38+
? `Key "${key}" deleted successfully`
39+
: `Key "${key}" not found`;
40+
}
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NestFactory } from '@nestjs/core';
2+
import { AppModule } from './app.module';
3+
4+
async function bootstrap() {
5+
const app = await NestFactory.create(AppModule);
6+
await app.listen(process.env.PORT ?? 3000);
7+
}
8+
9+
void bootstrap();

0 commit comments

Comments
 (0)