80 lines
2.1 KiB
TypeScript
80 lines
2.1 KiB
TypeScript
|
|
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 { app } from 'electron'
|
|||
|
|
import { BookTaskModel } from '../../model/bookTask'
|
|||
|
|
import { PresetModel } from '../../model/preset'
|
|||
|
|
|
|||
|
|
// 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'), 'Database/option.realm') // Production path
|
|||
|
|
|
|||
|
|
// 版本迁移
|
|||
|
|
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: 19, // 数据库版本号,修改时需要增加
|
|||
|
|
migration: migration
|
|||
|
|
}
|
|||
|
|
this.realm = await Realm.open(config)
|
|||
|
|
} catch (error) {
|
|||
|
|
throw error
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|