Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spatial support for PostgreSQL using PostGIS #2423

Merged
merged 18 commits into from Jul 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion test/functional/query-builder/order-by/entity/Post.ts
@@ -1,6 +1,7 @@
import {Entity} from "../../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {Index} from "../../../../../src";

@Entity({
orderBy: {
Expand All @@ -21,4 +22,8 @@ export class Post {
@Column()
num2: number = 1;

}
@Column({ type: "geometry" })
@Index({ spatial: true })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it broke the build; some of the before/after hooks are now hanging. Is there a good way to diagnose this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, it looks like introducing support for spatial indexes in Postgres is conflicts with tests that also execute in MySQL. I think I've just pushed a fix for this.

geom: object;

}
56 changes: 56 additions & 0 deletions test/functional/spatial/postgres/spatial-postgres.ts
Expand Up @@ -116,4 +116,60 @@ describe("spatial-postgres", () => {
expect(foundPost!.geog).to.deep.equal(geom);
})
));

it("should be able to order geometries by distance", () => Promise.all(connections.map(async connection => {

const geoJson1 = {
type: "Point",
coordinates: [
139.9341032213472,
36.80798008559315
]
};

const geoJson2 = {
type: "Point",
coordinates: [
139.933053,
36.805711
]
};

const origin = {
type: "Point",
coordinates: [
139.933227,
36.808005
]
};

const post1 = new Post();
post1.geom = geoJson1;

const post2 = new Post();
post2.geom = geoJson2;
await connection.manager.save([post1, post2]);

const posts1 = await connection.manager
.createQueryBuilder(Post, "post")
.where("ST_Distance(post.geom, ST_GeomFromGeoJSON(:origin)) > 0")
.orderBy({
"ST_Distance(post.geom, ST_GeomFromGeoJSON(:origin))": {
order: "ASC",
nulls: "NULLS FIRST"
}
})
.setParameters({ origin: JSON.stringify(origin) })
.getMany();

const posts2 = await connection.manager
.createQueryBuilder(Post, "post")
.orderBy("ST_Distance(post.geom, ST_GeomFromGeoJSON(:origin))", "DESC")
.setParameters({ origin: JSON.stringify(origin) })
.getMany();

expect(posts1[0].id).to.be.equal(post1.id);
expect(posts2[0].id).to.be.equal(post2.id);
})));

});