From bc08549ac177f5571155a6d9e29d2fcc5b3342f4 Mon Sep 17 00:00:00 2001 From: Matthias Rohmer Date: Fri, 9 Jul 2021 18:13:58 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=A1=20Add=20factory=20and=20seeder=20f?= =?UTF-8?q?or=20Location=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/factories/Location.ts | 44 ++++++++++++++++++++++ database/seeders/1625845965284_Location.ts | 29 ++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 database/factories/Location.ts create mode 100644 database/seeders/1625845965284_Location.ts diff --git a/database/factories/Location.ts b/database/factories/Location.ts new file mode 100644 index 00000000..b7a9d13c --- /dev/null +++ b/database/factories/Location.ts @@ -0,0 +1,44 @@ +import Factory from '@ioc:Adonis/Lucid/Factory'; +import Location, { + LocationTranslation, + LocationStatus, +} from 'App/Models/Location'; +import { AddressFactory } from './Address'; +import { LinkFactory } from './Link'; +import { DateTime } from 'luxon'; + +export const LocationFactory = Factory.define(Location, ({ faker }) => { + const createdAt = faker.date.recent(120).toISOString(); + const updatedAt = faker.date.between(createdAt, new Date()).toISOString(); + + return { + createdAt: DateTime.fromISO(createdAt), + updatedAt: DateTime.fromISO(updatedAt), + status: faker.random.arrayElement([ + LocationStatus.DRAFT, + LocationStatus.PUBLISHED, + ]), + }; +}) + .state('draft', (location, { faker }) => { + location.status = LocationStatus.DRAFT; + }) + .state('published', (location, { faker }) => { + location.status = LocationStatus.PUBLISHED; + }) + .relation('translations', () => + Factory.define(LocationTranslation, ({ faker }) => { + faker.locale = 'de'; + + return { + name: faker.company.companyName(), + description: faker.datatype.boolean() + ? faker.lorem.paragraph() + : undefined, + }; + }).build() + ) + .relation('address', () => AddressFactory) + .relation('links', () => LinkFactory) + + .build(); diff --git a/database/seeders/1625845965284_Location.ts b/database/seeders/1625845965284_Location.ts new file mode 100644 index 00000000..dd558024 --- /dev/null +++ b/database/seeders/1625845965284_Location.ts @@ -0,0 +1,29 @@ +import BaseSeeder from '@ioc:Adonis/Lucid/Seeder'; +import Location from 'App/Models/Location'; +import Organizer from 'App/Models/Organizer'; +import faker from 'faker'; +import { LocationFactory } from 'Database/factories/Location'; + +export default class LocationSeeder extends BaseSeeder { + public async run() { + const organizers = faker.random.arrayElements( + await Organizer.query().preload('translations').preload('address') + ); + for (const organizer of organizers) { + const factory = LocationFactory.merge({}); + + for (const translation of organizer.translations) { + factory.with('translations', 1, (translationFactory) => { + translationFactory.merge(translation.serializeAttributes()); + }); + } + + factory.with('address', 1, (address) => { + address.merge(organizer.address.serializeAttributes()); + }); + + const location = await factory.create(); + console.log('Created', { location }); + } + } +}