Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
feat: add animal/breed seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanM64 committed Jan 7, 2022
1 parent 0182d84 commit e110ec4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
35 changes: 35 additions & 0 deletions prisma/animals.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
const breeds = await prisma.breed.findMany();
const users = await prisma.user.findMany();
const promises: Promise<Animal>[] = [];

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);
};
16 changes: 16 additions & 0 deletions prisma/breeds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as Faker from 'faker';
import { PrismaClient, Breed } from '@prisma/client';

const prisma = new PrismaClient();

export const seedBreeds = async (): Promise<void> => {
const promises: Promise<Breed>[] = [];

for (let index = 0; index < 10; index++) {
const items = {
name: Faker.animal.dog(),
};
promises.push(prisma.breed.create({ data: items }));
}
await Promise.all(promises);
};
9 changes: 9 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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()
Expand Down

0 comments on commit e110ec4

Please sign in to comment.