npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

data-generator-light

v1.0.3

Published

A simple utility for quickly generating mock data. An example use case would be, in conjunction with [json-server](https://github.com/typicode/json-server), mocking out an API.

Downloads

7

Readme

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.