- DrizzleInitiativeRepository: CRUD with nanoid ID generation - DrizzlePhaseRepository: findByInitiativeId ordered by number - DrizzlePlanRepository: findByPhaseId ordered by number - DrizzleTaskRepository: findByPlanId ordered by order field - All adapters use DI for DrizzleDatabase instance - Timestamps set automatically on create/update - Throws on update/delete of non-existent entities
79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
/**
|
|
* Drizzle Initiative Repository Adapter
|
|
*
|
|
* Implements InitiativeRepository interface using Drizzle ORM.
|
|
*/
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
import { nanoid } from 'nanoid';
|
|
import type { DrizzleDatabase } from '../../index.js';
|
|
import { initiatives, type Initiative } from '../../schema.js';
|
|
import type {
|
|
InitiativeRepository,
|
|
CreateInitiativeData,
|
|
UpdateInitiativeData,
|
|
} from '../initiative-repository.js';
|
|
|
|
/**
|
|
* Drizzle adapter for InitiativeRepository.
|
|
*
|
|
* Uses dependency injection for database instance,
|
|
* enabling isolated test databases.
|
|
*/
|
|
export class DrizzleInitiativeRepository implements InitiativeRepository {
|
|
constructor(private db: DrizzleDatabase) {}
|
|
|
|
async create(data: CreateInitiativeData): Promise<Initiative> {
|
|
const now = new Date();
|
|
const initiative = {
|
|
id: nanoid(),
|
|
...data,
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
};
|
|
|
|
await this.db.insert(initiatives).values(initiative);
|
|
|
|
return initiative as Initiative;
|
|
}
|
|
|
|
async findById(id: string): Promise<Initiative | null> {
|
|
const result = await this.db
|
|
.select()
|
|
.from(initiatives)
|
|
.where(eq(initiatives.id, id))
|
|
.limit(1);
|
|
|
|
return result[0] ?? null;
|
|
}
|
|
|
|
async findAll(): Promise<Initiative[]> {
|
|
return this.db.select().from(initiatives);
|
|
}
|
|
|
|
async update(id: string, data: UpdateInitiativeData): Promise<Initiative> {
|
|
const existing = await this.findById(id);
|
|
if (!existing) {
|
|
throw new Error(`Initiative not found: ${id}`);
|
|
}
|
|
|
|
const updated = {
|
|
...data,
|
|
updatedAt: new Date(),
|
|
};
|
|
|
|
await this.db.update(initiatives).set(updated).where(eq(initiatives.id, id));
|
|
|
|
return { ...existing, ...updated } as Initiative;
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
const existing = await this.findById(id);
|
|
if (!existing) {
|
|
throw new Error(`Initiative not found: ${id}`);
|
|
}
|
|
|
|
await this.db.delete(initiatives).where(eq(initiatives.id, id));
|
|
}
|
|
}
|