Skip to content
Logo Theodo

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:

This library has following features:

Let’s see it in action!

Quick install

npm install --save loopback-fixtures

Then, in your server/component-config.json, add :

  // Other components...
  "loopback-fixtures": {}

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:

This will generate 3 users with random name and email:

    name: "{{name.firstName}} {{name.lastName}}"  # <-- {{name.firstName}} : random first name
    email: "{{}}"                   # <-- {{}} : random email
    name: "{{name.firstName}} {{name.lastName}}"
    email: "{{}}"
  user_red:                                       # <-- user_red : should be an unique reference
    name: "{{name.firstName}} {{name.lastName}}"
    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_{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:

  humans:                               # <-- is referenced by @humans
    name: "The Humans"
    name: "We are ROBOTS"

    description: "I'm human n°{@}!"
    groupId: @humans                    # <-- this references "humans" Group
    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:

    name: "Group n°{@}"

    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.

Liked this article?