Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@entity-factory/core

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@entity-factory/core

Create entities on the fly for mocking and testing

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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
  • improve docs
  • resolve nested objects
  • resolve nested array
  • allow guid id's for object adapter
  • add method to generate random objects on the fly without a blueprint definition

Keywords

FAQs

Package last updated on 21 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc