Files
landscape-template/prisma/seed/index.js

257 lines
7.4 KiB
JavaScript

const { PrismaClient } = require("@prisma/client");
const { generatePrivateKey, getPublicKey } = require("../../api/utils/nostr-tools");
const { categories, projects, tags, hackathons, roles, skills } = require("./data");
const Chance = require('chance');
const { getCoverImage, randomItems, random } = require("./helpers");
const { tournament: tournamentMock } = require("./data/tournament.seed");
const chance = new Chance();
const prisma = new PrismaClient()
async function purge() {
await prisma.award.deleteMany();
await prisma.tag.deleteMany();
await prisma.vote.deleteMany();
await prisma.project.deleteMany();
await prisma.category.deleteMany();
}
async function generateNostrKeys() {
const allUsers = await prisma.user.findMany({
where: {
nostr_prv_key: null
}
})
for (const user of allUsers) {
const prvkey = generatePrivateKey();
const pubkey = getPublicKey(prvkey);
await prisma.user.update({
where: {
id: user.id,
},
data: {
nostr_prv_key: prvkey,
nostr_pub_key: pubkey
}
})
}
}
async function main() {
// console.log("Purging old data");
// await purge()
// await createCategories();
// await createTags();
// await createProjects();
// await createStories();
// await createHackathons();
// await fillUserKeysTable()
// await createRoles();
// await createSkills();
await createTournament();
}
async function createCategories() {
console.log("Creating Categories");
await prisma.category.createMany({
data: categories.map(item => ({
id: item.id,
title: item.title,
cover_image: 'https://via.placeholder.com/1920x850.png?text=Category+Cover+Image',
icon: '🎭'
}))
})
}
async function createTags() {
console.log("Creating Tags");
await prisma.tag.createMany({
data: tags.map(item => ({
id: item.id,
title: item.title,
description: item.description,
isOfficial: item.isOfficial,
icon: item.icon,
}))
})
}
async function createProjects() {
console.log("Creating Projects");
for (let i = 0; i < projects.length; i++) {
const item = projects[i];
const { tags, ...feilds } = item
await prisma.project.create({
data: {
...feilds,
tags: {
connect: tags.map(t => ({
id: t.id
}))
},
description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.",
screenshots: Array(4).fill('https://via.placeholder.com/1280x729.png?text=Project+Screenshot')
}
})
}
}
async function createStories() {
console.log("Creating Stories");
const user = await prisma.user.findFirst();
for (let i = 0; i < 15; i++) {
const randomTags = randomItems(random(2, 5), tags)
await prisma.story.create({
data: {
body: chance.paragraph(),
excerpt: chance.paragraph({ sentences: 1 }),
title: chance.sentence({ words: chance.integer({ min: 3, max: 7 }) }),
cover_image: getCoverImage(),
is_published: true,
tags: {
connect: randomTags.map(t => ({ id: t.id }))
},
user_id: user.id,
votes_count: Math.floor(random(10, 6600)),
}
})
}
}
async function createHackathons() {
console.log("Creating Hackathons");
for (let i = 0; i < hackathons.length; i++) {
const item = hackathons[i];
const { tags, ...feilds } = item
await prisma.hackathon.create({
data: {
...feilds,
tags: {
connect: tags.map(t => ({
id: t.id
}))
},
description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.",
}
})
}
}
async function fillUserKeysTable() {
console.log('Filling Users Keys Table');
const allUsers = await prisma.user.findMany({
select: {
id: true,
pubKey: true,
}
})
await prisma.userKey.createMany({
data: allUsers.filter(u => !!u.pubKey).map(u => ({
key: u.pubKey,
user_id: u.id
}))
})
}
async function createRoles() {
console.log("Creating Users Roles");
await prisma.workRole.createMany({
data: roles.map(item => ({
id: item.id,
title: item.title,
icon: item.icon,
}))
})
}
async function createSkills() {
console.log("Creating Users Skills");
await prisma.skill.createMany({
data: skills.map(item => ({
id: item.id,
title: item.title,
}))
})
}
async function createTournament() {
console.log("Creating Tournament");
await prisma.tournamentFAQ.createMany({
data: tournamentMock.faqs.map(i => ({
tournament_id: 1,
question: i.question,
answer: i.answer
}))
})
return
const createdTournament = await prisma.tournament.create({
data: {
title: tournamentMock.title,
description: tournamentMock.description,
start_date: tournamentMock.start_date,
end_date: tournamentMock.end_date,
thumbnail_image: tournamentMock.thumbnail_image,
cover_image: tournamentMock.cover_image,
location: tournamentMock.location,
website: tournamentMock.website,
faqs: {
createMany: {
data: tournamentMock.faqs.map(f => ({ question: f.question, answer: f.answer }))
}
},
prizes: {
createMany: {
data: tournamentMock.prizes.map(p => ({ title: p.title, image: p.image, amount: p.amount }))
}
},
judges: {
createMany: {
data: tournamentMock.judges.map(j => ({ name: j.name, company: j.company, twitter: j.twitter, avatar: j.avatar }))
}
},
events: {
createMany: {
data: tournamentMock.events.map(e => ({ title: e.title, description: e.description, starts_at: e.starts_at, ends_at: e.ends_at, image: e.image, location: e.location, type: e.type, website: e.website }))
}
},
}
})
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})