node-model-factory
A laravel-like model factory using faker.js
Install
Node dependencies
npm install node-model-factory
Initializing
node-model-factory
includes the latest 4.1 version of faker.js. It is injected as the first agument of your definition closure.
API
Factory::define(name, closure)
Factory::times(amount)
Factory::make(name)
Example
./my-factories/factory.js
//Define your models...let factory = ; factory; // include other models (see hometown)factory; ;
Sample Use Cases
Make One
// Make 1 Personvar person = factory; console;/*{ first_name: "Bob", last_name: "Smith", hometown: { city: "Wilmington" state: "NC" }}*/
Make Several
// Make 1 Personvar people = factory; console;/*[ { first_name: "Bob", last_name: "Smith", hometown: { city: "Wilmington" state: "NC" } }, { first_name: "Jane", last_name: "Doe", hometown: { city: "Charleston" state: "SC" } }]*/
Override Attributes
// Make 1 Personvar person = factory; console;/*{ first_name: "John", //first name is 'John' and not random from faker last_name: "Smith", hometown: { city: "Wilmington" state: "NC" }}*/
Not using Faker.js
In case you want to use another library, just include it in the constructor when you initialize the Factory class.
let factory = randomLib; factory