cvpilot-tool/apps/desktop/electron/core/db.ts
2024-09-06 15:09:32 +08:00

53 lines
1.2 KiB
TypeScript

import path from "node:path";
import { JSONFilePreset } from "lowdb/node";
import { app } from "electron";
import { Low } from "node_modules/lowdb/lib/core/Low";
import { StructuredMetadata } from "./dicom";
import { merge } from "lodash";
export interface ICreateDatabase {
name: string;
}
export type SeriesTableType = StructuredMetadata & {
createTime?: number;
updateTime?: number;
};
export type SettingTableType = {
/**
* 推理硬件默认是GPU、可选CPU、NPU
* @default GPU
*/
inferDevice: string;
/**
* 用户文件位置
* @description 用于存储算法输出、应用生成的产物的路径
*/
outputPath: string;
};
export interface DbTable {
series: SeriesTableType[];
setting: SettingTableType;
}
const defaultTable: DbTable = {
series: [],
setting: {
inferDevice: "gpu",
outputPath: path.join(app.getPath("userData"), "output"),
},
};
export let db: Low<DbTable>;
export const createDatabase = async (config: ICreateDatabase) => {
const { name } = config;
const dbJsonFilePath = path.join(app.getPath("userData"), name);
db = await JSONFilePreset(dbJsonFilePath, defaultTable);
// 表结构更新,增量合并
db.data = merge({}, defaultTable, db.data);
await db.write();
};