Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 275x 252x 252x 252x 252x 251x 154x 154x 20x 19x 65x 65x 1x 64x 3x 3x 1x 32x 32x 32x 38x 38x 3x 3x | /**
* Drizzle Phase Repository Adapter
*
* Implements PhaseRepository interface using Drizzle ORM.
*/
import { eq, asc, and } from 'drizzle-orm';
import { nanoid } from 'nanoid';
import type { DrizzleDatabase } from '../../index.js';
import { phases, phaseDependencies, type Phase } from '../../schema.js';
import type {
PhaseRepository,
CreatePhaseData,
UpdatePhaseData,
} from '../phase-repository.js';
/**
* Drizzle adapter for PhaseRepository.
*
* Uses dependency injection for database instance,
* enabling isolated test databases.
*/
export class DrizzlePhaseRepository implements PhaseRepository {
constructor(private db: DrizzleDatabase) {}
async create(data: CreatePhaseData): Promise<Phase> {
const { id: providedId, ...rest } = data;
const id = providedId ?? nanoid();
const now = new Date();
const [created] = await this.db.insert(phases).values({
id,
...rest,
status: data.status ?? 'pending',
createdAt: now,
updatedAt: now,
}).returning();
return created;
}
async findById(id: string): Promise<Phase | null> {
const result = await this.db
.select()
.from(phases)
.where(eq(phases.id, id))
.limit(1);
return result[0] ?? null;
}
async findByInitiativeId(initiativeId: string): Promise<Phase[]> {
return this.db
.select()
.from(phases)
.where(eq(phases.initiativeId, initiativeId))
.orderBy(asc(phases.createdAt));
}
async findDependenciesByInitiativeId(initiativeId: string): Promise<Array<{ phaseId: string; dependsOnPhaseId: string }>> {
return this.db
.select({ phaseId: phaseDependencies.phaseId, dependsOnPhaseId: phaseDependencies.dependsOnPhaseId })
.from(phaseDependencies)
.innerJoin(phases, eq(phaseDependencies.phaseId, phases.id))
.where(eq(phases.initiativeId, initiativeId));
}
async update(id: string, data: UpdatePhaseData): Promise<Phase> {
const [updated] = await this.db
.update(phases)
.set({ ...data, updatedAt: new Date() })
.where(eq(phases.id, id))
.returning();
if (!updated) {
throw new Error(`Phase not found: ${id}`);
}
return updated;
}
async delete(id: string): Promise<void> {
const [deleted] = await this.db.delete(phases).where(eq(phases.id, id)).returning();
if (!deleted) {
throw new Error(`Phase not found: ${id}`);
}
}
async createDependency(phaseId: string, dependsOnPhaseId: string): Promise<void> {
const id = nanoid();
const now = new Date();
await this.db.insert(phaseDependencies).values({
id,
phaseId,
dependsOnPhaseId,
createdAt: now,
});
}
async getDependencies(phaseId: string): Promise<string[]> {
const result = await this.db
.select({ dependsOnPhaseId: phaseDependencies.dependsOnPhaseId })
.from(phaseDependencies)
.where(eq(phaseDependencies.phaseId, phaseId));
return result.map((row) => row.dependsOnPhaseId);
}
async getDependents(phaseId: string): Promise<string[]> {
const result = await this.db
.select({ phaseId: phaseDependencies.phaseId })
.from(phaseDependencies)
.where(eq(phaseDependencies.dependsOnPhaseId, phaseId));
return result.map((row) => row.phaseId);
}
async removeDependency(phaseId: string, dependsOnPhaseId: string): Promise<void> {
await this.db
.delete(phaseDependencies)
.where(
and(
eq(phaseDependencies.phaseId, phaseId),
eq(phaseDependencies.dependsOnPhaseId, dependsOnPhaseId),
),
);
}
}
|