This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🤡 Add factory and seeder for Location data
- Loading branch information
1 parent
f15125d
commit bc08549
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
} |