NPM

npm versionGitHub license

Linux Build statusWindows Build statusCodecov coverageCodecov coverage

Dependency StatusDevDependency Status

GitHub licenseAwesome license

Support link

A simplified way to generate massive mock data based on a schema, using the awesome fake/random data generators like (FakerJs, ChanceJs, CasualJs and RandExpJs), all in one tool to generate your fake data for testing.

Getting started

Install the module with: npm install mocker-data-generator

var user = {
    firstName: {
        faker: 'name.firstName'
    },
    lastName: {
        faker: 'name.lastName'
    },
    country: {
        faker: 'address.country'
    },
    createdAt: {
        faker: 'date.past'
    },
    username:{
        function: function() {
            return this.object.lastName.substring(0, 5) + this.object.firstName.substring(0, 3) + Math.floor(Math.random() * 10)
        }
    }
};
var group = {
    description: {
        faker: 'lorem.paragraph'
    },
    users: [{
        function: function() {
            return this.faker.random.arrayElement(this.db.users).username
        },
        length: 10,
        fixedLength: false
    }]
};
var conditionalField = {
    type:{
        values: ['HOUSE', 'CAR', 'MOTORBIKE']
    },
    'object.type=="HOUSE",location':{
        faker: 'address.city'
    },
    'object.type=="CAR"||object.type=="MOTORBIKE",speed':{
        faker: 'random.number'
    }
};

mocker()
    .schema('user', user, 2)
    .schema('group', group, 2)
    .schema('conditionalField', conditionalField, 2)
    .build(function(data) {
        console.log(util.inspect(data, { depth: 10 }))
//This returns an object
// {
//      user:[array of users],
//      group: [array of groups],
//      conditionalField: [array of conditionalFields]
// }
        })

Documentation

Data generation goes with model based composed by generators, the generators can have access to the data generated and to the entity generated. Generators run syncronously, take care of the related entities!!

Methods

Schema definition

Every schema should contains the specified fields. Key can be 2 types:

Inside every value you can put:

        { static: 'hello im fixed field' }
        { self: 'id' } //will get the id of the generated entity
        { db: 'user[0].id' } //will get the first user id
        { eval: 'object.id' } //will get the first user id

        //OR

        { eval: 'db.user[0]' } //will get the first user id

        //OR

        { eval: 'faker.lorem.words()' } //will get the first user id
        { incrementalId: 0 }
        { function: function(){
            //this.db
            //this.object
            //this.faker
            //this.chance
            //this.casual
            return yourValue
        } }

        //OR:

        { function(){
            //this.db
            //this.object
            //this.faker
            //this.chance
            //this.casual
            return yourValue
        } }
        { faker: 'lorem.words' }                            //Run faker.lorem.words()
        { faker: 'lorem.words()' }                          //Run faker.lorem.words()
        { faker: 'lorem.words(1)' }                         //Run faker.lorem.words(1)
        { faker: 'integer({"min": 1, "max": 10})' }         //Run faker.lorem.words(1) and take the first
        { faker: 'random.arrayElement(db.users)' }          //Run faker.arrayElement over a generated user entity
        { faker: 'random.arrayElement(db.users)["userId"]' }  //Run faker.arrayElement over a generated user entity and take the userId only
        { chance: 'integer' }                                //Run chance.integer()
        { chance: 'integer()' }                              //Run chance.integer()
        { chance: 'integer({"min": 1, "max": 10})' }         //Run chance.integer({"min": 1, "max": 10})
        { chance: 'street_suffixes()[0]["name"]' }           //Run chance.street_suffixes() takes first result and the name inside
        { casual: 'country' }
        { chance: 'array_of_digits()' }
        { casual: 'array_of_digits(3)[0]' }
        { randexp: /hello+ (world|to you)/ }

Optional fields

    {
        //Any generator
            //Faker  
        faker: 'random.arrayElement(db.users)[userId]'
            //Chance  
        chance: 'integer'
            //static
        static: 'any static field'
            //Function  
        function: function (){ return /**/ }

        //with the virtual option
        virtual: true

    }

Data generation

Initialize mocker with the config, and then generate any entity with promises style, use generate function that accepts the name of the model and the amount of data to generate. Like the example:

mocker()
    .schema('user', user, 2)
    .schema('group', group, 2)
    .schema('conditionalField', conditionalField, 2)
    .build(function(data) {
        console.log(util.inspect(data, { depth: 10 }))
//This returns an object
// {
//      user:[array of users],
//      group: [array of groups],
//      conditionalField: [array of conditionalFields]
// }
        })

You can also pass instead of the number, an object with the a config, from now {uniqueField}. If this field exists tells to the generator that instead of init a fixed length of data, generate an amount of data depending of the values of the field you will specify. You have 2 way to deal with this, check the examples See the output of this example:

//
// First way, using an 'values' embedded object
//

var cat = {
    name: {
        values: ['txuri', 'pitxi', 'kitty']
    }
};
var m = mocker()
    .schema('cat', cat, 10)
    .schema('cat2', cat, {uniqueField: 'name'})
    .build(function(data){
        console.log(util.inspect(data, {depth:10}))
    })

//
// Second way, without 'values' embedded.
//

var cat = {
    name: ['txuri', 'pitxi', 'kitty']
};
var m = mocker()
    .schema('cat', cat, 10)
    .schema('cat2', cat, {uniqueField: 'name'})
    .build(function(data){
        console.log(util.inspect(data, {depth:10}))
    })

More, Coming soon

Online API

You can visit the repo url here: https://github.com/danibram/mocker-api-tester

Or visit the api directly: https://mocker-api.herokuapp.com/

Development

Run npm install;npm run dev to watch the project, webpack compile the code automatically. Run npm build to build the normal and minified version.

Why not use json-schema-faker?

json-schema-faker is awesome and works really nice, but i need a simplified and fast way to generate mock data for my projects, so i created this.

Credits

I couldn't do this without this awesome libraries, so thanks to all: - Faker: [https://github.com/Marak/faker.js] - Chance: [https://github.com/victorquinn/chancejs] - Casual: [https://github.com/boo1ean/casual] - RandExpJs: [https://github.com/fent/randexp.js]

License

Licensed under the MIT license. 2015