typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.6k stars 6.22k forks source link

Repository.prototype.save results for geography types are not consistent with find methods #10852

Open mkhstar opened 1 month ago

mkhstar commented 1 month ago

Issue description

Geography column types such as geometry, point etc save method returns a hex representation of the geography type in the database, while .find methods return the json representation of it

Expected Behavior

I expect .save with method results to be consistent with what is returned in the find methods. I would like to also get json when i use .save method

Actual Behavior

A hex representation of the geography type is returned.

Steps to reproduce

Given the entity below

import type { Geometry } from "geojson";

@Entity 
class MyEntity{

@PrimaryGeneratedColumn()
 public id: number;

  @Column({
    type: "geography",
    spatialFeatureType: "Geometry",
    srid: 4326,
    nullable: true,
    default: null,
  })
  public geometry: Geometry | null;
}

Example usage below with expectation and actual

const myEntityRepository = datasource.getRepository(MyEntity);

const polygon = {
  type: "Polygon",
  coordinates: [
    [
      [15.308124039486808, 49.80101227755168],
      [15.322886917904777, 49.79735581080685],
      [15.317222092465324, 49.788268849425094],
      [15.30108592303173, 49.78926628214111],
    ],
  ],
};

const saved = await myEntityRepository.save({ geometry: polygon });
console.log(saved.geometry) // Returns hex eg. 0101000020E61000003D0AD7A3703D28409A99999999B94640, i expect exact value polygon declared above

const found = await myEntityRepository.findOne({id: 1});

console.log(found.geometry) // Returns exact value like polygon declared above

My Environment

Dependency Version
Operating System macos
Node.js version v20.12.0
Typescript version 5.4.3
TypeORM version ^0.3.20

Additional Context

I can see from the tests here that typeorm is aware of this https://github.com/typeorm/typeorm/blob/master/test/functional/spatial/postgres/spatial-postgres.ts#L184. As they assign the value to the result of save.

Relevant Database Driver(s)

Are you willing to resolve this issue by submitting a Pull Request?

No