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

@linnify/typeorm-factory

v1.0.12

Published

Factory for typeorm

Downloads

5,311

Readme

Typeorm Factory

codecov Build Status

This package makes testing easier by creating factories for your TypeORM entities. It is inspired by the python package Factory Boy.

Installation

NPM

npm install @linnify/typeorm-factory --save-dev

Yarn

yarn add @linnify/typeorm-factory --dev

Usage

This section provides examples on how to use this library's features.

Factories

Declaration

For declaring a factory, we make use of typescript classes and add as properties on the class the fields from our entity with the desired values

import { Factory } from '@linnify/typeorm-factory';

export class NormalUserFactory extends Factory<User> {
  entity = User;
  
  firstName = 'John'
  lastName = 'Doe'
  email = '[email protected]'
  role = 'normal_user'
}

export class AdminUserFactory extends Factory<User> {
  entity = User;

  firstName = 'Admin'
  lastName = 'Factory'
  email = '[email protected]'
  role = 'admin'
}

Usage

After defining our factories, we can make use of them by creating a new instance of a factory:

const userFactory: NormalUserFactory = new NormalUserFactory();

When creating the factory, we make user of the create function. By default, the entity created and saved to the database by using the properties defined on the factory.

const user: User = await userFactory.create();

If we want to modify the default attributes from the factory, we add them as parameter to the create function:

const user: User = await userFactory.create({firstName: 'AnotherFirstName', lastName: 'AnotherLastName'});

We can create multiple instances by using the createMany function. In the below example we create 5 users:

const users: User[] = await userFactory.createMany(5)

In the case when we have some unique attributes by which we define entities. We do not want to create multiple objects with some properties, we make use of the getOrCreate function. We define the attributes by which the entity should be unique. Calling the create method multiple times will return the same object.

In our example, we do not want to create 2 users with the same email.

import { Factory } from '@linnify/typeorm-factory';

export class NormalUserFactory extends Factory<User> {
  ...

  protected getOrCreate(): string[] {
    return ['email'];
  }
}
...

// Creates the user 
const user: User = await userFactory.create();

// Gets the user that was created above
const sameUser: User = await userFactory.create();

SubFactories

A common case is that the entities have relations between them. In this case we create factories for all the entities and make use of SubFactory to create a link between them.

Example

If the user has multiple addresses we add the UserFactory as a subfactory on the AddressFactory

import { Factory, SubFactory } from '@linnify/typeorm-factory';

export class AddressFactory extends Factory<Address> {
  street = 'Factory Street'
  number = '100'
  user = new SubFactory(UserFactory)
}

Sequences

We can add sequences on our factories when we want a property to have a different every time we create an object using the same factory

import { Factory, Sequence } from '@linnify/typeorm-factory';

export class NormalUserFactory extends Factory<User> {
  ...
  email = new Sequence((i: number) => `john.doe.${i}@typeorm-factory.com`)

}

Post Generations

Sometimes we have the need to call some functions after the object was created. For this, we have created a PostGeneration decorator that can be used on multiple functions and all of them will be called after the entity was created.

import { Factory, PostGeneration } from '@linnify/typeorm-factory';

export class NormalUserFactory extends Factory<User> {
  ...
  
  @PostGeneration()
  addLogsForUser() {
    // create some logs for the created user
  }
  
  @PostGeneration()
  async anotherFunctionToBeCalled(createdUser: User) {
    // do something with the created user
  }
}