Update video synchronization to use ORM for database operations

Refactor video synchronization logic in `server/videoSync.ts` to utilize Drizzle ORM for all database interactions, replacing raw SQL queries for selecting, inserting, and updating video records. This change also includes adding default values for potentially missing video properties.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 45a1dcfc-f8a2-475a-a6b9-96fbb841dc27
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/60d372ff-2c10-46c7-b01b-10c3435136b0/45a1dcfc-f8a2-475a-a6b9-96fbb841dc27/pjFeepJ
This commit is contained in:
sebastjanartic 2025-09-15 06:19:49 +00:00
parent 1185bfc3af
commit f2aa7d8e5e

View File

@ -96,18 +96,18 @@ class VideoSyncService {
for (const video of batch) { for (const video of batch) {
try { try {
// Check if video exists in database using raw SQL // Check if video exists in database using Drizzle ORM
const existingVideo = await db.execute(sql`SELECT id FROM videos WHERE id = ${video.id} LIMIT 1`); const existingVideo = await db.select({ id: videos.id }).from(videos).where(eq(videos.id, video.id)).limit(1);
const videoData = { const videoData = {
id: video.id, id: video.id,
title: video.title, title: video.title || '',
description: video.description || '', description: video.description || '',
thumbnailUrl: video.thumbnailUrl, thumbnailUrl: video.thumbnailUrl || '',
customThumbnailUrl: video.customThumbnailUrl || null, customThumbnailUrl: video.customThumbnailUrl || null,
videoUrl: video.videoUrl, videoUrl: video.videoUrl || '',
duration: video.duration, duration: video.duration || 0,
views: video.views, views: video.views || 0,
category: video.category || '', category: video.category || '',
tags: Array.isArray(video.tags) ? video.tags : [], tags: Array.isArray(video.tags) ? video.tags : [],
isPublic: video.isPublic !== false, isPublic: video.isPublic !== false,
@ -115,23 +115,27 @@ class VideoSyncService {
updatedAt: new Date(), updatedAt: new Date(),
}; };
if (existingVideo.rows.length === 0) { if (existingVideo.length === 0) {
// Insert new video using raw SQL to avoid schema issues // Insert new video using Drizzle ORM
await db.execute(sql` await db.insert(videos).values(videoData);
INSERT INTO videos (id, title, description, thumbnail_url, video_url, duration, views, category, custom_thumbnail_url, tags, is_public, created_at, updated_at)
VALUES (${videoData.id}, ${videoData.title}, ${videoData.description}, ${videoData.thumbnailUrl}, ${videoData.videoUrl}, ${videoData.duration}, ${videoData.views}, ${videoData.category}, ${videoData.customThumbnailUrl}, ${'{' + videoData.tags.join(',') + '}'}, ${videoData.isPublic}, ${videoData.createdAt.toISOString()}, ${videoData.updatedAt.toISOString()})
`);
insertedCount++; insertedCount++;
} else { } else {
// Update existing video using raw SQL // Update existing video using Drizzle ORM
await db.execute(sql` await db.update(videos)
UPDATE videos .set({
SET title = ${videoData.title}, description = ${videoData.description}, thumbnail_url = ${videoData.thumbnailUrl}, title: videoData.title,
video_url = ${videoData.videoUrl}, duration = ${videoData.duration}, views = ${videoData.views}, description: videoData.description,
category = ${videoData.category}, custom_thumbnail_url = ${videoData.customThumbnailUrl}, thumbnailUrl: videoData.thumbnailUrl,
tags = ${'{' + videoData.tags.join(',') + '}'}, is_public = ${videoData.isPublic}, updated_at = ${videoData.updatedAt.toISOString()} customThumbnailUrl: videoData.customThumbnailUrl,
WHERE id = ${video.id} videoUrl: videoData.videoUrl,
`); duration: videoData.duration,
views: videoData.views,
category: videoData.category,
tags: videoData.tags,
isPublic: videoData.isPublic,
updatedAt: videoData.updatedAt,
})
.where(eq(videos.id, video.id));
updatedCount++; updatedCount++;
} }
} catch (error) { } catch (error) {
@ -171,17 +175,24 @@ class VideoSyncService {
setTimeout(() => reject(new Error('Database sync timeout after 30 seconds')), 30000) setTimeout(() => reject(new Error('Database sync timeout after 30 seconds')), 30000)
); );
await Promise.race([ try {
this.syncVideosToDatabase(), await Promise.race([
syncTimeout this.syncVideosToDatabase(),
]); syncTimeout
]);
console.log('✅ Database sync completed successfully');
} catch (syncError) {
console.error('❌ Database sync failed:', syncError);
console.log('⚠️ Continuing with cached data only - videos will still be available from Bunny.net');
// Don't throw here, just log the error and continue
}
this.startPeriodicSync(); this.startPeriodicSync();
console.log('✅ Video sync service initialized successfully'); console.log('✅ Video sync service initialized successfully');
} catch (error) { } catch (error) {
console.error('❌ Failed to initialize video sync service:', error); console.error('❌ Failed to initialize video sync service:', error);
console.log('⚠️ Continuing without database sync - app will still work with cached data'); console.log('⚠️ Continuing without video sync - app will work but videos might not be available');
// Continue without crashing the server // Continue without crashing the server - this ensures the app starts even if video sync fails
} }
} }