How to Get Fixtures with Loopback in 5 minutes
Samy Ghribi4 min read
Are you a Loopback user?
Have you ever wondered how to get extra quickly randomized data fixtures for your project?
Are you fed up with writing for loop, using Math.random or generating INSERT INTO statements with Excel?
On my first project with Loopback in Theodo, that was the kind of issues I was facing with data fixtures generation.
We developed a visualization tool of financial data using tables and graphs with my team. We had to use dozens of raw data stored in our database in order to display one cell of a table or a single point of a graph. And we can not use real production data for security and privacy reasons.
At Theodo, we work with the Scrum methodology, which implies in particular that each developed feature must be validated by the Product Owner of the project. To enable the Product Owner to validate this feature, we should then be able to produce a consistent and representative set of data, so that we can verify that the developed visualisation tool provides a clear enough vision to deliver real business value to users.
Our problem was to generate large sets of random data.
After a tedious attempt where I used Excel to generate INSERT statements in SQL, and another where I used a combo of for loops, Math.random and MomentJS: the generated code was becoming so unreadable and so complex that I could not adjust parameters to generate relevant set of data.
Above all, I come from the world of PHP, I have worked on many projects in Symfony2.
In PHP, nelmio/alice is a simple and pragmatic solution, and I decided to take inspiration from this library to solve my problem.
I have written a Loopback component published on NPM that enables to quickly generate huge sets of random data:
https://github.com/sghribi/loopback-fixtures
This library has following features:
- Generate data based on your Loopback model (and not your database scheme)
- Load data with YAML
- Integrate a fake data generator: FakerJS
- Generate massive range of data thanks to helpers
- Handle easily relations between your models
Let’s see it in action!
Quick install
- Step 1: install component
npm install --save loopback-fixtures
- Step 2: enable component
Then, in your server/component-config.json
, add :
{
// Other components...
"loopback-fixtures": {}
}
- Step 3: write a YAML fixture file:
fixture/data/data.yml
Let’s see how it works with examples in the next part.
Demo with User and Group models
Let’s suppose we have two models:
- Group: name
- User: name, email and description, and linked to Group model
This will generate 3 users with random name and email:
User:
user_blue:
name: "{{name.firstName}} {{name.lastName}}" # <-- {{name.firstName}} : random first name
email: "{{internet.email}}" # <-- {{internet.email}} : random email
user_white:
name: "{{name.firstName}} {{name.lastName}}"
email: "{{internet.email}}"
user_red: # <-- user_red : should be an unique reference
name: "{{name.firstName}} {{name.lastName}}"
email: "{{internet.email}}"
In this first example, we are using FakerJS to provide random names for these three generated users: {{name.firstName}} {{name.lastName}}.
This will generate 1000 users with random data:
User:
user_{1..1000}: # <-- {1..1000} : duplicate this line 1000 times
name: "{{name.firstName}}"
description: "I'm user n°{@}!" # <-- '{@}' will be remplaced by : 1, 2... 1000
The helper user_{1..1000} is a shortcut to write: user_1, user_2… user_1000.
This will generate 1000 users in two groups:
Group:
humans: # <-- is referenced by @humans
name: "The Humans"
robots:
name: "We are ROBOTS"
User:
human{1..500}:
description: "I'm human n°{@}!"
groupId: @humans # <-- this references "humans" Group
robot{1..500}:
description: "I'm robot n°{@}!"
groupId: @robots
The notation @humans is used to handle relations between generated data: it refers to the humans group.
This will generate 1000 users randomly dispatched in 10 groups:
Group:
group_{1..10}:
name: "Group n°{@}"
User:
users_{1..1000}:
name: "{{name.firstName}} {{name.lastName}}"
groupId: @group.*
These examples show you how you can customize your fixture file to get extra quickly massive randomized data.
You can read and discover all awesome features provided by this component by reading the README.