mirror of https://github.com/certd/certd
				
				
				
			pref: 自动优化数据库,释放被删除空间
							parent
							
								
									952e01ab7d
								
							
						
					
					
						commit
						869e14bad9
					
				| 
						 | 
					@ -1,4 +1,4 @@
 | 
				
			||||||
import { Autoload, Init, Inject, Scope, ScopeEnum } from '@midwayjs/core';
 | 
					import { Autoload, Config, Init, Inject, Scope, ScopeEnum } from '@midwayjs/core';
 | 
				
			||||||
import { logger } from '../../utils/logger.js';
 | 
					import { logger } from '../../utils/logger.js';
 | 
				
			||||||
import { UserService } from '../authority/service/user-service.js';
 | 
					import { UserService } from '../authority/service/user-service.js';
 | 
				
			||||||
import { SysSettingsService } from '../system/service/sys-settings-service.js';
 | 
					import { SysSettingsService } from '../system/service/sys-settings-service.js';
 | 
				
			||||||
| 
						 | 
					@ -17,12 +17,16 @@ export class AutoInitSite {
 | 
				
			||||||
  @Inject()
 | 
					  @Inject()
 | 
				
			||||||
  userService: UserService;
 | 
					  userService: UserService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  @Config('typeorm.dataSource.default.type')
 | 
				
			||||||
 | 
					  dbType: string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Inject()
 | 
					  @Inject()
 | 
				
			||||||
  sysSettingsService: SysSettingsService;
 | 
					  sysSettingsService: SysSettingsService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @Init()
 | 
					  @Init()
 | 
				
			||||||
  async init() {
 | 
					  async init() {
 | 
				
			||||||
    logger.info('初始化站点开始');
 | 
					    logger.info('初始化站点开始');
 | 
				
			||||||
 | 
					    this.startOptimizeDb();
 | 
				
			||||||
    //安装信息
 | 
					    //安装信息
 | 
				
			||||||
    const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
 | 
					    const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
 | 
				
			||||||
    if (!installInfo.siteId) {
 | 
					    if (!installInfo.siteId) {
 | 
				
			||||||
| 
						 | 
					@ -57,4 +61,17 @@ export class AutoInitSite {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logger.info('初始化站点完成');
 | 
					    logger.info('初始化站点完成');
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  startOptimizeDb() {
 | 
				
			||||||
 | 
					    //优化数据库
 | 
				
			||||||
 | 
					    //检查当前数据库类型为sqlite
 | 
				
			||||||
 | 
					    if (this.dbType === 'better-sqlite3') {
 | 
				
			||||||
 | 
					      const optimizeDb = () => {
 | 
				
			||||||
 | 
					        this.userService.repository.query('VACUUM');
 | 
				
			||||||
 | 
					        logger.info('sqlite数据库空间优化完成');
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					      optimizeDb();
 | 
				
			||||||
 | 
					      setInterval(optimizeDb, 1000 * 60 * 60 * 24);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -30,10 +30,7 @@ export class UserSettingsService extends BaseService<UserSettingsEntity> {
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  async getByKey(
 | 
					  async getByKey(key: string, userId: number): Promise<UserSettingsEntity | null> {
 | 
				
			||||||
    key: string,
 | 
					 | 
				
			||||||
    userId: number
 | 
					 | 
				
			||||||
  ): Promise<UserSettingsEntity | null> {
 | 
					 | 
				
			||||||
    if (!key || !userId) {
 | 
					    if (!key || !userId) {
 | 
				
			||||||
      return null;
 | 
					      return null;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue