{"id":16265980,"url":"https://github.com/robinck/typeorm-fixtures","last_synced_at":"2025-05-14T04:07:37.957Z","repository":{"id":37444603,"uuid":"154982575","full_name":"RobinCK/typeorm-fixtures","owner":"RobinCK","description":":pill: Fixtures loader for typeorm 🇺🇦","archived":false,"fork":false,"pushed_at":"2025-01-26T20:10:16.000Z","size":2121,"stargazers_count":583,"open_issues_count":29,"forks_count":47,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-09T07:21:35.984Z","etag":null,"topics":["cli","faker","fixture-loader","fixtures","orm","typeorm","typeorm-fixtures","typescript"],"latest_commit_sha":null,"homepage":"https://robinck.github.io/typeorm-fixtures/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RobinCK.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"typeorm-fixtures"}},"created_at":"2018-10-27T16:00:18.000Z","updated_at":"2025-05-07T08:08:06.000Z","dependencies_parsed_at":"2024-01-10T20:10:52.467Z","dependency_job_id":"0fd3a637-9b2b-432c-bb01-e0a26c2674ed","html_url":"https://github.com/RobinCK/typeorm-fixtures","commit_stats":{"total_commits":347,"total_committers":29,"mean_commits":11.96551724137931,"dds":0.446685878962536,"last_synced_commit":"7911a664d9142a2c5c5438c659ac00e752ec55db"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinCK%2Ftypeorm-fixtures","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinCK%2Ftypeorm-fixtures/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinCK%2Ftypeorm-fixtures/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobinCK%2Ftypeorm-fixtures/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobinCK","download_url":"https://codeload.github.com/RobinCK/typeorm-fixtures/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254069557,"owners_count":22009558,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","faker","fixture-loader","fixtures","orm","typeorm","typeorm-fixtures","typescript"],"created_at":"2024-10-10T17:21:22.622Z","updated_at":"2025-05-14T04:07:37.879Z","avatar_url":"https://github.com/RobinCK.png","language":"TypeScript","funding_links":["https://opencollective.com/typeorm-fixtures","https://opencollective.com/typeorm-fixtures/contribute","https://opencollective.com/typeorm-fixtures/organization/0/website","https://opencollective.com/typeorm-fixtures/organization/1/website","https://opencollective.com/typeorm-fixtures/organization/2/website","https://opencollective.com/typeorm-fixtures/organization/3/website","https://opencollective.com/typeorm-fixtures/organization/4/website","https://opencollective.com/typeorm-fixtures/organization/5/website","https://opencollective.com/typeorm-fixtures/organization/6/website","https://opencollective.com/typeorm-fixtures/organization/7/website","https://opencollective.com/typeorm-fixtures/organization/8/website","https://opencollective.com/typeorm-fixtures/organization/9/website"],"categories":[],"sub_categories":[],"readme":"# TypeORM fixtures cli\n\n[![CircleCI](https://circleci.com/gh/RobinCK/typeorm-fixtures.svg?style=svg)](https://circleci.com/gh/RobinCK/typeorm-fixtures)\n![GitHub CI](https://github.com/RobinCK/typeorm-fixtures/workflows/Build%20CI/badge.svg?branch=master)\n[![OpenCollective](https://opencollective.com/typeorm-fixtures/all/badge.svg?label=financial+contributors)](https://opencollective.com/typeorm-fixtures)\n[![Coverage Status](https://coveralls.io/repos/github/RobinCK/typeorm-fixtures/badge.svg?branch=master\u0026service=github\u0026random=1)](https://coveralls.io/github/RobinCK/typeorm-fixtures?branch=master)\n[![Version](https://img.shields.io/npm/v/typeorm-fixtures-cli.svg?style=flat-square)](https://www.npmjs.com/package/typeorm-fixtures-cli)\n[![License](https://img.shields.io/npm/l/typeorm-fixtures-cli.svg?style=flat-square)](https://github.com/RobinCK/typeorm-fixtures/blob/master/LICENSE)\n[![Backers on Open Collective](https://opencollective.com/typeorm-fixtures/backers/badge.svg)](#backers)\n[![Sponsors on Open Collective](https://opencollective.com/typeorm-fixtures/sponsors/badge.svg)](#sponsors)\n\nRelying on [faker.js](https://github.com/faker-js/faker), 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.\n\n## Table of Contents\n\n- [Install](#install)\n- [Development Setup](#development-setup)\n- [Example](#example)\n- [Creating Fixtures](#creating-fixtures)\n  - [Fixture Ranges](#fixture-ranges)\n  - [Fixture Reference](#fixture-reference)\n  - [Fixture Lists](#fixture-lists)\n  - [Calling Methods](#calling-sync-and-async-methods)\n- [Handling Relations](#handling-relations)\n- [Advanced Guide](#advanced-guide)\n  - [Parameters](#parameters)\n  - [Faker Data](#faker-data)\n  - [EJS templating](#ejs-templating)\n  - [Load Processor](#load-processor)\n  - [Entity Schemas](#entity-schemas)\n- [Samples](#samples)\n- [Usage](#usage)\n\n## Install\n\n#### NPM\n\n```bash\nnpm install typeorm-fixtures-cli --save-dev\n```\n\n#### Yarn\n\n```bash\nyarn add typeorm-fixtures-cli --dev\n```\n\n## Development Setup\n\n```bash\n# install dependencies\nnpm install\n\n# build dist files\nnpm run build\n```\n\n## Example\n\n`fixtures/Comment.yml`\n\n```yaml\nentity: Comment\nitems:\n  comment{1..10}:\n    fullName: '{{name.firstName}} {{name.lastName}}'\n    email: '{{internet.email}}'\n    text: '{{lorem.paragraphs}}'\n    post: '@post*'\n```\n\n`fixtures/Post.yml`\n\n```yaml\nentity: Post\nitems:\n  post1:\n    title: '{{name.title}}'\n    description: '{{lorem.paragraphs}}'\n    user: '@user($current)'\n  post2:\n    title: '{{name.title}}'\n    description: '{{lorem.paragraphs}}'\n    user: '@user($current)'\n```\n\n`fixtures/User.yml`\n\n```yaml\nentity: User\nitems:\n  user1:\n    firstName: '{{name.firstName}}'\n    lastName: '{{name.lastName}}'\n    email: '{{internet.email}}'\n    profile: '@profile1'\n    __call:\n      setPassword:\n        - foo\n  user2:\n    firstName: '{{name.firstName}}'\n    lastName: '{{name.lastName}}'\n    email: '{{internet.email}}'\n    profile: '@profile2'\n    __call:\n      setPassword:\n        - foo\n```\n\n`fixtures/Profile.yml`\n\n```yaml\nentity: Profile\nitems:\n  profile1:\n    aboutMe: \u003c%= ['about string', 'about string 2', 'about string 3'].join(\", \") %\u003e\n    skype: skype-account\u003e\n    language: english\n  profile2:\n    aboutMe: \u003c%= ['about string', 'about string 2', 'about string 3'].join(\", \") %\u003e\n    skype: skype-account\n    language: english\n```\n\n## Creating Fixtures\n\nThe most basic functionality of this library is to turn flat yaml files into objects\n\n```yaml\nentity: User\nitems:\n  user0:\n    username: bob\n    fullname: Bob\n    birthDate: 1980-10-10\n    email: bob@example.org\n    favoriteNumber: 42\n\n  user1:\n    username: alice\n    fullname: Alice\n    birthDate: 1978-07-12\n    email: alice@example.org\n    favoriteNumber: 27\n```\n\n### Fixture Ranges\n\nThe first step is to let create many copies of an object for you to remove duplication from the yaml file.\n\nYou can do that by defining a range in the fixture name:\n\n```yaml\nentity: User\nitems:\n  user{1..10}:\n    username: bob\n    fullname: Bob\n    birthDate: 1980-10-10\n    email: bob@example.org\n    favoriteNumber: 42\n```\n\nNow 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.\n\n### Fixture Reference\n\nYou can also specify a reference to a previously created list of fixtures:\n\n```yaml\nentity: Post\nitems:\n  post1:\n    title: 'Post title'\n    description: 'Post description'\n    user: '@user1'\n```\n\n### Fixture Lists\n\nYou can also specify a list of values instead of a range:\n\n```yaml\nentity: Post\nitems:\n  post{1..10}:\n    title: 'Post title'\n    description: 'Post description'\n    user: '@user($current)'\n```\n\nIn the case of a range (e.g. user{1..10}), `($current)` will return 1 for user1, 2 for user2 etc.\n\nThe current iteration can be used as a string value:\n\n```yaml\nentity: Post\nitems:\n  post{1..10}:\n    title: 'Post($current)'\n    description: 'Post description'\n```\n\n`Post($current)` will return Post1 for post1, Post2 for post2 etc.\n\nYou can mutate this output by using basic math operators:\n\n```yaml\nentity: Post\nitems:\n  post{1..10}:\n    title: 'Post($current*100)'\n    description: 'Post description'\n```\n\n`Post($current*100)` will return Post100 for post1, Post200 for post2 etc.\n\n### Calling Sync and Async Methods\n\nSometimes 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.\n\n```yaml\nentity: User\nitems:\n  user{1..10}:\n    username: bob\n    fullname: Bob\n    birthDate: 1980-10-10\n    email: bob@example.org\n    favoriteNumber: 42\n    __call:\n      setPassword:\n        - foo\n```\n\n## Handling Relations\n\n```yaml\nentity: User\nitems:\n  user1:\n    # ...\n\nentity: Group\nitems:\n  group1:\n    name: '\u003c{names.admin}\u003e'\n    owner: '@user1'\n    members:\n      - '@user2'\n      - '@user3'\n\n```\n\nIf 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:\n\n```yaml\nentity: User\nitems:\n  user1:\n    # ...\n\nentity: Group\nitems:\n  group{1..10}:\n    name: 'name'\n    owner: '@user($current)'\n    members:\n      - '@user2'\n      - '@user3'\n\n```\n\nIf you would like a random user instead of a fixed one, you can define a reference with a wildcard:\n\n```yaml\nentity: User\nitems:\n  user1:\n    # ...\n\nentity: Group\nitems:\n  group{1..10}:\n    name: 'name'\n    owner: '@user*'\n    members:\n      - '@user2'\n      - '@user3'\n\n```\n\nor\n\n```yaml\nentity: User\nitems:\n  user1:\n    # ...\n\nentity: Group\nitems:\n  group{1..10}:\n    name: 'name'\n    owner: '@user{1..2}' # @user1 or @user2\n    members:\n      - '@user2'\n      - '@user3'\n\n```\n\n## Advanced Guide\n\n### Parameters\n\nYou can set global parameters that will be inserted everywhere those values are used to help with readability. For example:\n\n```yaml\nentity: Group\nparameters:\n  names:\n    admin: Admin\nitems:\n  group1:\n    name: '\u003c{names.admin}\u003e' # \u003c--- set Admin\n    owner: '@user1'\n    members:\n      - '@user2'\n      - '@user3'\n```\n\n### Faker Data\n\nThis library integrates with the [faker.js](https://github.com/faker-js/faker) library. Using {{foo}} you can call Faker data providers to generate random data.\n\nLet's turn our static bob user into a randomized entry:\n\n```yaml\nentity: User\nitems:\n  user{1..10}:\n    username: '{{internet.userName}}'\n    fullname: '{{name.firstName}} {{name.lastName}}'\n    birthDate: '{{date.past}}'\n    email: '{{internet.email}}'\n    favoriteNumber: '{{datatype.number}}'\n    __call:\n      setPassword:\n        - foo\n```\n\n### EJS templating\n\nThis library integrates with the [EJS](https://github.com/mde/ejs)\n\n```yaml\nentity: Profile\nitems:\n  profile1:\n    aboutMe: \u003c%= ['about string', 'about string 2', 'about string 3'].join(\", \") %\u003e\n    skype: skype-account\u003e\n    language: english\n```\n\n### Load Processor\n\nProcessors allow you to process objects before and/or after they are persisted. Processors must implement the: `IProcessor`\n\n```typescript\nimport { IProcessor } from 'typeorm-fixtures-cli';\n```\n\nHere is an example:\n\n`processor/UserProcessor.ts`\n\n```typescript\nimport { IProcessor } from 'typeorm-fixtures-cli';\nimport { User } from '../entity/User';\n\nexport default class UserProcessor implements IProcessor\u003cUser\u003e {\n  preProcess(name: string, object: any): any {\n    return { ...object, firstName: 'foo' };\n  }\n\n  postProcess(name: string, object: { [key: string]: any }): void {\n    object.name = `${object.firstName} ${object.lastName}`;\n  }\n}\n```\n\nfixture config `fixtures/user.yml`\n\n```yaml\nentity: User\nprocessor: ../processor/UserProcessor\nitems:\n  user1:\n    firstName: '{{name.firstName}}'\n    lastName: '{{name.lastName}}'\n    email: '{{internet.email}}'\n```\n\n#### Alternative Javascript Syntax for CommonJS\n\nIf 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:\n\n`processor/UserProcessor.js`\n\n```javascript\n\nclass UserProcessor {\n  preProcess(name, obj) {\n    return { ...obj, firstName: 'foo' };\n  }\n\n  postProcess(name, obj) {\n    obj.name = `${obj.firstName} ${obj.lastName}`;\n  }\n}\n\nmodule.exports = { default: UserProcessor }\n\n```\n\n\n### Entity Schemas\n\nIf 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. \n\nAs a workaround, you should be able to duplicate the same method functionality in the [Load Processor](#load-processor) preProcess method (Note that the object passed in will be a plain object and not your entity object).\n\n## Usage\n\n```\nUsage: fixtures load [options] \u003cpath\u003e Fixtures folder/file path\n\nUse -h or --help to show details of options: fixtures load -h\n```\n\n##### If entities files are in typescript (like [typeorm](https://typeorm.io/using-cli#if-entities-files-are-in-typescript))\n\nThis 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.\n\nYou may setup ts-node in your project to ease the operation as follows:\n\nInstall ts-node:\n\n```\nnpm install ts-node --save-dev\n```\n\nAdd typeorm command under scripts section in package.json\n\n```\n\"scripts\": {\n    ...\n    \"fixtures\": \"fixtures-ts-node-commonjs\"\n}\n```\n\nFor ESM projects add this instead:\n\n```\n\"scripts\": {\n    ...\n    \"fixtures\": \"fixtures-ts-node-esm\"\n}\n```\n\n##### Require multiple additional modules\n\nIf you're using multiple modules at once (e.g. ts-node and tsconfig-paths)\nyou have the ability to require these modules with multiple require flags. For example:\n\n```\nfixtures load ./fixtures --dataSource=./dataSource.ts --sync --require=ts-node/register --require=tsconfig-paths/register\n```\n\n### Programmatically loading fixtures\n\nAlthough typeorm-fixtures-cli is intended to use as a CLI, you can still load\nfixtures via APIs in your program.\n\nFor example, the below code snippet will load all fixtures exist in `./fixtures` directory:\n\n```typescript\nimport * as path from 'path';\nimport { Builder, fixturesIterator, Loader, Parser, Resolver } from 'typeorm-fixtures-cli/dist';\nimport { createConnection, getRepository } from 'typeorm';\nimport { CommandUtils } from 'typeorm/commands/CommandUtils';\n\nconst loadFixtures = async (fixturesPath: string) =\u003e {\n  let dataSource: DataSource | undefined = undefined;\n\n  try {\n    dataSource = await CommandUtils.loadDataSource(dataSourcePath);\n    await dataSource.initialize();\n    await dataSource.synchronize(true);\n\n    const loader = new Loader();\n    loader.load(path.resolve(fixturesPath));\n\n    const resolver = new Resolver();\n    const fixtures = resolver.resolve(loader.fixtureConfigs);\n    const builder = new Builder(connection, new Parser(), false);\n\n    for (const fixture of fixturesIterator(fixtures)) {\n      const entity: any = await builder.build(fixture);\n      await dataSource.getRepository(fixture.entity).save(entity);\n    }\n  } catch (err) {\n    throw err;\n  } finally {\n    if (dataSource) {\n      await dataSource.destroy();\n    }\n  }\n};\n\nloadFixtures('./fixtures')\n  .then(() =\u003e {\n    console.log('Fixtures are successfully loaded.');\n  })\n  .catch((err) =\u003e {\n    console.log(err);\n  });\n```\n\n## Samples\n\n- [typeorm-fixtures-sample](https://github.com/RobinCK/typeorm-fixtures-sample)\n\n## Contributors\n\n### Code Contributors\n\nThis project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].\n\u003ca href=\"https://github.com/RobinCK/typeorm-fixtures/graphs/contributors\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/contributors.svg?width=890\u0026button=false\" /\u003e\u003c/a\u003e\n\n### Financial Contributors\n\nBecome a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/typeorm-fixtures/contribute)]\n\n#### Individuals\n\n\u003ca href=\"https://opencollective.com/typeorm-fixtures\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/individuals.svg?width=890\"\u003e\u003c/a\u003e\n\n#### Organizations\n\nSupport this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/typeorm-fixtures/contribute)]\n\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/0/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/0/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/1/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/1/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/2/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/2/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/3/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/3/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/4/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/4/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/5/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/5/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/6/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/6/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/7/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/7/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/8/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/8/avatar.svg\"\u003e\u003c/a\u003e\n\u003ca href=\"https://opencollective.com/typeorm-fixtures/organization/9/website\"\u003e\u003cimg src=\"https://opencollective.com/typeorm-fixtures/organization/9/avatar.svg\"\u003e\u003c/a\u003e\n\nMIT © [Igor Ognichenko](https://github.com/RobinCK)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinck%2Ftypeorm-fixtures","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobinck%2Ftypeorm-fixtures","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinck%2Ftypeorm-fixtures/lists"}