修复全局模块,创建数据库连接池

master
expressgy 2 years ago
parent ecd9d2b8f4
commit c406590923
  1. 9
      config/development.ts
  2. 9
      config/production.ts
  3. 8
      src/GDATABASE/gdatabase.module.ts
  4. 29
      src/GDATABASE/gdatabase.service.ts
  5. 1
      src/GLOGGER/glogger.module.ts
  6. 14
      src/app.module.ts
  7. 29
      src/starlight/starlight.controller.ts

@ -14,4 +14,13 @@ export default {
log: { log: {
level: 'all', level: 'all',
}, },
databases: {
starLight: {
host: 'localhost',
port: 3306,
username: 'root',
password: 'Hxl1314521',
database: 'Starlight',
},
},
}; };

@ -14,4 +14,13 @@ export default {
log: { log: {
level: 'info', level: 'info',
}, },
databases: {
starLight: {
host: 'localhost',
port: 3306,
username: 'root',
password: 'Hxl1314521',
database: 'Starlight',
},
},
}; };

@ -0,0 +1,8 @@
import { Global, Module } from '@nestjs/common';
import { GdatabaseService } from './gdatabase.service';
@Global()
@Module({
providers: [GdatabaseService],
exports: [GdatabaseService],
})
export class GdatabaseModule {}

@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import * as mysql from 'mysql2/promise';
import config from '../../config';
@Injectable()
export class GdatabaseService {
public DB;
constructor() {
this.start();
}
private async start() {
const DBConfig = config().databases.starLight;
const DB = mysql.createPool({
host: DBConfig.host,
port: DBConfig.port,
user: DBConfig.username,
password: DBConfig.password,
database: DBConfig.database,
connectionLimit: 20, // 用于指定连接池中最大的链接数,默认属性值为10.
multipleStatements: true, //是否允许执行多条sql语句,默认值为false
waitForConnections: true, // 超过最大连接时排队
queueLimit: 0, // 排队最大数量(0 代表不做限制)
maxIdle: 20, // 最大空闲连接数
idleTimeout: 60000, // 空闲连接超时,以毫秒为单位,默认值60000
});
this.DB = DB;
return DB;
}
}

@ -3,5 +3,6 @@ import { GloggerService } from './glogger.service';
@Global() @Global()
@Module({ @Module({
providers: [GloggerService], providers: [GloggerService],
exports: [GloggerService],
}) })
export class GloggerModule {} export class GloggerModule {}

@ -1,10 +1,11 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { StarlightModule } from './starlight/starlight.module';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
// import { ResponseInterceptorModule } from './response-interceptor/response-interceptor.module'; import { GloggerModule } from './GLOGGER/glogger.module';
import { GloggerModule } from './glogger/glogger.module'; import { StarlightModule } from './starlight/starlight.module';
import { GdevinterceptorModule } from './gdevinterceptor/gdevinterceptor.module';
import config from '../config'; import config from '../config';
import { GloggerService } from './GLOGGER/glogger.service';
import { GdatabaseModule } from './GDATABASE/gdatabase.module';
import { GdatabaseService } from './GDATABASE/gdatabase.service';
@Module({ @Module({
imports: [ imports: [
@ -12,10 +13,9 @@ import config from '../config';
isGlobal: true, // 作用于全局 isGlobal: true, // 作用于全局
load: [config], // 加载自定义配置项 load: [config], // 加载自定义配置项
}), }),
StarlightModule,
GloggerModule, GloggerModule,
// GdevinterceptorModule, StarlightModule,
// ResponseInterceptorModule, GdatabaseModule,
], ],
controllers: [], controllers: [],
providers: [], providers: [],

@ -8,15 +8,18 @@ import {
Delete, Delete,
UseGuards, UseGuards,
Req, Req,
Inject,
} from '@nestjs/common'; } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ApiTags } from '@nestjs/swagger';
import { StarlightGuard } from './starlight.guard';
import { StarlightService } from './starlight.service'; import { StarlightService } from './starlight.service';
import { CreateStarlightDto } from './dto/create-starlight.dto'; import { CreateStarlightDto } from './dto/create-starlight.dto';
import { UpdateStarlightDto } from './dto/update-starlight.dto'; import { UpdateStarlightDto } from './dto/update-starlight.dto';
import { StarlightGuard } from './starlight.guard';
import { ApiTags } from '@nestjs/swagger'; import { GloggerService } from '../GLOGGER/glogger.service';
// import { LoggerService } from '../Logger/logger.service'; import { GdatabaseService } from '../GDATABASE/gdatabase.service';
import { ConfigService } from '@nestjs/config';
// import { LogService } from '../log/log.service';
@ApiTags('starlight') @ApiTags('starlight')
@Controller('starlight') @Controller('starlight')
@ -24,9 +27,10 @@ import { ConfigService } from '@nestjs/config';
@UseGuards(StarlightGuard) @UseGuards(StarlightGuard)
export class StarlightController { export class StarlightController {
constructor( constructor(
private readonly config: ConfigService,
private readonly starlightService: StarlightService, private readonly starlightService: StarlightService,
// private readonly logger: LoggerService, private readonly logger: GloggerService,
private readonly config: ConfigService, // private readonly logService: LogService, private readonly database: GdatabaseService,
) {} ) {}
@Post() @Post()
@ -35,10 +39,13 @@ export class StarlightController {
} }
@Get() @Get()
findAll() { async findAll() {
// this.logger.error(this.config.get('master')); this.logger.debug(this.config.get('master'));
// this.logger.warn('xsxs'); this.logger.debug('xsxs');
// this.logService.getName(); const [rows, fields] = await this.database.DB.execute(
`SELECT * from user_info_base;`,
);
this.logger.debug(rows);
return this.starlightService.findAll(); return this.starlightService.findAll();
} }

Loading…
Cancel
Save