typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

MoreThan/LessThan function not working with columns having transformer

Open smasilamani-cfins opened this issue 2 years ago • 1 comments

Hello

I noticed an error when using any functions like MoreThan/LessThan on a column that has a transformer. For e.g. we have a column called amount that is of type numeric in our Postgres database and we use a transformer to convert the string to number before sending it to the client and when we try to use any of these function on these columns always throws an error as below.

TypeOrm Version : 0.3.12

Entity

	@Column({ name: 'due_amount', type: 'numeric', precision: 12, scale: 2, transformer: new StrToNumTransformer(false) })
	dueAmount: number;

Transformer

export class StrToNumTransformer implements ValueTransformer {
	round: boolean = false;
	defaultToNullIfEmpty: boolean = true;

	constructor(round?: boolean, defaultToNullIfEmpty: boolean = true) {
		this.round = round;
		this.defaultToNullIfEmpty = defaultToNullIfEmpty;
	}

	to(value: number): string {
		const tmpVal: number = value !== null && value !== undefined ? (this.round ? Math.round(value) : value) : null;
		return tmpVal != null ? tmpVal.toString() : this.defaultToNullIfEmpty ? null : '0';
	}

	from(value: string): number {
		return value != null && value !== '' && !isNaN(Number(value)) ? (this.round ? Math.round(Number(value)) : Number(value)) : this.defaultToNullIfEmpty ? null : 0;
	}
}

Query

return this.populateMultiple(
			await this.dbService
				.getConnection()
				.getRepository(PaymentAutoPayEntity)
				.find({
					where: [
						{
							invoiceDueDate: asOfDate,
							dueAmount: MoreThan(10)
						},
						{
							retryInd: true,
							noOfAttempts: LessThanOrEqual(maxRetryAttempts),
							dueAmount: MoreThan(10)
						}
					]
				})
		);

Error

error: error: invalid input syntax for type numeric: "[object Object]" 2023-02-17 14:50:03.406 [error] - [ReqId : dgsKapj6w8wsOlIj3Xw_U] - [User : Auto Pay, Id : 103] : QueryFailedError: invalid input syntax for type numeric: "[object Object]" at PostgresQueryRunner.query (/mnt/c/Repos/payment-portal/server/src/driver/postgres/PostgresQueryRunner.ts:299:19) at processTicksAndRejections (node:internal/process/task_queues:96:5) at SelectQueryBuilder.loadRawResults (/mnt/c/Repos/payment-portal/server/src/query-builder/SelectQueryBuilder.ts:3724:25) at SelectQueryBuilder.executeEntitiesAndRawResults (/mnt/c/Repos/payment-portal/server/src/query-builder/SelectQueryBuilder.ts:3487:26) at SelectQueryBuilder.getRawAndEntities (/mnt/c/Repos/payment-portal/server/src/query-builder/SelectQueryBuilder.ts:1661:29) at SelectQueryBuilder.getMany (/mnt/c/Repos/payment-portal/server/src/query-builder/SelectQueryBuilder.ts:1751:25) at PaymentAutoPayDAO.getPaymentAutoPayToBeProcessed (/mnt/c/Repos/payment-portal/server/src/payment/dao/daos/others/payment_autopay.dao.ts:227:4) at PaymentAutoPayService.getPaymentAutoPayToBeProcessed (/mnt/c/Repos/payment-portal/server/src/payment/services/others/payment_autopay.service.ts:206:38) at AutoPayProcessBatch.processAutoPay (/mnt/c/Repos/payment-portal/server/src/batch/batches/autopay/services/autopay_process.batch.ts:88:39) at /mnt/c/Repos/payment-portal/server/src/batch/runners/autopay_process.runner.ts:32:7

smasilamani-cfins avatar Feb 17 '23 19:02 smasilamani-cfins

PR #9777 should fix that.

ertl avatar Feb 17 '23 19:02 ertl