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

Avoid duplicate data selection from arrays, while using hasMany : 'schemaName' inside another object schema #46

Closed swatjoshi closed 5 years ago

swatjoshi commented 6 years ago

Hi I have been facing problem due to the duplicate data generation in the array, which is used inside another schema. When I say array usage inside another schema, I mean using hasMany : 'schemaName' for populating multiple values of schemaName I am attaching my JS file and related screen-shot to make you understand better Thanks

var mocker = require('mocker-data-generator').default;
  var {ObjectID} = require('mongodb');
  var user = {
    email : {
        faker : 'internet.email'
    },
    org : {
        values : ['deloitte', 'tcs', 'infosys', 'wipro']
    },
    name : {
        faker : 'name.findName'
    },
    _id : {
        function : function () {
            return new ObjectID();
        }
    }
} 
var finalSchema = {
    //I want two unique users to be inserted inside my finalSchema
    userDetails : {
        hasMany : 'users',
        amount : 2 
    },
    date : {
        faker : 'date.past'
    }
}
mocker()
.schema('users', user, {uniqueField :'_id', min : 10, max : 10})
.schema('finalObject', finalSchema, 2)
.build()
.then(function (data) {
    console.log(JSON.stringify(data.finalObject, undefined, 2));
});

issue.zip capture

danibram commented 6 years ago

First of all thank you for helping to improve mocker. Try the example below in the playground, I think is that you need, the problem is like uniqueField is made only for fields that are array of strings, not for array of objects:

Also I m going to implement unique option in hasMany to make more clear.

For example uniqueField it will work with uniqueField:'org' in the case of user model Thank you, let me if it works!

var user = {
    email : {
        faker : 'internet.email'
    },
    org : {
        values : ['deloitte', 'tcs', 'infosys', 'wipro']
    },
    name : {
        faker : 'name.findName'
    },
    _id : {
        function : function () {
            return Math.floor(Math.random() * 100);
        }
    }
}
var finalSchema = {
userDetails : {
    hasMany : 'users',
        amount : 2
    },
    date : {
        faker : 'date.past'
    }
}

mocker()
    .schema('users', user, 10)
    .schema('finalObject', finalSchema, 2)
swatjoshi commented 6 years ago

Hi Thank you for your reply !! The code you asked me to try in playground, is working the way I want but the problem is that I have requirement that my data should have mongoose id only which is 24 character string with hexa-decimal values For example "_id": "5a1d0ec1ba89971c146af1a1" which is a string and Math.random() may not work for this

danibram commented 6 years ago

yes, but in my playgroud i dont want to install external libs, so i substitiute for a native one. Change for your ObjectId and its done

swatjoshi commented 6 years ago

Hello Again !! I replaced ObjectId with the Math.random(). But it created the duplicates again. Even I tried with Math.random() and they are generated again. I am attaching the screenshots for both Math.random() and ObjectId outputs var mocker = require('mocker-data-generator').default; var {ObjectID} = require('mongodb'); var user = { email : { faker : 'internet.email' }, org : { values : ['deloitte', 'tcs', 'infosys', 'wipro'] }, name : { faker : 'name.findName' }, _id : { function : function () { //return Math.floor(Math.random() * 100); return new ObjectID(); } } } var finalSchema = { //I want two unique users to be inserted inside my finalSchema userDetails : { hasMany : 'users', amount : 2 }, date : { faker : 'date.past' } } mocker() .schema('users', user, 10) .schema('finalObject', finalSchema, 2) .build() .then(function (data) { console.log(JSON.stringify(data.finalObject, undefined, 2)); }); objectid randomid

danibram commented 6 years ago

Yes, as i said, hasMany was not be implemented to get unique values, so right now i recomend you to increase the number of users. And im going to develop that feature!Thanks for your contribution!

swatjoshi commented 6 years ago

Oh sure..I will work on your suggestion. Thank you for your guidance !!

bcl-lcb commented 6 years ago

Is the hasOne guarantee get a unique result? I want to create two table that has One to One relationship.

danibram commented 6 years ago

Thanks for the comments. Unfortunately no right now hasMany is based on hasOne, and hasOne take a random element

hasOne(cfg) {
        let db = this.DB
        let i = Math.floor(db[cfg.hasOne].length * Math.random())
        let entity = db[cfg.hasOne][i]

        if (cfg.get) {
            return eval('entity.' + cfg.get)
        } else {
            return entity
        }
    }

    hasMany(cfg) {
        /* ... */
        return Array.from(new Array(amount)).map(() => this.hasOne(newCfg))
    }
}

Rigth now you need to increase the number of the related entity, but, yes, this is not the correct way. Im developing that feature, this issues helps me a lot to know what features need to be released, so if you feel miss something dont be shy, open issues. So thank you again, and stay tunned! I will close this in the next version!

bcl-lcb commented 6 years ago

OK. Thank you! Looking forward about this. It will be awesome if there are ways to associate tables with One to One, One to Many and Many to Many relationship.

coffenbacher commented 6 years ago

Thanks for looking into this! The library looks really good. What I am hoping for to adopt it is some way to generate Many-to-Many relationships that are unique.

For example, imagine a social network with a Users table that is N:N with itself, using a Friendships table as a through table. How can we generate unique Friendships? I believe this is doable via the ask for a HasOne unique constraint.

danibram commented 5 years ago

Hi guys, I just released the version 2.6.6 that has unique option in hasMany, so I will close that. The feature that @CraigLee17 is talking maybe it includes a rework of the core so I will open another issue about that. Thanks for using mocker-data-generator!