|
1 |
| -import type { BookmarkEdit } from '$lib/types/Bookmark.type'; |
| 1 | +import type { Bookmark, BookmarkEdit } from '$lib/types/Bookmark.type'; |
2 | 2 | import { db } from '$lib/database/db';
|
3 | 3 | import { getOrCreateCategory } from '$lib/database/repositories/Category.repository';
|
4 | 4 | import { getOrCreateTag } from '$lib/database/repositories/Tag.repository';
|
5 | 5 | import { bookmarkSchema, bookmarksToTagsSchema } from '$lib/database/schema';
|
6 |
| - |
7 | 6 | import { createSlug } from '../create-slug';
|
| 7 | +import type { ImportExecutionResult, ImportProgress } from '$lib/types/BookmarkImport.type'; |
| 8 | +import { Storage } from '$lib/storage/storage'; |
| 9 | +import { eq } from 'drizzle-orm'; |
| 10 | + |
| 11 | +const BATCH_SIZE = 50; |
| 12 | + |
| 13 | +const storage = new Storage(); |
8 | 14 |
|
9 |
| -import type { ImportExecutionResult } from '$lib/types/BookmarkImport.type'; |
10 | 15 | export async function executeImport(
|
11 | 16 | bookmarks: BookmarkEdit[],
|
12 |
| - userId: number |
| 17 | + userId: number, |
| 18 | + onProgress?: (progress: ImportProgress) => void |
13 | 19 | ): Promise<ImportExecutionResult> {
|
14 |
| - const results = []; |
| 20 | + const results: ImportExecutionResult['results'] = []; |
| 21 | + let processedCount = 0; |
15 | 22 |
|
16 |
| - for (const bookmark of bookmarks) { |
17 |
| - try { |
18 |
| - const category = await getOrCreateCategory(userId, { |
19 |
| - name: bookmark.category.name, |
20 |
| - slug: createSlug(bookmark.category.name) |
21 |
| - }); |
| 23 | + const batches = Array.from({ length: Math.ceil(bookmarks.length / BATCH_SIZE) }, (_, i) => |
| 24 | + bookmarks.slice(i * BATCH_SIZE, (i + 1) * BATCH_SIZE) |
| 25 | + ); |
| 26 | + |
| 27 | + for (const batch of batches) { |
| 28 | + await db.transaction(async (tx) => { |
| 29 | + for (const bookmark of batch) { |
| 30 | + try { |
| 31 | + if (!bookmark.url || !bookmark.title || !bookmark.category?.name) { |
| 32 | + throw new Error('Missing required fields'); |
| 33 | + } |
| 34 | + |
| 35 | + const category = await getOrCreateCategory(userId, { |
| 36 | + name: bookmark.category.name, |
| 37 | + slug: createSlug(bookmark.category.name) |
| 38 | + }); |
| 39 | + |
| 40 | + const [newBookmark] = await tx |
| 41 | + .insert(bookmarkSchema) |
| 42 | + .values({ |
| 43 | + url: bookmark.url.trim(), |
| 44 | + domain: bookmark.domain, |
| 45 | + title: bookmark.title.trim(), |
| 46 | + description: bookmark.description?.trim() || null, |
| 47 | + iconUrl: bookmark.iconUrl, |
| 48 | + mainImageUrl: bookmark.mainImageUrl, |
| 49 | + importance: bookmark.importance, |
| 50 | + flagged: bookmark.flagged, |
| 51 | + contentHtml: bookmark.contentHtml, |
| 52 | + contentText: bookmark.contentText, |
| 53 | + contentType: bookmark.contentType, |
| 54 | + author: bookmark.author, |
| 55 | + contentPublishedDate: bookmark.contentPublishedDate, |
| 56 | + slug: createSlug(bookmark.title), |
| 57 | + ownerId: userId, |
| 58 | + categoryId: category.id, |
| 59 | + created: new Date(), |
| 60 | + updated: new Date() |
| 61 | + } as typeof bookmarkSchema.$inferInsert) |
| 62 | + .returning(); |
| 63 | + |
| 64 | + let iconId: number | null = null; |
| 65 | + let mainImageId: number | null = null; |
22 | 66 |
|
23 |
| - const [newBookmark] = await db |
24 |
| - .insert(bookmarkSchema) |
25 |
| - .values({ |
26 |
| - url: bookmark.url, |
27 |
| - domain: bookmark.domain, |
28 |
| - title: bookmark.title, |
29 |
| - description: bookmark.description || null, |
30 |
| - iconUrl: bookmark.iconUrl, |
31 |
| - mainImageUrl: bookmark.mainImageUrl, |
32 |
| - importance: bookmark.importance, |
33 |
| - flagged: bookmark.flagged, |
34 |
| - contentHtml: bookmark.contentHtml, |
35 |
| - contentText: bookmark.contentText, |
36 |
| - contentType: bookmark.contentType, |
37 |
| - author: bookmark.author, |
38 |
| - contentPublishedDate: bookmark.contentPublishedDate, |
39 |
| - slug: createSlug(bookmark.title), |
40 |
| - ownerId: userId, |
41 |
| - categoryId: category.id, |
42 |
| - created: new Date(), |
43 |
| - updated: new Date() |
44 |
| - } as typeof bookmarkSchema.$inferInsert) |
45 |
| - .returning(); |
46 |
| - |
47 |
| - if (bookmark.bookmarkTags?.length) { |
48 |
| - const tags = await Promise.all( |
49 |
| - bookmark.bookmarkTags.map((tag) => |
50 |
| - getOrCreateTag(userId, { |
51 |
| - name: tag.label, |
52 |
| - slug: createSlug(tag.label), |
53 |
| - ownerId: userId |
54 |
| - }) |
55 |
| - ) |
56 |
| - ); |
57 |
| - |
58 |
| - await db.insert(bookmarksToTagsSchema).values( |
59 |
| - tags.map((tag) => ({ |
60 |
| - bookmarkId: newBookmark.id, |
61 |
| - tagId: tag.id |
62 |
| - })) |
63 |
| - ); |
| 67 | + if (bookmark.mainImageUrl) { |
| 68 | + ({ id: mainImageId } = await storage.storeImage( |
| 69 | + bookmark.mainImageUrl, |
| 70 | + bookmark.title, |
| 71 | + userId, |
| 72 | + newBookmark.id |
| 73 | + )); |
| 74 | + } |
| 75 | + |
| 76 | + if (bookmark.iconUrl) { |
| 77 | + ({ id: iconId } = await storage.storeImage( |
| 78 | + bookmark.iconUrl, |
| 79 | + bookmark.title, |
| 80 | + userId, |
| 81 | + newBookmark.id |
| 82 | + )); |
| 83 | + } |
| 84 | + |
| 85 | + if (iconId || mainImageId) { |
| 86 | + await tx |
| 87 | + .update(bookmarkSchema) |
| 88 | + .set({ |
| 89 | + iconId, |
| 90 | + mainImageId, |
| 91 | + updated: new Date() |
| 92 | + }) |
| 93 | + .where(eq(bookmarkSchema.id, newBookmark.id)); |
| 94 | + } |
| 95 | + |
| 96 | + if (bookmark.bookmarkTags?.length) { |
| 97 | + const tags = await Promise.all( |
| 98 | + bookmark.bookmarkTags.map((tag) => |
| 99 | + getOrCreateTag(userId, { |
| 100 | + name: tag.label.trim(), |
| 101 | + slug: createSlug(tag.label), |
| 102 | + ownerId: userId |
| 103 | + }) |
| 104 | + ) |
| 105 | + ); |
| 106 | + |
| 107 | + await tx.insert(bookmarksToTagsSchema).values( |
| 108 | + tags.map((tag) => ({ |
| 109 | + bookmarkId: newBookmark.id, |
| 110 | + tagId: tag.id |
| 111 | + })) |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + results.push({ |
| 116 | + success: true, |
| 117 | + bookmark: { |
| 118 | + id: bookmark.id, |
| 119 | + url: bookmark.url, |
| 120 | + title: bookmark.title, |
| 121 | + category: bookmark.category.name, |
| 122 | + success: true |
| 123 | + } |
| 124 | + }); |
| 125 | + } catch (error) { |
| 126 | + console.error('Failed to import bookmark:', bookmark.url, error); |
| 127 | + results.push({ |
| 128 | + success: false, |
| 129 | + error: error instanceof Error ? error.message : 'Unknown error', |
| 130 | + bookmark: { |
| 131 | + id: bookmark.id, |
| 132 | + url: bookmark.url, |
| 133 | + title: bookmark.title, |
| 134 | + category: bookmark.category.name, |
| 135 | + success: false |
| 136 | + } |
| 137 | + }); |
| 138 | + } |
64 | 139 | }
|
| 140 | + }); |
65 | 141 |
|
66 |
| - results.push({ |
67 |
| - success: true, |
68 |
| - bookmark: newBookmark.id |
69 |
| - }); |
70 |
| - } catch (error) { |
71 |
| - console.error(error); |
72 |
| - results.push({ |
73 |
| - success: false, |
74 |
| - error: error instanceof Error ? error.message : 'Unknown error', |
75 |
| - bookmark: bookmark.url |
| 142 | + processedCount += batch.length; |
| 143 | + |
| 144 | + if (onProgress) { |
| 145 | + onProgress({ |
| 146 | + processed: processedCount, |
| 147 | + total: bookmarks.length, |
| 148 | + successful: results.filter((r) => r.success).length, |
| 149 | + failed: results.filter((r) => !r.success).length |
76 | 150 | });
|
77 | 151 | }
|
78 | 152 | }
|
79 | 153 |
|
80 |
| - const result = { |
| 154 | + const result: ImportExecutionResult = { |
81 | 155 | total: bookmarks.length,
|
82 | 156 | successful: results.filter((r) => r.success).length,
|
83 | 157 | failed: results.filter((r) => !r.success).length,
|
84 | 158 | results
|
85 |
| - } as unknown as ImportExecutionResult; |
| 159 | + }; |
86 | 160 |
|
87 | 161 | return result;
|
88 | 162 | }
|
0 commit comments