Shruti sharma
Shruti sharma

Reputation: 211

making generated column optional in NestJS/TypeORM

I have a scenario in which I have to make generated column optional. actually we have to generate the field if it is not present.

@ApiProperty()
@Generated('rowid')  //this is generating value automatically
@Column({name:"certificate_no",type: 'varchar'})
certificateNo?: string;

actually in UI we have a functionality in which we have to duplicate the row. so when API gets called and certificateNo is not present then this value should be generated. if it is present then present value should be inserted into database.

I am putting in DTO.

@ApiProperty()
@IsOptional()
@IsString()
certificateNo?: string;

Actually it is generating certificateNo everytime even if it is present. is there anyway to handle this scenario?

enter image description here

if user clicks on + button same row should be duplicated.

any suggestions please.

Upvotes: 1

Views: 1430

Answers (1)

rashmi
rashmi

Reputation: 228

You can create primary key separately.

export class Certificate extends BaseEntity {
    @ApiProperty()
    @Generated('rowid')
    @Column({ name: 'certificate_no', type: 'varchar' })
    certificateNo: string;

    @ApiProperty()
    @Column({ name: 'request_status', type: 'varchar', length: 100 })
    requestStatus: RequestStatus;

    @ApiProperty()
    @Column({ name: 'sponsor', type: 'varchar', length: 100 })
    sponsor: string;

and you can extend BaseEntity like above. in BaseEntity your primary key will be generated. send the field when you recreate the record..only that field will be stored. means dont make this field as primary key.

Upvotes: 1

Related Questions