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

[Question] How to call `chance.pickone` in the schema? #60

Closed dinfer closed 6 years ago

dinfer commented 6 years ago
var cat = {
    id: {
        incrementalId: 0,
    },
};
var room = {
    cats: {
        eval: "chance.pickset(db.cats, 2).map(v => v.id)" // works fine
    },
    cats2: {
        chance: 'pickset(db.cats, 2)' //  picks nothing
    }
}

mocker()
    .schema('cats', cat, 9)
    .schema('rooms', room,1)

There maybe a better method to call chance.js or faker.js

var room = {
    cats: {
        eval: "chance.pickset(db.cats, 2).map(v => v.id)"
    },
    cats2: {
        chance: {
            method: 'pickset', // throw error if there is no such a method
            arguments: ['this.db', 3, '"string"'],
            eval: true, // all elements in the arguments are `eval`ed if eval is true
        }
    }
}
danibram commented 6 years ago

Thanks for open a issue. Yes, the problem is that in the firsts versions of mocker, I used eval for everything, now I created that function to try to decompose more complex sintax without using eval to improve the performance, it works but It decrease the freedom of the user. Right now to doing that, there is the 3 ways you said:

var cat = {
    id: {
        incrementalId: 0,
    },
};
var room = {
    cats: {
        eval: "chance.pickset(db.cats, 2).map(v => v.id)"
    }
}

mocker()
    .schema('cats', cat, 9)
    .schema('rooms', room,1)

OR:

var cat = {
    id: {
        incrementalId: 0,
    },
};
var room = {
    cats: {
        chance: "pickone(db.cats)", // right now there is a bug here!
        eval: true
    }
}

mocker()
    .schema('cats', cat, 9)
    .schema('rooms', room,1)

OR:

var cat = {
    id: {
        incrementalId: 0,
    },
};
var room = {
    cats: {
        function() {
            return this.chance.pickone(this.db.cats)
        }
    }
}

mocker()
    .schema('cats', cat, 9)
    .schema('rooms', room,1)

Oh! I discover that there is an error on the eval: true option, I will release today a fix! Right now you can use the other 2 options, Im sorry for the incovenience! In additionn I will try if we will have time to improve my parse string to function to provde more freedom. Thanks again for the issue and for your time!

dinfer commented 6 years ago

You are very kind.

By the way, Is there a way to choose which library will be used? I got a warning, and I didn't use faker.

 WARNING  Compiled with 1 warnings                                                                                                             13:28:03
 warning  in ./node_modules/faker/locale/ReadMe.md

Module parse failed: Unexpected character '#' (1:0)
You may need an appropriate loader to handle this file type.
| # Read Me
|
| The files in this directory have been auto-generated from the `gulpfile`.

 @ ./node_modules/faker/locale ^\.\/.*$
 @ ./node_modules/mocker-data-generator/build/module/lib/Generator.js
 @ ./node_modules/mocker-data-generator/build/module/index.js
 @ ./src/config/mock.ts
 @ ./src/config/index.ts
 @ ./src/main.ts
 @ multi (webpack)-dev-server/client?http://localhost:8081 webpack/hot/dev-server ./src/main.ts

Thanks again for your awesome library and quick reply! 👍

danibram commented 6 years ago

umm... thats bad... I thought that for future versions, maybe something that, you can decide what generators use or not, but I discard this thought because since the begining I provide a bundle and include them. Now you report an error, and another user in the past reported me another error related to chance, so maybe I need to think a better way to manage it. In addition you can open in fakerJs an issue about that. For you, I know is not really good solution, but maybe you can in every npm install remove every file *.md from the fakerJs locale folder or add a markdown loader, but I know is not a good solution... Also I can think a better way to manage that because I saw that importing all faker, Im importing all the locales, and this is what is broken, maybe with the other fix I will try to import faker on demand like I do now with the locales. Thanks for the issue stay tunned! 🎉🎉

danibram commented 6 years ago

This should fixed in v2.6.4

adamkasztenny commented 5 years ago

Not fixed, I mentioned it in #33.