DEV Community

Adam Miedema
Adam Miedema

Posted on • Originally published at Medium on

Create a data factory to seed your database with dummy data of your liking

Photo by Ehud Neuhaus on Unsplash

If you’re working in a lower environment, chances are, you are working with data and you need a good way to seed your databases after a database refresh.

I’ll show you how you can expand the built-in data factory in the Alpas framework to quickly seed your database with the dummy data of your liking so that you can get back to coding.

If you are working off of the Alpas Starter Template, you will notice a couple of folders under the database folder: factories and seeds. These two folders contain default database factory and seed files.

In UserFactory.kt , you’ll see

internal class UserFactory(private val hasher: Hasher) : EntityFactory<User>() {
    override val table = Users

    override fun entity(): User {
        return User {  
email = faker.internet().safeEmailAddress()
            password = hasher.hash("secret")
            name = faker.name().name()
            updatedAt = Instant.now()
            createdAt = Instant.now()
}  
}

and in DatabaseSeeder.kt :

internal class DatabaseSeeder : Seeder() {
    override fun run(app: Application) {
        val user = from(UserFactory(app.make()), "name" to "Jane Doe")
        "Seeded ${user.email}".printAsSuccess()
    }
}

Essentially, if you enable Alpas’s authentication scaffolding, you can seed the database with dummy users when you trigger the database seeder.

A lot of times, it is beneficial to use data that is more “real” than the standard lorem ipsum text. The reason is multi-faceted: it helps developers see how production like data will occupy spaces; avoids happy path silos; is preferred for QA and UAT testing.

When creating a factory, the choice is yours on how prod-like you want the data to be.

Create a new factory

First up, let’s create a new data factory.

In the root folder of your project, use the command ./alpas make:factory <factory name> to make the new factory. You can create as many factories as you need for your entities.

This action will create a new file under the factories folder and the file will look similar to the UserFactory.kt file shown above.

For this example, let’s say I am working on a to-do project and I want to seed the database with some sample tasks. In that case, I named my factory TaskFactory and updated the file to the following:

internal class TaskFactory() : EntityFactory<Task, Tasks>() {
    override val table = Tasks

    override fun entity(): Task {

        return Task {  
            name = faker.chuckNorris().fact()  
            completed = listOf<Boolean>(true, false).random()
            updatedAt = Instant.now()
            createdAt = faker.date().past(1, TimeUnit.HOURS).toInstant()
     }  
   }
}

Alpas comes bundled with the Java Faker library. And… it’s a ton of fun! In the above, you can see for my task name, I am just pulling a Chuck Norris fact. Check out the Faker library as there are a lot of fun placeholders you can pull. Obviously, I’m not going for prod-like data in this example. 😂

Create a seeder

Now that I have a random entity data generator, let’s work on the seeder. 🌱

You can either create a new seeder using ./alpas make:seeder <seeder name> command or just override the existing database seeder if you only plan to call one seeder.

You can run the seeder by using the ./alpas db:seed command which will run the database seeder by default. Or, to target a different seeder, just explicitly point to that seeder, such as ./alpas db:seed TaskSeeder.

For this example, I’ll just change up the DatabaseSeeder.kt file to make it easier.

internal class DatabaseSeeder : Seeder() {
    override fun run(app: Application) {
        val task = from(TaskFactory(app._make_()), 15){  
        } }
}

Essentially, I am just pointing to the TaskFactory and making 15 entries. Pretty easy, huh?

If I run the ./alpas db:seed command and look at the data I created, I see the following:

Chuck Norris is pretty impressive, right???

Just to show a variation of sorts with seeding a database, in one of my previous projects, I was looking to have example data ready to go for user’s that cloned my loadmore sample project on GitHub. I wanted to use images and descriptions taken from Unsplash.com to highlight a ‘Show More’ feature using various methods.

For that project, I chose to hard-code some entries directly in the DatabaseSeeder.kt file which you can check out by clicking here.

I encourage you to look into the Alpas data factories and seeders and use them to your advantage. The ability to quickly seed your database goes a long way not just for you as you code your project, but for those that are helping out with quality assurance and user acceptance.

For more info, check out Alpas’s entity factory and database seeding documentation.


Top comments (0)