Crud: [Feature Request] Option to change id on query

Created on 2 May 2019  路  3Comments  路  Source: nestjsx/crud

I noticed that whenever I'm using the CRUD, he finds anything by searching with and comparing with an "id" column. I don't see any option so that we can change this as a parameter of some sort, for example if in my table for some reason I don't have an "id" column, if not a "key" column, then I should be able to tell the CRUD that he should search and compare against the column "key".
This is similar to the issue #62.

Most helpful comment

@zMotivat0r I already made that implementation but the query still has id. Let me show you the example code:

  • CRUD controller:
import { UseGuards, Controller } from '@nestjs/common';
import { ApiUseTags } from '@nestjs/swagger';
import { Crud } from '@nestjsx/crud';
import { AuthGuard } from '@nestjs/passport';
import { Master } from '../../models/master.entity';
import { MasterService } from './master.service';

@ApiUseTags('master')
@UseGuards(AuthGuard())
@Crud(Master, {
    options: {
        cache: 3000
    },
    params: {
        key: 'string',
    },

    validation: {
        validationError: {
            target: false,
            value: false
        }
    },

})
@Controller('master')
export class MasterControllerCrud {
  constructor(private readonly service: MasterService) {}
}
  • Master entity:
import { Entity, Column, PrimaryColumn } from 'typeorm';
import { ApiModelProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { CREATE } from '@nestjsx/crud';
@Entity()
export class Master {

  @ApiModelProperty()
  @IsNotEmpty({ ...CREATE })
  @IsString()
  @PrimaryColumn() // automatically set .
  key: string;

  @ApiModelProperty()
  @IsNotEmpty({ ...CREATE })
  @IsString()
  @Column()
  value: string;
}

  • Service
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { RepositoryService } from '@nestjsx/crud/typeorm';
import { Repository } from 'typeorm';
import { Master } from '../../models/master.entity';
import { Like } from 'typeorm';

@Injectable()
export class MasterService extends RepositoryService<Master>{
  constructor(
    @InjectRepository(Master)
    repo: Repository<Master>,
  ) { super(repo); }

  async WhereLike(like: string): Promise<Master[]> {
    return this.repo.find({ key: Like(like) });
  }

  async Update(master: Master): Promise<Master> {
    return this.repo.save(master);
  }
  async Find(key: string): Promise<Master> {
    return this.repo.findOne({
      where: { key },
    });
  }
}

When doing a GET /master/:key the resulting query executes like this:

SELECT "Master"."key" AS "Master_key", "Master"."value" AS "Master_value", Master.id FROM "master" "Master" WHERE "Master"."key" = $1

Notice that Master.id keeps showing on the Query

All 3 comments

Please take a look here

@zMotivat0r I already made that implementation but the query still has id. Let me show you the example code:

  • CRUD controller:
import { UseGuards, Controller } from '@nestjs/common';
import { ApiUseTags } from '@nestjs/swagger';
import { Crud } from '@nestjsx/crud';
import { AuthGuard } from '@nestjs/passport';
import { Master } from '../../models/master.entity';
import { MasterService } from './master.service';

@ApiUseTags('master')
@UseGuards(AuthGuard())
@Crud(Master, {
    options: {
        cache: 3000
    },
    params: {
        key: 'string',
    },

    validation: {
        validationError: {
            target: false,
            value: false
        }
    },

})
@Controller('master')
export class MasterControllerCrud {
  constructor(private readonly service: MasterService) {}
}
  • Master entity:
import { Entity, Column, PrimaryColumn } from 'typeorm';
import { ApiModelProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
import { CREATE } from '@nestjsx/crud';
@Entity()
export class Master {

  @ApiModelProperty()
  @IsNotEmpty({ ...CREATE })
  @IsString()
  @PrimaryColumn() // automatically set .
  key: string;

  @ApiModelProperty()
  @IsNotEmpty({ ...CREATE })
  @IsString()
  @Column()
  value: string;
}

  • Service
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { RepositoryService } from '@nestjsx/crud/typeorm';
import { Repository } from 'typeorm';
import { Master } from '../../models/master.entity';
import { Like } from 'typeorm';

@Injectable()
export class MasterService extends RepositoryService<Master>{
  constructor(
    @InjectRepository(Master)
    repo: Repository<Master>,
  ) { super(repo); }

  async WhereLike(like: string): Promise<Master[]> {
    return this.repo.find({ key: Like(like) });
  }

  async Update(master: Master): Promise<Master> {
    return this.repo.save(master);
  }
  async Find(key: string): Promise<Master> {
    return this.repo.findOne({
      where: { key },
    });
  }
}

When doing a GET /master/:key the resulting query executes like this:

SELECT "Master"."key" AS "Master_key", "Master"."value" AS "Master_value", Master.id FROM "master" "Master" WHERE "Master"."key" = $1

Notice that Master.id keeps showing on the Query

I have few tables with composite id, that i can't use crud rest with (at least not in a simple way)
I completely understand the lib tradeoffs, just want to point it out.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nikitammf picture nikitammf  路  3Comments

cuni0716 picture cuni0716  路  3Comments

btd1337 picture btd1337  路  4Comments

josimarz picture josimarz  路  4Comments

Darkein picture Darkein  路  3Comments