All files / dal uploads.dal.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 9/9
100% Lines 15/15

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              25x     1x       1x       2x         2x       2x         2x             2x         2x       1x         1x       2x                                 2x             2x       1x          
// Data Access Layer for Uploads (presigned URL upload lifecycle)
import { eq, and, lt } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import * as schema from "../db/schema";
import type { Upload, NewUpload } from "../db/schema";
 
export class UploadsDal {
	constructor(private db: DrizzleD1Database<typeof schema>) {}
 
	async create(input: NewUpload): Promise<Upload> {
		const result = await this.db
			.insert(schema.uploads)
			.values(input)
			.returning();
		return result[0];
	}
 
	async getById(id: string): Promise<Upload | null> {
		const result = await this.db
			.select()
			.from(schema.uploads)
			.where(eq(schema.uploads.id, id))
			.limit(1);
		return result[0] ?? null;
	}
 
	async getByStorageKey(storageKey: string): Promise<Upload | null> {
		const result = await this.db
			.select()
			.from(schema.uploads)
			.where(eq(schema.uploads.storageKey, storageKey))
			.limit(1);
		return result[0] ?? null;
	}
 
	async updateStatus(
		id: string,
		status: Upload["status"],
	): Promise<Upload> {
		const result = await this.db
			.update(schema.uploads)
			.set({ status, dateUpdated: new Date() })
			.where(eq(schema.uploads.id, id))
			.returning();
		return result[0];
	}
 
	async updateFileSize(id: string, fileSize: number): Promise<Upload> {
		const result = await this.db
			.update(schema.uploads)
			.set({ fileSize, dateUpdated: new Date() })
			.where(eq(schema.uploads.id, id))
			.returning();
		return result[0];
	}
 
	async getExpiredUploads(): Promise<Upload[]> {
		return this.db
			.select()
			.from(schema.uploads)
			.where(
				and(
					eq(schema.uploads.status, "uploading"),
					lt(schema.uploads.expiresAt, new Date()),
				),
			);
	}
 
	/**
	 * Atomically set status to 'ready' only if currently 'uploading'.
	 * Returns the updated record if the transition happened, null if already ready/expired.
	 * This prevents race conditions between client confirm and R2 event handler.
	 */
	async setReadyIfUploading(id: string): Promise<Upload | null> {
		const result = await this.db
			.update(schema.uploads)
			.set({ status: "ready", dateUpdated: new Date() })
			.where(
				and(eq(schema.uploads.id, id), eq(schema.uploads.status, "uploading")),
			)
			.returning();
		return result[0] ?? null;
	}
 
	async delete(id: string): Promise<void> {
		await this.db
			.delete(schema.uploads)
			.where(eq(schema.uploads.id, id));
	}
}