From 336484c13afa4746b3a8e01e90472838fdb480d9 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Mon, 28 Apr 2025 10:31:04 +0100 Subject: [PATCH] chore: include dummy channels with seeds --- prisma/seed.ts | 69 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/prisma/seed.ts b/prisma/seed.ts index cae2eb9..ff260e2 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -2,19 +2,70 @@ import Prisma, { PrismaClient } from "../generated/prisma"; const client = new PrismaClient(); -async function createManyFeeds() { - const records: any = { data: [] }; +function getRandomDate(from: Date, to: Date) { + const fromDate = from.getTime(); + return new Date(fromDate + Math.random() * (to.getTime() - fromDate)); +} - for (let i = 0; i < 35; i++) { - records.data.push({ - name: `News Network ${i}`, - url: `https://news-network-${i}.com/rss`, - guild_id: "1204426362794811453", - active: true +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.feed.createMany(records); + 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() {