Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inject TypeOrm by InversifyJs #56

Merged
merged 7 commits into from
Dec 8, 2022

Conversation

felicityin
Copy link
Contributor

Description

Inject TypeOrm by InversifyJs.

Usage

We assume that you are familiar with TypeOrm.

Install the required dependencies.

npm install --save @kuai/typeorm typeorm pg

Define the User entity.

// user.entity.ts

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

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

  @Column()
  name!: string
}

Then bind the Datasource and EntityManager 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 '@kuai/typeorm'
import { User } from './user.entity'

await TypeOrmManager.importRoot({
  type: 'postgres',
  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 '@kuai/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 '@kuai/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, you can close the connection with the database. Once connection is closed, you cannot use repositories or perform any operations except opening connection again.

// app.ts

await TypeOrmManager.destroyDataSource()

packages/typeorm/__tests__/index.ts Outdated Show resolved Hide resolved
packages/typeorm/src/decorators/kuai-entity.decorator.ts Outdated Show resolved Hide resolved
packages/typeorm/src/typeorm.utils.ts Outdated Show resolved Hide resolved
packages/typeorm/src/typeorm.utils.ts Outdated Show resolved Hide resolved
packages/typeorm/src/typeorm.manager.ts Outdated Show resolved Hide resolved
Development.md Outdated Show resolved Hide resolved
package.json Outdated Show resolved Hide resolved
packages/typeorm/package.json Outdated Show resolved Hide resolved
packages/typeorm/package.json Outdated Show resolved Hide resolved
packages/typeorm/tsconfig.json Outdated Show resolved Hide resolved
packages/typeorm/src/decorators/typeorm.decorators.ts Outdated Show resolved Hide resolved
packages/typeorm/src/typeorm.manager.ts Outdated Show resolved Hide resolved
packages/typeorm/src/typeorm.manager.ts Outdated Show resolved Hide resolved
packages/typeorm/src/typeorm.manager.ts Outdated Show resolved Hide resolved
@Keith-CY
Copy link
Member

Keith-CY commented Dec 1, 2022

Please add more tests, typeorm.manager, typeorm.utils are not covered

@felicityin
Copy link
Contributor Author

Please add more tests, typeorm.manager, typeorm.utils are not covered

I can add a few tests because it need to connect to the database. Examples will be added after this pr merged.

@Keith-CY
Copy link
Member

Keith-CY commented Dec 2, 2022

Please add more tests, typeorm.manager, typeorm.utils are not covered

I can add a few tests because it need to connect to the database. Examples will be added after this pr merged.

Functions/classes in typeorm.manager and typeorm.utils could be tested by mocking DataSource

@felicityin
Copy link
Contributor Author

Please add more tests, typeorm.manager, typeorm.utils are not covered

I can add a few tests because it need to connect to the database. Examples will be added after this pr merged.

Functions/classes in typeorm.manager and typeorm.utils could be tested by mocking DataSource

OK, I will figure out how.

packages/typeorm/src/typeorm.utils.ts Outdated Show resolved Hide resolved
packages/typeorm/package.json Outdated Show resolved Hide resolved
@Keith-CY
Copy link
Member

Keith-CY commented Dec 5, 2022

There're still some unresolved conversions but they are collapsed by GitHub, please have a look
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants