diff --git a/prisma/animals.ts b/prisma/animals.ts new file mode 100644 index 0000000..5daf334 --- /dev/null +++ b/prisma/animals.ts @@ -0,0 +1,35 @@ +import * as Faker from 'faker'; +import { PrismaClient, Animal } from '@prisma/client'; +import { random } from 'faker'; + +const prisma = new PrismaClient(); + +export const seedAnimals = async (): Promise => { + const breeds = await prisma.breed.findMany(); + const users = await prisma.user.findMany(); + const promises: Promise[] = []; + + for (let index = 0; index < 10; index++) { + const user = random.arrayElement(users); + + const items = { + name: Faker.name.findName(), + height: 100, + weight: 100, + description: Faker.lorem.lines(2), + castrated: false, + birthYear: Faker.date.between('2000-01-01', new Date()).getFullYear(), + breeds: { + connect: random.arrayElement(breeds.map((m) => ({ id: m.id }))), + }, + user: { + connect: { + id: user.id, + }, + }, + sex: 1, + }; + promises.push(prisma.animal.create({ data: items })); + } + await Promise.all(promises); +}; diff --git a/prisma/breeds.ts b/prisma/breeds.ts new file mode 100644 index 0000000..35a4d65 --- /dev/null +++ b/prisma/breeds.ts @@ -0,0 +1,16 @@ +import * as Faker from 'faker'; +import { PrismaClient, Breed } from '@prisma/client'; + +const prisma = new PrismaClient(); + +export const seedBreeds = async (): Promise => { + const promises: Promise[] = []; + + for (let index = 0; index < 10; index++) { + const items = { + name: Faker.animal.dog(), + }; + promises.push(prisma.breed.create({ data: items })); + } + await Promise.all(promises); +}; diff --git a/prisma/seed.ts b/prisma/seed.ts index b485f37..0e172a0 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -3,6 +3,8 @@ import * as dotenv from 'dotenv'; import * as Faker from 'faker'; import { seedUsers } from './users'; import { seedCities } from './cities'; +import { seedBreeds } from './breeds'; +import { seedAnimals } from './animals'; const prisma = new PrismaClient(); @@ -11,12 +13,19 @@ async function main() { console.log('Seeding...'); await prisma.user.deleteMany({}); await prisma.city.deleteMany({}); + await prisma.breed.deleteMany({}); console.log('seeding Cities...'); await seedCities(); + console.log('seeding Breeds...'); + await seedBreeds(); + console.log('seeding Users...'); await seedUsers(); + + console.log('seeding Animal...'); + await seedAnimals(); } main()