npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

typeorm-ez-transaction

v1.0.6

Published

📚 Abstracted Typeorm transaction handler with Typescript decorators, to facilitate Typeorm transactions management and make the code cleaner.

Downloads

268

Readme

Typeorm Easy Transaction Manager with Typescript Decorators

📚 Abstracted Typeorm transaction control for Typescript decorators, to facilitate transaction management and make the code cleaner.

tests ci

Only supported ORMs: [Typeorm]

Why to use

This is an abstract implementation to work with Typeorm transactions.

The main objective is to facilitate the flow of implementation and provide a powerfull feature, you can use the @Transactional decorator at multiple usecases/services/adapters layers without the fear of coupling the layers of your application with the ORM specific syntax.

Quickstart

Configuration:

import { DataSource } from 'typeorm'
import { TransactionManager } from 'typeorm-ez-transaction'

const dataSource = new DataSource({
	...options
})

dataSource.initialize()

TransactionManager.setDatasource(dataSource)

Example of use:

  • Let's say you need to handle multiple transactions at the same time, if there is some error you'll need to rollback all and if success you commit all.
import { Transactional } from 'typeorm-ez-transaction'

class Example1 {
	constructor(...) {...}

	@Transactional()
	methodToBeCalled() {
		usersRepository.createUser()
		addressRepository.createAddress()
		paymentsRepository.newPayment()
	}
}
  • Important to say that all repositories have to use the EntityManager from TransactionManager.getManager() that is a Typeorm EntityManager concurrent-proof.
import { TransactionManager } from 'typeorm-ez-transaction'

class UsersRepository {
	const repository = TransactionManager
		.getManager()
		.getRepository(UserEntity)

	async createUser(data) {
		await repository.create(data)
	}
}

Optional arguments:

TransactionalOptions {
  logging?: boolean
}

If you pass logging: true, then you'll see logs like that:

TRACE:

[01:21:05.906] INFO: [OrderUsecase][executeWithSuccess] is being intercepted by Transactional decorator...
[01:21:05.914] INFO: [OrderUsecase][executeWithFailure] is being intercepted by Transactional decorator...

SUCCESS: 

[01:23:29.425] INFO: [OrderUsecase][executeWithSuccess][Typeorm] transaction initialized.
[01:23:29.463] INFO: [OrderUsecase][executeWithSuccess][Typeorm] transaction completed successfully.

FAILURES: 

[01:21:12.494] INFO: [OrderUsecase][executeWithFailure][Typeorm] transaction initialized.
[01:21:12.526] INFO: [OrderUsecase][executeWithFailure][Typeorm] has failed. Rollback realized successfully.