-
Notifications
You must be signed in to change notification settings - Fork 0
ServiceTypeorm
A CRUD service for relational databases built with TypeORM.
npm i @nestjs-crud/typeorm @nestjs/typeorm typeormStart 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) {}
}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.
updateOne, replaceOne, and deleteOne run inside a QueryRunner transaction at READ COMMITTED isolation. This closes the v1 read-modify-write race.
@Crud({ query: { cache } }) requires a DataSource({ cache: ... }) provider. Without one, the first cached read throws CrudCacheNotConfiguredError. See Caching.
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.
Pass an optional LoggerService as the constructor's second argument. See Logging.
Unknown sort, filter, or search fields throw RequestQueryException. See the v2 Migration guide.