npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

decorated-factory

v1.0.2

Published

A factory decorators for creating objects with faker data

Downloads

14

Readme

Decorated Factory

A factory decorators for creating objects with faker data

Purpose

Decorated Factory is a tool for creating instances of classes, especially useful in testing scenarios where you need to generate data.

It uses decorators to define how to generate data for each field of a class, also supports relationships between entities and arrays of entities.

This project was inspired by the way queries are made in PrismaORM.

Installation

npm i reflect-metadata
npm i decorated-factory @faker-js/faker -d
yarn add decorated-factory @faker-js/faker --dev
yarn add reflect-metadata

Usage

Basic Usage

Ensure import reflect-metadata at the entry point of your application.

import 'reflect-metadata';
// rest of your code...

To use the Factory utility, you first need to define a class and use the @FactoryField decorator to specify how to generate data for each field. The decorator takes a function that receives a faker instance and returns the generated data.

class DummyEntity {
  @FactoryField((faker) => faker.number.int())
  id: number;

  @FactoryField((faker) => faker.lorem.words({ min: 1, max: 3 }))
  name: string;
}

Then, you can use the Factory utility to create instances of this class.

const factory = new Factory(faker);
const dummyEntity = factory.new(DummyEntity);

Relationships

The Factory utility also supports relationships between entities. You can use the @FactoryRelationField decorator to specify a related entity.

class DummyRelationEntity {
  @FactoryField((faker) => faker.number.int())
  id: number;

  @FactoryField((faker) => faker.lorem.words({ min: 1, max: 3 }))
  name: string;
}

class DummyEntity {
  @FactoryRelationField(() => DummyRelationEntity)
  field: DummyRelationEntity;
}

Then, you can create an instance of the main entity, and the Factory utility will automatically create an instance of the related entity.

const factory = new Factory(faker);
const dummyEntity = factory.new(DummyEntity, {
  field: true,
});

Arrays

The Factory utility can also handle arrays of entities. You can specify an array of a certain entity in the @FactoryRelationField decorator.

class DummyEntity {
  @FactoryRelationField(() => [DummyRelationEntity])
  field: DummyRelationEntity[];
}

Then, you can create an instance of the main entity, and the Factory utility will automatically create an array of instances of the related entity.

const factory = new Factory(faker);
const dummyEntity = factory.new(DummyEntity, {
  field: [1],
});

You can also specify the instances of array relationships

const factory = new Factory(faker);
const dummyEntity = factory.new(DummyEntity, {
  field: [1, {
      anotherField: true
  }],
});

Difference between "new" and "create"

The new method of the Factory utility creates a new instance of a class, while the create method creates an instance of the Overridable class that wraps the original instance. The Overridable class provides a override method that allows you to override the values of the instance.

const factory = new Factory(faker);
const overridable = factory.create(DummyEntity);
const dummyEntity = overridable.override(() => ({ name: 'Hello World' }));

In this example, the name field of the DummyEntity instance will be 'Hello World', regardless of the function provided in the @FactoryField decorator.

Overriding

The Overridable class provides a way to override the values of an instance. You can use the override method and provide a function that returns an object with the fields to override. The function receives the current instance as a parameter.

const overridable = factory.create(DummyEntity);
const dummyEntity = overridable.override((instance) => ({
  name: 'New Name',
}));

In this example, the name field of the DummyEntity instance will be 'New Name', regardless of the function provided in the @FactoryField decorator.

Creating Lists of Entities

The Factory utility provides methods to create lists of entities, which can be useful for generating data for testing collections or arrays of objects.

newList Method

The newList method allows you to create a list of new instances of a class.

const factory = new Factory(faker);
const amount = 3;
const dummyEntities = factory.newList(DummyEntity, amount);
expect(dummyEntities).toHaveLength(amount);

createList Method

The createList method allows you to create a Overridable instance of generated objects, which can then be overridden as needed.

const factory = new Factory(faker);
const amount = 3;
const overridable = factory.createList(DummyEntity, amount);
expect(overridable).toBeInstanceOf(Overridable);