Closed swatjoshi closed 5 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)
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
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
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));
});
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!
Oh sure..I will work on your suggestion. Thank you for your guidance !!
Is the hasOne guarantee get a unique result? I want to create two table that has One to One relationship.
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!
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.
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.
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!
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
issue.zip![capture](https://user-images.githubusercontent.com/28627090/33304731-82bbe606-d430-11e7-90c0-0a969ba77ba9.PNG)