# datagen-rs-node

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

Latest version **0.1.0** (published 2023-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install datagen-rs-node
pnpm add datagen-rs-node
yarn add datagen-rs-node
bun add datagen-rs-node
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2023-09-26 |
| First published | 2023-09-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 5 |
| Unpacked size | 26.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | MarkusJx |
| Maintainers | markusjx |

## Links

- npm: https://www.npmjs.com/package/datagen-rs-node
- Repository: https://github.com/MarkusJx/datagen
- Homepage: https://github.com/MarkusJx/datagen#readme
- Issues: https://github.com/MarkusJx/datagen/issues
- npm.io page: https://npm.io/package/datagen-rs-node

## Dependencies (5)

- [datagen-rs-node-darwin-x64](https://npm.io/package/datagen-rs-node-darwin-x64.md) 0.1.0
- [datagen-rs-node-darwin-arm64](https://npm.io/package/datagen-rs-node-darwin-arm64.md) 0.1.0
- [datagen-rs-node-linux-x64-gnu](https://npm.io/package/datagen-rs-node-linux-x64-gnu.md) 0.1.0
- [datagen-rs-node-win32-x64-msvc](https://npm.io/package/datagen-rs-node-win32-x64-msvc.md) 0.1.0
- [datagen-rs-node-win32-ia32-msvc](https://npm.io/package/datagen-rs-node-win32-ia32-msvc.md) 0.1.0

## Recent versions

- 0.1.0 (latest) — 2023-09-26

## README

# @datagen-rs/node

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

## Installation

```bash
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`](https://markusjx.github.io/datagen/docs/packages/nodeTypes/) package:

```bash
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:

```ts
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](https://markusjx.github.io/datagen/docs/options/#serializer).

### 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:

```ts
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:

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

Check the [`progress-plugin` documentation](https://markusjx.github.io/datagen/docs/plugins/default/progress/)
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](https://markusjx.github.io/datagen/docs/plugins/node/create/)
for more information on creating node.js plugins.

```ts
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:

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

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

---
_Source: https://npm.io/package/datagen-rs-node · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
