### Install NestJS TypeORM Repository Module Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md Installs the @toxicoder/nestjs-typeorm-repository package using npm. This is the primary step to integrate the module into your NestJS project. ```bash npm install @toxicoder/nestjs-typeorm-repository ``` -------------------------------- ### Basic Example: Repository Usage in NestJS Service Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md A complete example demonstrating the definition of a custom repository, its configuration in a module, and its usage within a NestJS service for data access operations. ```typescript // user.repository.ts import { TypeormRepository } from '@toxicoder/nestjs-typeorm-repository'; import { Repository } from 'typeorm'; import { User } from './user.entity'; @TypeormRepository(User) export class UserRepository extends Repository { findByUsername(username: string) { return this.findOne({ where: { username } }); } } // user.module.ts import { Module } from '@nestjs/common'; import { TypeormRepositoryModule } from '@toxicoder/nestjs-typeorm-repository'; import { UserRepository } from './user.repository'; import { UserService } from './user.service'; @Module({ imports: [ TypeormRepositoryModule.forFeature(UserRepository), ], providers: [UserService], exports: [UserService], }) export class UserModule {} // user.service.ts import { Injectable } from '@nestjs/common'; import { UserRepository } from './user.repository'; @Injectable() export class UserService { constructor(private userRepository: UserRepository) {} findByUsername(username: string) { return this.userRepository.findByUsername(username); } } ``` -------------------------------- ### Multiple Data Sources Configuration Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md Demonstrates how to configure multiple TypeORM data sources in the AppModule and define repositories for each data source in the UserRepository and UserModule. This setup allows for distinct read/write or different database connections. ```typescript // app.module.ts import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { UserModule } from './user/user.module'; @Module({ imports: [ TypeOrmModule.forRoot({ name: 'default', type: 'postgres', host: 'localhost', port: 5432, username: 'postgres', password: 'postgres', database: 'app', entities: [__dirname + '/**/*.entity{.ts,.js}'] }), TypeOrmModule.forRoot({ name: 'readOnly', type: 'postgres', host: 'localhost', port: 5432, username: 'readonly_user', password: 'readonly_password', database: 'app', entities: [__dirname + '/**/*.entity{.ts,.js}'] }), UserModule, ], }) export class AppModule {} // user.repository.ts import { TypeormRepository } from '@toxicoder/nestjs-typeorm-repository'; import { Repository } from 'typeorm'; import { User } from './user.entity'; @TypeormRepository(User) export class UserRepository extends Repository { // Regular repository with write access } @TypeormRepository(User, 'readOnly') export class UserReadOnlyRepository extends Repository { // Read-only repository } // user.module.ts import { Module } from '@nestjs/common'; import { TypeormRepositoryModule } from '@toxicoder/nestjs-typeorm-repository'; import { UserReadOnlyRepository, UserRepository } from './user.repository'; import { UserService } from './user.service'; @Module({ imports: [ TypeormRepositoryModule.forFeature([ UserRepository, UserReadOnlyRepository, ]), ], providers: [UserService], exports: [UserService], }) export class UserModule {} // user.service.ts import { Injectable } from '@nestjs/common'; import { UserReadOnlyRepository, UserRepository } from './user.repository'; @Injectable() export class UserService { constructor( private userRepository: UserRepository, private userReadOnlyRepository: UserReadOnlyRepository, ) {} // Use userRepository for write operations async create(userData: any) { return this.userRepository.save(userData); } // Use userReadOnlyRepository for read operations async findAll() { return this.userReadOnlyRepository.find(); } } ``` -------------------------------- ### Define Custom Repository with TypeORM Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md Demonstrates how to create a custom repository by extending TypeORM's `Repository` class and applying the `@TypeormRepository` decorator. This allows for custom data access methods. ```typescript import { TypeormRepository } from '@toxicoder/nestjs-typeorm-repository'; import { Repository } from 'typeorm'; import { User } from './user.entity'; @TypeormRepository(User) export class UserRepository extends Repository { // Custom repository methods findByEmail(email: string) { return this.findOne({ where: { email } }); } } ``` -------------------------------- ### Data Source Priority: Decorator vs. Module Configuration Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md Illustrates how the data source specified in the `@TypeormRepository` decorator takes precedence over the data source specified in the `TypeormRepositoryModule.forFeature()` method. It also shows the fallback behavior when no data source is explicitly defined. ```typescript // Decorator data source takes precedence @TypeormRepository(User, 'readOnly') export class UserReadOnlyRepository extends Repository {} // Module configuration @Module({ imports: [ // Even though 'default' is specified here, 'readOnly' from the decorator will be used TypeormRepositoryModule.forFeature([UserReadOnlyRepository], 'default'), ], }) export class UserModule {} // No data source specified in decorator @TypeormRepository(User) export class UserRepository extends Repository {} // Module configuration @Module({ imports: [ // 'default' will be used since no data source is specified in the decorator TypeormRepositoryModule.forFeature([UserRepository], 'default'), ], }) export class UserModule {} ``` -------------------------------- ### Specify Data Source for Repository Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md Illustrates how to associate a repository with a specific TypeORM data source by providing the data source name as the second argument to the `@TypeormRepository` decorator. ```typescript @TypeormRepository(User, 'readOnlyConnection') export class UserReadOnlyRepository extends Repository { // Read-only operations } ``` -------------------------------- ### Configure Repository Module in NestJS Feature Module Source: https://github.com/mekh/nestjs-typeorm-repository/blob/main/README.md Shows how to import and configure the `TypeormRepositoryModule` in a NestJS feature module using `forFeature()`. This makes the custom repository available within the module's scope. ```typescript import { Module } from '@nestjs/common'; import { TypeormRepositoryModule } from '@toxicoder/nestjs-typeorm-repository'; import { UserRepository } from './user.repository'; @Module({ imports: [ TypeormRepositoryModule.forFeature(UserRepository), ], providers: [UserService], exports: [UserService], }) export class UserModule {} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.