[Factory] Seed custom created_at values

Hi everyone! :wave:

I’m trying to seed a table with a custom created_at value for testing purposes.

I created the factory like this:

const dayjs = require('dayjs');

Factory.blueprint('App/Models/Technology', (faker) => {
	return {
		title: faker.sentence({ words: 3 }),
		created_at: dayjs()
			.subtract(Math.random() * 365, 'days')
			.format('YYYY-MM-DD HH:mm:ss'),
	};
});

But the created_at was not replaced in the database like a factory rule.

I also tried insert this in data param.

Factory.blueprint('App/Models/Technology', (faker, i, data = {}) => {
	return {
		title: faker.sentence({ words: 3 }),
		...data,
	};
});

// seeder

await Factory.model('App/Models/Technology').createMany(5, {
	created_at: dayjs()
		.subtract(Math.random() * 365, 'days')
		.format('YYYY-MM-DD HH:mm:ss'),
});

How can I do that? Is it currently possible in Adonis? :slightly_frowning_face:

1 Like