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 🙏

© 2024 – Pkg Stats / Ryan Hefner

fixture-factory

v1.5.1

Published

Factory for faker.js fixture generator

Downloads

2,120

Readme

fixture-factory

Generate massive amounts of fixtures based on predefined model using faker.js methods.

Installation

npm install fixture-factory --save-dev

Usage

Register your model

var fixtureFactory = require('fixture-factory');

var userDataModel = {
  firstName: 'name.firstName',
  lastName: 'name.lastName',
};

fixtureFactory.register('user', userDataModel);

Generate single fixture

fixtureFactory.generateOne('user');

Expected Output

{
 firstName: <generated first name>,
 lastName: <generated last name>
}

Generate multiple fixtures

fixtureFactory.generate('user', 10);

Expected Output

[{
  firstName: <generated first name>,
  lastName: <generated last name>
}, ... 9 more
]

Generate nested objects

fixtureFactory.register('user',{
    type: 'admin',
    firstName: 'Daniel',
    role: {
        id: 'random.uuid',
        name: 'internet.userName'
    }
  });

fixtureFactory.generateOne('user');

Expected Output

{ type: 'admin',
  firstName: 'Daniel',
  role:
  {
     id: '15751f0a-569d-4789-89cc-8f7c8405f007',
     name: 'German_Glover10'
  }
}

Generate array of nested objects

fixtureFactory.register('user',{
    type: 'admin',
    firstName: 'Daniel',
    roles: [{
        id: 'random.uuid',
        name: 'internet.userName'
    }, 10]
  });

fixtureFactory.generateOne('user');

Expected Output

{ type: 'admin',
  firstName: 'Daniel',
  roles:
  [
     {
       id: '1b8df9da-3f1a-4f9b-ab96-1de8cc4844c5',
       name: 'Dawn_Dooley30'
     },
     ... 9 more
  ]
}

Generate with extra fields

fixtureFactory.generate('user',1 ,{
    type: 'admin',
    firstName: 'Daniel'
  });

Expected Output

{
 firstName: 'Daniel',
 lastName: <generated last name>,
 type: 'admin'
}

Generate without registered model

fixtureFactory.generateOne({
  email: 'internet.email'
  });

Expected Output

{
 email: <generated email>
}

Get your own instance

In case you want to have multiple instances of factory you can call noConflict.

var fixtureFactory = require('fixture-factory');
var secondFixtureFactory = fixtureFactory.noConflict();

Values in dataModel

fixed field values

If defined method won't be found in faker.js it will be treated as simple string to be used as field value

var fixtureFactory = require('fixture-factory');

var userDataModel = {
  staticField: 'someValue'
};

fixtureFactory.register('user', userDataModel);
fixtureFactory.generateOne('user');

Expected Output

{
 staticField: 'someValue'
}

functions

You may define a function in the data model which will be processed after all other fixtures have been generated

var fixtureFactory = require('fixture-factory');

var userDataModel = {
  name: 'name.firstName',
  email: function(fixtures) {
    return fixtures.name + '@acme.com';
  }
};

fixtureFactory.register('user', userDataModel);
fixtureFactory.generateOne('user');

Expected Output

{
 staticField: '<generated name>@acme.com'
}

passing options to faker.js methods

if given method requires additional parameters you can pass it by adding args property

var fixtureFactory = require('fixture-factory');

var userDataModel = {
  age: {
    method: 'random.number',
    args: [
      {
        min: 18,
        max: 90
      }
    ]
  }
};

fixtureFactory.register('user', userDataModel);
fixtureFactory.generateOne();

Expected Output

{
 age: <number between 18 and 90>
}

passing parser method

you can pass parser method to change modify value of acquired fixture field

var fixtureFactory = require('fixture-factory');

var userDataModel = {
  firstName : 'name.firstName'
};

fixtureFactory.register('user', userDataModel);

fixtureFactory.generateOne('user', {
  firstName: function (fixtures, options, dataModel, faker) {
    return 'sir '+ faker.name.firstName();
  }
});

Expected Output

{
 firstName: 'sir <some generated name>'
}

Reference Plugin

The Reference plugin is an example extension of the fixture factory that allows embeding other models into your definition. It is enabled by default.

Embed another model in your fixture

fixtureFactory.register('user',{
  type: 'admin',
  firstName: 'Daniel'
});

fixtureFactory.register('role',{
  id: 'random.uuid',
  name: 'internet.userName'
});

fixtureFactory.generateOne('user', {
  role: 'model.role'  
});

Expected Output

{
  type: 'admin',
  firstName: 'Daniel',
  role:
  {
     id: '15751f0a-569d-4789-89cc-8f7c8405f007',
     name: 'German_Glover10'
  }
}

Embed another model in your fixture and provide properties

fixtureFactory.register('user',{
  type: 'admin',
  firstName: 'Daniel'
});

fixtureFactory.register('role',{
  id: 'random.uuid',
  name: 'internet.userName'
});

fixtureFactory.generateOne('user', {
  role: {
    method: 'model.role',
    reference: {
      properties: {
        active: true
      }
    }
  }
});

Expected Output

{
  type: 'admin',
  firstName: 'Daniel',
  role:
  {
    id: '8b23b7f8-c14b-4231-a768-5ecc407a5821',
    name: 'Dianna36',
    active: true
  }
}

Embed another model field in your fixture

fixtureFactory.register('user',{
  type: 'admin',
  firstName: 'Daniel'
});

fixtureFactory.register('role',{
  id: 'random.uuid',
  name: 'internet.userName'
});

fixtureFactory.generateOne('user', {
  roleId: 'model.role.id'  
});

Expected Output

{
  type: 'admin',
  firstName: 'Daniel',
  roleId: '17b6ec69-606d-4e97-b2c5-4eb9b3507e32'
}

TO DO

  • add support for defining own generators

##LICENSE

The MIT License (MIT)

Copyright (c) 2014 Peter Kaleta

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.