91 lines
2.5 KiB
TypeScript
91 lines
2.5 KiB
TypeScript
import { getRandListData, getRandomMin } from '@3rapp/utils';
|
|
import { faker } from '@faker-js/faker';
|
|
|
|
import { isNil } from 'lodash';
|
|
|
|
import { prisma } from '../client';
|
|
|
|
const forumTags = [
|
|
{
|
|
name: 'react',
|
|
},
|
|
{
|
|
name: 'next.js',
|
|
},
|
|
{
|
|
name: 'node.js',
|
|
},
|
|
{
|
|
name: 'nestjs',
|
|
},
|
|
{
|
|
name: 'php',
|
|
},
|
|
{
|
|
name: 'laravel',
|
|
},
|
|
];
|
|
|
|
export const main = async () => {
|
|
await prisma.forumTag.$truncate();
|
|
await prisma.forumPost.$truncate();
|
|
await prisma.forumReply.$truncate();
|
|
const tags = await Promise.all(
|
|
forumTags.map(async (tag) => prisma.forumTag.create({ data: tag })),
|
|
);
|
|
|
|
for (let index = 0; index < 45; index++) {
|
|
const publishedAt = Math.random() > 0.2 ? new Date() : undefined;
|
|
const post = await prisma.forumPost.create({
|
|
select: { id: true },
|
|
data: {
|
|
publishedAt,
|
|
title: faker.lorem.sentence({ min: 3, max: 10 }),
|
|
body: faker.lorem.paragraphs(getRandomMin(8), '<br/>\n'),
|
|
views: getRandomMin(2100),
|
|
tags: {
|
|
connect: getRandListData(tags),
|
|
},
|
|
},
|
|
});
|
|
await genRandomReplies(post.id, Math.floor(Math.random() * 8));
|
|
}
|
|
};
|
|
|
|
async function genRandomReplies(postId: string, count: number, parentId?: string) {
|
|
const replies: { id: string }[] = [];
|
|
for (let i = 0; i < count; i++) {
|
|
const body = faker.lorem.paragraph(Math.floor(Math.random() * 18) + 1);
|
|
const reply = !isNil(parentId)
|
|
? await (prisma.forumReply as any).createChild({
|
|
node: await prisma.forumReply.findUnique({ where: { id: parentId } }),
|
|
data: {
|
|
body,
|
|
post: {
|
|
connect: { id: postId },
|
|
},
|
|
},
|
|
select: { id: true },
|
|
})
|
|
: await (prisma.forumReply as any).createRoot({
|
|
data: {
|
|
body,
|
|
post: {
|
|
connect: { id: postId },
|
|
},
|
|
},
|
|
select: { id: true },
|
|
});
|
|
|
|
replies.push(reply);
|
|
if (Math.random() >= 0.8) {
|
|
reply.children = await genRandomReplies(
|
|
postId,
|
|
Math.floor(Math.random() * 2),
|
|
reply.id,
|
|
);
|
|
}
|
|
}
|
|
return replies;
|
|
}
|