fix:基本实现相关功能
This commit is contained in:
30
src/main/db/data-source.ts
Normal file
30
src/main/db/data-source.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import 'reflect-metadata'
|
||||
import { DataSource } from 'typeorm'
|
||||
import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import { ReadingReflectionTaskBatch } from '@main/db/entities/ReadingReflectionTaskBatch'
|
||||
import { ReadingReflectionTaskItem } from '@main/db/entities/ReadingReflectionTaskItem'
|
||||
|
||||
const dbPath = app.isPackaged
|
||||
? path.join(app.getPath('userData'), 'reflections.db')
|
||||
: path.join(process.cwd(), 'db.sqlite')
|
||||
console.log('--- 数据库存储绝对路径 ---')
|
||||
console.log(dbPath)
|
||||
console.log('-----------------------')
|
||||
export const AppDataSource = new DataSource({
|
||||
type: 'better-sqlite3',
|
||||
database: dbPath,
|
||||
synchronize: true, // 开发环境下自动同步表结构
|
||||
logging: true,
|
||||
entities: [ReadingReflectionTaskBatch, ReadingReflectionTaskItem],
|
||||
migrations: [],
|
||||
subscribers: []
|
||||
})
|
||||
|
||||
// 初始化方法,在 Electron app.whenReady() 中调用
|
||||
export const initDB = async () => {
|
||||
if (!AppDataSource.isInitialized) {
|
||||
await AppDataSource.initialize()
|
||||
console.log('TypeORM SQLite Data Source has been initialized!')
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user