Socket
Socket
Sign inDemoInstall

@entity-factory/typeorm

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/typeorm

TypeORM Adapter for entity-factory


Version published
Weekly downloads
5
decreased by-50%
Maintainers
1
Weekly downloads
 
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.

Features

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

Installation

npm install --save @entity-factory/core

Example

Entity blueprints can also be defined within classes to enable better code separation.

import { EntityFactory, ObjectBlueprint } from '@entity-factory/core';

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

        // Set the key used for identifying an object in the factory.
        // The key can be a string or a class.
        this.type('user');

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

        this.state('active', async faker => {
            return {
                active: true,
            };
        });

        // called after make()
        this.afterMaking(async (user, { faker, factory, adapter }) => {
            // perform operation on entity
        });

        // called after make() on entity with a specific state transform
        this.afterMakingState(
            'active',
            async (user, { faker, factory, adapter }) => {
                // perform operation on entity
            },
        );

        // called after create()
        this.afterCreating(async (user, { faker, factory, adapter }) => {
            // perform operation on entity
        });

        // called after create() on entity with a specific state transform
        this.afterCreatingState(
            'active',
            async (user, { faker, factory, adapter }) => {
                // perform operation on entity
            },
        );
    }
}

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

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

// output:
// [
//    { id: 1, username: 'Shawna_Muller77', email: 'Nelda55@hotmail.com', active: true },
//    { id: 2, username: 'Abraham_Emard', email: 'Skye_Champlin@hotmail.com', active: true },
//    { id: 3, username: 'Ena14', email: 'Suzanne.Hansen@yahoo.com', active: false }
// ]

Alternate Inline Blueprint Declaration

import { EntityFactory, ObjectBlueprint } from 'entity-factory';

interface User {
    id: number;
    username: string;
    email: string;
    active: boolean;
}

export const entityFactory = new EntityFactory();

entityFactory.register((profile: ObjectBlueprint<User>) => {
    // Set the key used for identifying an object in the factory.
    // The key can be a string or a class.
    profile.type('user');

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

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

// output:
// [
//     { id: 1, username: 'Mathias.Fay', email: 'Lisandro_Walker@yahoo.com', active: true },
//     { id: 2, username: 'Ashlynn77', email: 'Edd_Jenkins@hotmail.com', active: false },
//     { id: 3, username: 'Josefina99', email: 'Yesenia23@hotmail.com', active: true }
// ]

Additional Samples

TODO

  • imporove 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

FAQs

Package last updated on 05 Mar 2019

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