Files
Codewalkers/apps/server/db/repositories/drizzle/page.ts
Lukas May 34578d39c6 refactor: Restructure monorepo to apps/server/ and apps/web/ layout
Move src/ → apps/server/ and packages/web/ → apps/web/ to adopt
standard monorepo conventions (apps/ for runnable apps, packages/
for reusable libraries). Update all config files, shared package
imports, test fixtures, and documentation to reflect new paths.

Key fixes:
- Update workspace config to ["apps/*", "packages/*"]
- Update tsconfig.json rootDir/include for apps/server/
- Add apps/web/** to vitest exclude list
- Update drizzle.config.ts schema path
- Fix ensure-schema.ts migration path detection (3 levels up in dev,
  2 levels up in dist)
- Fix tests/integration/cli-server.test.ts import paths
- Update packages/shared imports to apps/server/ paths
- Update all docs/ files with new paths
2026-03-03 11:22:53 +01:00

118 lines
2.7 KiB
TypeScript

/**
* 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}`);
}
}
}