Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create database seeding script #137

Merged
merged 14 commits into from
Jul 16, 2024
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
"debug": "nodemon --watch 'src/**/*.ts' --exec ts-node-dev --inspect --respawn src/server.ts",
"typeorm:db": "npm run build && npx typeorm -d dist/src/configs/dbConfig.js",
"migration:generate": "npm run typeorm:db -- migration:generate",
"migration:run": "npm run typeorm:db -- migration:run"
"migration:run": "npm run typeorm:db -- migration:run",
"seed": "npm run build && node dist/src/scripts/seed-db.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@faker-js/faker": "^8.4.1",
"@tsed/passport": "^7.57.1",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.2",
Expand Down
24 changes: 12 additions & 12 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import express from 'express'
import type { Express } from 'express'
import bodyParser from 'body-parser'
import cookieParser from 'cookie-parser'
import cors from 'cors'
import { dataSource } from './configs/dbConfig'
import authRouter from './routes/auth/auth.route'
import profileRouter from './routes/profile/profile.route'
import adminRouter from './routes/admin/admin.route'
import mentorRouter from './routes/mentor/mentor.route'
import categoryRouter from './routes/category/category.route'
import type { Express } from 'express'
import express from 'express'
import fs from 'fs'
import passport from 'passport'
import { dataSource } from './configs/dbConfig'
import { CLIENT_URL } from './configs/envConfig'
import './configs/google-passport'
import './configs/linkedin-passport'
import { CLIENT_URL } from './configs/envConfig'
import cookieParser from 'cookie-parser'
import menteeRouter from './routes/mentee/mentee.route'
import fs from 'fs'
import adminRouter from './routes/admin/admin.route'
import authRouter from './routes/auth/auth.route'
import categoryRouter from './routes/category/category.route'
import emailRouter from './routes/emails/emails.route'
import menteeRouter from './routes/mentee/mentee.route'
import mentorRouter from './routes/mentor/mentor.route'
import profileRouter from './routes/profile/profile.route'

const app = express()
const staticFolder = 'uploads'
Expand Down
174 changes: 174 additions & 0 deletions src/scripts/seed-db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { faker } from '@faker-js/faker'
import { dataSource } from '../configs/dbConfig'
import Category from '../entities/category.entity'
import Email from '../entities/email.entity'
import EmailTemplate from '../entities/emailTemplate.entity'
import Mentee from '../entities/mentee.entity'
import Mentor from '../entities/mentor.entity'
import Platform from '../entities/platform.entity'
import Profile from '../entities/profile.entity'
import { ApplicationStatus, EmailStatusTypes, ProfileTypes } from '../enums'

export const seedDatabaseService = async (): Promise<string> => {
try {
await dataSource.initialize()
const profileRepository = dataSource.getRepository(Profile)
const categoryRepository = dataSource.getRepository(Category)
const emailRepository = dataSource.getRepository(Email)
const emailTemplateRepository = dataSource.getRepository(EmailTemplate)
const menteeRepository = dataSource.getRepository(Mentee)
const mentorRepository = dataSource.getRepository(Mentor)
const platformRepository = dataSource.getRepository(Platform)

await menteeRepository.remove(await menteeRepository.find())
await mentorRepository.remove(await mentorRepository.find())
await profileRepository.remove(await profileRepository.find())
await platformRepository.remove(await platformRepository.find())
await categoryRepository.remove(await categoryRepository.find())
await emailRepository.remove(await emailRepository.find())
await emailTemplateRepository.remove(await emailTemplateRepository.find())

const genProfiles = faker.helpers.multiple(createRandomProfile, {
count: 100
})

const profiles = await profileRepository.save(genProfiles)

const genEmails = faker.helpers.multiple(
() => {
return {
recipient: faker.internet.email(),
subject: faker.lorem.sentence(),
content: faker.lorem.paragraph(),
state: faker.helpers.enumValue(EmailStatusTypes)
}
},
{
count: 100
}
)

await emailRepository.save(genEmails)

const genEmailTemplates = faker.helpers.multiple(
() => {
return {
subject: faker.lorem.sentence(),
content: faker.lorem.paragraph()
}
},
{
count: 100
}
)

await emailTemplateRepository.save(genEmailTemplates)

const genCategories = faker.helpers.multiple(
() => {
return {
category: faker.lorem.word()
}
},
{
count: 100
}
)
const categories = await categoryRepository.save(genCategories)

const genPlatforms = faker.helpers.multiple(
() => {
return {
description: faker.lorem.sentence(),
mentor_questions: {
name: faker.lorem.word(),
email: faker.internet.email()
} as unknown as JSON,
image_url: faker.image.url(),
landing_page_url: faker.internet.url(),
title: faker.lorem.word()
}
},
{
count: 100
}
)

await platformRepository.save(genPlatforms)

const genMentors = (
categories: Category[],
profiles: Profile[]
): Mentor[] => {
const firstTenProfiles = profiles.slice(0, 10)

return firstTenProfiles.map((profile, index) => {
const category = categories[index]
return createMentor(category, profile)
})
}
const mentorsEntities = genMentors(categories, profiles)
const mentors = await mentorRepository.save(mentorsEntities)

const genMentees = (mentors: Mentor[], profiles: Profile[]): Mentee[] => {
const lastProfilesWithoutFirstTen = profiles.slice(10, profiles.length)

return lastProfilesWithoutFirstTen.map((profile, index) => {
const mentor =
mentors[faker.number.int({ min: 0, max: mentors.length - 1 })]
return new Mentee(
faker.helpers.enumValue(ApplicationStatus),
{
name: faker.lorem.word(),
email: faker.internet.email()
},
profile,
mentor
)
})
}

const menteesEntities = genMentees(mentors, profiles)

await menteeRepository.save(menteesEntities)

return 'Database seeded successfully'
} catch (err) {
console.error(err)
throw err
}
}

const createRandomProfile = (): Partial<Profile> => {
const profile = {
primary_email: faker.internet.email(),
first_name: faker.person.firstName(),
last_name: faker.person.lastName(),
image_url: faker.image.avatar(),
type: faker.helpers.enumValue(ProfileTypes),
password: '12345'
}

return profile
}

const createMentor = (category: Category, profile: Profile): Mentor => {
return {
state: faker.helpers.enumValue(ApplicationStatus),
category: category,

Check warning on line 158 in src/scripts/seed-db.ts

View workflow job for this annotation

GitHub Actions / build

Expected property shorthand
application: {
name: faker.lorem.word(),
email: faker.internet.email()
},
availability: faker.datatype.boolean(),
profile: profile

Check warning on line 164 in src/scripts/seed-db.ts

View workflow job for this annotation

GitHub Actions / build

Expected property shorthand
} as unknown as Mentor
}

seedDatabaseService()
.then((res) => {
console.log(res)
})
.catch((err) => {
console.error(err)
})
Loading