# What is Entity Factory

Entity Factory is a library used for quickly creating fixture data from plain objects or classes using faker. Inspired by laravel's factories for generating test data. Currently the library supports plain JS objects and Typeorm entities.

## Features

* Generate plain javascript objects on demand
* Generate objects with nested relations
* Typeorm Support - Generate and Persist Entities and nested relations

## Installation

```
npm install --save @entity-factory/core
```

## Example

Try it on [Runkit](https://runkit.com/jcloutz/entity-factory-quick-example)

```javascript
// index.js
const { EntityFactory } = require('@entity-factory/core');
const { UserBlueprint } = require('./UserBlueprint');
const { PostBlueprint } = require('./PostBlueprint');

const entityFactory = new EntityFactory({
    // blueprints can be an array of glob patterns, blueprint classes and/or blueprint instances
    blueprints: [UserBlueprint, PostBlueprint],
});

// Generate entities
entityFactory
    .for('user') // get builder instance for 'user'
    .state('with-posts') // generate users with posts
    .create(3); // generate 3 users with incrementing id's
```

```javascript
// UserBlueprint.js
const { ObjectBlueprint } = require('@entity-factory/core');

export class UserBlueprint extends ObjectBlueprint {
    constructor() {
        super();

        this.type('user');

        this.define(async ({ faker, factory }) => ({
            username: faker.internet.userName(),
            email: faker.internet.email(),
            active: false,
        }));

        this.state('with-posts', async ({ faker, factory }) => ({
            posts: await factory.for('post').create(2),
        }));
    }
}
```

```javascript
// PostBlueprint.js
const { ObjectBlueprint } = require('@entity-factory/core');

class PostBlueprint extends ObjectBlueprint {
    constructor() {
        super();

        this.type('post');

        this.define(async ({ faker, factory }) => ({
            title: faker.company.bsBuzz(),
            body: faker.lorem.sentences(2),
        }));
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://entity-factory.gitbook.io/entity-factory/master.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
