# factory-box

> A factory bot library for Node.js and the browser inspired by FactoryBot for Ruby.

Latest version **1.3.0** (published 2023-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install factory-box
pnpm add factory-box
yarn add factory-box
bun add factory-box
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2023-12-11 |
| First published | 2023-12-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=12.0 |
| Dependencies | 0 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Minh Ngo |
| Maintainers | minhngo023 |
| Keywords | boilerplate, template, typescript, vscode, jest, husky, commitizen, semantic-release, codecov |

## Links

- npm: https://www.npmjs.com/package/factory-box
- Repository: https://github.com/immint023/factory-box
- Homepage: https://github.com/immint023/factory-box#readme
- Issues: https://github.com/immint023/factory-box/issues
- npm.io page: https://npm.io/package/factory-box

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2023-12-11
- 1.2.0 — 2023-12-11

## README

# factory-bot

> The factory lib to create seed data for testing easily


## Install

```bash
npm install factory-box
```

## Usage
### Define factory
```typescript
// user.factory.ts

 define(UserEntity, (f: Factory) => {
    f.trait('withPosts', async (user: UserEntity) => {
        user.posts = await factory(PostEntity).saveMany(3, { title: 'Post title', body: 'Post body' });
    });

    f.trait('withAdmin', (user: UserEntity) => {
        user.role = 'admin';
    });

    f.build((options: Record<string, any>) => {
        const user = new UserEntity();

        user.id = options.id || 1;
        user.name = options.name || 'John Doe';
        user.email = options.email || 'example@gmail.com';

        return user;
    });
});
```

### Create data
#### Apply trait
```typescript
const user = await factory(UserEntity).withTraits('withAdmin').saveOne();
```


#### Override properties
```typescript
const user = await factory(UserEntity).
    saveOne({
        email: 'override_email@example.com'
    });
```

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