Some checks failed
ci/woodpecker/push/ci Pipeline failed
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
59 lines
1.7 KiB
TypeScript
59 lines
1.7 KiB
TypeScript
import { eq, or, type Db, agents } from '@mosaic/db';
|
|
|
|
export type Agent = typeof agents.$inferSelect;
|
|
export type NewAgent = typeof agents.$inferInsert;
|
|
|
|
export function createAgentsRepo(db: Db) {
|
|
return {
|
|
async findAll(): Promise<Agent[]> {
|
|
return db.select().from(agents);
|
|
},
|
|
|
|
async findById(id: string): Promise<Agent | undefined> {
|
|
const rows = await db.select().from(agents).where(eq(agents.id, id));
|
|
return rows[0];
|
|
},
|
|
|
|
async findByName(name: string): Promise<Agent | undefined> {
|
|
const rows = await db.select().from(agents).where(eq(agents.name, name));
|
|
return rows[0];
|
|
},
|
|
|
|
async findByProject(projectId: string): Promise<Agent[]> {
|
|
return db.select().from(agents).where(eq(agents.projectId, projectId));
|
|
},
|
|
|
|
async findSystem(): Promise<Agent[]> {
|
|
return db.select().from(agents).where(eq(agents.isSystem, true));
|
|
},
|
|
|
|
async findAccessible(ownerId: string): Promise<Agent[]> {
|
|
return db
|
|
.select()
|
|
.from(agents)
|
|
.where(or(eq(agents.ownerId, ownerId), eq(agents.isSystem, true)));
|
|
},
|
|
|
|
async create(data: NewAgent): Promise<Agent> {
|
|
const rows = await db.insert(agents).values(data).returning();
|
|
return rows[0]!;
|
|
},
|
|
|
|
async update(id: string, data: Partial<NewAgent>): Promise<Agent | undefined> {
|
|
const rows = await db
|
|
.update(agents)
|
|
.set({ ...data, updatedAt: new Date() })
|
|
.where(eq(agents.id, id))
|
|
.returning();
|
|
return rows[0];
|
|
},
|
|
|
|
async remove(id: string): Promise<boolean> {
|
|
const rows = await db.delete(agents).where(eq(agents.id, id)).returning();
|
|
return rows.length > 0;
|
|
},
|
|
};
|
|
}
|
|
|
|
export type AgentsRepo = ReturnType<typeof createAgentsRepo>;
|