All Projects → linnify → typeorm-factory

linnify / typeorm-factory

Licence: ISC license
Typeorm factory that makes testing easier

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to typeorm-factory

Factory Bot
🤖 Provides a fixture factory for doctrine/orm entities.
Stars: ✭ 57 (+103.57%)
Mutual labels:  factory, entity
DynamicEntitySummarization-DynES
Dynamic Entity Summarization (DynES)
Stars: ✭ 21 (-25%)
Mutual labels:  entity
server
Core server in the Alkemio platform, offering a GraphQL api for interacting with the logical domain model.
Stars: ✭ 20 (-28.57%)
Mutual labels:  typeorm
Parceler
简单的Bundle数据注入框架
Stars: ✭ 107 (+282.14%)
Mutual labels:  entity
node-ts-api-base
REST API boilerplate made with Express + NodeJS
Stars: ✭ 87 (+210.71%)
Mutual labels:  typeorm
interface-forge
Graceful mock-data and fixtures generation using TypeScript
Stars: ✭ 58 (+107.14%)
Mutual labels:  factory
Wern-Fullstack-Template
React, Next.js, MaterialUI, Styled-Components, TypeGraphQL, URQL, ApolloServer (express), TypeORM, PostgreSQL, Node.js, TypeScript
Stars: ✭ 173 (+517.86%)
Mutual labels:  typeorm
typeorm-factories
Create factories for your TypeORM entities. Useful for NestJS applications
Stars: ✭ 43 (+53.57%)
Mutual labels:  typeorm
angular-github-api-factory
AngularJS Factory for GitHub v3 JSON REST API requests
Stars: ✭ 13 (-53.57%)
Mutual labels:  factory
sf-nest-admin
🚀 基于NestJs + TypeScript + TypeORM + Redis + MySql + Vue2 + Element-UI编写的一款简单高效的前后端分离的权限管理系统
Stars: ✭ 125 (+346.43%)
Mutual labels:  typeorm
IssueTracker-40
Github IssueTracker Clone Project
Stars: ✭ 18 (-35.71%)
Mutual labels:  typeorm
DTE
Generate C# class from database table
Stars: ✭ 26 (-7.14%)
Mutual labels:  entity
pydantic-factories
Simple and powerful mock data generation using pydantic or dataclasses
Stars: ✭ 380 (+1257.14%)
Mutual labels:  factory
prime-nestjs
A production-ready NestJS boilerplate using Typescript, Postgres, TypeORM, and Docker.
Stars: ✭ 140 (+400%)
Mutual labels:  typeorm
angular-youtube-api-factory
AngularJS Factory for Youtube JSON REST API requests
Stars: ✭ 21 (-25%)
Mutual labels:  factory
random-jpa
Create random test data for JPA/Hibernate entities.
Stars: ✭ 23 (-17.86%)
Mutual labels:  entity
microservice-template
📖 Nest.js based microservice repository template
Stars: ✭ 131 (+367.86%)
Mutual labels:  typeorm
angular-react-microfrontend
🚧 React vs Angular ? Why not both ! Micro frontend demo using Angular and React alongs with a NodeJS API
Stars: ✭ 17 (-39.29%)
Mutual labels:  typeorm
api-server-nodejs
Nodejs API Server - Express / SQLite / TypeORM | AppSeed
Stars: ✭ 171 (+510.71%)
Mutual labels:  typeorm
babel-example
Example how to use TypeORM with JavaScript + Babel.
Stars: ✭ 51 (+82.14%)
Mutual labels:  typeorm

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 use of the create function. By default, the entity is 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
  }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].