AdonisJs. Implementing Seeds and Factories with logging to a file

Artem Diashkin
Apr 7, 2020 · 3 min read

Did you know that after creating tables in your database you can easily fill it with fake data? It can be done in a blink of an eye.

I will assume that you already know how migrations work in AdonisJs, if not — you can check my previous story: AdonisJs. Beginner’s Guide.

Let’s assume we have /database/migrations/***_user.js migration file, that is similar (with few changes) to migration file that is created when you creating new adonis project with adonis new projectname --api-only cli command:

After UserSchema file, we will create new additional migration file for creating Profile table, with relationship/foreign-key to User:

/database/migrations/***_profile.js migration file:

After running our migration, using adonis migration:run we will see successfully created empty tables in our database:

To fill that data with fake entities, we will use Seeds and Factory

I will show you the result of this story before describing the implementation, so you would know what to expect, and so it would be much interesting to investigate how it is done 😉

After your investigation of this story will be done and running cli command adonis seed you will get… 🥁

User database:

Profile database:

And beautiful (I don’t know how about you guys) log in /tmp/adonis.log file:

Why to even bother implementing log? Because passwords will be hashed before storing in database (if you implemented it of course), like this:

$2a$10$ENaedK0S9DgtXfrBKAOHPOOG7hLnD3TiuV5BaK0A0yL09iIs0Nmka

So, if you would want to login to your app using one of those accounts, you will not be able to do that, not knowing user’s passwords.

So, let’s get to work 🙂

Creating Seed

To create new Seed file just run cli command:

adonis make:seed User

After that you will notice new created /database/seeds/UserSeeder.js file.

You can copy-paste code bellow into that UserSeeder.js file.

I wrote few lines of documentation for explaining how it works.

You can easily use simple implementation from example below, but I wanted to create a function, that will create multiple entities in database, not just one. It will be frustrating to run script each time just for one record

Creating Factory

After that, copy-paste code below to /database/factory.js file:

Configuring Logger

And last modification will be to the Logger configuration.

In /config/app.js file, before module.exports = { copy and paste this lines:

winston npm package already included in node-modules . You just need to run npm i -S winston command.

const { format } = require('winston');

const moment = use('moment');

const { combine, timestamp, printf } = format;

const myFormat = printf(({ level, message, timestamp }) => `${level.toUpperCase()} [${moment(timestamp)
.format('YYYY MMMM DD, h:mm:ss')}]\n${message}`);

This is the last one, I promise.

And your logger configuration should look like this:

Thats it! You’ve done it! Don’t forget to run cli command for magic to happen:

adonis seed

Great job!

LITSLINK

LITSLINK’s team is sharing their experience

Sign up for LITSLINK Newsletter

By LITSLINK

It's a test newsletter! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Artem Diashkin

Written by

Java, Spring, Node.js, AdonisJs, React.js and Flutter developer

LITSLINK

LITSLINK

Hands-on up to date experience sharing of the most skilled & talented software developers right here, right now. Subscribe to learn & understand more about the Software World.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium