80 lines
2.1 KiB
TypeScript
Raw Normal View History

2025-08-19 14:33:59 +08:00
import Realm from 'realm'
import { BaseService } from './baseService'
import path from 'path'
import { BookModel } from '../../model/book'
import {
BookTaskDetailModel,
MJMessage,
ReversePrompt,
SDConfig,
Subtitle,
VideoMessage,
WebuiConfig
} from '../../model/bookTaskDetail'
import { TaskListModel } from '../../model/taskList'
import { OptionModel } from '../../model/options'
import { BookTaskModel } from '../../model/bookTask'
import { PresetModel } from '../../model/preset'
const { app } = require('electron')
2025-08-19 14:33:59 +08:00
// Determine database path based on environment
const isDev = !app.isPackaged
let dbPath = isDev
? path.resolve(process.cwd(), 'Database/option.realm') // Development path
: path.resolve(app.getPath('userData'), '../laitool-pro/Database/option.realm') // Production path
2025-08-19 14:33:59 +08:00
// 版本迁移
const migration = (_oldRealm: Realm, _newRealm: Realm) => {}
export class RealmBaseService extends BaseService {
static instance: RealmBaseService | null = null
protected realm: Realm | null = null
dbpath: string
protected constructor() {
super()
this.dbpath = dbPath
}
public static async getInstance() {
if (RealmBaseService.instance === null) {
RealmBaseService.instance = new RealmBaseService()
await RealmBaseService.instance.open()
}
return RealmBaseService.instance
}
/**
*
* @returns
*/
async open() {
try {
if (this.realm != null) return
// 判断当前全局是不是又当前这个
const config = {
schema: [
BookModel,
BookTaskDetailModel,
TaskListModel,
OptionModel,
MJMessage,
VideoMessage,
WebuiConfig,
SDConfig,
ReversePrompt,
Subtitle,
BookTaskModel,
PresetModel
],
path: this.dbpath,
schemaVersion: 21, // 数据库版本号,修改时需要增加
2025-08-19 14:33:59 +08:00
migration: migration
}
this.realm = await Realm.open(config)
} catch (error) {
throw error
}
}
}