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 { UserService } from '../authority/service/user-service.js';
 | 
			
		||||
import { SysSettingsService } from '../system/service/sys-settings-service.js';
 | 
			
		||||
| 
						 | 
				
			
			@ -17,12 +17,16 @@ export class AutoInitSite {
 | 
			
		|||
  @Inject()
 | 
			
		||||
  userService: UserService;
 | 
			
		||||
 | 
			
		||||
  @Config('typeorm.dataSource.default.type')
 | 
			
		||||
  dbType: string;
 | 
			
		||||
 | 
			
		||||
  @Inject()
 | 
			
		||||
  sysSettingsService: SysSettingsService;
 | 
			
		||||
 | 
			
		||||
  @Init()
 | 
			
		||||
  async init() {
 | 
			
		||||
    logger.info('初始化站点开始');
 | 
			
		||||
    this.startOptimizeDb();
 | 
			
		||||
    //安装信息
 | 
			
		||||
    const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
 | 
			
		||||
    if (!installInfo.siteId) {
 | 
			
		||||
| 
						 | 
				
			
			@ -57,4 +61,17 @@ export class AutoInitSite {
 | 
			
		|||
 | 
			
		||||
    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(
 | 
			
		||||
    key: string,
 | 
			
		||||
    userId: number
 | 
			
		||||
  ): Promise<UserSettingsEntity | null> {
 | 
			
		||||
  async getByKey(key: string, userId: number): Promise<UserSettingsEntity | null> {
 | 
			
		||||
    if (!key || !userId) {
 | 
			
		||||
      return null;
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue