AdonisJs. Implementing Seeds and Factories with log to a file

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 innode-modules
. You just need to runnpm 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!
