Skip to content

Commit

Permalink
feat: 后端会在库里记录当前版本信息,方便修改数据结构后的清洗
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Sep 6, 2022
1 parent 470f54d commit 781d747
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
14 changes: 2 additions & 12 deletions packages/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { MetaProvider } from './provider/meta/meta.provider';
// import { ArticleProvider } from './provider/article/article.provider';

import { NestExpressApplication } from '@nestjs/platform-express';
import { config as globalConfig } from './config/index';
import { checkOrCreate } from './utils/checkFolder';
import * as path from 'path';
import { ISRProvider } from './provider/isr/isr.provider';
import { WalineProvider } from './provider/waline/waline.provider';
import { InitProvider } from './provider/init/init.provider';
import fs from 'fs';
import { json } from 'express';
// import { LogProvider } from './provider/log/log.provider';
// import { EventType } from './provider/log/types';

async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(AppModule);
Expand Down Expand Up @@ -46,6 +43,7 @@ async function bootstrap() {
await app.listen(3000);

const initProvider = app.get(InitProvider);
initProvider.initVersion();
if (await initProvider.checkHasInited()) {
const metaProvider = app.get(MetaProvider);
metaProvider.updateTotalWords('首次启动');
Expand All @@ -68,13 +66,5 @@ async function bootstrap() {
console.log('项目主页: https://vanblog.mereith.com');
console.log('开源地址: https://github.mereith/mereithhh/van-blog');
}, 3000);
// 测试用的
// const logProvider = app.get(LogProvider);
// logProvider.searchLog(1, 1, EventType.LOGIN);
// const articleProvider = app.get(ArticleProvider);
// await articleProvider.washViewerInfoToVisitProvider();
// console.log('done');
// const res = await articleProvider.getAllImageLinks();
// console.log(res);
}
bootstrap();
28 changes: 27 additions & 1 deletion packages/server/src/provider/init/init.provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { InitDto } from 'src/types/init.dto';
import { MetaDocument } from 'src/scheme/meta.schema';
import { UserDocument } from 'src/scheme/user.schema';
import { WalineProvider } from '../waline/waline.provider';
import { SettingProvider } from '../setting/setting.provider';
import { version } from '../../utils/loadConfig';

@Injectable()
export class InitProvider {
logger = new Logger(InitProvider.name);
constructor(
@InjectModel('Meta') private metaModel: Model<MetaDocument>,
@InjectModel('User') private userModel: Model<UserDocument>,
private readonly walineProvider: WalineProvider,
private readonly settingProvider: SettingProvider,
) {}

async init(initDto: InitDto) {
Expand Down Expand Up @@ -54,4 +58,26 @@ export class InitProvider {
}
return true;
}
async initVersion() {
if (!version || version == 'dev') {
this.logger.debug('开发版本');
return;
}
try {
const versionSetting = await this.settingProvider.getVersionSetting();
if (!versionSetting || !versionSetting?.version) {
// 没有版本信息,加进去
await this.settingProvider.updateVersionSetting({
version: version,
});
} else {
// TODO 后面这里会判断版本执行一些版本迁移的数据清洗脚本
await this.settingProvider.updateVersionSetting({
version,
});
}
} catch (err) {
this.logger.error(`初始化版本信息失败: ${JSON.stringify(err, null, 2)}`);
}
}
}
28 changes: 25 additions & 3 deletions packages/server/src/provider/setting/setting.provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { forwardRef, Inject, Injectable } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import {
HttpsSetting,
LayoutSetting,
LoginSetting,
SettingType,
StaticSetting,
VersionSetting,
WalineSetting,
} from 'src/types/setting.dto';
import { SettingDocument } from 'src/scheme/setting.schema';
import { PicgoProvider } from '../static/picgo.provider';
import { encode, decode } from 'js-base64';
import { encode } from 'js-base64';

@Injectable()
export class SettingProvider {
Expand All @@ -27,6 +27,13 @@ export class SettingProvider {
}
return null;
}
async getVersionSetting(): Promise<any> {
const res = await this.settingModel.findOne({ type: 'version' }).exec();
if (res) {
return res?.value;
}
return null;
}
async importSetting(setting: any) {
for (const [k, v] of Object.entries(setting)) {
if (k == 'static') {
Expand Down Expand Up @@ -102,6 +109,21 @@ export class SettingProvider {
);
return res;
}
async updateVersionSetting(dto: VersionSetting) {
const oldValue = await this.getVersionSetting();
const newValue = { ...oldValue, ...dto };
if (!oldValue) {
return await this.settingModel.create({
type: 'version',
value: newValue,
});
}
const res = await this.settingModel.updateOne(
{ type: 'version' },
{ value: newValue },
);
return res;
}

async updateWalineSetting(dto: WalineSetting) {
const oldValue = await this.getWalineSetting();
Expand Down
14 changes: 12 additions & 2 deletions packages/server/src/types/setting.dto.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
export type SettingType = 'static' | 'https' | 'waline' | 'layout' | 'login';
export type SettingType =
| 'static'
| 'https'
| 'waline'
| 'layout'
| 'login'
| 'version';
export type SettingValue =
| StaticSetting
| HttpsSetting
| WalineSetting
| LayoutSetting;
| LayoutSetting
| VersionSetting;
export type StorageType = 'picgo' | 'local';
export type StaticType = 'img';
export interface LoginSetting {
enableMaxLoginRetry: boolean;
maxRetryTimes: number;
durationSeconds: number;
}
export interface VersionSetting {
version: string;
}

// export interface ScriptItem {
// type: 'code' | 'link';
Expand Down

0 comments on commit 781d747

Please sign in to comment.