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

28 lines
676 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";
import { fileURLToPath } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const initDb = async () => {
// Read or create db.json
const defaultData = { posts: [] };
const db = await JSONFilePreset(
path.join(app.getPath("userData"), "db.json"),
defaultData
);
// 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);
};
initDb();