GithubHelp home page GithubHelp logo

cambalab / fake-data-generator Goto Github PK

View Code? Open in Web Editor NEW
49.0 5.0 14.0 945 KB

Just a small open-source script to create fake data given a simple JSON model.

License: GNU General Public License v3.0

JavaScript 97.05% Shell 2.95%
fake-data script npm faker open-source node data-generator data-generation fixture-generator hacktoberfest

fake-data-generator's Introduction

Fake Data Generator

Just a small open-source script to create fake data given a simple JSON model.

Build Status Npm version License License

Introduction

This is a tiny package motivated by the need of generating certain amount of fake data to populate backend fixtures. We started implementing and editing a single .js file with specific characteristics of some backend models and the desired amount we wanted to generate until we ended up with something like this. We personally decided to use the output files in the API endpoints of a test server but you could use them any way you like, they're just .json files.

Built-In Dependencies

  • Faker: we use the Faker API to create fake data

Installation

There are a few ways you can get this library installed:

  • Install as a standalone forked repository
# clone our project or fork your own
git clone https://github.com/Cambalab/fake-data-generator.git
# install dependencies
npm install
  • Install as an npm dependency for your own project
# install it as a dependency or dev-dependency of our own project
npm install --save-dev fake-data-generator
  • Use it globally from a terminal
# install it globally
npm install -g fake-data-generator

Usage

Usage from a forked or cloned repository

  1. Write a .json model in the models directory. Article Example

  2. Run the generate script from a terminal

The following command writes a .json file with an array of 50 elements to the output directory, where:

  • 1st param example: is the name of your model.json file.
  • 2nd param 10: the numbers of models to generate.
  • 3rd param example.json: the name of the output file.
npm run generate example 50 example.json

Output Example

Usage as an npm dependency

  1. Write a model as explained before. It can be a .json file or a javascript Object
  2. Use it in your own module

Params description

amountArg:

  • Type: Number
  • Description: describes how many elements should be created from a given model
  • Required

modelArg:

  • Type: Object | Json file
  • Description: when inputType param is json, modelArg behaves as a file path to that json file. For object inputType values, modelArg behaves like a javascript object, where the model should be defined.
  • Required

fileName:

  • Type: String
  • Description when inputType is json fileName will describe the output path where the file will be writen to.

inputType:

  • Type: String
  • Options: object | json
  • Description: describes the kind of input the generator will receive and read the model from.

outputType:

  • Type: String
  • Options: object | json
  • Description: describes the kind of output the generator will write or return.
// Requires the package
const { generateModel } = require('fake-data-generator')
// Requires a model
const model = require('./models/example.json')
// Generate the model
const amountArg = 50
const modelArg = model
const inputType = 'object'
const outputType = 'object'
const generatedModel = generateModel({ amountArg, modelArg, inputType, outputType })

Note that when using required or import on a .json file the returned value behaves like a javascript Object.

Usage as a global npm dependency

  1. Create a models directory, an output directory and write a .json model as explained before.

    mkdir models
    mkdir output
  2. Run the global npm bin script.

    fake-data-generator example 10 example.json

Models Format

config

  • Type: (optional) Object

  • Details: general configuration.

  • Properties:

    • locale: language used for faker.locale.

amount

  • Type: (optional) Number

  • Details: an amount of objects to generate.

When this value is present, the amount value given from a cli or the generateModel function from the npm package is overwritten.

model

  • Type: Object

  • Details: A declaration of your object model

  • Properties:

    • attributeName an attribute of your model. Example: id
      • type one of fake-data-generator types. Example: faker, randomNumberBetween, Object, Array.
      • value a value corresponding to the specified type.
      • options configuration options for the specified type (required by some types).

Types and Values

A valid format would be an object with the following keys:

  • type
  • value
  • options (optional)

faker

Currently the script supports faker methods that return Date, String or Number data only. It's not ready to handle faker methods that receive arguments yet.

If you're not familiar with faker, take a look at their docs, it's really simple to use.

Any other faker method can be used in the value attribute like this:

suppose we want to generate a company attribute with faker, then we would declare in the model:

{
  "company": {    
    "type": "faker",
    "value": "company.companyName"
  }
}  

Literal

This is simply a pass-through for those occasions when a known value is desired.

value: any

Case with a String

{
  "operating_system": {    
    "type": "Literal",
    "value": "Linux"
  }
}

Case using an Array of elements

{
  "resources": {    
    "type": "Literal",
    "value": ["memory", "disk", "network", "cpu"]
  }
}

Object

This is how the script knows we want to nest objects

say we want to declare a more complex company model:

value: Object an object with a type, value, options structure

{
  "company": {    
    "type": "Object",
    "value": {
      "name": {    
        "type": "faker",
        "value": "company.companyName"
      },
      "address": {
        "type": "Object",
        "value": {
          "street": {
            "type": "faker",
            "value": "address.streetAddress"
          },
          "city": {
            "type": "faker",
            "value": "address.city"
          },
          "state": {
            "type": "faker",
            "value": "address.state"
          }
        }
      }
    }
  }
}

Numbers

randomNumberBetween

The script provides a simple way to get a random number between a range of numbers

value: Array<Number> a range of values to compute the random number

{
  "timesIWatchedNicolasCageMovies": {
    "type": "randomNumberBetween",
    "value": [150, 2587655]
  }
}
randomElementInArray

The script provides a simple way to get a random element from an array of options.

value: Array a list of options to pick from.

{
  "whichMovieToWatchTonight": {
    "type": "randomElementInArray",
    "value": ["Frozen", "Mulan", "The Lion King", "Aladdin", "Pulp Fiction"]
  }
}

output

{
  "whichMovieToWatchTonight": "Pulp Fiction"
}
randomElementsInArray

This one returns a random group of elements from an array of options.

value: Array a list of options to pick from.

{
  "whichMoviesToWatchTonight": {
    "type": "randomElementsInArray",
    "value": ["Frozen", "Mulan", "The Lion King", "Aladdin", "Pulp Fiction"]
  }
}

output

{
  "whichMoviesToWatchTonight": ["Pulp Fiction", "Aladdin"]
}
randomNumberBetweenWithString

Just another version of randomNumberBetween that accepts a range of numbers, a prefix as a string and a suffix as a string

options:

  • prefix: String a value to be interpolated as the number prefix
  • suffix: String a value to be interpolated as the number suffix
{
  "publication": {
    "type": "randomNumberBetweenWithString",
    "value": [1, 2500000],
    "options": {
      "prefix": "#",
      "suffix": "*"
    }
  }
}
incrementNumber

You can get incremental numbers based on the given amount for a model

The value attribute is ignored

options:

  • from: Number starts incrementing from a given number
{
  "brownies": {
    "type": "incrementNumber",
    "options": {
      "from": 420
    }
  }
}

Output using an amount of 3:

[
  {
    "brownies": 420
  },
  {
    "brownies": 421
  },
  {
    "brownies": 422
  },
]

Array

Defines an Array of elements to be created with the same type.

options

  • size: Number How many objects to create. Required, is mutually exclusive with size: Array
  • size: Array A two value array where the first value is the minimum number of entries and the second is the maximum. Required, is mutually exclusive with size: Number

Extending the company model a little further:

as a Number

{
  "company": {    
    "type": "Object",
    "value": {
      "name": {    
        "type": "faker",
        "value": "company.companyName"
      },
      "addresses": {
        "type": "Array",
        "options": {
          "size": 10
        },
        "value": {
          "type": "Object",
          "value": {
            "street": {
              "type": "faker",
              "value": "address.streetAddress"
            },
            "city": {
              "type": "faker",
              "value": "address.city"
            },
            "state": {
              "type": "faker",
              "value": "address.state"
            }
          }
        }
      }
    }
  }
}

as an Array

{
  "company": {    
    "type": "Object",
    "value": {
      "name": {    
        "type": "faker",
        "value": "company.companyName"
      },
      "addresses": {
        "type": "Array",
        "options": {
          "size": [5, 20]
        },
        "value": {
          "type": "Object",
          "value": {
            "street": {
              "type": "faker",
              "value": "address.streetAddress"
            },
            "city": {
              "type": "faker",
              "value": "address.city"
            },
            "state": {
              "type": "faker",
              "value": "address.state"
            }
          }
        }
      }
    }
  }
}
Concatenate
prepend

Adds a fixed String in front of another dynamic value generated by one of the other datatypes.

options

  • text: String The text to be prepended. required
{
  "issue": {
    "type": "prepend",
    "options": {"text": "#"},
    "value": {
      "type": "randomNumberBetween",
      "value": [1, 2500]
    }
  }
}
append

Adds a fixed String at the back of another dynamic value generated by one of the other datatypes.

options

  • text: String The text to be appended. required
{
  "fileName": {
    "type": "append",
    "options": {"text": ".pdf"},
    "value": {
      "type": "faker",
      "value": "random.words"
    }
  }
}

Contribution

Please make sure to read the Contributing Guide before submitting pull requests. There you'll find development environment instructions, common scripts and the project structure summary.

Feel free to open an issue if any faker method is not working as expcected or if you would like support for another data generator module.

License

GNU General Public License version 3

👩‍💻 With 💚 💜 ❤️ by Cambá Coop 🌎 Buenos Aires, Argentina

fake-data-generator's People

Contributors

anthonydeaver avatar dependabot[bot] avatar glmaljkovich avatar jphetphoumy avatar sgobotta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

fake-data-generator's Issues

Cannot find module: model is not being resolved.

Describe the bug
When the package is used as an npm dependency, models must be defined in /models...

Thrown:
{ Error: Cannot find module '/home/xxx/Documents/fake-data-generator/dist/models/authors'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at getModel (/home/xxx/Documents/fake-data-generator/dist/lib/create-model.js:17:19)
    at createSingle (/home/xxx/Documents/fake-data-generator/dist/lib/create-model.js:25:15) code: 'MODULE_NOT_FOUND' }

To Reproduce
Steps to reproduce the behavior:

  1. Create a model
  2. Install fake-data-generator globally
  3. Run node
  4. Past this code and modify it according to your model name.
  5. Error is thrown

Expected behavior
The executable should be able to read the /models directory to find the model that has been assigned to it.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Debian 9
  • Node: 10.16.3

Additional context
In future features, it would be nice the script does not depend on a /models directory and maybe rely on a single hardcoded model that works as input to create a single or many objects of the given model.

As a user i want to get a subgroup of arrays from given options

Is your feature request related to a problem? Please describe.
We already have randomElementInArray. It would be useful to implement a randomElementsInArray function.

Describe the solution you'd like
Given an array of elements returns a random subgroup of those elements.

'node_modules' n’est pas reconnu en tant que commande interne ou externe, un programme exécutable ou un fichier de commandes

Describe the bug

After cloning the git, the command line "npm install" does'nt work.
i have error:
'node_modules' n’est pas reconnu en tant que commande interne
ou externe, un programme exécutable ou un fichier de commandes.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] build: rimraf dist/ && node_modules/.bin/babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./npm-debug.log --copy-files
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\mbiap\AppData\Roaming\npm-cache_logs\2020-12-18T02_46_27_093Z-debug.log
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] prepare: npm run build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] prepare script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\mbiap\AppData\Roaming\npm-cache_logs\2020-12-18T02_46_27_670Z-debug.log

Please can you help me?

Improves test coverage

Is your feature request related to a problem? Please describe.
Test untested modules

Describe the solution you'd like
Write missing unit tests

As a User I want to define an inputType to be able to use different kind of models

Is your feature request related to a problem? Please describe.
The script currently allows .json models coming from a specific folder only.

Describe the solution you'd like
I'd like to use Object type models to generate data.

Describe alternatives you've considered
Add a new attribute inputType and implement the different input types handling.

Propagate options through the parsing process

Is your feature request related to a problem? Please describe.
Keep iterations count and amount data through the parsing process to provide new feature updates

Describe the solution you'd like
Propagate options through the parsing process inside the parseModel function

Dockerize the github changelog generation release step

Is your feature request related to a problem? Please describe.
Maintainers would have to have the github_changelog_generator locally installed to perform a release. That includes installing ruby, which is becoming kind of painful. Even using tools like rvm aliases have to be created in the runtime configuration files.

Describe the solution you'd like
This programs comes with a dockerized version, which can be used to generate a changelog in the virtual machine and getting it back to the local environment.

Describe alternatives you've considered
Add the docker script to generate a changelog during releases.

Additional context
Add documentation for maintainers in the CONTRIBUTING guide.

npm run build error

Hi All,
When i npm run build or prepare, i get

[email protected] build C:_dev\fake-data-generator
node node_modules/.bin/rimraf dist/ && node_modules/.bin/babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./npm-debug.log --copy-files

C:_dev\fake-data-generator\node_modules.bin\rimraf:2
**basedir=$(dirname "$(echo "$0" | sed -e 's,\,/,g')")
^^^^^^^

SyntaxError: missing ) after argument list**
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
Thanks in advance

Create a website that interacts with the fake data generator api

Is your feature request related to a problem? Please describe.
Most of this kind of tools are available in websites. It would be nice to build a simple website to use the generator services

Describe the solution you'd like
Starts with a simple Vuejs SPA with a title and a text area that does nothing. Eventually we can add a json parser and features to interact with the generator api.

Additional context
Is there any easy way to set up simple js webites in github?

Add eslint

Just define a rules extension in the package.json

Example seems to generate null instead of date.between

Describe the bug
Example seems to generate null instead of date.between:

"publicationDate": {
        "type": "faker",
        "value": "date.between",
        "options": ["2019-01-02", "2019-12-29"]

To Reproduce
Steps to reproduce the behavior:

  1. Execute the example provided by fake-data-generator
  2. The field "publicationDate" is always null

Expected behavior
dates should be generated randomly between both dates

Add support for faker date between

Is your feature request related to a problem? Please describe.
Another faker feature that we'd like to add to this generator

Describe the solution you'd like
Pass options in the faker modelAttributeTypes attribute.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.