bentonam / fakeit

Generates JSON documents based on models defined in YAML and adds them to a Couchbase Bucket
MIT License
87 stars 21 forks source link

Problem implementing API #167

Open eriknyk opened 7 years ago

eriknyk commented 7 years ago

If I have 2 models a.yaml and b,yaml

and if I execute:

fakeit.generate('glob/to/models/**/*.yaml')
  .then((data) => {
    console.log(data)
  })

data is an array like:

[ '[\n  {\n    "name": "Fahey, Botsford and Schaden",\n    "code": "4c49c6c0-8307-4eea-8e67-99b04930e1d2"\n  }\n]',
  '[\n  {\n    "name": "Oma",\n    "last_name": "Lebsack",\n    "username": "Saige.OConnell14",\n    "password": "ydKAoYYf8hfWLZJ",\n    "email": "Faye.McDermott@gmail.com",\n    "phone": "(925) 798-7312"\n  }\n]' ]

The question here is, how I can determine which array position is what to be used?

I mean my a.yaml file has defined

name: Users
type: object

and b.yaml

name: Companies
type: object

I need a way to get it something like this:

fakeit.generate('glob/to/models/**/*.yaml')
  .then((data) => {
    data.foreEach(item => {
        const modelName = item.name; // -> this should to have Users|Companies
        const records = JSON.parse(item.data); // -> this should contains the serialized data

        records.foreEach(record => {
             db.collection(modelName).insertOne(record) // on mongo db
        })
    })
  })

Note.- Syntax is only a example like: pseudo-code

Actually there is no way to archive this, because it returns an array of strings and there is no way to know what serialized result is for what source name,

Regards.

tjbenton commented 7 years ago

This looks like a bug because you should be able to know what the name is if you're returning the documents. We might have to add a output type of none or object.

For a quick fix to get your stuff working you can changed line 121 to be const parsed = documents. This will disable all other formats, and just return an object. Then you can access the name through result[0].__name. It's not a long term solution but it will get you up and running quickly.

It looks like you're just trying to upload documents to mongo db. It might be easier if you just create a PR that adds support for mongo db. You should be able to duplicate the couchbase output and convert it to mongo db.

Over the next couple months we will be working on making major improvements to fakeit by adding support for plugins which this would be a great use case for a plugin so we will keep this in mind when creating the initial plugins.