Code Monkey home page Code Monkey logo

propagated-transactions's People

Contributors

vkondratiuk482 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

propagated-transactions's Issues

Isolation Levels

Package consumer has to be able to specify one of the following isolation levels:

  • READ COMMITED
  • READ UNCOMMITTED
  • REPEATABLE READ
  • SERIALIZABLE

Fix: Typescript Example

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 ๐Ÿ˜„

CJS/ESM Singletons

As for now PropagatedTransaction implements Singleton Pattern, but in fact we don't really need it since CJS/ESM imports are singletons by default

Enum typings for Typescript

As for now if we use Typescript we can't just do ptx.start('REPEATABLE READ') due to the problems with enum typings

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.