Skip to content

Instantly share code, notes, and snippets.

View neerajsonii's full-sized avatar
🎯
Focusing

Neeraj neerajsonii

🎯
Focusing
  • Goa, India
  • 18:52 (UTC -12:00)
View GitHub Profile
import { Injectable, NestInterceptor, ExecutionContext, CallHandler, HttpException } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
export interface Response<T> {
data: T;
}
@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
@neerajsonii
neerajsonii / repository.ts
Created December 19, 2022 11:57
Transaction factory class for managing the database transactions using Typeorm in Nestjs.
/* Repository method */
saveWithTransactions(data, transactionManager): Promise<Entity> {
if (transactionManager) return transactionManager.save(Entity, data);
return this.repository.save(data);
}
@neerajsonii
neerajsonii / typeorm-transaction-example-snippet.ts
Created December 19, 2022 11:52
Methods to handle transactions in Typeorm.
/* 1. Using DataSource or EntityManager: */
await myDataSource.manager.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.save(users)
await transactionalEntityManager.save(photos)
// ...
});
/* 2. Using a QueryRunner: */