Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

loopback-bakery

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loopback-bakery

Toolkit to create sample data for loopback models

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
4
100%
Maintainers
1
Weekly downloads
 
Created
Source

loopback-bakery

This is a toolkit to easily create test data for loopback based applications. It is heavily inspired by Django's Model Mommy.

Installation

npm install --save loopback-bakery

Basic Usage

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

Dynamic Data

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);
});

Users and Roles

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.

Utils

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
//...

Logging

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...'
});

Keywords

loopback

FAQs

Package last updated on 22 Feb 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts