Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

322
Views
Typeorm Postgis order by nearest Point

I have a postgis database with a bunch of points and I would like to get the points in order of the distance to my own position (longitude and latitude).

So I want to do something like:

const markers = await this.markerRepo.find({
  where: {
    type: "test"
  },
  order: { position: 'ASC' },
});

But I want to order them in relation to my current position.

This id my table entity (Nestjs):

@Entity()
 export class Marker {
   @PrimaryGeneratedColumn()
   id: number;

   @Column({
     type: 'geography',
     spatialFeatureType: 'Point',
     srid: 4326,
     nullable: true,
   })
   position: Point;
 
   @Column()
   type: string;
 }

If you need any more information please let me know

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Turns out Typeorm does not support Postgis (Who would have thought)

The solution is to just send a SQL request like this:

const markers = await this.markerRepo.query(
  `SELECT * , ST_Distance(ST_MakePoint(${yourLatitude}, ${yourLongitude} ), position) AS dist FROM marker ORDER BY dist LIMIT 10;`,
);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!