# All files / src/db/repositories/drizzle change-set.ts

13.04% Statements 3/23
12.5% Branches 2/16
22.22% Functions 2/9
14.28% Lines 3/21

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

    84x

    6x

    6x

     /**
     * Drizzle Change Set Repository Adapter
     *
     * Implements ChangeSetRepository interface using Drizzle ORM.
     */

    import { eq, desc, asc } from 'drizzle-orm';
    import { nanoid } from 'nanoid';
    import type { DrizzleDatabase } from '../../index.js';
    import { changeSets, changeSetEntries, type ChangeSet } from '../../schema.js';
    import type {
      ChangeSetRepository,
      CreateChangeSetData,
      CreateChangeSetEntryData,
      ChangeSetWithEntries,
    } from '../change-set-repository.js';

    export class DrizzleChangeSetRepository implements ChangeSetRepository {
      constructor(private db: DrizzleDatabase) {}

      async createWithEntries(data: CreateChangeSetData, entries: CreateChangeSetEntryData[]): Promise<ChangeSet> {
        const id = nanoid();
        const now = new Date();

        // Use transaction for atomicity
        return this.db.transaction(async (tx) => {
          const [created] = await tx.insert(changeSets).values({
            id,
            agentId: data.agentId,
            agentName: data.agentName,
            initiativeId: data.initiativeId,
            mode: data.mode,
            summary: data.summary ?? null,
            status: 'applied',
            createdAt: now,
          }).returning();

          if (entries.length > 0) {
            const entryRows = entries.map((e, i) => ({
              id: nanoid(),
              changeSetId: id,
              entityType: e.entityType,
              entityId: e.entityId,
              action: e.action,
              previousState: e.previousState ?? null,
              newState: e.newState ?? null,
              sortOrder: e.sortOrder ?? i,
              createdAt: now,
            }));

            await tx.insert(changeSetEntries).values(entryRows);
          }

          return created;
        });
      }

      async findById(id: string): Promise<ChangeSet | null> {
        const result = await this.db
          .select()
          .from(changeSets)
          .where(eq(changeSets.id, id))
          .limit(1);

        return result[0] ?? null;
      }

      async findByIdWithEntries(id: string): Promise<ChangeSetWithEntries | null> {
        const cs = await this.findById(id);
        if (!cs) return null;

        const entries = await this.db
          .select()
          .from(changeSetEntries)
          .where(eq(changeSetEntries.changeSetId, id))
          .orderBy(asc(changeSetEntries.sortOrder));

        return { ...cs, entries };
      }

      async findByInitiativeId(initiativeId: string): Promise<ChangeSet[]> {
        return this.db
          .select()
          .from(changeSets)
          .where(eq(changeSets.initiativeId, initiativeId))
          .orderBy(desc(changeSets.createdAt));
      }

      async findByAgentId(agentId: string): Promise<ChangeSet[]> {
        return this.db
          .select()
          .from(changeSets)
          .where(eq(changeSets.agentId, agentId))
          .orderBy(desc(changeSets.createdAt));
      }

      async markReverted(id: string): Promise<ChangeSet> {
        const [updated] = await this.db
          .update(changeSets)
          .set({ status: 'reverted', revertedAt: new Date() })
          .where(eq(changeSets.id, id))
          .returning();

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

        return updated;
      }
    }