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

28% Statements 7/25
16.66% Branches 2/12
30% Functions 3/10
30.43% Lines 7/23

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

    84x

    4x
    4x

    4x

    4x

    2x

    2x

     /**
     * Drizzle Page Repository Adapter
     *
     * Implements PageRepository interface using Drizzle ORM.
     */

    import { eq, isNull, and, asc, inArray } from 'drizzle-orm';
    import { nanoid } from 'nanoid';
    import type { DrizzleDatabase } from '../../index.js';
    import { pages, type Page } from '../../schema.js';
    import type {
      PageRepository,
      CreatePageData,
      UpdatePageData,
    } from '../page-repository.js';

    export class DrizzlePageRepository implements PageRepository {
      constructor(private db: DrizzleDatabase) {}

      async create(data: CreatePageData): Promise<Page> {
        const id = nanoid();
        const now = new Date();

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

        return created;
      }

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

        return result[0] ?? null;
      }

      async findByIds(ids: string[]): Promise<Page[]> {
        if (ids.length === 0) return [];
        return this.db
          .select()
          .from(pages)
          .where(inArray(pages.id, ids));
      }

      async findByInitiativeId(initiativeId: string): Promise<Page[]> {
        return this.db
          .select()
          .from(pages)
          .where(eq(pages.initiativeId, initiativeId))
          .orderBy(asc(pages.sortOrder));
      }

      async findByParentPageId(parentPageId: string): Promise<Page[]> {
        return this.db
          .select()
          .from(pages)
          .where(eq(pages.parentPageId, parentPageId))
          .orderBy(asc(pages.sortOrder));
      }

      async findRootPage(initiativeId: string): Promise<Page | null> {
        const result = await this.db
          .select()
          .from(pages)
          .where(
            and(
              eq(pages.initiativeId, initiativeId),
              isNull(pages.parentPageId),
            ),
          )
          .limit(1);

        return result[0] ?? null;
      }

      async getOrCreateRootPage(initiativeId: string): Promise<Page> {
        const existing = await this.findRootPage(initiativeId);
        if (existing) return existing;

        return this.create({
          initiativeId,
          parentPageId: null,
          title: 'Untitled',
          content: null,
          sortOrder: 0,
        });
      }

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

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

        return updated;
      }

      async delete(id: string): Promise<void> {
        const [deleted] = await this.db.delete(pages).where(eq(pages.id, id)).returning();

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