relay/prisma/seed.ts

81 lines
2.2 KiB
TypeScript

import Prisma, { PrismaClient } from "../generated/prisma";
const client = new PrismaClient();
function getRandomDate(from: Date, to: Date) {
const fromDate = from.getTime();
return new Date(fromDate + Math.random() * (to.getTime() - fromDate));
}
function generateRandomChannelId() {
const min = 1000000000;
const max = 9999999999;
return Math.floor(Math.random() * (max - min + 1)) + min + '';
}
function createRandomChannels(): Prisma.Prisma.ChannelCreateNestedManyWithoutFeedInput {
if (Math.random() < 0.5) {
return { createMany: { data: [] } };
}
const channels: Prisma.Prisma.ChannelCreateNestedManyWithoutFeedInput = {
createMany: {
data: Array.from({ length: 5 }, () => ({
channel_id: generateRandomChannelId()
}))
}
};
return channels;
}
async function createManyChannels(feedId: number) {
const channelData: Prisma.Prisma.ChannelCreateManyInput[] = [ ];
if (Math.random() < .5) return;
for (let i = 0; i < Math.floor(Math.random() * 10) + 1; i++) {
channelData.push({
channel_id: generateRandomChannelId(),
feedId: feedId
});
}
await client.channel.createMany({ data: channelData });
}
async function createManyFeeds() {
const feedData: Prisma.Prisma.FeedCreateManyInput[] = [ ];
for (let i = 0; i < 35; i++) {
feedData.push({
name: `News Network ${i}`,
url: `https://news-network-${i}.com/rss`,
guild_id: "1204426362794811453",
active: Math.random() < .5,
created_at: getRandomDate(
new Date("2020-01-01T00:00:00.000Z"),
new Date("2025-04-27T00:00:00.000Z")
)
});
}
const feedIds = await client.feed.createManyAndReturn({
data: feedData,
select: { id: true }
});
feedIds.forEach(async feed => await createManyChannels(feed.id));
}
async function main() {
await createManyFeeds();
}
main()
.then(async () => { await client.$disconnect() })
.catch(async (error: unknown) => {
console.log(error);
await client.$disconnect();
process.exit(1);
});