import { TransactionHost } from '@nestjs-transactions/core'; import { TransactionalAdapterTypeOrm } from '@nestjs-cls/transactional-adapter-typeorm'; import { Repository } from 'typeorm'; import type { EntityTarget, ObjectLiteral } from 'typeorm'; /** * Base class for custom repositories — a real `Repository` whose * inherited methods (`find`, `save`, `createQueryBuilder`, …) always run on * the *current* EntityManager: the transactional one inside `@Transactional()`, * the base one outside. Hand-rolled repository classes and `repo.extend()` * hold on to a fixed EntityManager and cannot be silently intercepted — * extend this instead and call the `Repository` API directly on `this`. * * The entity and the {@link TransactionHost} are supplied through the * constructor, so subclasses stay plain classes (no abstract field, no mixin * factory) and user-defined base repositories are just generic subclasses. * * ```ts * @Injectable() * export class MemberRepository extends NestjsTypeormRepository { * constructor(txHost: TransactionHost) { * super(Member, txHost); * } * * findByEmail(email: string) { * return this.findOneBy({ email }); * } * } * ``` * * Share behaviour across repositories with your own generic base — a plain * abstract subclass, no factories. It can pull in extra request context (e.g. * `ClsService` for the current user) and pass it up: * * ```ts * export abstract class BaseRepository extends NestjsTypeormRepository { * constructor( * entity: EntityTarget, * txHost: TransactionHost, * protected readonly cls: ClsService, * ) { * super(entity, txHost); * } * * findAll(): Promise { * return this.find(); * } * * protected get currentUserId(): string | undefined { * return this.cls.get('userId'); * } * } * * @Injectable() * export class MemberRepository extends BaseRepository { * constructor(txHost: TransactionHost, cls: ClsService) { * super(Member, txHost, cls); * } * } * ``` * * For a named connection, inject the matching host with * `@InjectTransactionHost('name')`: * * ```ts * @Injectable() * export class StatRepository extends NestjsTypeormRepository { * constructor(@InjectTransactionHost('stats') txHost: TransactionHost) { * super(Stat, txHost); * } * } * ``` * * Notes: * - Tree entities: `TreeRepository`'s extra methods are not inherited here — * call `this.manager.getTreeRepository(this.target)` inside a method (still * transaction-aware), or inject the entity's repository with * `@InjectRepository` (its provider resolves a `TreeRepository`). * - Do not re-declare `manager` (or `target`/`queryRunner`) as a field in a * subclass — under ES2022 class-field semantics it would bury the live * `manager` accessor this class installs. */ export declare class NestjsTypeormRepository extends Repository { protected readonly txHost: TransactionHost; constructor(entity: EntityTarget, txHost: TransactionHost); /** * TypeORM's `Repository#extend` destructures `manager` (freezing the live * accessor to whatever manager was current) and re-invokes `this.constructor` * with Repository's positional `(target, manager, queryRunner)` — which is a * subclass constructor expecting `(entity, txHost)`. Both silently corrupt * the result, so clone through the prototype chain instead: the clone keeps * inheriting the live `manager` accessor and all subclass methods. */ extend(customs: CustomRepository & ThisType): this & CustomRepository; } //# sourceMappingURL=nestjs-typeorm.repository.d.ts.map