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

Generates Dates for Mongo DB? #56

Closed anumalasri closed 6 years ago

anumalasri commented 6 years ago

We need to generate Mocks for Mongo DB in ISO Format.

{ dateField: ISODate("2003-05-13T00:00:00Z"), description: "The First government unveiled a new version of the $20 bill the first to be colorized in an effort to thwart counterfeiters." }

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')"

danibram commented 6 years ago

Hi! Umm... it depends what you want, I give you alternatives:

First example

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)

Second example

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)

Third example

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! 🎉🎉

anumalasri commented 6 years ago

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 ?

danibram commented 6 years ago

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.

anumalasri commented 6 years ago

Whatever we do, it will generate a string in data .. I guess, we don't have much options here.

danibram commented 6 years ago

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! 🎉🎉

anumalasri commented 6 years ago

I resolved by generating a String and then later converted into a mongoDB function.

danibram commented 6 years ago

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!