How get custom DTO type in TypeOrmCrudService?
pavlo-dot-dev opened this issue · 0 comments
pavlo-dot-dev commented
I have entity with unique field, i want check unique of field in EntityService.
And i have extra column "extra_field" i want use this field only when checking unique for field "name";
Problem here: "override async createOne(req: CrudRequest, dto: CreateDto) {"
Type CreateDTO not allowed, only DeepPartical allowed.
What do?
my.entity.ts
@Entity()
class MyEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({unique: true})
name: string;
}
create.dto.ts
class CreateDto {
@IsString()
@IsNotEmpty()
name: string;
@IsString()
@IsNotEmpty()
extra_field: string;
}
my.service.ts
class MyService extendesextends TypeOrmCrudService<MyEntity> {
constructor(
@InjectRepository(MyEntity) repository: Repository<MyEntity>,
) {
super(repository);
}
override async createOne(req: CrudRequest, dto: CreateDto) {
const checkUniqueName = await this.count({
where: {
name: dto.name,
},
});
if( checkUniqueName ) {
throw new Error('Field name is unique;');
}
const generatedName = dto.name + ':'+dto.extra_field;
return super.createOne(req, {
name: generatedName,
});
}
}