All files / dal blog-categories.dal.ts

100% Statements 39/39
100% Branches 26/26
100% Functions 9/9
100% Lines 39/39

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137                          57x             8x   8x 3x 1x   2x     8x 2x 2x     8x                   8x 4x     4x       6x   6x 2x 1x   1x     6x 1x 1x     6x         6x       3x       2x         2x       2x         2x       1x       1x             2x         2x       2x       2x       5x 5x 1x   5x       5x      
// Data Access Layer for Blog Categories
import { eq, like, sql, desc, and } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type { BlogCategory, NewBlogCategory } from "../db/schema";
import { sanitizeSearchInput } from "../lib/utils";
 
export type BlogCategoryFilters = {
	parentId?: string | null;
	search?: string;
};
 
export class BlogCategoriesDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	async findAll(
		filters: BlogCategoryFilters = {},
		offset = 0,
		limit = 100,
	): Promise<BlogCategory[]> {
		const conditions = [];
 
		if (filters.parentId !== undefined) {
			if (filters.parentId === null) {
				conditions.push(sql`${schema.blogCategories.parentId} IS NULL`);
			} else {
				conditions.push(eq(schema.blogCategories.parentId, filters.parentId));
			}
		}
		if (filters.search) {
			const sanitized = sanitizeSearchInput(filters.search);
			conditions.push(like(schema.blogCategories.name, `%${sanitized}%`));
		}
 
		const query = this.db
			.select()
			.from(schema.blogCategories)
			.orderBy(
				schema.blogCategories.displayOrder,
				desc(schema.blogCategories.dateCreated),
			)
			.limit(limit)
			.offset(offset);
 
		if (conditions.length > 0) {
			return query.where(and(...conditions));
		}
 
		return query;
	}
 
	async count(filters: BlogCategoryFilters = {}): Promise<number> {
		const conditions = [];
 
		if (filters.parentId !== undefined) {
			if (filters.parentId === null) {
				conditions.push(sql`${schema.blogCategories.parentId} IS NULL`);
			} else {
				conditions.push(eq(schema.blogCategories.parentId, filters.parentId));
			}
		}
		if (filters.search) {
			const sanitized = sanitizeSearchInput(filters.search);
			conditions.push(like(schema.blogCategories.name, `%${sanitized}%`));
		}
 
		const query = this.db
			.select({ count: sql<number>`count(*)` })
			.from(schema.blogCategories);
 
		const result =
			conditions.length > 0
				? await query.where(and(...conditions))
				: await query;
 
		return result[0]?.count ?? 0;
	}
 
	async findById(id: string): Promise<BlogCategory | undefined> {
		const result = await this.db
			.select()
			.from(schema.blogCategories)
			.where(eq(schema.blogCategories.id, id))
			.limit(1);
		return result[0];
	}
 
	async findBySlug(slug: string): Promise<BlogCategory | undefined> {
		const result = await this.db
			.select()
			.from(schema.blogCategories)
			.where(eq(schema.blogCategories.slug, slug))
			.limit(1);
		return result[0];
	}
 
	async create(data: NewBlogCategory): Promise<BlogCategory> {
		const result = await this.db
			.insert(schema.blogCategories)
			.values(data)
			.returning();
		return result[0];
	}
 
	async update(
		id: string,
		data: Partial<Omit<BlogCategory, "id" | "dateCreated">>,
	): Promise<BlogCategory | undefined> {
		const result = await this.db
			.update(schema.blogCategories)
			.set(data)
			.where(eq(schema.blogCategories.id, id))
			.returning();
		return result[0];
	}
 
	async delete(id: string): Promise<boolean> {
		const result = await this.db
			.delete(schema.blogCategories)
			.where(eq(schema.blogCategories.id, id))
			.returning();
		return result.length > 0;
	}
 
	async slugExists(slug: string, excludeId?: string): Promise<boolean> {
		const conditions = [eq(schema.blogCategories.slug, slug)];
		if (excludeId) {
			conditions.push(sql`${schema.blogCategories.id} != ${excludeId}`);
		}
		const result = await this.db
			.select({ count: sql<number>`count(*)` })
			.from(schema.blogCategories)
			.where(and(...conditions));
		return (result[0]?.count ?? 0) > 0;
	}
}