vkondratiuk482 / propagated-transactions Goto Github PK
View Code? Open in Web Editor NEWLibrary that provides a wrapper to propagate and manage database transactions
License: MIT License
Library that provides a wrapper to propagate and manage database transactions
License: MIT License
Package consumer has to be able to specify one of the following isolation levels:
Add unit testing examples into README.md
As for now PropagatedTransaction
implements Singleton Pattern, but in fact we don't really need it since CJS/ESM imports are singletons by default
As for now if we use Typescript we can't just do ptx.start('REPEATABLE READ')
due to the problems with enum typings
Hi @mokuteki225
Thanks for this brilliant package. I was looking for something like this specifically.
I faced an issue recently when using it with Typescript -
The PropagatedTransaction
class expects a type variable to be passed whenever using it in the code. The current Typescript example in the documentation suggests doing the following -
// Creation
export const ptx = new PropagatedTransaction(TypeormTransactionRunner);
// Usage
export class UserService {
constructor(
private readonly ptx: PropagatedTransaction,
private readonly userRepository: IUserRepository,
private readonly walletRepository: IWalletRepository,
) {}
But according to this class definition -
export class PropagatedTransaction<T extends unknown> {
connection: T;
constructor(runner: ITransactionRunner<T>, als?: AsyncLocalStorage<T>);
run<R>(callback: () => R, isolationLevel?: IsolationLevelValue): R;
}
Typescript would force to pass the type variable to the PropagatedTransaction
class and it will look like this -
// Creation
export const ptx = new PropagatedTransaction<QueryRunner>(TypeormTransactionRunner);
// Usage
export class UserService {
constructor(
private readonly ptx: PropagatedTransaction<QueryRunner>,
private readonly userRepository: IUserRepository,
private readonly walletRepository: IWalletRepository,
) {}
Should I fix this example?
Also, I like how the library promotes abstraction, I created an interface
for PropagatedTransaction
myself so that my UserService
class doesn't have to rely upon the concrete type variable QueryRunner
, which would expose the implementation details of the underlying database being used to the service. Here's the interface -
import { IsolationLevelValue } from '@mokuteki/propagated-transactions';
// Interface for import { PropagatedTransaction } from '@mokuteki/propagated-transactions';
export default interface IPropagatedTransaction {
connection: unknown;
run<R>(callback: () => R, isolationLevel?: IsolationLevelValue): R;
}
Then I wrote my UserService like the following -
// Usage
export class UserService {
constructor(
private readonly ptx: IPropagatedTransaction,
private readonly userRepository: IUserRepository,
private readonly walletRepository: IWalletRepository,
) {}
Let me what you think of this. Also, I'm happy to create a PR for the following changes if you approve ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.