typeorm-transactional-cls-hooked icon indicating copy to clipboard operation
typeorm-transactional-cls-hooked copied to clipboard

Support the latest version of typeorm (v0.3.x)

Open micalevisk opened this issue 2 years ago • 16 comments

even tho the peer dep semver range allows the usage of typeorm@^0.3.0, maybe few deprecations & drops on the latest version of typeorm could break [email protected]

https://github.com/odavid/typeorm-transactional-cls-hooked/blob/c16e19f1727e3e55832848a4e135cc19fb7160fb/package.json#L63-L65

Please, read this when you have time: https://github.com/typeorm/typeorm/releases/tag/0.3.0 ~And close this Issue when you confirm that we can use your package with the latest version of typeorm. I can't find this out by myself yet.~

micalevisk avatar Mar 18 '22 20:03 micalevisk

Just made a simple test that showed it's not working because no connection could be found. Looking forward to the update. Thank you!

Jedliu avatar Apr 18 '22 00:04 Jedliu

any progress?

ondrakub avatar May 19 '22 19:05 ondrakub

since the getConnection is deprecated, we need to find a way to pass the connection to the hook

https://github.com/typeorm/typeorm/pull/8616

ymchun avatar May 20 '22 04:05 ymchun

Hey, are there any updates on this? nestjs has already released support for 0.3. recently https://github.com/nestjs/typeorm/pull/1233 in @nestjs/typeorm.

raethlo avatar Jun 03 '22 16:06 raethlo

Hey, are there any updates on this?

hygo2025 avatar Jun 07 '22 20:06 hygo2025

A possible solution I found was to add to the Transactional.ts file with the NestJS Inject decorator like so:

import { Inject } from '@nestjs/common'
import { DataSource } from 'typeorm'
import { Options, wrapInTransaction } from './wrapInTransaction'

/**
 * Used to declare a Transaction operation. In order to use it, you must use {@link BaseRepository} custom repository in order to use the Transactional decorator
 * @param connectionName - the typeorm connection name. 'default' by default
 * @param propagation - The transaction propagation type. see {@link Propagation}
 * @param isolationLevel - The transaction isolation level. see {@link IsolationLevel}
 */
export function Transactional(options?: Options): MethodDecorator {
  return (target: any, methodName: string | symbol, descriptor: TypedPropertyDescriptor<any>) => {
    Inject(DataSource)(target, '__data_source_key__')
    const originalMethod = descriptor.value
    descriptor.value = wrapInTransaction(originalMethod, { ...options, name: methodName })

    Reflect.getMetadataKeys(originalMethod).forEach((previousMetadataKey) => {
      const previousMetadata = Reflect.getMetadata(previousMetadataKey, originalMethod)
      Reflect.defineMetadata(previousMetadataKey, previousMetadata, descriptor.value)
    })

    Object.defineProperty(descriptor.value, 'name', { value: originalMethod.name, writable: false })
  }
}

Then in the wrapInTransaction.ts file you can access it like so: const dataSource: DataSource = (this as any)?.['__data_source_key__'];

However, after doing this only the test_nestjs.ts file passes and the test_simple.ts does not.

I'll keep working on this and try to get a PR but maybe this will help someone else figure it out faster.

EDIT:

If you want to try it out:

  "typeorm-transactional-cls-hooked": "git+https://github.com/jczacharia/typeorm-transactional-cls-hooked.git",

use in your package.json

jczacharia avatar Jun 08 '22 20:06 jczacharia

this works for me.

sadiqumar20 avatar Jun 13 '22 10:06 sadiqumar20

A possible solution I found was to add to the Transactional.ts file with the NestJS Inject decorator like so:

import { Inject } from '@nestjs/common'
import { DataSource } from 'typeorm'
import { Options, wrapInTransaction } from './wrapInTransaction'

/**
 * Used to declare a Transaction operation. In order to use it, you must use {@link BaseRepository} custom repository in order to use the Transactional decorator
 * @param connectionName - the typeorm connection name. 'default' by default
 * @param propagation - The transaction propagation type. see {@link Propagation}
 * @param isolationLevel - The transaction isolation level. see {@link IsolationLevel}
 */
export function Transactional(options?: Options): MethodDecorator {
  return (target: any, methodName: string | symbol, descriptor: TypedPropertyDescriptor<any>) => {
    Inject(DataSource)(target, '__data_source_key__')
    const originalMethod = descriptor.value
    descriptor.value = wrapInTransaction(originalMethod, { ...options, name: methodName })

    Reflect.getMetadataKeys(originalMethod).forEach((previousMetadataKey) => {
      const previousMetadata = Reflect.getMetadata(previousMetadataKey, originalMethod)
      Reflect.defineMetadata(previousMetadataKey, previousMetadata, descriptor.value)
    })

    Object.defineProperty(descriptor.value, 'name', { value: originalMethod.name, writable: false })
  }
}

Then in the wrapInTransaction.ts file you can access it like so: const dataSource: DataSource = (this as any)?.['__data_source_key__'];

However, after doing this only the test_nestjs.ts file passes and the test_simple.ts does not.

I'll keep working on this and try to get a PR but maybe this will help someone else figure it out faster.

EDIT:

If you want to try it out:

  "typeorm-transactional-cls-hooked": "git+https://github.com/jczacharia/typeorm-transactional-cls-hooked.git",

use in your package.json

I can add to your solution how not to use BaseRepository

patchRepositoryManager(DataSource.prototype)

and inside code

const repo = dataSource.getRepository(UserEntity);

yookky avatar Jun 15 '22 17:06 yookky

Is this why "ERROR [ExceptionHandler] Class constructor repository cannot be invoked without 'new'" occurs when using BaseRepository in "[email protected]"?

my version is this "typeorm": "^0.3.6" "@nestjs/typeorm": "^8.1.3"

so, i use CustomRepository on entity.ts like this. @CustomRepository(User) export class UserRepository extends BaseRepository<User> {}

at that time this Error occur.

export class UserRepository extends Repository<User> {} it does not occur any Error.

i already tried to change my tsconfig.json file's compilerOptions but it doesn't work. 🥲

mahns1201 avatar Jun 17 '22 06:06 mahns1201

Any updates ?

Mcheikh2 avatar Jun 28 '22 14:06 Mcheikh2

The above code from @jczacharia works for me, with one addition (No need to extend the base repo from this lib this way): patchRepositoryManager(Repository.prototype) after initializeTransactionalContext() You can then do this.somethingRepo.find(...) like you normally would with the usage of the decorator

xtrinch avatar Jul 11 '22 16:07 xtrinch

Hi guys, thanks a lot to the author for such a great library. But I'm really sorry that the lib hasn't support last TypeORM changes.

I needed this library functionality for my new project but with support of newer versions of TypeORM, so I made a fork of this library and did necessary changes and improvements. I also made a package for easy use in my project.

If it helps anyone, you can take a look: https://github.com/Aliheym/typeorm-transactional

Aliheym avatar Jul 31 '22 12:07 Aliheym

Hey @odavid, @Aliheym's solution still keeps the original library design but creates a breaking change, rather than trying to keep two libraries, why not @Aliheym create a merge in this library with your modifications?

Also, maybe you can share the maintainer responsibilities so that it doesn't overwhelm you too much, what do you think?

H4ad avatar Jul 31 '22 13:07 H4ad

It will be cool.

Aliheym avatar Jul 31 '22 13:07 Aliheym

The TLDR (am I getting this right?): This npm package is no longer being updated (per the latest PR's comment), but there is a forked npm package that is updated: https://www.npmjs.com/package/typeorm-transactional

ckawell-sb avatar Sep 01 '22 18:09 ckawell-sb

Can i use @Aliheym package and this together ? Like i already have a codebase with this package or i can use @Aliheym and unistall without causing code breaks ? Seems this package have been abandon

Caesarsage avatar Feb 15 '23 03:02 Caesarsage