This is a toolkit to easily create test data for loopback based applications. It is heavily inspired by Django's Model Mommy.
npm install --save loopback-bakery
Import the module and create a recipe for a loopback PersistedModel:
var app = require('../server'); //path to your loopback server script
var bakery = require('loopback-bakery');
//...
var userRecipe = bakery.Recipe(app.models.User);
userRecipe({
email: 'user@loopback.test',
password: 'xxx'
}).then((user) => {
console.log(user);
});
Or use await:
let newUser = await userRecipe({email: 'user@loopback.test', password: 'xxx'});
You can pass default values when creating the recipe:
var userRecipe = bakery.Recipe(app.models.User, {password: 'xxx'});
var user = await userRecipe({email: 'user@loopback.test'});
You can create multiple samples with quantity()
:
var userList = await recipe.quantity(3)({name: 'Steven', email: 'steven@mail.test'});
console.log(userList.length); //3
Instead of fixed attributes you can use functions that are resolved to attribute values before the new record is created:
var userRecipe = bakery.Recipe(app.models.User, {
password: 'xxx',
email: () => {
return 'user@loopback.test';
}
});
userRecipe().then((user) => {
console.log(user);
});
This is handy if you use fakerjs to generate your test samples:
var faker = require('faker/locale/de');
//...
var userRecipe = bakery.Recipe(app.models.User, {
password: 'xxx',
email: faker.internet.email
});
userRecipe().then((user) => {
console.log(user);
});
Support for Promises is also available. Instead of returning an attribute your function can return a Promise:
var userRecipe = bakery.Recipe(app.models.User, {
password: 'xxx',
email: () => {
return new Promise((resolve) => {
process.nextTick(() => {
resolve('user@loopback');
});
});
}
});
userRecipe().then((user) => {
console.log(user);
});
The bakery allows to easily create users and roles. Use the built-in UserRecipe:
var app = require('../server'); //path to your loopback server script
var bakery = require('loopback-bakery');
//...
var adminUserRecipe = bakery.UserRecipe(app.models.User).withRole('admin', app.models.Role);
let adminUser = await adminUserRecipe({email: 'admin@loopback.test', password: 'admin'});
The recipe will create a new role in case the required user role does not exist.
Use cycle()
to rotate a list of sample values:
let pets = bakery.cycle(['dog', 'cat', 'rabbit']);
console.log(pets()) //dog
console.log(pets()) //cat
console.log(pets()) //rabbit
console.log(pets()) //dog
//...
var bakery = require('loopback-bakery');
var logger = require('debug')('samples');
bakery.withLogging(logger);
var todoRecipe = bakery.Recipe(app.models.TODO);
// Logs: 'Created TODO with attributes {"title":"Write Email to John","text":"Some more infos about the TODO..."}'
todoRecipe({
title: 'Write Email to John',
text: 'Some more infos about the TODO...'
});