Closed anumalasri closed 6 years ago
Hi! Umm... it depends what you want, I give you alternatives:
var test = {
dateField: {
function: () => 'ISODate("2003-05-13T00:00:00Z")'
},
description: {
static: "The First government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters."
}
};
mocker()
.schema('tests', test, 1)
Using FakerJs (one of the supported generators), in his documentation you can see how to generate an iso date -> https://github.com/marak/Faker.js/
var test = {
dateField: {
faker: 'date.past'
},
description: {
static: "The First government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters."
}
};
mocker()
.schema('tests', test, 1)
Using FakerJs and mocker-data-generator function
var test = {
dateField: {
function: function() {
return `ISODate("${this.faker.date.past().toISOString()}")`
}
},
description: {
static: "The First government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters."
}
};
mocker()
.schema('tests', test, 1)
I hope this helps you, I tested this examples in the playground, you can do it, simply pasting the code in the left side of the editor here -> https://danibram.github.io/mocker-data-generator/
Thank you for using mocker-data-generator! 🎉🎉
All three example will return a string value.
However, for Mongo, we need value as Mongo function with timestamp
"dateField": ISODate("2003-05-13T00:00:00Z") // Required
vs
"dateField": "ISODate('2003-05-13T00:00:00Z')" // Not required
Any possibility to achieve the first one ?
To keep the library ligth as possible, I dont include external libraries, so if you want to return ISODate object as you need, I recomend you to use function generator using like that:
const ISODate = require(' module to construct an ISODate ')
var test = {
dateField: {
function: function() {
return ISODate("with", "the", "args", "you", "need")
}
}
};
So with the function you can create whathever structure you need.
Whatever we do, it will generate a string in data .. I guess, we don't have much options here.
Sorry for the delay, I think that kind of data is related only to mongoDB console, or mongoDB queries. I had mongoDB Databases and when I export I cant see the Structure you said... Try to export in plain Json a database and you can check that. But if you can provide me a way to generate this kind of structure in Javascript, I can consider add it to the mocker. If not think that mocker-data-generator is a plain JSON helper, wrapping all common libraries to generate mock data, but it try to provide an JSON plain output. Thank you for using mocker-data-generator and for your time! 🎉🎉
I resolved by generating a String and then later converted into a mongoDB function.
NIce to hear that, if you pass here the code or you make a pull request I can add your function as a generator. Thanks for your time and for using this module!
We need to generate Mocks for Mongo DB in ISO Format.
How to generate dateField like above ??
All my tries are treating that field as String and data got generated dateField: "ISODate('2003-05-13T00:00:00Z')"