2024-10-17 20:31:18 +00:00
|
|
|
import { JSONFilePreset } from "lowdb/node";
|
|
|
|
|
import { type Low } from "lowdb";
|
|
|
|
|
import { AppContext, Repository } from "./types.js";
|
|
|
|
|
|
|
|
|
|
type Schema = {
|
|
|
|
|
repos: {
|
|
|
|
|
[key: string]: Repository;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type Database = Low<Schema>;
|
|
|
|
|
|
|
|
|
|
export const loadDB = async (ctx: AppContext): Promise<Database> => {
|
|
|
|
|
const db = await JSONFilePreset<Schema>(`${ctx.cachePath}/db.json`, { repos: {} });
|
|
|
|
|
return db;
|
|
|
|
|
}
|
2024-11-01 17:51:14 +00:00
|
|
|
export const updateRepository = async (repoId: string, data: Repository, db: Database) => {
|
2024-10-17 20:31:18 +00:00
|
|
|
db.data.repos[repoId] = {
|
|
|
|
|
...db.data.repos[repoId],
|
|
|
|
|
...data,
|
|
|
|
|
}
|
|
|
|
|
await db.write();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const createRepository = async (repo: Repository, db: Database) => {
|
|
|
|
|
db.data.repos[repo.id] = repo;
|
|
|
|
|
await db.write();
|
|
|
|
|
}
|