feat(12-02): implement getNextNumber in DrizzlePlanRepository
- Import max from drizzle-orm for aggregate query - Query MAX(number) where phase_id matches - Return max + 1, or 1 if no plans exist - Pattern follows DrizzlePhaseRepository.getNextNumber
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
* Implements PlanRepository interface using Drizzle ORM.
|
* Implements PlanRepository interface using Drizzle ORM.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { eq, asc } from 'drizzle-orm';
|
import { eq, asc, max } from 'drizzle-orm';
|
||||||
import { nanoid } from 'nanoid';
|
import { nanoid } from 'nanoid';
|
||||||
import type { DrizzleDatabase } from '../../index.js';
|
import type { DrizzleDatabase } from '../../index.js';
|
||||||
import { plans, type Plan } from '../../schema.js';
|
import { plans, type Plan } from '../../schema.js';
|
||||||
@@ -58,6 +58,16 @@ export class DrizzlePlanRepository implements PlanRepository {
|
|||||||
.orderBy(asc(plans.number));
|
.orderBy(asc(plans.number));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getNextNumber(phaseId: string): Promise<number> {
|
||||||
|
const result = await this.db
|
||||||
|
.select({ maxNumber: max(plans.number) })
|
||||||
|
.from(plans)
|
||||||
|
.where(eq(plans.phaseId, phaseId));
|
||||||
|
|
||||||
|
const maxNumber = result[0]?.maxNumber ?? 0;
|
||||||
|
return maxNumber + 1;
|
||||||
|
}
|
||||||
|
|
||||||
async update(id: string, data: UpdatePlanData): Promise<Plan> {
|
async update(id: string, data: UpdatePlanData): Promise<Plan> {
|
||||||
const existing = await this.findById(id);
|
const existing = await this.findById(id);
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
|
|||||||
Reference in New Issue
Block a user