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

@plinioduartt/ts-transaction-manager

v1.0.15

Published

๐Ÿ“š Abstracted transaction control for Typescript decorators, to facilitate transaction management and make the code cleaner.

Downloads

22

Readme

Transaction Manager with Typescript Decorators

๐Ÿ“š Abstracted transaction control for Typescript decorators, to facilitate transaction management and make the code cleaner.

tests ci

Supported ORMs

  • [x] Typeorm (since v1.0.0)
  • [x] Knex (since v1.0.15)
  • [ ] Prisma (soon)
  • [ ] Mongoose (soon)
  • [ ] Sequelize (soon)

Why to use

This is an abstract implementation to work with ORM transactions.

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

Quickstart

Configuration:

  • With TypeOrm
import { DataSource } from 'typeorm'
import { TransactionManager } from '@plinioduartt/ts-transaction-manager'

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

dataSource.initialize()

TransactionManager
	.addDataSource(dataSource)
	.setDefaultDataSource(dataSource)
  • With Knex
mport knex, { Knex } from 'knex'
import { TransactionManager } from '@plinioduartt/ts-transaction-manager'

const dataSource: Knex = knex({
	...options
})

dataSource.initialize()

TransactionManager
	.addDataSource(dataSource)
	.setDefaultDataSource(dataSource)

Example of use:

  • With TypeOrm
import { Transactional } from '@plinioduartt/ts-transaction-manager'

class Example1 {

	@Transactional()
	methodToBeCalled() {
		// all the database actions here are encapsulated by typeorm transaction...
		// you can do the database operations here or pass it to whatever other layer
	}
}
  • With Knex
import { TransactionManager, Transactional } from '@plinioduartt/ts-transaction-manager'

class Example1 {

	@Transactional()
	methodToBeCalled() {
		// all the database actions here are encapsulated by knex transaction...
		// you can do the database operations here or pass it to whatever other layer

		const transaction: Knex.Transaction = await TransactionManager.getKnexTransaction()

		await knex('table_name').insert(data).transacting(transaction)
	}
}

*Obs: You'll need to remember to always retrieve the transaction from TransactionManager.getKnexTransaction() and pass inside .transacting() method

The arguments are optionals:

TransactionalOptions {
  orm?: SupportedOrms
  logging?: boolean
}

*Consider SupportedOrms = 'typeorm' | 'knex'.
In the future, it'll be available with more options for dataSource configuration.

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

[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...


@Transactional({ orm: 'typeorm', logging: true })
async executeWithSuccess(request: CreateOrderRequest): Promise<CreateOrderResponse> {
	// Logic with success
}

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


@Transactional({ orm: 'typeorm', logging: true })
async executeWithFailure(request: CreateOrderRequest): Promise<CreateOrderResponse> {
	// Logic with failure
}

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