bentonam / fakeit

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

Writing to a file not writing all the documents #150

Closed akshay2604 closed 7 years ago

akshay2604 commented 7 years ago

Writing something like this fakeit directory output products.yaml --count 3 results in output/user_undefined.json

{
  "_id": "591165ca80ff8eed1a18bfe9",
  "xAddr": {
    "xCO": "Suite 885",
    "xLine1": "Lisette Court",
    "xLine2": "477 Reese Mount",
    "xCity": "Port Chynafurt",
    "xSOP": "Delaware",
    "xPostCode": "77249",
    "xCountry": "Guernsey"
  }
}"
  }
}  }
}

However running fakeit console products.yaml --count 3 is producing the desired output.

[
  {
    "_id": "591166ac3897c9ed43d495d9",
    "xAddr": {
      "xCO": "Suite 730",
      "xLine1": "Brayan Heights",
      "xLine2": "0812 Streich Run",
      "xCity": "Carmellaview",
      "xSOP": "Mississippi",
      "xPostCode": "75998-1282",
      "xCountry": "Monaco"
    }
  },
  {
    "_id": "591166ac3897c9ed43d495da",
    "xAddr": {
      "xCO": "Apt. 410",
      "xLine1": "Jaden Junction",
      "xLine2": "72132 Trever Spur",
      "xCity": "Pourosview",
      "xSOP": "New York",
      "xPostCode": "20663",
      "xCountry": "Jersey"
    }
  },
  {
    "_id": "591166ac3897c9ed43d495db",
    "xAddr": {
      "xCO": "Suite 702",
      "xLine1": "Greenholt Forks",
      "xLine2": "732 Rolando Garden",
      "xCity": "Joshstad",
      "xSOP": "Tennessee",
      "xPostCode": "85367",
      "xCountry": "Timor-Leste"
    }
  }
]

File is saved with UTF-8 encoding.

tjbenton commented 7 years ago

Can you provide the products.yaml here and if there're other dependencies could you create a gist and just post the link.

akshay2604 commented 7 years ago

products.yaml

name: Products
type: object
key:
  data:
    build: `user_${this.user_id}`
data:
  inputs: /Users/sahusoft/Sites/projects/cloud9pg-api/input/product.json, /Users/sahusoft/Sites/projects/cloud9pg-api/input/abc.json
  pre_run: |
    console.log("inputs", inputs)
    globals.user_counter = 0;
properties:
  _id:
    type: string
    description: The document id
    data:
      build: |
        return require('/Users/sahusoft/Sites/projects/cloud9pg-api/node_modules/mongoose').Types.ObjectId()
  xAddr:
    type: object
    properties:
      xCO:
        data:
          fake: '{{address.secondaryAddress}}'
      xLine1:
        data:
          fake: '{{address.secondaryAddress}}'
      xLine2:
        data:
          fake: '{{address.streetAddress}}'
      xCity:
        data:
          fake: '{{address.city}}'
      xSOP:
        data:
          fake: '{{address.state}}'
      xPostCode:
        data:
          fake: '{{address.zipCode}}'
      xCountry:
        data:
          fake: '{{address.country}}'
tjbenton commented 7 years ago

So It looks like the problem is the way you are creating the key which is what determines what the name of the document is.

key:
  data:
    build: `user_${this.user_id}`

Here you're returning a key of user_undefined every time which is why you're only seeing 1 file generated because each document that's generated overwrites the last one. The reason for this is that this.user_id isn't defined because you haven't defined that property yet

properties:
  _id:
    type: string
    description: The document id
    data:
      build: require('/Users/sahusoft/Sites/projects/cloud9pg-api/node_modules/mongoose').Types.ObjectId()
  user_id: 
    type: string
    data:
      build: faker.random.uuid()

Here's what the output is after adding the user_id property.

screen shot 2017-05-09 at 9 17 46 am

The following doesn't have anything to do with the problem just a helpful tip that will hopefully make things easier for you.

Also it looks like you're in the folder of /Users/sahusoft/Sites/projects/cloud9pg-api and the way this project works is by using relative paths. So instead of specifying the absolute path which will only work on your computer it's better to specify them like this.

data:
  inputs: 
    - input/product.json
    - input/abc.json

Also node works very similarly. So for the properties._id.data.build all you need is require('mongoose').Types.ObjectId() because node will look inside of the node_modules directory for the folder mongoose and then look at it's package.json to find the main file to import.

akshay2604 commented 7 years ago

Hey, Thanks for that. It worked. But how to get all the output in one file instead?

akshay2604 commented 7 years ago

⠋ Products[19:17:56] ✖ error: { Error: propertiesIdDataBuild failed, Cannot find module 'mongoose'.

This error occurs if i remove the the absolute path and write require('mongoose').Types.ObjectId() for input removing absolute path works fine

tjbenton commented 7 years ago

It looks like there is a bug with that, I will open an issue to look into that. For now you'll have to keep the require statements as full paths.

tjbenton commented 7 years ago

As for getting it to output into a single file we currently don't support that. I've thought about adding support for it but I haven't got around to it yet.

For now here's an easy way to do what I think you're wanting to do.

mkdir -p output && fakeit console products.yaml --count 10 >> output/users.json
tjbenton commented 6 years ago

@akshay2604 If you update to fakeit to 1.2.1 you should now be able to require mongoose correctly require('require')

akshay2604 commented 6 years ago

Thanks @tjbenton really appreciate it.