Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

postgis - Get entities within 100km with postgresql and typeorm ordered

I get this error Cannot read property 'databaseName' of undefined

https://github.com/typeorm/typeorm/issues/4270

I follow github issue but it not help, the query works correctly If I delete addOrderBy method but I need the results ordered by the distance so there is a way to make this work ?

Entity

@Entity('restaurant')
export class RestaurantEntity extends AbstractEntity {
  @Column()
  name: string;

  @Column()
  description: string;

  @OneToMany(() => MenuEntity, (menu) => menu.restaurant, {
    onDelete: 'CASCADE',
    cascade: true,
  })
  menus: MenuEntity[];

  @Column()
  type: string;

  @Column()
  location: string;

  @Column('geometry', {
    name: 'geo',
    nullable: true,
    spatialFeatureType: 'Point',
  })
  geoLocation: object;

  @Column({
    nullable: true,
  })
  likes: number;

  @OneToMany(
    () => RestaurantImageEntity,
    (restaurantImage) => restaurantImage.restaurant,
    {
      onDelete: 'CASCADE',
      cascade: true,
    },
  )
  restaurantImages: RestaurantImageEntity[];

  @Column({
    nullable: true,
  })
  views: number;

  @Column({
    nullable: true,
  })
  totalFavorites: number;

  @Column({
    nullable: true,
  })
  telephone: string;

  @Column({
    nullable: true,
  })
  web: string;

  @Column({
    nullable: true,
  })
  email: string;

  @ManyToOne(() => UserEntity, (user) => user.restaurants, {
    onDelete: 'CASCADE',
  })
  creator: UserEntity;
}

Query

const query.geoLocation = [x,y];
restaurants = await this.restaurantRepository
        .createQueryBuilder('restaurant')
        .leftJoinAndSelect('restaurant.restaurantImages', 'restaurantImages')
        .where(ST_DWithin(restaurant.geoLocation, ST_MakePoint(${query.geoLocation[0]},${query.geoLocation[1]})::geography, 100000))
        .orderBy(sort ? sort : { 'restaurant.id': 'DESC' })
        .addOrderBy(`restaurant.geoLocation <-> ST_MakePoint(${query.geoLocation[0]},${query.geoLocation[1]})::geography`)
        .skip((page - 1) * perPage)
        .take(perPage)
        .getMany();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...