I was wondering if something like this is possible.
I am using a NestJS and TypeORM for my project, and want to use transactions. I am trying to replace this usage:
getConnection().transaction(async (transactionManager) => {
const repositoryOne = transactionManager.getCustomRepository(RepostiroyOne) // obviusly types are inherited here
const repositoryTwo = transactionManager.getCustomRepository(RepostiroyTwo)
// code in transaction
})
with something like this:
const transaction = async(callback, repositories) => {
const getTransactionRepositories = (manager: EntityManager) => {
return repositories.map((repository) => manager.getCustomRepository(repository));
};
return getManager().transaction(async (transactionManager) =>
callback(...getTransactionRepositories(transactionManager)),
);
};
then in the code to use it:
transaction(async (repoOne, repoTwo) => {
// code to run inside transaction
// but types here are not inherited
// if i say repoOne: RepositoryOne it is working fine, but I tried to minimize repetition and
// would like if repoOne is a type of RepositoryOne without manually adding it
}, [RepositoryOne, RepositoryTwo])
So as I mentioned in the first example what I am trying to exclude:
const repositoryOne = transactionManager.getCustomRepository(RepositoryOne)
then this repostiryOne becomes:
repositoryOne: RepositoryOne
So in my function, 1st param, e.g. repoOne would have a type of RepositoryOne provided as a first item in the array and so on.
note: Talking strictly about TypeORM tranasaction decorators, I won't use them since there are some issues with nest testing module and typeorm decorators.
Any help is appreciated