-
-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathsync-content-table.ts
More file actions
332 lines (292 loc) · 9.27 KB
/
sync-content-table.ts
File metadata and controls
332 lines (292 loc) · 9.27 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Sync Content Table
*
* This script populates the unified content table from:
* - post table (as ARTICLE type)
* - aggregated_article table (as LINK type)
*
* Run with: npx tsx scripts/sync-content-table.ts
*/
import { db } from "@/server/db";
import {
content,
post,
aggregated_article,
user,
feed_source,
} from "@/server/db/schema";
import { eq, isNotNull, sql } from "drizzle-orm";
import crypto from "crypto";
// Track used slugs to avoid conflicts
const usedSlugs = new Set<string>();
// Estimate read time based on excerpt/title
// Average reading speed: 200-250 words/minute
// Excerpt is usually 10-20% of article, so we estimate full article length
function estimateReadTime(title: string, excerpt: string | null): number {
const text = `${title} ${excerpt || ""}`;
const wordCount = text.split(/\s+/).filter(Boolean).length;
// Excerpt is typically ~200 words, full article is ~5x longer on average
const estimatedArticleWords = wordCount * 5;
// Reading speed: ~225 words per minute
const readTimeMinutes = Math.ceil(estimatedArticleWords / 225);
// Clamp between 2 and 15 minutes (reasonable range for most articles)
return Math.max(2, Math.min(15, readTimeMinutes));
}
// Generate a unique slug by adding a suffix if needed
async function getUniqueSlug(
baseSlug: string,
contentId: string,
): Promise<string> {
let slug = baseSlug;
let suffix = 1;
while (true) {
// Check if slug is already used in this sync run
if (!usedSlugs.has(slug)) {
// Check if slug exists in database with a different ID
const existing = await db
.select({ id: content.id })
.from(content)
.where(eq(content.slug, slug))
.limit(1);
if (existing.length === 0 || existing[0].id === contentId) {
usedSlugs.add(slug);
return slug;
}
}
// Try with suffix
slug = `${baseSlug}-${suffix}`;
suffix++;
// Safety limit
if (suffix > 100) {
// Use UUID suffix as fallback
slug = `${baseSlug}-${crypto.randomUUID().slice(0, 8)}`;
usedSlugs.add(slug);
return slug;
}
}
}
async function syncPostsToContent() {
console.log("Syncing published posts to content table as ARTICLE type...");
// Get all published posts with their authors
const posts = await db
.select({
id: post.id,
title: post.title,
body: post.body,
excerpt: post.excerpt,
userId: post.userId,
slug: post.slug,
canonicalUrl: post.canonicalUrl,
coverImage: post.coverImage,
readTimeMins: post.readTimeMins,
published: post.published,
createdAt: post.createdAt,
updatedAt: post.updatedAt,
showComments: post.showComments,
upvotes: post.upvotes,
downvotes: post.downvotes,
})
.from(post)
.where(isNotNull(post.published));
console.log(`Found ${posts.length} published posts to sync`);
let synced = 0;
let errors = 0;
for (const p of posts) {
try {
// Check if this post already exists in content table
const existing = await db
.select({ id: content.id })
.from(content)
.where(eq(content.id, p.id))
.limit(1);
// Get unique slug for this post
const uniqueSlug = await getUniqueSlug(p.slug, p.id);
if (existing.length > 0) {
// Update existing content
await db
.update(content)
.set({
type: "POST",
title: p.title,
body: p.body,
excerpt: p.excerpt,
userId: p.userId,
slug: uniqueSlug,
canonicalUrl: p.canonicalUrl,
coverImage: p.coverImage,
readTimeMins: p.readTimeMins,
published: true,
publishedAt: p.published,
showComments: p.showComments,
upvotes: p.upvotes,
downvotes: p.downvotes,
updatedAt: p.updatedAt,
})
.where(eq(content.id, p.id));
} else {
// Insert new content
await db.insert(content).values({
id: p.id, // Keep the same ID for referential integrity
type: "POST",
title: p.title,
body: p.body,
excerpt: p.excerpt,
userId: p.userId,
slug: uniqueSlug,
canonicalUrl: p.canonicalUrl,
coverImage: p.coverImage,
readTimeMins: p.readTimeMins,
published: true,
publishedAt: p.published,
showComments: p.showComments,
upvotes: p.upvotes,
downvotes: p.downvotes,
createdAt: p.createdAt,
updatedAt: p.updatedAt,
});
}
synced++;
} catch (error) {
console.error(`Error syncing post ${p.id}:`, error);
errors++;
}
}
console.log(`Synced ${synced} posts, ${errors} errors`);
return { synced, errors };
}
async function syncAggregatedArticlesToContent() {
console.log("Syncing aggregated articles to content table as LINK type...");
// Get all aggregated articles with their source info
const articles = await db
.select({
id: aggregated_article.id,
shortId: aggregated_article.shortId,
title: aggregated_article.title,
excerpt: aggregated_article.excerpt,
externalUrl: aggregated_article.externalUrl,
imageUrl: aggregated_article.imageUrl,
ogImageUrl: aggregated_article.ogImageUrl,
sourceAuthor: aggregated_article.sourceAuthor,
slug: aggregated_article.slug,
sourceId: aggregated_article.sourceId,
publishedAt: aggregated_article.publishedAt,
upvotes: aggregated_article.upvotes,
downvotes: aggregated_article.downvotes,
clickCount: aggregated_article.clickCount,
createdAt: aggregated_article.createdAt,
})
.from(aggregated_article);
console.log(`Found ${articles.length} aggregated articles to sync`);
let synced = 0;
let errors = 0;
for (const a of articles) {
try {
// Generate a unique content ID from the aggregated article ID
// Using a deterministic ID so we can update on re-runs
const contentId = `link-${a.id}`;
// Check if already exists
const existing = await db
.select({ id: content.id })
.from(content)
.where(eq(content.id, contentId))
.limit(1);
// Get unique slug for this article
const baseSlug = a.slug || a.shortId || `link-${a.id}`;
const uniqueSlug = await getUniqueSlug(baseSlug, contentId);
// Estimate read time for the external article
const readTimeMins = estimateReadTime(a.title, a.excerpt);
if (existing.length > 0) {
// Update existing
await db
.update(content)
.set({
type: "LINK",
title: a.title,
excerpt: a.excerpt,
externalUrl: a.externalUrl,
imageUrl: a.imageUrl,
ogImageUrl: a.ogImageUrl,
sourceAuthor: a.sourceAuthor,
slug: uniqueSlug,
sourceId: a.sourceId,
published: true,
publishedAt: a.publishedAt,
upvotes: a.upvotes,
downvotes: a.downvotes,
clickCount: a.clickCount,
readTimeMins,
})
.where(eq(content.id, contentId));
} else {
// Insert new
await db.insert(content).values({
id: contentId,
type: "LINK",
title: a.title,
excerpt: a.excerpt,
externalUrl: a.externalUrl,
imageUrl: a.imageUrl,
ogImageUrl: a.ogImageUrl,
sourceAuthor: a.sourceAuthor,
slug: uniqueSlug,
sourceId: a.sourceId,
published: true,
publishedAt: a.publishedAt,
upvotes: a.upvotes,
downvotes: a.downvotes,
clickCount: a.clickCount,
readTimeMins,
createdAt: a.createdAt,
showComments: true,
});
}
synced++;
} catch (error) {
console.error(`Error syncing aggregated article ${a.id}:`, error);
errors++;
}
}
console.log(`Synced ${synced} aggregated articles, ${errors} errors`);
return { synced, errors };
}
async function getContentStats() {
const stats = await db
.select({
type: content.type,
count: sql<number>`count(*)::int`,
})
.from(content)
.where(eq(content.published, true))
.groupBy(content.type);
console.log("\nContent table stats:");
for (const stat of stats) {
console.log(` ${stat.type}: ${stat.count} items`);
}
const total = stats.reduce((sum, s) => sum + s.count, 0);
console.log(` TOTAL: ${total} items`);
}
async function main() {
console.log("=== Content Table Sync Script ===\n");
try {
// Sync posts first (ARTICLE type)
const postResult = await syncPostsToContent();
console.log("");
// Sync aggregated articles (LINK type)
const articleResult = await syncAggregatedArticlesToContent();
console.log("\n=== Summary ===");
console.log(
`Posts synced: ${postResult.synced} (${postResult.errors} errors)`,
);
console.log(
`Articles synced: ${articleResult.synced} (${articleResult.errors} errors)`,
);
// Show final stats
await getContentStats();
console.log("\n=== Sync Complete ===");
process.exit(0);
} catch (error) {
console.error("Fatal error during sync:", error);
process.exit(1);
}
}
main();