Giter Site home page Giter Site logo

nest-tx's Introduction

nest-tx - database transaction manager for NestJS

lerna

Motivation

Transaction management is usually done in application services, which should not have knowledge of implementation details like DB engine or DB access library (like TypeORM). This way you can change the underlying DB technology without modifying business logic. @leocode/nest-tx-core provides an abstract way of managing transactions - it is not tied to any particular database, driver or ORM. It can be used with many libraries and drivers by using adapters ( like @leocode/nest-tx-typeorm).

Currently supported ORMs/query builders/drivers:

  • Knex
  • TypeORM

Installation

@leocode/nest-tx-core must always be installed:

yarn add @leocode/nest-tx-core

Then you can install particular adapters (and its peer dependencies) for your application, for example:

yarn add @leocode/nest-tx-typeorm

Usage

First, you need to register an adapter:

import { Module } from '@nestjs/common';
import { TypeORMTransactionManagerModule } from '@leocode/nest-tx-typeorm';

@Module({
  imports: [
    TypeORMTransactionManagerModule.forRoot(),
  ],
})
export class AppModule {}

Then you can use

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

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

  async save() {
    await this.transactionManager.withTransaction(async (tx) => {
      /**
       * NOTE: Usually you should write code like this in a separate repository class -
       * it's written here for the example brevity.
       */
      const manager = getEntityManagerFromTypeORMTransaction(tx);

      await manager.query('SELECT * FROM "table"');
    });
  }
}

Consult the README file in each package to see how to use it:

nest-tx's People

Contributors

tdzienniak avatar

Stargazers

 avatar

Watchers

Maciej Turkiewicz avatar James Cloos avatar  avatar Konrad Kotelczuk avatar

Forkers

tdzienniak

nest-tx's Issues

Optional transaction

It would be nice to add optional "ready" transaction to withTransaction method - in which case transaction will be reused if it is provided (Transaction | null | undefined), but if it is not defined, then new transaction will be created. Such transaction can be passed down from the parent, but may not be necessary

It will simplify API a lot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.