mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-09 23:16:51 +02:00
👷♂️ Add db seed script (#619)
This commit is contained in:
parent
ed8b1d6765
commit
3d9dd00736
5 changed files with 229 additions and 7 deletions
108
packages/database/prisma/seed.ts
Normal file
108
packages/database/prisma/seed.ts
Normal file
|
@ -0,0 +1,108 @@
|
|||
import { faker } from "@faker-js/faker";
|
||||
import { PrismaClient, VoteType } from "@prisma/client";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const randInt = (max = 1, floor = 0) => {
|
||||
return Math.round(Math.random() * max) + floor;
|
||||
};
|
||||
|
||||
async function main() {
|
||||
// Create some users
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
name: faker.name.fullName(),
|
||||
email: faker.internet.email(),
|
||||
},
|
||||
});
|
||||
|
||||
// Create some polls
|
||||
const polls = await Promise.all(
|
||||
Array.from({ length: 20 }).map(async () => {
|
||||
const poll = await prisma.poll.create({
|
||||
include: {
|
||||
participants: true,
|
||||
options: true,
|
||||
},
|
||||
data: {
|
||||
id: faker.random.alpha(10),
|
||||
title: `${faker.animal.cat()} meetup - ${faker.date.month()}`,
|
||||
description: faker.lorem.paragraph(),
|
||||
location: faker.address.streetAddress(),
|
||||
deadline: faker.date.future(),
|
||||
type: "date",
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
},
|
||||
},
|
||||
timeZone: "America/New_York",
|
||||
options: {
|
||||
create: faker.date
|
||||
.betweens(
|
||||
Date.now(),
|
||||
Date.now() + 1000 * 60 * 60 * 24 * 30,
|
||||
randInt(16, 1),
|
||||
) //
|
||||
.map((date) => {
|
||||
return {
|
||||
value: date.toISOString().substring(0, 10),
|
||||
};
|
||||
}),
|
||||
},
|
||||
participants: {
|
||||
create: Array.from({ length: Math.round(Math.random() * 10) }).map(
|
||||
() => ({
|
||||
name: faker.name.fullName(),
|
||||
email: faker.internet.email(),
|
||||
}),
|
||||
),
|
||||
},
|
||||
adminUrlId: faker.random.alpha(10),
|
||||
participantUrlId: faker.random.alpha(10),
|
||||
},
|
||||
});
|
||||
return poll;
|
||||
}),
|
||||
);
|
||||
|
||||
// Create some votes
|
||||
for (const poll of polls) {
|
||||
for (const participant of poll.participants) {
|
||||
await prisma.vote.createMany({
|
||||
data: poll.options.map((option) => {
|
||||
const randomNumber = randInt(100);
|
||||
const vote =
|
||||
randomNumber > 90 ? "ifNeedBe" : randomNumber > 50 ? "yes" : "no";
|
||||
return {
|
||||
participantId: participant.id,
|
||||
pollId: poll.id,
|
||||
optionId: option.id,
|
||||
type: vote,
|
||||
};
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const poll of polls) {
|
||||
const commentCount = randInt(3);
|
||||
if (commentCount) {
|
||||
await prisma.comment.createMany({
|
||||
data: Array.from({ length: commentCount }).map(() => ({
|
||||
pollId: poll.id,
|
||||
authorName: faker.name.fullName(),
|
||||
content: faker.lorem.sentence(),
|
||||
})),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
console.info(`Seeded database with user: ${user.email}`);
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => console.error(e))
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue