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

datagen-rs-node

v0.1.0

Published

This package contains node.js bindings to `datagen` for generating random data. The bindings are built using [napi-rs](https://napi.rs).

Downloads

1

Readme

@datagen-rs/node

This package contains node.js bindings to datagen for generating random data. The bindings are built using napi-rs.

Installation

npm install datagen-rs-node

If you want to use the bindings in a TypeScript project, you may also need to install the datagen-rs-types package:

npm install --save-dev @datagen-rs/types

Usage

Generate random data

In order to generate random data, you must pass a JSON schema to the generateRandomData function:

import { generateRandomData } from 'datagen-rs-node';

const generated = await generateRandomData({
  type: 'string',
  generator: {
    type: 'uuid',
  },
});

The result will be the serialized generated data as a string. In order to customize the serialization, you can pass a serializer into the options object.

Generate random data with progress

If you want to get progress updates while generating data, you can pass a progress callback to the generateRandomData function:

import { generateRandomData } from 'datagen-rs-node';

const generated = await generateRandomData(
  {
    type: 'string',
    generator: {
      type: 'uuid',
    },
  },
  ({ current, total }) => {
    console.log(`Generated ${current}/${total} items`);
  }
);

The progress value is of type GenerateProgress and has the following structure:

interface GenerateProgress {
  current: number;
  total: number;
}

Check the progress-plugin documentation for further information on how the progress is calculated.

Generate random data using a plugin

You can pass additional plugins to the generateRandomData function. These plugins will be loaded before the generation starts and can be used to extend the functionality of datagen. Check out the plugin documentation for more information on creating node.js plugins.

import { generateRandomData, CurrentSchema } from 'datagen-rs-node';

const generated = await generateRandomData(
  {
    type: 'plugin',
    pluginName: 'myPlugin',
    args: {
      name: 'test',
    },
  },
  null,
  {
    myPlugin: {
      generate(schema: CurrentSchema, args: any): any {
        return 'Hello World!';
      },
    },
  }
);

Note that this package also exports a CurrentSchema type that can be used to type the schema parameter of the generate function. This is simply the implementation of the CurrentSchema interface from the @datagen/types package.

Retrieve the JSON schema

In order to retrieve the JSON schema, you can use the getJsonSchema or getJsonSchemaAsync functions:

import { getJsonSchema, getJsonSchemaAsync } from 'datagen-rs-node';

const schema = getJsonSchema();
const schemaAsync = await getJsonSchemaAsync();