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

Commit

Permalink
feat: add seeder account
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanM64 committed Jan 21, 2022
1 parent c8204fb commit 3651c3a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
34 changes: 34 additions & 0 deletions prisma/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as Faker from 'faker';
import { PrismaClient, Account, User } from '@prisma/client';
import { random } from 'faker';

const prisma = new PrismaClient();

export const seedAccounts = async (): Promise<void> => {
const cities = await prisma.city.findMany();

const UserItem = {
firstName: 'Test',
lastName: 'Test',
username: 'Test',
birthDate: Faker.date.between('1985-01-01', '2000-01-05'),
email: '[email protected]',
city: { connect: random.arrayElement(cities.map((m) => ({ id: m.id }))) },
sex: 1,
latitude: 44.837789 + generateRandomFloatInRange(-0.5, 0.5),
longitude: -0.57918 + generateRandomFloatInRange(-0.5, 0.5),
avatar: '',
};

const AccountItem = {
username: 'Test',
password: 'test',
};

await prisma.account.create({ data: AccountItem });
await prisma.user.create({ data: UserItem });
};

function generateRandomFloatInRange(min, max) {
return Math.random() * (max - min + 1) + min;
}
4 changes: 2 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ model User {
birthDate DateTime? @db.Timestamptz(6)
email String @unique
sex Int @default(0)
latitude Float
longitude Float
latitude Float?
longitude Float?
cityId String @db.Uuid()
city City @relation(fields: [cityId], references: [id])
animals Animal[]
Expand Down
4 changes: 4 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { seedUsers } from './users';
import { seedCities } from './cities';
import { seedBreeds } from './breeds';
import { seedAnimals } from './animals';
import { seedAccounts } from './accounts';

const prisma = new PrismaClient();

Expand All @@ -26,6 +27,9 @@ async function main() {
console.log('seeding Users...');
await seedUsers();

console.log('seeding Accounts...');
await seedAccounts();

console.log('seeding Animal...');
await seedAnimals();
}
Expand Down

0 comments on commit 3651c3a

Please sign in to comment.