25 lines
569 B
TypeScript
25 lines
569 B
TypeScript
|
import path from "node:path";
|
||
|
import { JSONFilePreset } from "lowdb/node";
|
||
|
import { app } from "electron";
|
||
|
|
||
|
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();
|