mirror of
https://github.com/Gimanh/taskview-community.git
synced 2026-09-12 05:49:01 +00:00
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { eq } from 'drizzle-orm';
|
|
import { GraphRelationsSchema } from 'taskview-db-schemas';
|
|
import { Database } from '../../modules/db';
|
|
import { callWithCatch } from '../../utils/helpers';
|
|
import type { GraphArgRelationsType, GraphReturnRelationsType } from './types';
|
|
|
|
export class GraphRepository {
|
|
private readonly db: Database;
|
|
|
|
constructor() {
|
|
this.db = Database.getInstance();
|
|
}
|
|
|
|
public async addEdge(data: GraphArgRelationsType): Promise<GraphReturnRelationsType[]> {
|
|
const result = await callWithCatch(() =>
|
|
this.db.dbDrizzle.insert(GraphRelationsSchema).values(data).returning()
|
|
);
|
|
return result ?? [];
|
|
}
|
|
|
|
public async fetchById(id: number): Promise<GraphReturnRelationsType | null> {
|
|
const result = await callWithCatch(() =>
|
|
this.db.dbDrizzle.select().from(GraphRelationsSchema).where(eq(GraphRelationsSchema.id, id))
|
|
);
|
|
return result?.[0] ?? null;
|
|
}
|
|
|
|
public async fetchAllEdges(goalId: number): Promise<GraphReturnRelationsType[]> {
|
|
const result = await callWithCatch(() =>
|
|
this.db.dbDrizzle.select().from(GraphRelationsSchema).where(eq(GraphRelationsSchema.goalId, goalId))
|
|
);
|
|
return result ?? [];
|
|
}
|
|
|
|
public async deleteEdge(id: number): Promise<boolean> {
|
|
const result = await callWithCatch(() =>
|
|
this.db.dbDrizzle.delete(GraphRelationsSchema).where(eq(GraphRelationsSchema.id, id))
|
|
);
|
|
return !!result;
|
|
}
|
|
}
|