From f77c0da28a163048e791919d342992b159f84cfd Mon Sep 17 00:00:00 2001 From: sebastjanartic <45803536-sebastjanartic@users.noreply.replit.com> Date: Thu, 28 Aug 2025 17:24:05 +0000 Subject: [PATCH] Improve database video retrieval and update operations Refactors video data handling to use raw SQL queries for improved performance and flexibility. Updates the `getVideos`, `getVideo`, and `updateVideoViews` methods to execute SQL directly, improving efficiency and error handling. Replit-Commit-Author: Agent Replit-Commit-Session-Id: d7424866-83d1-4486-a212-ac12b4c7becf Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/8cc42625-c1f5-4e43-99bd-77f2c4dedee2/d7424866-83d1-4486-a212-ac12b4c7becf/KMT7BRC --- server/storage.ts | 96 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 74 insertions(+), 22 deletions(-) diff --git a/server/storage.ts b/server/storage.ts index c54390d..7e306cc 100644 --- a/server/storage.ts +++ b/server/storage.ts @@ -54,29 +54,77 @@ export interface IStorage { export class DatabaseStorage implements IStorage { // Video operations async getVideos(limit = 20, offset = 0, search?: string): Promise { - let query = db.select().from(videos); - - if (search) { - const searchTerm = `%${search}%`; - query = query.where( - or( - like(videos.title, searchTerm), - like(videos.description, searchTerm) - ) - ) as any; + try { + let sqlQuery = sql` + SELECT * FROM video_metadata + `; + + if (search && search.length >= 2) { + const searchTerm = `%${search.toLowerCase()}%`; + sqlQuery = sql` + SELECT * FROM video_metadata + WHERE LOWER(title) LIKE ${searchTerm} + OR LOWER(description) LIKE ${searchTerm} + `; + } + + sqlQuery = sql`${sqlQuery} ORDER BY created_at DESC LIMIT ${limit} OFFSET ${offset}`; + + const result = await db.execute(sqlQuery); + console.log(`📊 DatabaseStorage: Found ${result.rows.length} videos (search: "${search || 'none'}")`); + + // Transform database rows to Video objects + return result.rows.map((row: any) => ({ + id: row.id, + title: row.title, + description: row.description, + thumbnailUrl: row.thumbnail_url, + videoUrl: row.video_url, + duration: row.duration, + views: row.views, + category: row.category, + tags: [], + isPublic: true, + uploadStatus: "completed", + originalFileName: row.title, + createdAt: row.created_at, + updatedAt: row.updated_at, + })); + } catch (error) { + console.error("❌ Database query failed:", error); + return []; } - - const result = await query - .orderBy(desc(videos.createdAt)) - .limit(limit) - .offset(offset); - - return result; } async getVideo(id: string): Promise