This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add multi-dimensional cube support for PostgreSQL (typeorm#4378)
- Loading branch information
1 parent
e12479e
commit b6d6278
Showing
5 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import "reflect-metadata"; | ||
import { expect } from "chai"; | ||
import { Connection } from "../../../../src/connection/Connection"; | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases | ||
} from "../../../utils/test-utils"; | ||
import { Post } from "./entity/Post"; | ||
|
||
describe("cube-postgres", () => { | ||
let connections: Connection[]; | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
enabledDrivers: ["postgres"] | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should create correct schema with Postgres' cube type", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const queryRunner = connection.createQueryRunner(); | ||
const schema = await queryRunner.getTable("post"); | ||
await queryRunner.release(); | ||
expect(schema).not.to.be.undefined; | ||
const cubeColumn = schema!.columns.find( | ||
tableColumn => | ||
tableColumn.name === "color" && | ||
tableColumn.type === "cube" | ||
); | ||
expect(cubeColumn).to.not.be.undefined; | ||
}) | ||
)); | ||
|
||
it("should persist cube correctly", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const color = [255, 0, 0]; | ||
const postRepo = connection.getRepository(Post); | ||
const post = new Post(); | ||
post.color = color; | ||
const persistedPost = await postRepo.save(post); | ||
const foundPost = await postRepo.findOne(persistedPost.id); | ||
expect(foundPost).to.exist; | ||
expect(foundPost!.color).to.deep.equal(color); | ||
}) | ||
)); | ||
|
||
it("should update cube correctly", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const color = [255, 0, 0]; | ||
const color2 = [0, 255, 0]; | ||
const postRepo = connection.getRepository(Post); | ||
const post = new Post(); | ||
post.color = color; | ||
const persistedPost = await postRepo.save(post); | ||
|
||
await postRepo.update( | ||
{ id: persistedPost.id }, | ||
{ color: color2 } | ||
); | ||
|
||
const foundPost = await postRepo.findOne(persistedPost.id); | ||
expect(foundPost).to.exist; | ||
expect(foundPost!.color).to.deep.equal(color2); | ||
}) | ||
)); | ||
|
||
it("should re-save cube correctly", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const color = [255, 0, 0]; | ||
const color2 = [0, 255, 0]; | ||
const postRepo = connection.getRepository(Post); | ||
const post = new Post(); | ||
post.color = color; | ||
const persistedPost = await postRepo.save(post); | ||
|
||
persistedPost.color = color2; | ||
await postRepo.save(persistedPost); | ||
|
||
const foundPost = await postRepo.findOne(persistedPost.id); | ||
expect(foundPost).to.exist; | ||
expect(foundPost!.color).to.deep.equal(color2); | ||
}) | ||
)); | ||
|
||
it("should be able to order cube by euclidean distance", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
const color1 = [255, 0, 0]; | ||
const color2 = [255, 255, 0]; | ||
const color3 = [255, 255, 255]; | ||
|
||
const post1 = new Post(); | ||
post1.color = color1; | ||
const post2 = new Post(); | ||
post2.color = color2; | ||
const post3 = new Post(); | ||
post3.color = color3; | ||
await connection.manager.save([post1, post2, post3]); | ||
|
||
const posts = await connection.manager | ||
.createQueryBuilder(Post, "post") | ||
.orderBy("color <-> '(0, 255, 0)'", "DESC") | ||
.getMany(); | ||
|
||
const postIds = posts.map(post => post.id); | ||
expect(postIds).to.deep.equal([post1.id, post3.id, post2.id]); | ||
}) | ||
)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Entity} from "../../../../../src/decorator/entity/Entity"; | ||
import {Column} from "../../../../../src/decorator/columns/Column"; | ||
|
||
@Entity() | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column("cube", { | ||
nullable: true | ||
}) | ||
color: number[]; | ||
} |