npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@toxicoder/nestjs-typeorm-repository

v0.0.1

Published

TypeORM repository module for NestJS

Readme

NestJS TypeORM Repository Module

A lightweight module for NestJS that simplifies the implementation of the Repository pattern with TypeORM.

Table of Contents

Description

This module provides a clean and efficient way to implement the Repository pattern in NestJS applications using TypeORM. It helps maintain separation of concerns by decoupling the data access logic from the business logic.

Key features:

  • Simple decorator-based repository creation
  • Support for multiple data sources
  • Flexible configuration options
  • Seamless integration with NestJS and TypeORM

Packages used:

  • @nestjs/common - Core NestJS functionality
  • @nestjs/core - Core NestJS functionality
  • @nestjs/typeorm - NestJS TypeORM integration
  • typeorm - TypeORM ORM

Installation

npm install @toxicoder/nestjs-typeorm-repository

Dependencies

This module requires the following peer dependencies:

npm install @nestjs/common @nestjs/core @nestjs/typeorm typeorm

Make sure you have TypeORM and NestJS properly configured in your application.

Usage

Basic Configuration

  1. Create a repository class that extends TypeORM's Repository:
import { TypeormRepository } from '@toxicoder/nestjs-typeorm-repository';
import { Repository } from 'typeorm';
import { User } from './user.entity';

@TypeormRepository(User)
export class UserRepository extends Repository<User> {
  // Custom repository methods
  findByEmail(email: string) {
    return this.findOne({ where: { email } });
  }
}
  1. Import the module in your feature module:
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 {}

Multiple Data Sources

You can specify which data source to use for a repository:

@TypeormRepository(User, 'readOnlyConnection')
export class UserReadOnlyRepository extends Repository<User> {
  // Read-only operations
}

Important Notes

⚠️ Warning: Do not add repository classes to the providers array in your module. This will cause errors as the repositories are already provided by the TypeormRepositoryModule.forFeature() method. Simply declare them in the forFeature() method.

Incorrect:

@Module({
  imports: [
    TypeormRepositoryModule.forFeature(UserRepository),
  ],
  providers: [UserService, UserRepository], // Don't add UserRepository here!
  exports: [UserService],
})

Correct:

@Module({
  imports: [
    TypeormRepositoryModule.forFeature(UserRepository),
  ],
  providers: [UserService],
  exports: [UserService],
})

Examples

Basic Example

// 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<User> {
  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 Example

// 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}'],
      readonly: true,
    }),
    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<User> {
  // Regular repository with write access
}

@TypeormRepository(User, 'readOnly')
export class UserReadOnlyRepository extends Repository<User> {
  // 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();
  }
}

Data Source Priority

When configuring repositories, you can specify the data source at two levels:

  1. In the @TypeormRepository decorator
  2. In the TypeormRepositoryModule.forFeature() method

The data source specified in the decorator takes precedence over the one specified in the forFeature() method.

// Decorator data source takes precedence
@TypeormRepository(User, 'readOnly')
export class UserReadOnlyRepository extends Repository<User> {}

// Module configuration
@Module({
  imports: [
    // Even though 'default' is specified here, 'readOnly' from the decorator will be used
    TypeormRepositoryModule.forFeature([UserReadOnlyRepository], 'default'),
  ],
})
export class UserModule {}

If no data source is specified in the decorator, the one from forFeature() will be used:

// No data source specified in decorator
@TypeormRepository(User)
export class UserRepository extends Repository<User> {}

// Module configuration
@Module({
  imports: [
    // 'default' will be used since no data source is specified in the decorator
    TypeormRepositoryModule.forFeature([UserRepository], 'default'),
  ],
})
export class UserModule {}

If no data source is specified in either place, the default data source will be used.

License

ISC