Skip to content
This repository has been archived by the owner on Sep 7, 2023. It is now read-only.

Commit

Permalink
🤡 Add factory and seeder for Location data
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasrohmer committed Jul 9, 2021
1 parent f15125d commit bc08549
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
44 changes: 44 additions & 0 deletions database/factories/Location.ts
Original file line number Diff line number Diff line change
@@ -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();
29 changes: 29 additions & 0 deletions database/seeders/1625845965284_Location.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
}

0 comments on commit bc08549

Please sign in to comment.