cvpilot-tool/apps/desktop/electron/core/db.ts

53 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-08-29 16:59:25 +08:00
import path from "node:path";
import { JSONFilePreset } from "lowdb/node";
import { app } from "electron";
2024-09-02 14:18:06 +08:00
import { Low } from "node_modules/lowdb/lib/core/Low";
import { StructuredMetadata } from "./dicom";
2024-09-06 14:27:41 +08:00
import { merge } from "lodash";
2024-08-29 16:59:25 +08:00
2024-09-06 14:27:41 +08:00
export interface ICreateDatabase {
2024-09-02 14:18:06 +08:00
name: string;
}
2024-09-06 14:27:41 +08:00
export type SeriesTableType = StructuredMetadata & {
2024-09-02 14:18:06 +08:00
createTime?: number;
updateTime?: number;
};
2024-08-29 16:59:25 +08:00
2024-09-06 14:27:41 +08:00
export type SettingTableType = {
/**
* GPUCPUNPU
* @default GPU
*/
inferDevice: string;
/**
*
* @description
*/
outputPath: string;
};
export interface DbTable {
2024-09-02 14:18:06 +08:00
series: SeriesTableType[];
2024-09-06 14:27:41 +08:00
setting: SettingTableType;
2024-09-02 14:18:06 +08:00
}
2024-08-29 16:59:25 +08:00
2024-09-06 14:27:41 +08:00
const defaultTable: DbTable = {
series: [],
setting: {
inferDevice: "gpu",
outputPath: path.join(app.getPath("userData"), "output"),
},
};
export let db: Low<DbTable>;
2024-08-29 16:59:25 +08:00
2024-09-02 14:18:06 +08:00
export const createDatabase = async (config: ICreateDatabase) => {
const { name } = config;
2024-09-06 14:27:41 +08:00
const dbJsonFilePath = path.join(app.getPath("userData"), name);
db = await JSONFilePreset(dbJsonFilePath, defaultTable);
// 表结构更新,增量合并
db.data = merge({}, defaultTable, db.data);
await db.write();
2024-08-29 16:59:25 +08:00
};