# All files / src/db/repositories/drizzle initiative.ts

100% Statements 17/17
100% Branches 8/8
100% Functions 7/7
100% Lines 17/17

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

    287x

    236x
    236x

    236x

    236x

    27x

    27x

    2x

    4x

    2x

    2x
    1x

    1x

    4x

    4x

    4x
    1x

     /**
     * 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 { agents, 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 id = nanoid();
        const now = new Date();

        const [created] = await this.db.insert(initiatives).values({
          id,
          ...data,
          status: data.status ?? 'active',
          createdAt: now,
          updatedAt: now,
        }).returning();

        return created;
      }

      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 findByStatus(status: 'active' | 'completed' | 'archived'): Promise<Initiative[]> {
        return this.db
          .select()
          .from(initiatives)
          .where(eq(initiatives.status, status));
      }

      async update(id: string, data: UpdateInitiativeData): Promise<Initiative> {
        const [updated] = await this.db
          .update(initiatives)
          .set({ ...data, updatedAt: new Date() })
          .where(eq(initiatives.id, id))
          .returning();

        if (!updated) {
          throw new Error(`Initiative not found: ${id}`);
        }

        return updated;
      }

      async delete(id: string): Promise<void> {
        // Detach agents before deleting — agents.initiative_id FK may lack ON DELETE SET NULL
        // in databases that haven't applied migration 0025 yet.
        await this.db.update(agents).set({ initiativeId: null }).where(eq(agents.initiativeId, id));

        const [deleted] = await this.db.delete(initiatives).where(eq(initiatives.id, id)).returning();

        if (!deleted) {
          throw new Error(`Initiative not found: ${id}`);
        }
      }
    }