Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

typeorm-fixtures-cli

RobinCK124.7kMIT4.1.0TypeScript support: included

CircleCI GitHub CI [![OpenCollective](https

typeorm, fixtures, orm, cli, typescript, faker, fixture-loader, typeorm-fixtures

readme

TypeORM fixtures cli

CircleCI GitHub CI OpenCollective Coverage Status Version License Backers on Open Collective Sponsors on Open Collective

Relying on faker.js, typeorm-fixtures-cli allows you to create a ton of fixtures/fake data for use while developing or testing your project. It gives you a few essential tools to make it very easy to generate complex data with constraints in a readable and easy to edit way, so that everyone on your team can tweak the fixtures if needed.

Table of Contents

Install

NPM

npm install typeorm-fixtures-cli --save-dev

Yarn

yarn add typeorm-fixtures-cli --dev

Development Setup

# install dependencies
npm install

# build dist files
npm run build

Example

fixtures/Comment.yml

entity: Comment
items:
  comment{1..10}:
    fullName: '{{name.firstName}} {{name.lastName}}'
    email: '{{internet.email}}'
    text: '{{lorem.paragraphs}}'
    post: '@post*'

fixtures/Post.yml

entity: Post
items:
  post1:
    title: '{{name.title}}'
    description: '{{lorem.paragraphs}}'
    user: '@user($current)'
  post2:
    title: '{{name.title}}'
    description: '{{lorem.paragraphs}}'
    user: '@user($current)'

fixtures/User.yml

entity: User
items:
  user1:
    firstName: '{{name.firstName}}'
    lastName: '{{name.lastName}}'
    email: '{{internet.email}}'
    profile: '@profile1'
    __call:
      setPassword:
        - foo
  user2:
    firstName: '{{name.firstName}}'
    lastName: '{{name.lastName}}'
    email: '{{internet.email}}'
    profile: '@profile2'
    __call:
      setPassword:
        - foo

fixtures/Profile.yml

entity: Profile
items:
  profile1:
    aboutMe: <%= ['about string', 'about string 2', 'about string 3'].join(", ") %>
    skype: skype-account>
    language: english
  profile2:
    aboutMe: <%= ['about string', 'about string 2', 'about string 3'].join(", ") %>
    skype: skype-account
    language: english

Creating Fixtures

The most basic functionality of this library is to turn flat yaml files into objects

entity: User
items:
  user0:
    username: bob
    fullname: Bob
    birthDate: 1980-10-10
    email: bob@example.org
    favoriteNumber: 42

  user1:
    username: alice
    fullname: Alice
    birthDate: 1978-07-12
    email: alice@example.org
    favoriteNumber: 27

Fixture Ranges

The first step is to let create many copies of an object for you to remove duplication from the yaml file.

You can do that by defining a range in the fixture name:

entity: User
items:
  user{1..10}:
    username: bob
    fullname: Bob
    birthDate: 1980-10-10
    email: bob@example.org
    favoriteNumber: 42

Now it will generate ten users, with IDs user1 to user10. Pretty good but we only have 10 bobs with the same name, username and email, which is not so fancy yet.

Fixture Reference

You can also specify a reference to a previously created list of fixtures:

entity: Post
items:
  post1:
    title: 'Post title'
    description: 'Post description'
    user: '@user1'

Fixture Lists

You can also specify a list of values instead of a range:

entity: Post
items:
  post{1..10}:
    title: 'Post title'
    description: 'Post description'
    user: '@user($current)'

In the case of a range (e.g. user{1..10}), ($current) will return 1 for user1, 2 for user2 etc.

The current iteration can be used as a string value:

entity: Post
items:
  post{1..10}:
    title: 'Post($current)'
    description: 'Post description'

Post($current) will return Post1 for post1, Post2 for post2 etc.

You can mutate this output by using basic math operators:

entity: Post
items:
  post{1..10}:
    title: 'Post($current*100)'
    description: 'Post description'

Post($current*100) will return Post100 for post1, Post200 for post2 etc.

Calling Sync and Async Methods

Sometimes though you need to call a method to initialize some more data, you can do this just like with properties but instead using the method name and giving it an array of arguments.

entity: User
items:
  user{1..10}:
    username: bob
    fullname: Bob
    birthDate: 1980-10-10
    email: bob@example.org
    favoriteNumber: 42
    __call:
      setPassword:
        - foo

Handling Relations

entity: User
items:
  user1:
    # ...

entity: Group
items:
  group1:
    name: '<{names.admin}>'
    owner: '@user1'
    members:
      - '@user2'
      - '@user3'

If you want to create ten users and ten groups and have each user own one group, you can use ($current) which is replaced with the current ID of each iteration when using fixture ranges:

entity: User
items:
  user1:
    # ...

entity: Group
items:
  group{1..10}:
    name: 'name'
    owner: '@user($current)'
    members:
      - '@user2'
      - '@user3'

If you would like a random user instead of a fixed one, you can define a reference with a wildcard:

entity: User
items:
  user1:
    # ...

entity: Group
items:
  group{1..10}:
    name: 'name'
    owner: '@user*'
    members:
      - '@user2'
      - '@user3'

or

entity: User
items:
  user1:
    # ...

entity: Group
items:
  group{1..10}:
    name: 'name'
    owner: '@user{1..2}' # @user1 or @user2
    members:
      - '@user2'
      - '@user3'

Advanced Guide

Parameters

You can set global parameters that will be inserted everywhere those values are used to help with readability. For example:

entity: Group
parameters:
  names:
    admin: Admin
items:
  group1:
    name: '<{names.admin}>' # <--- set Admin
    owner: '@user1'
    members:
      - '@user2'
      - '@user3'

Faker Data

This library integrates with the faker.js library. Using {{foo}} you can call Faker data providers to generate random data.

Let's turn our static bob user into a randomized entry:

entity: User
items:
  user{1..10}:
    username: '{{internet.userName}}'
    fullname: '{{name.firstName}} {{name.lastName}}'
    birthDate: '{{date.past}}'
    email: '{{internet.email}}'
    favoriteNumber: '{{datatype.number}}'
    __call:
      setPassword:
        - foo

EJS templating

This library integrates with the EJS

entity: Profile
items:
  profile1:
    aboutMe: <%= ['about string', 'about string 2', 'about string 3'].join(", ") %>
    skype: skype-account>
    language: english

Load Processor

Processors allow you to process objects before and/or after they are persisted. Processors must implement the: IProcessor

import { IProcessor } from 'typeorm-fixtures-cli';

Here is an example:

processor/UserProcessor.ts

import { IProcessor } from 'typeorm-fixtures-cli';
import { User } from '../entity/User';

export default class UserProcessor implements IProcessor<User> {
  preProcess(name: string, object: any): any {
    return { ...object, firstName: 'foo' };
  }

  postProcess(name: string, object: { [key: string]: any }): void {
    object.name = `${object.firstName} ${object.lastName}`;
  }
}

fixture config fixtures/user.yml

entity: User
processor: ../processor/UserProcessor
items:
  user1:
    firstName: '{{name.firstName}}'
    lastName: '{{name.lastName}}'
    email: '{{internet.email}}'

Alternative Javascript Syntax for CommonJS

If you need to run the fixtures under CommonJS and are having problems using typescript with the load processors, this alternative example should work for you:

processor/UserProcessor.js


class UserProcessor {
  preProcess(name, obj) {
    return { ...obj, firstName: 'foo' };
  }

  postProcess(name, obj) {
    obj.name = `${obj.firstName} ${obj.lastName}`;
  }
}

module.exports = { default: UserProcessor }

Entity Schemas

If you are using Entity Schemas in typeorm, you are likely to encounter problems with the __call feature in typeorm-fixtures due to the way the entity object is constructed.

As a workaround, you should be able to duplicate the same method functionality in the Load Processor preProcess method (Note that the object passed in will be a plain object and not your entity object).

Usage

Usage: fixtures load [options] <path> Fixtures folder/file path

Use -h or --help to show details of options: fixtures load -h
If entities files are in typescript (like typeorm)

This CLI tool is written in javascript and to be run on node. If your entity files are in typescript, you will need to transpile them to javascript before using CLI. You may skip this section if you only use javascript.

You may setup ts-node in your project to ease the operation as follows:

Install ts-node:

npm install ts-node --save-dev

Add typeorm command under scripts section in package.json

"scripts": {
    ...
    "fixtures": "fixtures-ts-node-commonjs"
}

For ESM projects add this instead:

"scripts": {
    ...
    "fixtures": "fixtures-ts-node-esm"
}
Require multiple additional modules

If you're using multiple modules at once (e.g. ts-node and tsconfig-paths) you have the ability to require these modules with multiple require flags. For example:

fixtures load ./fixtures --dataSource=./dataSource.ts --sync --require=ts-node/register --require=tsconfig-paths/register

Programmatically loading fixtures

Although typeorm-fixtures-cli is intended to use as a CLI, you can still load fixtures via APIs in your program.

For example, the below code snippet will load all fixtures exist in ./fixtures directory:

import * as path from 'path';
import { Builder, fixturesIterator, Loader, Parser, Resolver } from 'typeorm-fixtures-cli/dist';
import { createConnection, getRepository } from 'typeorm';
import { CommandUtils } from 'typeorm/commands/CommandUtils';

const loadFixtures = async (fixturesPath: string) => {
  let dataSource: DataSource | undefined = undefined;

  try {
    dataSource = await CommandUtils.loadDataSource(dataSourcePath);
    await dataSource.initialize();
    await dataSource.synchronize(true);

    const loader = new Loader();
    loader.load(path.resolve(fixturesPath));

    const resolver = new Resolver();
    const fixtures = resolver.resolve(loader.fixtureConfigs);
    const builder = new Builder(connection, new Parser(), false);

    for (const fixture of fixturesIterator(fixtures)) {
      const entity: any = await builder.build(fixture);
      await dataSource.getRepository(fixture.entity).save(entity);
    }
  } catch (err) {
    throw err;
  } finally {
    if (dataSource) {
      await dataSource.destroy();
    }
  }
};

loadFixtures('./fixtures')
  .then(() => {
    console.log('Fixtures are successfully loaded.');
  })
  .catch((err) => {
    console.log(err);
  });

Samples

Contributors

Code Contributors

This project exists thanks to all the people who contribute. [Contribute].

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute]

Individuals

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

MIT © Igor Ognichenko

changelog

4.0.0 (2023-09-26)

Bug Fixes

  • glob path separator must use '/' on Windows (#203) (c68a692)
  • use graphs instead (098f4e4)

3.0.1 (2022-08-18)

Bug Fixes

  • amend deprecated faker method (f0f41c4)

3.0.0 (2022-07-02)

Bug Fixes

  • chalk: revert to chalk version 4 (6347787)
  • cli: add cli to have esm and cjs mode like typeorm cli (aec7d09)
  • cli: fix yargs arguments (c328299)
  • fix call function (5f2f31b)
  • fix: fix call function (c6f1e9c)
  • lint: fix configuration of eslint (3fce893)
  • test: provide new argument ignoreDecorators (aa3c261)

Code Refactoring

  • all: update outdated packages, use yargs (e523d55)

Features

  • datasource: use new method to load dataSource (78559af)

BREAKING CHANGES

  • all: use new fakerjs package, some random functions was removed
  • datasource: ormconfig file should not work anymore, use dataSource file instead.

2.0.0 (2022-06-17)

Bug Fixes

  • ci: update github actions configuration to use last images (8a1c340)
  • update packages to have a compatibility with typeorm 0.3.* (c4ee8f7), closes #189

BREAKING CHANGES

  • Imcompatible with typeorm 0.2.0, this version uses the new datasource class of typeorm 0.3.0.

1.11.1 (2022-03-29)

Bug Fixes

1.11.0 (2022-03-20)

1.10.1 (2022-01-15)

1.10.0 (2022-01-15)

Features

  • support parsing null values as null (b5db29b)

1.9.1 (2021-01-17)

Bug Fixes

  • Recognize EJS over multiple lines (b3f6043)
  • update postinstall script (3cdc24f), closes #147

1.9.0 (2020-12-25)

Bug Fixes

  • removed ignore flag for class transformer decorators (f500747)

1.8.1 (2020-11-11)

1.8.0 (2020-09-16)

Bug Fixes

  • large datasets times scaling exponential (06a0235)
  • table names with underscores cannot be loaded (7aadf55)

Features

  • added faker locale support (2385ab9)

1.6.0 (2020-05-23)

Bug Fixes

  • check if entity method (191dd0f)
  • package: update cli-progress to version 3.0.0 (8cf6ff8)
  • package: update commander to version 3.0.0 (978d4c0)
  • package: update yargs-parser to version 14.0.0 (ce4f273)
  • package: update yargs-parser to version 15.0.0 (cba52a0)

Features

  • parses parameters from process.env (53e6f64)

1.3.6 (2019-08-16)

Bug Fixes

Features

  • add calculations to current (370cf46)
  • added support "export default" for ormconfig (0712ffc)
  • builder: added class-transformer support (32b242c)
  • current resolved as string (97fab7b)

Reverts

  • Revert "chore(release): 1.3.3 :tada:" (451fcdf)

1.1.5 (2019-06-02)

Bug Fixes

  • getting repository from established connection rather than default (868528a)
  • package: update resolve-from to version 5.0.0 (6197630)
  • update execute logic for __call (f105e52)

1.1.1 (2019-01-30)

Bug Fixes

  • processors: fixed unecessary usage of promise.resolve (1a56360)

Features

  • processors: use async processors (a2db3a5)

1.1.0 (2019-01-20)

Features

  • allow multiple paths in cli arguments (f672390)

1.0.0 (2019-01-14)

Bug Fixes

  • resolver: null peroperty (54437b8)

Features

  • cli: added require options (430e02c)

0.3.6 (2019-01-11)

0.3.5 (2019-01-10)

Bug Fixes

0.3.4 (2019-01-10)

Bug Fixes

  • parser: create getters for regexp (e3daccf)
  • resolver: resolve for nullable property (ce97ba3)

0.3.3 (2019-01-10)

Bug Fixes

  • cli: progress bar length (4825f3f)
  • resolver: resolve dependencies if used $current (47bd691)

Features

  • parser: add support booalen and number for faker (7d894b5)

0.2.4 (2019-01-07)

Bug Fixes

0.2.3 (2019-01-07)

Bug Fixes

  • processor: relevant path (c4ed99e)
  • test: integration assets (d287e4d)

Features

  • loader: load fixtures from file (9287180)

0.2.2 (2019-01-07)

Bug Fixes

0.2.1 (2019-01-05)

Features

0.2.0 (2019-01-03)

Bug Fixes

Features