# inversify-typeorm

> Dependency injection and service container integration with TypeORM using InversifyJs library.

Latest version **0.0.6** (published 2025-09-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install inversify-typeorm
pnpm add inversify-typeorm
yarn add inversify-typeorm
bun add inversify-typeorm
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2025-09-03 |
| First published | 2022-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 5 |
| Unpacked size | 34.7 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | felicityin |
| Maintainers | felicityin |

## Links

- npm: https://www.npmjs.com/package/inversify-typeorm
- Repository: https://github.com/felicityin/inversify-typeorm
- Homepage: https://github.com/felicityin/inversify-typeorm#readme
- Issues: https://github.com/felicityin/inversify-typeorm/issues
- npm.io page: https://npm.io/package/inversify-typeorm

## Dependencies (5)

- [rxjs](https://npm.io/package/rxjs.md) 7.8.2
- [tslib](https://npm.io/package/tslib.md) 2.8.1
- [typeorm](https://npm.io/package/typeorm.md) 0.3.26
- [inversify](https://npm.io/package/inversify.md) 6.2.2
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) 0.2.2

## Recent versions

- 0.0.6 (latest) — 2025-09-03
- 0.0.5 — 2025-08-24
- 0.0.4 — 2022-12-21
- 0.0.3 — 2022-12-18

## README

# Description

Dependency injection and service container integration with [TypeOrm](https://github.com/typeorm/typeorm) using [InversifyJs](https://github.com/inversify/InversifyJS) library.

The implementation is strongly inspired by [@nestjs/typeorm](https://github.com/nestjs/typeorm).

# Usage

We assume that you are familiar with [TypeOrm](https://github.com/typeorm/typeorm).

Install the required dependencies.

```
npm install --save inversify-typeorm
```

We need at least one entity, so we define the `User` entity firstly.

```
// user.entity.ts

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id!: number

  @Column()
  name!: string
}
```

To use `Datasource` and `EntityManager`, we need to bind them to the container using `TypeOrmManager.importRoot()`, and they will be available to inject across the entire project.

- Note: To begin using the `User` entity, we need to let `TypeORM` know about it by inserting it into the entities array (unless you use a static glob path).

```
// app.ts

import { TypeOrmManager } from 'inversify-typeorm'
import { User } from './user.entity'

await TypeOrmManager.importRoot({
  type: 'mysql',
  host: 'localhost',
  port: 5432,
  username: 'test',
  password: 'test',
  database: 'test',
  synchronize: true,
  logging: false,
  entities: [User],
  migrations: [],
  subscribers: [],
})
```

TypeORM supports the repository design pattern, so each entity has its own repository. These repositories can be obtained from the database data source.

We can use `TypeOrmManager.importRepository()` to define which repositories are bound to the container. So the bound repositories will be also available to inject across the entire project.

```
// app.ts

await TypeOrmManager.importRepository([User])
```

Let's define the `UserService` to operate `User` entities.

Then we can inject the `DataSource`, `EntityManager` or `UsersRepository` into the `UsersService`:

- inject `DataSource` using the `@InjectDataSource()`
- inject `EntityManager` using the `@InjectEntityManager()`
- inject `UsersRepository` using the `@InjectRepository()`

And the `UserService` also needed to be bounded to the container by `@Service()`, so that it can be created correctly by the container using the injected `DataSource`, `EntityManager` or `UsersRepository`.

```
// user.service.ts

import { DataSource, EntityManager, Repository } from 'typeorm'
import { InjectDataSource, InjectRepository, InjectEntityManager, Service } from 'inversify-typeorm'
import { User } from './user.entity'

@Service()
export class UserService {
  @InjectDataSource()
  private dataSource!: DataSource

  @InjectEntityManager()
  private entityManager!: EntityManager

  constructor(@InjectRepository(User) private repository: Repository<User>) {}

  saveByRepository(user: User): Promise<User> {
    return this.repository.save(user)
  }

  saveByManager(user: User): Promise<User> {
    return this.entityManager.save(user)
  }

  async saveManyByTransaction(users: User[]): Promise<void> {
    const queryRunner = this.dataSource.createQueryRunner()

    await queryRunner.connect()
    await queryRunner.startTransaction()
    try {
      for (const user of users) {
        await queryRunner.manager.save(user)
      }
      await queryRunner.commitTransaction()
    } catch (err) {
      await queryRunner.rollbackTransaction()
    } finally {
      await queryRunner.release()
    }
  }

  findAll(): Promise<User[]> {
    return this.repository.find()
  }
}
```

It's time to get the created `UserService` from the container by `container.get<UserService>(UserService.name)` and operate `User` entities!

```
// app.ts

import { contanier } from 'inversify-typeorm'

const userOne = new User()
userOne.id = 1
userOne.name = `Alice`

const userTwo = new User()
userTwo.id = 2
userTwo.name = `Bob`

const userThree = new User()
userThree.id = 3
userThree.name = `Olivia`

const userFour = new User()
userFour.id = 4
userFour.name = `Ada`

const service = container.get<UserService>(UserService.name)

/** Save through the various methods. */
await service.saveByRepository(userOne)
await service.saveByManager(userTwo)
await service.saveManyByTransaction([userThree, userFour])

/** Load the saved users so we can assert on them. */
const userList = await service.findAll()
console.log({ userList })
```

At last, we can close the connection with the database. Once connection is closed, we cannot use repositories or perform any operations except opening connection again.

```
// app.ts

try {
  await TypeOrmManager.destroyDataSource()
} cache (e) {
  // log error
}
```

# Test

```
npm i
npm test
```

# Examples

All examples are available [here](https://github.com/felicityin/inversify-typeorm/tree/main/examples).

---
_Source: https://npm.io/package/inversify-typeorm · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
