@entity-factory/core
TypeScript icon, indicating that this package has built-in type declarations

0.2.2 • Public • Published

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.

Documentation

Table of Contents

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

Quick Guide

Try it on Runkit

const { EntityFactory, ObjectBlueprint } = require('@entity-factory/core');

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

        this.type('user');

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

        // define a state transform to return a user with embedded posts
        this.state('with-posts', async ({ faker, factory }) => ({
            posts: await factory.for('post').create(2),
        }));
    }
}

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

        this.type('post');

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

export const entityFactory = new EntityFactory({
    blueprints: [UserBlueprint, PostBlueprint],
});

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

It is also possible to generate random data using the gen method available on EntityFactory

await entityFactory.gen(3, async ({ faker, factory }) => {
    return {
        name: faker.company.bsBuzz(),
        active: faker.random.boolean(),
    };
});

Additional Samples

TODO

  • [ ] Add Mongoose adapter
  • [ ] Add Sequelize adapter
  • [x] improve docs
  • [x] resolve nested objects
  • [x] resolve nested array
  • [x] allow guid id's for object adapter
  • [x] add method to generate random objects on the fly without a blueprint definition

Dependents (0)

Package Sidebar

Install

npm i @entity-factory/core

Weekly Downloads

1

Version

0.2.2

License

MIT

Unpacked Size

50.1 kB

Total Files

49

Last publish

Collaborators

  • jcloutz