Skip to content

ServiceTypeorm

github-actions[bot] edited this page Apr 26, 2026 · 3 revisions

A CRUD service for relational databases built with TypeORM.

Install

npm i @nestjs-crud/typeorm @nestjs/typeorm typeorm

Usage

Start with a TypeORM entity:

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class Company {
  @PrimaryGeneratedColumn() id: number;

  @Column() name: string;
}

Create a service:

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmCrudService } from '@nestjs-crud/typeorm';

import { Company } from './company.entity';

@Injectable()
export class CompaniesService extends TypeOrmCrudService<Company> {
  constructor(@InjectRepository(Company) repo) {
    super(repo);
  }
}

Wire it into a controller:

import { Controller } from '@nestjs/common';
import { Crud, CrudController } from '@nestjs-crud/core';

import { Company } from './company.entity';
import { CompaniesService } from './companies.service';

@Crud({
  model: {
    type: Company,
  },
})
@Controller('companies')
export class CompaniesController implements CrudController<Company> {
  constructor(public service: CompaniesService) {}
}

v2.0.0 notes

Architecture

TypeOrmCrudService is now a ~250-line orchestrator. Query composition lives in a shared QueryTranslator<SelectQueryBuilder, Brackets> facade made of WhereBuilder + QueryComposer + FetchHelper. There is no consumer-visible API change. Background: CONTRIBUTING.md — Adapter shape.

Transactions

updateOne, replaceOne, and deleteOne run inside a QueryRunner transaction at READ COMMITTED isolation. This closes the v1 read-modify-write race.

Cache wiring

@Crud({ query: { cache } }) requires a DataSource({ cache: ... }) provider. Without one, the first cached read throws CrudCacheNotConfiguredError. See Caching.

Relation loading

Opt into the 'query' strategy with @Crud({ query: { relationLoadStrategy: 'query' } }) to avoid Cartesian explosion when you read multiple OneToMany relations on the same parent. See RelationLoadStrategy.

Logging

Pass an optional LoggerService as the constructor's second argument. See Logging.

Field allowlist

Unknown sort, filter, or search fields throw RequestQueryException. See the v2 Migration guide.

See also

Clone this wiki locally