danibram / mocker-data-generator

A simplified way to generate masive mock data based on a schema, you can use the awesome fake/random data generators like (FakerJs, ChanceJs, CasualJs and RandExpJs)
https://danibram.github.io/mocker-data-generator/
MIT License
426 stars 45 forks source link

When using db and uniqueField no data will be generated #71

Closed Chrisigrimm closed 5 years ago

Chrisigrimm commented 5 years ago

Hello,

when I using db and uniqueField no data will be generated. Sample:

`var customer = { idCounter: { incrementalId: 0, virtual: true }, id: { function: function() { return this.object.idCounter } }, customerId: { faker: 'company.bsBuzz' }, name: { faker: 'company.companyName' } }; var settings = { customerId: { hasOne: "customers", get: "id" }, };

mocker() .schema('customers', customer, 3) .schema('settings', settings, {uniqueField: 'customerId'}) `

Thanks

danibram commented 5 years ago

Hi, Sorry I didnt see that issue... I review your issue and the problem is that uniqueField option dont work the way you want in this example. The way that uniqueField works is that takes an array and extract the values and create the same amount of data as the values extracted from the array, it didnt check if the value is unique inside the array, simply extract the value and generate an entity. I need to improve the error checking for that kind of issues. For that kind of Unique values you can use https://danibram.github.io/mocker-data-generator/#unique This is an example of using uniqueValues if this helps you to understand how works:

var customer = {
    idCounter: {
        incrementalId: 0,
        virtual: true
    },
    id: {
        function: function() {
            return this.object.idCounter
        }
    },
    customerId: {
        faker: 'company.bsBuzz'
    },
    name: {
        faker: 'company.companyName'
    }
};

var settings = {
    customerId: {
        hasOne: "customers",
        get: "id"
    },
    test: [1,2,3,1,5]
};

mocker()
    .schema('customers', customer, 3)
    .schema('settings', settings, { uniqueField: "test" })

And this other maybe more in the way you want:

var customer = {
    idCounter: {
        incrementalId: 0,
        virtual: true
    },
    id: {
        function: function() {
            return this.object.idCounter
        }
    },
    customerId: {
        faker: 'company.bsBuzz'
    },
    name: {
        faker: 'company.companyName'
    }
};

var settings = {
    customerId: {
        hasMany: "customers",
        get: "customerId",
        min: 2,
        unique:true
    }
};

mocker()
    .schema('customers', customer, 2)
    .schema('settings', settings, 2)

The problem is that in the same object sometimes there is not an async field generation of the data, so you cant take as uniqueField some data that is actually calculating by the generator, you have to use virtualfields and helper entities maybe.

Thanks for the issue, I hope this help you! Dani