Search code examples
node.jstypescriptnestjstypeorm

How can I use ABS() and minus opertator in typerom querybuilder orderBy function?


I'm trying to order by the closest latitude using this sql query

ORDER BY ABS(a.latitude  - 41.876698387331906) ASC

It works fine,

But in typeorm when I try

.orderBy('ABS(address.latitude - 41.41.876698387331906)', 'ASC')

It tells me that the relation 'ABS(address' doesn't exist. Any idea on this?


Solution

  • try adding the ABS in you select as the following:

    .addSelect('ABS(address.latitude - 41.41.876698387331906)', 'absLat')
    

    and then order by absLat

    .orderBy('absLat', 'ASC')