How to order by custom field with typeorm?

Viewed 96

I am using typeorm to query my db. I am facing the following issue when I try to order by distance: ER_WRONG_ARGUMENTS: Incorrect arguments to st_distance_sphere

const queryBuilder = this.storeRepository
      .createQueryBuilder('store')

    if (pageOptionsDto?.lat > 0 && pageOptionsDto?.lng > 0) {
      queryBuilder
        .addSelect(
          "ST_Distance_Sphere(ST_GeomFromText('point(" +
            pageOptionsDto?.lng +
            ' ' +
            pageOptionsDto?.lat +
            ")'), ST_GeomFromText(CONCAT('point(',`store`.`long`,' ',`store`.`lat`,')')))",
          'store_distance',
        )
        .orderBy('store.distance', 'DESC');
    }

I would like to encapsulate the selection like this:

SELECT t1.* 
FROM (  SELECT ST_Distance_Sphere(ST_GeomFromText('point(127.0205034 37.4955253)'), ST_GeomFromText(CONCAT('point(',`store`.`long`,' ',`store`.`lat`,')'))) AS distance 
       FROM `stores` `store`  LIMIT 15 
     ) t1 
order by t1.distance;

I've tried different ways like subquery, etc but I could not find the proper solution.

0 Answers
Related