import type { UnifiedPost } from '../types.js';
export interface PostRow {
id: string;
platform: string;
author_username: string;
author_display_name: string;
author_followers: number;
author_verified: boolean;
content: string;
url: string;
timestamp: string; // ISO string
likes: number;
comments: number;
shares: number;
views: number;
media_type: string;
media_urls: string; // JSON array
hashtags: string; // comma-separated
metadata: string; // JSON
scraped_at: string; // ISO string
vector: number[]; // Float32[1536]
}
export function postToRow(post: UnifiedPost, vector: number[]): PostRow {
return {
id: post.id,
platform: post.platform,
author_username: post.author.username,
author_display_name: post.author.displayName,
author_followers: post.author.followers ?? 0,
author_verified: post.author.verified ?? false,
content: post.content,
url: post.url,
timestamp: post.timestamp.toISOString(),
likes: post.engagement.likes,
comments: post.engagement.comments,
shares: post.engagement.shares,
views: post.engagement.views ?? 0,
media_type: post.media?.type ?? '',
media_urls: JSON.stringify(post.media?.urls ?? []),
hashtags: post.hashtags.join(','),
metadata: JSON.stringify(post.metadata),
scraped_at: new Date().toISOString(),
vector,
};
}
export function rowToPost(row: PostRow): UnifiedPost {
return {
id: row.id,
platform: row.platform as UnifiedPost['platform'],
author: {
username: row.author_username,
displayName: row.author_display_name,
followers: row.author_followers || undefined,
verified: row.author_verified,
},
content: row.content,
url: row.url,
timestamp: new Date(row.timestamp),
engagement: {
likes: row.likes,
comments: row.comments,
shares: row.shares,
views: row.views || undefined,
},
media: row.media_type ? {
type: row.media_type as 'image' | 'video' | 'text',
urls: JSON.parse(row.media_urls || '[]'),
} : undefined,
hashtags: row.hashtags ? row.hashtags.split(',') : [],
metadata: JSON.parse(row.metadata || '{}'),
};
}