@leocode/nest-tx-typeorm
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

nest-tx-typeorm

A TypeORM connector for next-tx.

Installation

Yarn:

yarn add next-tx-core nest-tx-typeorm

NPM:

npm install next-tx-core nest-tx-typeorm

Usage

Initialize the module first. Here we are using @nestjs/typeorm package to initialize a TypeORM connection:

import { Module } from '@nestjs/common';
import { getDataSourceToken, TypeOrmModule } from '@nestjs/typeorm';
import { TransactionManager } from '@leocode/nest-tx-core';
import { TypeORMTransactionManagerModule } from '@leocode/nest-tx-typeorm';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'secret',
      database: 'postgres',
      entities: [TestEntitySchema],
    }),
    TypeORMTransactionManagerModule.forRoot({
      getDataSourceToken: () => getDataSourceToken(),
    }),
  ],
})
class AppModule {}

Then you can use it in your services:

import { Injectable, Module } from '@nestjs/common';
import { InjectTransactionManager, TransactionManager } from '@leocode/nest-tx-core';
import {
  getEntityManagerFromTypeORMTransaction,
  TypeORMTransactionManagerModule
} from '@leocode/nest-tx-typeorm';

@Injectable()
class CatsService {
  constructor(
    @InjectTransactionManager() private readonly transactionManager: TransactionManager,
  ) {}

  public async save() {
    await this.transactionManager.withTransaction(async tx => {
      const manager = tx.getEntityManager();

      const qb = await manager
        .createQueryBuilder()
        .select('id')
        .from('cats')
        .getRawMany();
    });
  }
}

If you have multiple named TypeORM connections, you can provide an instance of the transaction manager for each of them:

@Module({
  imports: [
    // default
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'secret',
      database: 'postgres',
      entities: [TestEntitySchema],
    }),
    // named 'secondConnection'
    TypeOrmModule.forRoot({
      name: 'secondConnection',
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'secret',
      database: 'postgres',
      entities: [TestEntitySchema],
    }),
    // tx manager for the default connection
    TypeORMTransactionManagerModule.forRoot({
      getDataSourceToken: () => getDataSourceToken(),
    }),
    // tx manager for the 'secondConnection' connection
    TypeORMTransactionManagerModule.forRoot({
      getDataSourceToken: () => getDataSourceToken('secondConnection'),
      name: 'secondConnection'
    }),
  ],
})
class AppModule {}

Then in the service, you need to use the connection name in the injection decorator:

@Injectable()
class CatsService {
  constructor(
    @InjectTransactionManager('secondConnection') private readonly transactionManager: TransactionManager,
  ) {}
}

Readme

Keywords

none

Package Sidebar

Install

npm i @leocode/nest-tx-typeorm

Weekly Downloads

2

Version

2.0.0

License

MIT

Unpacked Size

32.3 kB

Total Files

26

Last publish

Collaborators

  • leocode-admin
  • tdzienniak
  • soanvig