|
| 1 | +import { PrismaClient } from '../client'; |
| 2 | +import { faker } from '@faker-js/faker'; |
| 3 | +import bcrypt from 'bcryptjs'; |
| 4 | + |
| 5 | +const AUTHOR_PASSWORD = process.env.AUTHOR_PASSWORD; |
| 6 | + |
| 7 | +if (!AUTHOR_PASSWORD) { |
| 8 | + throw Error('The environment variable `AUTHOR_PASSWORD` is missed'); |
| 9 | +} |
| 10 | + |
| 11 | +const titles = [ |
| 12 | + 'Why TypeScript Is Worth the Learning Curve', |
| 13 | + 'Docker Compose for Local Development', |
| 14 | + 'Understanding JWT: Auth Made Simple', |
| 15 | + 'Top 5 VS Code Extensions for JavaScript Developers', |
| 16 | + "REST vs. GraphQL: A Developer's Perspective", |
| 17 | + 'Mastering Zod for Schema Validation', |
| 18 | + 'How to Secure Your Express App', |
| 19 | + 'Deploying Node.js Apps with Koyeb', |
| 20 | + 'CSS-in-JS: Should You Use It in 2025?', |
| 21 | + 'How I Built a Portfolio While Learning to Code', |
| 22 | +]; |
| 23 | + |
| 24 | +const postCount = titles.length; |
| 25 | + |
| 26 | +const tags = [ |
| 27 | + 'open_source', |
| 28 | + 'full_stack', |
| 29 | + 'javaScript', |
| 30 | + 'typeScript', |
| 31 | + 'security', |
| 32 | + 'frontend', |
| 33 | + 'software', |
| 34 | + 'testing', |
| 35 | + 'backend', |
| 36 | +]; |
| 37 | + |
| 38 | +const db = new PrismaClient(); |
| 39 | + |
| 40 | +async function main() { |
| 41 | + console.log('Resetting the database...'); |
| 42 | + await db.$transaction([ |
| 43 | + db.comment.deleteMany({}), |
| 44 | + db.votesOnPosts.deleteMany({}), |
| 45 | + db.tagsOnPosts.deleteMany({}), |
| 46 | + db.post.deleteMany({}), |
| 47 | + db.image.deleteMany({}), |
| 48 | + db.user.deleteMany({}), |
| 49 | + db.tag.deleteMany({}), |
| 50 | + ]); |
| 51 | + |
| 52 | + console.log('Seeding the database with authors...'); |
| 53 | + const passHashArgs = [AUTHOR_PASSWORD, 10] as [string, number]; |
| 54 | + const dbPostAuthors = await db.user.createManyAndReturn({ |
| 55 | + data: [ |
| 56 | + { |
| 57 | + password: bcrypt.hashSync(...passHashArgs), |
| 58 | + bio: 'From Nowhere land with love.', |
| 59 | + username: 'nowhere-man', |
| 60 | + fullname: 'Nowhere-Man', |
| 61 | + isAdmin: true, |
| 62 | + }, |
| 63 | + { |
| 64 | + password: bcrypt.hashSync(...passHashArgs), |
| 65 | + fullname: 'Clark Kent / Kal-El', |
| 66 | + bio: 'From Krypton with love.', |
| 67 | + username: 'superman', |
| 68 | + isAdmin: false, |
| 69 | + }, |
| 70 | + { |
| 71 | + password: bcrypt.hashSync(...passHashArgs), |
| 72 | + bio: 'From Gotham with love.', |
| 73 | + fullname: 'Bruce Wayne', |
| 74 | + username: 'batman', |
| 75 | + isAdmin: false, |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + |
| 80 | + console.log( |
| 81 | + 'Seeding the database with images, posts, categories, comment, and votes...' |
| 82 | + ); |
| 83 | + for (let i = 0; i < postCount; i++) { |
| 84 | + const postAuthor = faker.helpers.arrayElement(dbPostAuthors); |
| 85 | + const dbPostViewers = dbPostAuthors.filter( |
| 86 | + ({ id }) => id !== postAuthor.id |
| 87 | + ); |
| 88 | + |
| 89 | + const tagsCount = faker.number.int({ min: 2, max: 3 }); |
| 90 | + const postTags = faker.helpers.arrayElements(tags, tagsCount); |
| 91 | + |
| 92 | + const gapDays = 42; |
| 93 | + const dateFactor = postCount - i; |
| 94 | + const dayMS = 24 * 60 * 60 * 1000; |
| 95 | + const startDate = new Date(Date.now() - dateFactor * dayMS * gapDays); |
| 96 | + const endDate = new Date(Date.now() - (dateFactor - 1) * dayMS * gapDays); |
| 97 | + const postDate = faker.date.between({ from: startDate, to: endDate }); |
| 98 | + |
| 99 | + const dbImage = await db.image.create({ |
| 100 | + data: { |
| 101 | + src: `https://ndauvqaezozccgtddhkr.supabase.co/storage/v1/object/public/images/seed/${i}.jpg`, |
| 102 | + storageFullPath: `images/seed/${i}.jpg`, |
| 103 | + storageId: crypto.randomUUID(), |
| 104 | + ownerId: postAuthor.id, |
| 105 | + mimetype: 'image/jpeg', |
| 106 | + size: 7654321, |
| 107 | + height: 1080, |
| 108 | + width: 1920, |
| 109 | + posts: { |
| 110 | + create: [ |
| 111 | + { |
| 112 | + published: true, |
| 113 | + title: titles[i], |
| 114 | + createdAt: postDate, |
| 115 | + updatedAt: postDate, |
| 116 | + authorId: postAuthor.id, |
| 117 | + content: faker.lorem.paragraphs( |
| 118 | + faker.number.int({ min: 10, max: 15 }) |
| 119 | + ), |
| 120 | + tags: { |
| 121 | + create: postTags.map((name) => ({ |
| 122 | + tag: { |
| 123 | + connectOrCreate: { where: { name }, create: { name } }, |
| 124 | + }, |
| 125 | + })), |
| 126 | + }, |
| 127 | + }, |
| 128 | + ], |
| 129 | + }, |
| 130 | + }, |
| 131 | + include: { posts: true }, |
| 132 | + }); |
| 133 | + const dbPost = dbImage.posts[0]; |
| 134 | + |
| 135 | + const commentsCount = faker.number.int({ min: 3, max: 7 }); |
| 136 | + const commentDates = faker.date.betweens({ |
| 137 | + count: commentsCount, |
| 138 | + from: postDate, |
| 139 | + to: endDate, |
| 140 | + }); |
| 141 | + await db.comment.createMany({ |
| 142 | + data: Array.from({ length: commentsCount }).map((_, commentIndex) => ({ |
| 143 | + content: faker.lorem.sentences(faker.number.int({ min: 1, max: 3 })), |
| 144 | + authorId: faker.helpers.arrayElement(dbPostViewers).id, |
| 145 | + createdAt: commentDates[commentIndex], |
| 146 | + updatedAt: commentDates[commentIndex], |
| 147 | + postId: dbPost.id, |
| 148 | + })), |
| 149 | + }); |
| 150 | + |
| 151 | + const votesDates = faker.date.betweens({ |
| 152 | + count: dbPostViewers.length, |
| 153 | + from: postDate, |
| 154 | + to: endDate, |
| 155 | + }); |
| 156 | + await db.votesOnPosts.createMany({ |
| 157 | + data: dbPostViewers.map(({ id }, voteIndex) => ({ |
| 158 | + isUpvote: faker.helpers.arrayElement([true, false]), |
| 159 | + createdAt: votesDates[voteIndex], |
| 160 | + updatedAt: votesDates[voteIndex], |
| 161 | + postId: dbPost.id, |
| 162 | + userId: id, |
| 163 | + })), |
| 164 | + }); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +main() |
| 169 | + .then(async () => { |
| 170 | + await db.$disconnect(); |
| 171 | + }) |
| 172 | + .catch(async (e) => { |
| 173 | + console.error(e); |
| 174 | + await db.$disconnect(); |
| 175 | + process.exit(1); |
| 176 | + }); |
| 177 | + |
| 178 | +export const seed = main; |
| 179 | + |
| 180 | +export default main; |
0 commit comments