seed.ts184 lines, 5.7 KB
| 1 | import { db } from "./index"; |
| 2 | import { users, urls, userRoleEnum } from "./schema"; |
| 3 | import { nanoid } from "nanoid"; |
| 4 | import { randomUUID } from "crypto"; |
| 5 | import { hash } from "bcryptjs"; |
| 6 | |
| 7 | /** |
| 8 | * Seed the database with test data |
| 9 | */ |
| 10 | export async function seed() { |
| 11 | console.log("🌱 Seeding database..."); |
| 12 | |
| 13 | try { |
| 14 | // Create test users |
| 15 | const testUsers = [ |
| 16 | { |
| 17 | id: randomUUID(), |
| 18 | name: "Test User", |
| 19 | email: "test@example.com", |
| 20 | password: await hash("password123", 10), |
| 21 | role: userRoleEnum.enumValues[0], // "user" |
| 22 | createdAt: new Date(), |
| 23 | updatedAt: new Date(), |
| 24 | }, |
| 25 | { |
| 26 | id: randomUUID(), |
| 27 | name: "Demo User", |
| 28 | email: "demo@example.com", |
| 29 | password: await hash("demo123", 10), |
| 30 | role: userRoleEnum.enumValues[0], // "user" |
| 31 | createdAt: new Date(), |
| 32 | updatedAt: new Date(), |
| 33 | }, |
| 34 | { |
| 35 | id: randomUUID(), |
| 36 | name: "Admin User", |
| 37 | email: "admin@example.com", |
| 38 | password: await hash("admin123", 10), |
| 39 | role: userRoleEnum.enumValues[1], // "admin" |
| 40 | createdAt: new Date(), |
| 41 | updatedAt: new Date(), |
| 42 | }, |
| 43 | ]; |
| 44 | |
| 45 | console.log("👤 Creating test users..."); |
| 46 | |
| 47 | // Insert users one by one to handle potential duplicates |
| 48 | for (const user of testUsers) { |
| 49 | try { |
| 50 | await db.insert(users).values(user).onConflictDoNothing(); |
| 51 | console.log(`✅ Created user: ${user.email} (${user.role})`); |
| 52 | } catch (error) { |
| 53 | console.error(`❌ Error creating user ${user.email}:`, error); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Get the inserted users to use their IDs |
| 58 | const insertedUsers = await db.query.users.findMany({ |
| 59 | where: (users, { eq, or }) => |
| 60 | or( |
| 61 | eq(users.email, "test@example.com"), |
| 62 | eq(users.email, "demo@example.com"), |
| 63 | eq(users.email, "admin@example.com") |
| 64 | ), |
| 65 | }); |
| 66 | |
| 67 | if (insertedUsers.length === 0) { |
| 68 | throw new Error("Failed to retrieve inserted users"); |
| 69 | } |
| 70 | |
| 71 | // Create test URLs |
| 72 | const testUrls = [ |
| 73 | // URLs for the first user |
| 74 | { |
| 75 | originalUrl: "https://github.com", |
| 76 | shortCode: "github", |
| 77 | createdAt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), // 7 days ago |
| 78 | updatedAt: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000), |
| 79 | clicks: 42, |
| 80 | userId: insertedUsers[0].id, |
| 81 | }, |
| 82 | { |
| 83 | originalUrl: "https://nextjs.org", |
| 84 | shortCode: "nextjs", |
| 85 | createdAt: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000), // 5 days ago |
| 86 | updatedAt: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000), |
| 87 | clicks: 27, |
| 88 | userId: insertedUsers[0].id, |
| 89 | }, |
| 90 | { |
| 91 | originalUrl: "https://tailwindcss.com", |
| 92 | shortCode: "tailwind", |
| 93 | createdAt: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), // 3 days ago |
| 94 | updatedAt: new Date(Date.now() - 3 * 24 * 60 * 60 * 1000), |
| 95 | clicks: 15, |
| 96 | userId: insertedUsers[0].id, |
| 97 | }, |
| 98 | |
| 99 | // URLs for the second user |
| 100 | { |
| 101 | originalUrl: "https://react.dev", |
| 102 | shortCode: "react", |
| 103 | createdAt: new Date(Date.now() - 6 * 24 * 60 * 60 * 1000), // 6 days ago |
| 104 | updatedAt: new Date(Date.now() - 6 * 24 * 60 * 60 * 1000), |
| 105 | clicks: 31, |
| 106 | userId: insertedUsers[1].id, |
| 107 | }, |
| 108 | { |
| 109 | originalUrl: "https://typescriptlang.org", |
| 110 | shortCode: "typescript", |
| 111 | createdAt: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000), // 4 days ago |
| 112 | updatedAt: new Date(Date.now() - 4 * 24 * 60 * 60 * 1000), |
| 113 | clicks: 19, |
| 114 | userId: insertedUsers[1].id, |
| 115 | }, |
| 116 | |
| 117 | // URLs without a user (anonymous) |
| 118 | { |
| 119 | originalUrl: "https://example.com", |
| 120 | shortCode: "example", |
| 121 | createdAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), // 2 days ago |
| 122 | updatedAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), |
| 123 | clicks: 8, |
| 124 | userId: null, |
| 125 | }, |
| 126 | { |
| 127 | originalUrl: "https://google.com", |
| 128 | shortCode: "google", |
| 129 | createdAt: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), // 1 day ago |
| 130 | updatedAt: new Date(Date.now() - 1 * 24 * 60 * 60 * 1000), |
| 131 | clicks: 53, |
| 132 | userId: null, |
| 133 | }, |
| 134 | ]; |
| 135 | |
| 136 | console.log("🔗 Creating test URLs..."); |
| 137 | |
| 138 | // Insert URLs one by one to handle potential duplicates |
| 139 | for (const url of testUrls) { |
| 140 | try { |
| 141 | await db.insert(urls).values(url).onConflictDoNothing(); |
| 142 | console.log(`✅ Created URL: ${url.shortCode} -> ${url.originalUrl}`); |
| 143 | } catch (error) { |
| 144 | console.error(`❌ Error creating URL ${url.shortCode}:`, error); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Generate some random URLs with random short codes |
| 149 | console.log("🎲 Creating random URLs..."); |
| 150 | |
| 151 | for (let i = 0; i < 10; i++) { |
| 152 | const randomUrl = { |
| 153 | originalUrl: `https://random-site-${i + 1}.com`, |
| 154 | shortCode: nanoid(6), |
| 155 | createdAt: new Date( |
| 156 | Date.now() - Math.floor(Math.random() * 10) * 24 * 60 * 60 * 1000 |
| 157 | ), |
| 158 | updatedAt: new Date( |
| 159 | Date.now() - Math.floor(Math.random() * 10) * 24 * 60 * 60 * 1000 |
| 160 | ), |
| 161 | clicks: Math.floor(Math.random() * 100), |
| 162 | userId: |
| 163 | Math.random() > 0.5 |
| 164 | ? insertedUsers[Math.floor(Math.random() * insertedUsers.length)].id |
| 165 | : null, |
| 166 | }; |
| 167 | |
| 168 | try { |
| 169 | await db.insert(urls).values(randomUrl); |
| 170 | console.log( |
| 171 | `✅ Created random URL: ${randomUrl.shortCode} -> ${randomUrl.originalUrl}` |
| 172 | ); |
| 173 | } catch (error) { |
| 174 | console.error(`❌ Error creating random URL:`, error); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | console.log("✅ Seeding completed successfully!"); |
| 179 | return { success: true }; |
| 180 | } catch (error) { |
| 181 | console.error("❌ Seeding failed:", error); |
| 182 | return { success: false, error }; |
| 183 | } |
| 184 | } |