import Realm from 'realm' import { BaseService } from '../baseService' import { define } from '../../../define' import path from 'path' import { ComponentSize, SoftwareThemeType } from '../../../enum/softwareEnum' import { SoftwareModel } from '../../model/SoftWare/software' import { LoggerModel } from '../../model/SoftWare/logger' import { APIMjModel, BrowserMJModel, MjSettingModel, RemoteMJModel } from '../../model/SoftWare/mjSetting' import { MJImageType, MJRobotType } from '../../../enum/mjEnum' const { v4: uuidv4 } = require('uuid') let dbPath = path.resolve(define.db_path, 'software.realm') // 版本迁移 const migration = (oldRealm: Realm, newRealm: Realm) => { const oldBooks = oldRealm.objects('Software') const newBooks = newRealm.objects('Software') if (oldRealm.schemaVersion < 1) { } // 第二个版本 if (oldRealm.schemaVersion < 2) { newRealm.write(() => { const newSoftwares = newRealm.objects('Software') for (let software of newSoftwares) { software.theme = SoftwareThemeType.LIGHT // 为新属性设置默认值 } }) } // 第三个版本 if (oldRealm.schemaVersion < 3) { newRealm.write(() => { const newSoftwares = newRealm.objects('Software') for (let software of newSoftwares) { software.reverse_display_show = false software.reverse_show_book_striped = false software.reverse_data_table_size = ComponentSize.SMALL } }) } if (oldRealm.schemaVersion < 6) { newRealm.write(() => { const newSoftwares = newRealm.objects('MjSetting') for (let software of newSoftwares) { software.requestModel = MJImageType.API_MJ } }) } if (oldRealm.schemaVersion < 8) { newRealm.write(() => { const newSoftwares = newRealm.objects('MjSetting') for (let software of newSoftwares) { software.imageModel = MJRobotType.MJ } }) } if (oldRealm.schemaVersion < 9) { newRealm.write(() => { const newSoftwares = newRealm.objects('MjSetting') for (let software of newSoftwares) { software.accountId = null } }) } if (oldRealm.schemaVersion < 10) { newRealm.write(() => { const newSoftwares = newRealm.objects('MjSetting') for (let software of newSoftwares) { software.accountId = null } }) } } export class BaseSoftWareService extends BaseService { static instance: BaseSoftWareService | null = null protected realm: Realm dbpath: string protected constructor() { super() this.dbpath = dbPath } public static async getInstance() { if (BaseSoftWareService.instance === null) { BaseSoftWareService.instance = new BaseSoftWareService() await BaseSoftWareService.instance.open() } return BaseSoftWareService.instance } /** * 创建数据库连接,如果已经存在则直接返回 * @param dbPath 数据库文件地址 * @returns */ async open() { try { if (this.realm != null) return let config = { schema: [ SoftwareModel, LoggerModel, BrowserMJModel, RemoteMJModel, APIMjModel, MjSettingModel ], path: dbPath, schemaVersion: 10, // 当前版本号 migration: migration } // 判断当前全局是不是又当前这个 this.realm = await Realm.open(config) // 判断当前是不是有数据,一条都没有的话添加一条空白数据 if (this.realm.objects('Software').length == 0) { this.realm.write(() => { this.realm.create('Software', { id: uuidv4(), theme: SoftwareThemeType.LIGHT // 默认是亮色主题 }) }) } } catch (error) { console.log(error) } } }