npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

loopback-bakery

v1.0.2

Published

Toolkit to create sample data for loopback models

Readme

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: '[email protected]',
  password: 'xxx'
}).then((user) => {
  console.log(user);
});

Or use await:

let newUser = await userRecipe({email: '[email protected]', 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: '[email protected]'});

You can create multiple samples with quantity():

var userList = await recipe.quantity(3)({name: 'Steven', email: '[email protected]'});
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 '[email protected]';
  }
});

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: '[email protected]', 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...'
});