{"id":13528346,"url":"https://github.com/typestack/typeorm-typedi-extensions","last_synced_at":"2025-04-07T09:20:39.374Z","repository":{"id":11197798,"uuid":"68717736","full_name":"typestack/typeorm-typedi-extensions","owner":"typestack","description":"Dependency injection and service container integration with TypeORM using TypeDI library.","archived":false,"fork":false,"pushed_at":"2023-12-06T14:19:05.000Z","size":130,"stargazers_count":258,"open_issues_count":19,"forks_count":35,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-05-17T10:02:47.235Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/typestack.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"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}},"created_at":"2016-09-20T14:02:49.000Z","updated_at":"2024-06-03T23:33:38.712Z","dependencies_parsed_at":"2024-06-03T23:43:45.604Z","dependency_job_id":null,"html_url":"https://github.com/typestack/typeorm-typedi-extensions","commit_stats":{"total_commits":68,"total_committers":7,"mean_commits":9.714285714285714,"dds":0.6176470588235294,"last_synced_commit":"b6d540e2084c832a66573b23374384d367f725f5"},"previous_names":["typeorm/typeorm-typedi-extensions"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typestack%2Ftypeorm-typedi-extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typestack%2Ftypeorm-typedi-extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typestack%2Ftypeorm-typedi-extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typestack%2Ftypeorm-typedi-extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typestack","download_url":"https://codeload.github.com/typestack/typeorm-typedi-extensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247622983,"owners_count":20968575,"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":[],"created_at":"2024-08-01T06:02:27.036Z","updated_at":"2025-04-07T09:20:39.332Z","avatar_url":"https://github.com/typestack.png","language":"TypeScript","funding_links":[],"categories":["TypeScript","\u003ca name=\"TypeScript\"\u003e\u003c/a\u003eTypeScript"],"sub_categories":[],"readme":"# TypeDI Service container integration with TypeORM\n\n![Build Status](https://github.com/typeorm/typeorm-typedi-extensions/workflows/CI/badge.svg)\n[![npm version](https://badge.fury.io/js/typeorm-typedi-extensions.svg)](https://badge.fury.io/js/typeorm-typedi-extensions)\n[![Dependency Status](https://david-dm.org/typeorm/typeorm-typedi-extensions.svg)](https://david-dm.org/typeorm/typeorm-typedi-extensions)\n\nThis package provides decorators for TypeORM that can be used with [TypeDI](https://github.com/pleerock/typedi).\n\n## Installation\n\nTo start using TypeDI install the required packages via NPM:\n\n```bash\nnpm install typeorm-typedi-extensions typedi reflect-metadata\n```\n\nImport the `reflect-metadata` package at the **first line** of your application:\n\n```ts\nimport 'reflect-metadata';\n\n// Your other imports and initialization code\n// comes here after you imported the reflect-metadata package!\n```\n\nYou need to enable emitting decorator metadata in your Typescript config. Add these two lines to your `tsconfig.json` file under the `compilerOptions` key:\n\n```json\n\"emitDecoratorMetadata\": true,\n\"experimentalDecorators\": true,\n```\n\nConfigure TypeORM in your app to use the TypeDI container before you create a connection:\n\n```ts\nimport { createConnection, useContainer } from 'typeorm';\nimport { Container } from 'typeorm-typedi-extensions';\n//       ▲ Notice how we import container from this library, instead of TypeDI.\n\n/** Tell TypeORM to use the container provided by this lib to resolve it's dependencies. */\nuseContainer(Container);\n\n/** Create a connection and start using TypeORM. */\ncreateConnection({\n  /* \u003cconnection options\u003e */\n}).catch(error =\u003e {\n  console.error(`Couldn't connect to the database!`);\n  console.error(error);\n});\n```\n\n## Usage\n\nThis package exposes three decorators all three decorators can be used on properties or on constructor parameters.\n\n\u003e **IMPORTANT:**\n\u003e To allow TypeDI to resolve the dependencies on your classes you must mark them with `@Service` decorator from the TypeDI package.\n\n### `@InjectConnection` decorator\n\nInjects `Connection` from where you can access anything in your connection.\nOptionally, you can specify a connection to inject by name in the decorator parameter.\n\n```typescript\nimport { Service } from 'typedi';\nimport { Connection } from 'typeorm';\nimport { InjectConnection } from 'typeorm-typedi-extensions';\n\n@Service()\nexport class MyCustomClass {\n  @InjectConnection()\n  private propertyInjectedConnection: Connection;\n\n  constructor(@InjectConnection() private constructorInjectedConnection: Connection) {}\n}\n```\n\n### `@InjectManager` decorator\n\nInjects `EntityManager` from where you can access any entity in your connection.\nOptionally, you can specify a connection to inject by name in the decorator parameter.\n\n```ts\nimport { Service } from 'typedi';\nimport { EntityManager } from 'typeorm';\nimport { InjectManager } from 'typeorm-typedi-extensions';\n\n@Service()\nexport class MyCustomClass {\n  @InjectManager()\n  private propertyInjectedEntityManager: EntityManager;\n\n  constructor(@InjectManager() private constructorInjectedEntityManager: EntityManager) {}\n}\n```\n\n### `@InjectRepository` decorator\n\nInjects `Repository`, `MongoRepository`, `TreeRepository` or custom repository of some Entity.\nOptionally, you can specify a connection to inject by name in the decorator parameter.\n\n```typescript\nimport { Service } from 'typedi';\nimport { Repository } from 'typeorm';\nimport { InjectRepository } from 'typeorm-typedi-extensions';\n// MyDatabaseModel is a TypeORM entity (class marked with `@Entity()` decorator)\nimport { MyDatabaseModel } from './entities/post.entity.ts';\n\n@Service()\nexport class MyCustomClass {\n  @InjectRepository(MyDatabaseModel)\n  private propertyInjectedRepository: Repository\u003cMyDatabaseModel\u003e;\n\n  constructor(@InjectRepository(MyDatabaseModel) private constructorInjectedRepository: Repository\u003cMyDatabaseModel\u003e) {}\n}\n```\n\nExample with custom connection name:\n\n```ts\n@Service()\nexport class PostRepository {\n  @InjectRepository(Post, 'custom-con-name')\n  private repository: Repository\u003cPost\u003e;\n}\n```\n\nYou can also inject custom `Repository` of some Entity. To make this work have to create the class which extends the\ngeneric `Repository\u003cT\u003e` class and decorate it with `EntityRepository\u003cT\u003e` decorator.\n\n```typescript\nimport { Service } from 'typedi';\nimport { Repository, EntityRepository } from 'typeorm';\nimport { InjectRepository } from 'typeorm-typedi-extensions';\n// UserModel is a TypeORM entity (class marked with `@Entity()` decorator)\nimport { UserModel } from './entities/user.entity.ts';\n\n@Service()\n@EntityRepository(UserModel)\nexport class UserRepository extends Repository\u003cUserModel\u003e {\n  public findByEmail(email: string) {\n    return this.findOne({ email });\n  }\n}\n\n@Service()\nexport class PostService {\n  constructor(\n    @InjectRepository()\n    private readonly userRepository: UserRepository\n  ) {}\n\n  public async userExist(user: User): boolean {\n    return (await this.userRepository.findByEmail(user.email)) ? true : false;\n  }\n}\n```\n\n[typedi]: https://github.com/typestack/typedi\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypestack%2Ftypeorm-typedi-extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypestack%2Ftypeorm-typedi-extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypestack%2Ftypeorm-typedi-extensions/lists"}