mirror of
https://github.com/sourcebot-dev/sourcebot.git
synced 2025-12-12 20:35:24 +00:00
28 lines
765 B
TypeScript
28 lines
765 B
TypeScript
|
|
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;
|
||
|
|
}
|
||
|
|
export const updateRepository = async (repoId: string, data: Partial<Repository>, db: Database) => {
|
||
|
|
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();
|
||
|
|
}
|