Skip to content

ServiceTypeorm

Michael Yali edited this page Dec 27, 2019 · 3 revisions

This package provides a CRUD service for relational databases build with TypeORM

Install

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

Usage

Assume you have some TypeORM enitity:

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

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

  @Column() name: string;
}

Then you need to create a service:

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

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

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

After that you need to provide your service in a controller:

import { Controller } from "@nestjs/common";
import { Crud, CrudController } from "@nestjsx/crud";

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) {}
}