1.0.3 • Published 6 years ago

data-generator-light v1.0.3

Weekly downloads
6
License
ISC
Repository
github
Last release
6 years ago

Build Status Coverage Status

Data Generator

A simple utility for quickly generating mock data. An example use case would be, in conjunction with json-server, mocking out an API.

Installation

npm install --save-dev data-generator-light

Functionality

The generator creates N number of collections with N members.

The members each:

  • Have a numeric primary key.
  • Have N number of fields. Values for those fields are specified and generated using json-schema-faker.
  • Optionally have N number of foreign keys:
    • The foreign keys 'point' toward members of other generated collections.
    • Data is normalized. For example, if both an 'account' object and a 'user' object have a 'userName' field, they would both have the same values for that field.

Usage

The generator takes an array of object literals as its input. The object literals correspond to IJobDefinition and consist of two parts : parameters that are unique to this package, and a specification used by json-schema-faker.

export interface IJobDefinition {
    /**
     * Custom parameters used by the generator
     */
    parameters: IParameters;
    /**
     * Specification used by json-schema-faker. See https://github.com/json-schema-faker/json-schema-faker.
     */
    specification: any;
}
export interface IParameters {
    /**
     * The name associated with the collection. For example, a collection of user objects may be referred to as 'users.'
     */
    collectionName: string;
    /**
     * The number of objects to create.
     */
    numberToCreate: number;
    /**
     * The field associated with the objects primary key. Defaults to 'id' if not specified.
     * Note: If a non-numeric primary key is needed, generate it using the specification rather than here.
     */
    primaryKey?: string;
    /**
     * If a collection has any foreign keys. For example, an address object might be associated with a specific user.
     */
    foreignKeys?: IForeignKey[];
}
export interface IForeignKey {
    /**
     * The foreign key. For example, an account object might be associated with a specific user. It might refer
     * to that user by a field called 'userId'.
     */
    foreignKey: string
    /**
     * The collection it refers to. For example, an account object might be associated with 'users.'
     */
    forCollection: string
}

Example Job Definition

The job definition below would create:

  • An 'accounts' collection
    • With 30 members.
    • A primary key of "accountId."
    • One foreign key - 'uid' - that refers to a 'users' collection.
    • Two randomly generated properties - balance and userName.
    • Assuming userName also exists on users, both the object and its foreign reference would have the same value for this field.
    const accountsDefinition: IJobDefinition = {
        "parameters": {
            "collectionName": "account",
            "numberToCreate": 30,
            "primaryKey": "accountId",
            "foreignKeys": [
                {"foreignKey": "uid", "forCollection": "users"}
            ]
        },
        "specification": {
            "type": "object",
            "properties": {
                "balance": {
                    "type": "string",
                    "faker": {
                        "finance.amount": [100, 10000, 2, "$"]
                    }
                },
                "userName": {
                    "type": 'string',
                    "faker": 'name.findName'
                }
            },
            "required": [
                "userName", "balance"
            ]
        }
    };

Example Usage

This example script takes all .json files in a directory and plugs them into the generator. It outputs the generated data to standard out.

const jsf = require('json-schema-faker');
const jobDefinitionsDir = __dirname + "/../job-definitions/";

let jobDefinitions: IJobDefinition[] = [];
readdirSync(jobDefinitionsDir).forEach((file) => {
    if (file.indexOf(".json") > -1) {
        const jobDefinition: IJobDefinition = require(jobDefinitionsDir + file);
        const { collectionName, numberToCreate, primaryKey, foreignKeys } = jobDefinition.parameters;
        jobDefinitions.push({
            parameters: {
                collectionName,
                numberToCreate,
                primaryKey,
                foreignKeys
            },
            specification: jobDefinition.specification
        });
        jobDefinitions.push(require(jobDefinitionsDir + file));
    }
});

const generator = new Generator(jsf);
const data = generator.generate(jobDefinitions);
process.stdout.write(JSON.stringify(data));

Limitations

This data generator is a lightweight utility that's intended to simulate the types of data I receive from my own API's. I might be making different assumptions from those that inform your own API's. This utility is not intended to be comprehensive, nor is this utility intended to generate fixtures for mock data that you might use to populate a database.

Refer to json-schema-faker for information on how to populate the specifications.