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.
Please take a look here
@zMotivat0r I already made that implementation but the query still has id. Let me show you the example code:
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) {}
}
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;
}
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.
Most helpful comment
@zMotivat0r I already made that implementation but the query still has id. Let me show you the example code:
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