Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
micalevisk opened this issue Mar 18, 2022 · 16 comments
Open

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

micalevisk opened this issue Mar 18, 2022 · 16 comments

Comments

@micalevisk
Copy link

micalevisk commented Mar 18, 2022

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]

"peerDependencies": {
"reflect-metadata": ">= 0.1.12",
"typeorm": ">= 0.2.8"

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.

@Jedliu
Copy link

Jedliu commented Apr 18, 2022

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

@micalevisk micalevisk changed the title Does the current version (v0.1.21) supports the latest version of typeorm (v0.3.0) Support the latest version of typeorm (v0.3.x) Apr 18, 2022
@ondrakub
Copy link

any progress?

@ymchun
Copy link

ymchun commented May 20, 2022

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

typeorm/typeorm#8616

@raethlo
Copy link

raethlo commented Jun 3, 2022

Hey, are there any updates on this? nestjs has already released support for 0.3. recently nestjs/typeorm#1233 in @nestjs/typeorm.

@hygo2025
Copy link

hygo2025 commented Jun 7, 2022

Hey, are there any updates on this?

@jczacharia
Copy link

jczacharia commented Jun 8, 2022

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

@sadiqumar20
Copy link

this works for me.

@yookky
Copy link

yookky commented Jun 15, 2022

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);

@mahns1201
Copy link

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 {}

at that time this Error occur.

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

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

@Mcheikh2
Copy link

Any updates ?

@xtrinch
Copy link

xtrinch commented Jul 11, 2022

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

@Aliheym
Copy link

Aliheym commented Jul 31, 2022

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

@H4ad
Copy link

H4ad commented Jul 31, 2022

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?

@Aliheym
Copy link

Aliheym commented Jul 31, 2022

It will be cool.

@ckawell-sb
Copy link

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

@Caesarsage
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests