I'm using TypeORM on my application and I try to make a query filtered on multiple fields.
My entity look like this :
@Entity()
export default class Settings extends Base {
@IsString()
@IsDefined()
@MaxLength(256)
@MinLength(2)
@Column({ type: 'varchar', length: 256 })
public transport: string;
@Type(() => Beneficiary)
@OneToOne(() => Beneficiary, {
nullable: false,
eager: true,
})
@JoinColumn()
public beneficiary: Beneficiary
}
I want to filter my query on transport field with operator LIKE.
And also want to filter my query on beneficiary with operator LIKE.
Here is my query :
const [settings, totalSettings] = await getRepository(Settings).findAndCount(
{
loadRelationIds: true,
where: {
transport: Like('%bike%'),
beneficiary: Like('35'),
},
},
)
I used loadRelationIds to map the entity behind beneficiary to get the id directly rather than the complete object.
I saw that I can directly use the mapped field beneficiary like this in my where :
where: {
transport: Like('%bike%'),
beneficiary: '3',
},
But if I add the operator Like before, I got this error :
QueryFailedError: operator does not exist: integer ~~ unknown
Is there a way to add the operator LIKE to this field ?
It's look like I can't use Like on number or entity data.
Or I badly written my query.
Thanks
The only thing that I could think of was to use Raw typescript inside where option, so something like this:
where: {
transport: Like('%bike%'),
beneficiary: Raw((alias) => `CAST(${alias} as varchar) ILike '%${3}%'`)
},
Example:
return {
...this.createWithoutSearchCondition(memberId, isArchived),
...{
[fieldName]: Raw(
(alias) => `CAST(${alias} AS VARCHAR) ILike :search`,
{
search: `%${search}%`,
}
),
},
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With