All files / dal ho-profiles.dal.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 4/4
100% Lines 7/7

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                11x     6x               5x 5x 1x             4x               1x            
import { eq } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import type * as schema from "../db/schema";
import { hoProfiles } from "../db/schema";
import type { NewHoProfile } from "../db/schema";
import { nanoid } from "nanoid";
 
export class HoProfilesDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	async findByUserId(userId: string) {
		return this.db
			.select()
			.from(hoProfiles)
			.where(eq(hoProfiles.userId, userId))
			.get();
	}
 
	async upsert(userId: string, data: Partial<Omit<NewHoProfile, "id" | "userId" | "createdAt" | "updatedAt">>) {
		const existing = await this.findByUserId(userId);
		if (existing) {
			return this.db
				.update(hoProfiles)
				.set({ ...data, updatedAt: new Date() })
				.where(eq(hoProfiles.userId, userId))
				.returning()
				.get();
		}
		return this.db
			.insert(hoProfiles)
			.values({ id: nanoid(), userId, ...data })
			.returning()
			.get();
	}
 
	async deleteByUserId(userId: string) {
		return this.db
			.delete(hoProfiles)
			.where(eq(hoProfiles.userId, userId))
			.run();
	}
}