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

39 lines
935 B
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-08-29 16:59:25 +08:00
2024-09-02 14:18:06 +08:00
interface ICreateDatabase {
name: string;
}
type SeriesTableType = StructuredMetadata & {
createTime?: number;
updateTime?: number;
};
2024-08-29 16:59:25 +08:00
2024-09-02 14:18:06 +08:00
interface DbTable {
series: SeriesTableType[];
}
2024-08-29 16:59:25 +08:00
2024-09-02 14:18:06 +08:00
const defaultData: DbTable = { series: [] };
export let db: Low<{ series: SeriesTableType[] }>;
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;
db = await JSONFilePreset(
path.join(app.getPath("userData"), name),
defaultData
);
2024-08-29 16:59:25 +08:00
};
2024-09-02 14:18:06 +08:00
// Update db.json
// await db.update(({ posts }) => posts.push("hello world"));
// Alternatively you can call db.write() explicitely later to write to db.json
// db.data.posts.push("hello world");
// await db.write();
// console.log(db);