36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import 'reflect-metadata'
|
|
import { DataSource } from 'typeorm'
|
|
import { app } from 'electron'
|
|
import path from 'path'
|
|
import { ReadingReflectionTaskBatch } from './entities/ReadingReflectionTaskBatch'
|
|
import { ReadingReflectionTaskItem } from './entities/ReadingReflectionTaskItem'
|
|
import { ReadingPersona } from './entities/ReadingPersona' // 必须导入
|
|
|
|
const dbPath = app.isPackaged
|
|
? path.join(app.getPath('userData'), 'reflections.db')
|
|
: path.join(process.cwd(), 'db.sqlite')
|
|
|
|
export const AppDataSource = new DataSource({
|
|
type: 'better-sqlite3', // better-sqlite3 性能优于 sqlite3
|
|
database: dbPath,
|
|
synchronize: true,
|
|
logging: process.env.NODE_ENV === 'development', // 仅开发环境开启日志
|
|
entities: [
|
|
ReadingReflectionTaskBatch,
|
|
ReadingReflectionTaskItem,
|
|
ReadingPersona // 注册实体
|
|
]
|
|
})
|
|
|
|
export const initDB = async () => {
|
|
if (!AppDataSource.isInitialized) {
|
|
try {
|
|
await AppDataSource.initialize()
|
|
console.log('Database initialized successfully at:', dbPath)
|
|
} catch (err) {
|
|
console.error('Error during Data Source initialization', err)
|
|
}
|
|
}
|
|
return AppDataSource
|
|
}
|