Socket
Socket
Sign inDemoInstall

@unleashit/mock-data

Package Overview
Dependencies
10
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @unleashit/mock-data

Wrapper for mocker-data-generator providing a set of templates and arguments. Can optionally write to a file (node)


Version published
Weekly downloads
28
increased by1300%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Mock Data

NPM npm (scoped) npm bundle size

Wrapper for mocker-data-generator providing a set of templates with arguments. Can also optionally write to a file. This is mainly meant for the server, but if you have a good enough reason (please take note of the package size), everything works on the client except file writing.

So far the templates currently available are user, article, order and product. More will be added over time.

Install

npm install @unleashit/mock-data

Single schema

For a single schema, call the library with a config object. Possible properties are template, templates, total, args, name, hiddenFields and path.

If path is provided and the envirnment is Node, a JSON file will be written.

import mockData from "@unleashit/mock-data";

async function generateMockData() {
  // returns a promise
  return await mockData({
    template: "product",
    total: 20,
    args: {
      minPrice: 500,
      maxPrice: 10000,
      maxDescription: 10
    },
    hiddenFields: ["createdAt"]
  });
}

generateMockData();

// { product: [ ... ] }

With multiple schemas

For multiple schemas, add as an array on the templates property. Each template can have any of the same fields (except path and templates) as a single template.

const data = await mockData({
  templates: [
    {
      template: "user",
      total: 10,
      name: "authors"
    },
    {
      template: "article",
      total: 20,
      name: "blogs"
    }
  ],
  name: "mockData",
  path: "/home/myUser"
});

// data is returned and saved as /home/myUser/mockData.json
//
// {
//   authors: [...]
//   blogs: [...]
// }

Custom template(s)

Templates are in the same format as mocker-data-generator except must be supplied as a function. Supply any arguments to the args property as an object.

const args = {
  min: 18,
  max: 110,
  setProperty: "anything"
};

const template = () => ({
  id: {
    chance: "guid"
  },
  age: {
    faker: `random.number({"min": ${args.min}, "max": ${args.max}})`
  },
  setProperty: {
    static: `${args.setProperty}`
  }
});

const data = await mockData({
  template: template,
  total: 20,
  args,
  name: "myData"
});

// { myData: [...] }

Keywords

FAQs

Last updated on 29 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc