Skip to content

Commit

Permalink
feat: add name option to view column (#5962)
Browse files Browse the repository at this point in the history
Add 'name' option to @ViewColumn to align with @column
and allow a different property name for a column in a view.

Closes #5708
  • Loading branch information
dsbert committed May 16, 2020
1 parent 360122f commit 3cfcc50
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 22 deletions.
5 changes: 3 additions & 2 deletions src/decorator/columns/ViewColumn.ts
@@ -1,16 +1,17 @@
import {getMetadataArgsStorage} from "../../";
import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
import { ViewColumnOptions } from "../options/ViewColumnOptions";

/**
* ViewColumn decorator is used to mark a specific class property as a view column.
*/
export function ViewColumn(): Function {
export function ViewColumn(options?: ViewColumnOptions): Function {
return function (object: Object, propertyName: string) {
getMetadataArgsStorage().columns.push({
target: object.constructor,
propertyName: propertyName,
mode: "regular",
options: {}
options: options || {}
} as ColumnMetadataArgs);
};
}
9 changes: 9 additions & 0 deletions src/decorator/options/ViewColumnOptions.ts
@@ -0,0 +1,9 @@
/**
* Describes all view column's options.
*/
export interface ViewColumnOptions {
/**
* Column name in the database.
*/
name?: string;
}
Expand Up @@ -9,6 +9,7 @@ import {Photo} from "./Photo";
expression: (connection: Connection) => connection.createQueryBuilder()
.select("photo.id", "id")
.addSelect("photo.name", "name")
.addSelect("photo.albumId", "albumId")
.addSelect("category.name", "categoryName")
.addSelect("album.name", "albumName")
.from(Photo, "photo")
Expand All @@ -29,4 +30,7 @@ export class PhotoAlbumCategory {

@ViewColumn()
albumName: string;

@ViewColumn({ name: "albumId" })
photoAlbumId: number;
}
2 changes: 2 additions & 0 deletions test/functional/view-entity/general/view-entity-general.ts
Expand Up @@ -104,11 +104,13 @@ describe("view entity > general", () => {
photoAlbumCategories[1].albumName.should.be.equal("BMW photos");
photoAlbumCategories[1].categoryName.should.be.equal("Cars");

const albumId = connection.driver instanceof CockroachDriver ? "1" : 1;
const photoAlbumCategory = await connection.manager.findOne(PhotoAlbumCategory, { id: 1 });
photoAlbumCategory!.id.should.be.equal(photoId1);
photoAlbumCategory!.name.should.be.equal("BMW E39");
photoAlbumCategory!.albumName.should.be.equal("BMW photos");
photoAlbumCategory!.categoryName.should.be.equal("Cars");
photoAlbumCategory!.photoAlbumId.should.be.equal(albumId);

})));
});
4 changes: 2 additions & 2 deletions test/functional/view-entity/mssql/entity/PostCategory.ts
Expand Up @@ -11,8 +11,8 @@ export class PostCategory {
@ViewColumn()
id: number;

@ViewColumn()
name: string;
@ViewColumn({ name: "name" })
postName: string;

@ViewColumn()
categoryName: string;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/mssql/view-entity-mssql.ts
Expand Up @@ -51,12 +51,12 @@ describe("view entity > mssql", () => {

const postId1 = connection.driver instanceof CockroachDriver ? "1" : 1;
postCategories[0].id.should.be.equal(postId1);
postCategories[0].name.should.be.equal("About BMW");
postCategories[0].postName.should.be.equal("About BMW");
postCategories[0].categoryName.should.be.equal("Cars");

const postId2 = connection.driver instanceof CockroachDriver ? "2" : 2;
postCategories[1].id.should.be.equal(postId2);
postCategories[1].name.should.be.equal("About Boeing");
postCategories[1].postName.should.be.equal("About Boeing");
postCategories[1].categoryName.should.be.equal("Airplanes");

})));
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/mysql/entity/PostCategory.ts
Expand Up @@ -11,8 +11,8 @@ export class PostCategory {
@ViewColumn()
id: number;

@ViewColumn()
name: string;
@ViewColumn({ name: "name" })
postName: string;

@ViewColumn()
categoryName: string;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/mysql/view-entity-mysql.ts
Expand Up @@ -51,12 +51,12 @@ describe("view entity > mysql", () => {

const postId1 = connection.driver instanceof CockroachDriver ? "1" : 1;
postCategories[0].id.should.be.equal(postId1);
postCategories[0].name.should.be.equal("About BMW");
postCategories[0].postName.should.be.equal("About BMW");
postCategories[0].categoryName.should.be.equal("Cars");

const postId2 = connection.driver instanceof CockroachDriver ? "2" : 2;
postCategories[1].id.should.be.equal(postId2);
postCategories[1].name.should.be.equal("About Boeing");
postCategories[1].postName.should.be.equal("About Boeing");
postCategories[1].categoryName.should.be.equal("Airplanes");

})));
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/oracle/entity/PostCategory.ts
Expand Up @@ -11,8 +11,8 @@ export class PostCategory {
@ViewColumn()
id: number;

@ViewColumn()
name: string;
@ViewColumn({ name: "name" })
postName: string;

@ViewColumn()
categoryName: string;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/oracle/view-entity-oracle.ts
Expand Up @@ -51,12 +51,12 @@ describe("view entity > oracle", () => {

const postId1 = connection.driver instanceof CockroachDriver ? "1" : 1;
postCategories[0].id.should.be.equal(postId1);
postCategories[0].name.should.be.equal("About BMW");
postCategories[0].postName.should.be.equal("About BMW");
postCategories[0].categoryName.should.be.equal("Cars");

const postId2 = connection.driver instanceof CockroachDriver ? "2" : 2;
postCategories[1].id.should.be.equal(postId2);
postCategories[1].name.should.be.equal("About Boeing");
postCategories[1].postName.should.be.equal("About Boeing");
postCategories[1].categoryName.should.be.equal("Airplanes");

})));
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/postgres/entity/PostCategory.ts
Expand Up @@ -11,8 +11,8 @@ export class PostCategory {
@ViewColumn()
id: number;

@ViewColumn()
name: string;
@ViewColumn({ name: "name" })
postName: string;

@ViewColumn()
categoryName: string;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/postgres/view-entity-postgres.ts
Expand Up @@ -77,12 +77,12 @@ describe("view entity > postgres", () => {

const postId1 = connection.driver instanceof CockroachDriver ? "1" : 1;
postCategories[0].id.should.be.equal(postId1);
postCategories[0].name.should.be.equal("About BMW");
postCategories[0].postName.should.be.equal("About BMW");
postCategories[0].categoryName.should.be.equal("Cars");

const postId2 = connection.driver instanceof CockroachDriver ? "2" : 2;
postCategories[1].id.should.be.equal(postId2);
postCategories[1].name.should.be.equal("About Boeing");
postCategories[1].postName.should.be.equal("About Boeing");
postCategories[1].categoryName.should.be.equal("Airplanes");

})));
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/sqlite/entity/PostCategory.ts
Expand Up @@ -11,8 +11,8 @@ export class PostCategory {
@ViewColumn()
id: number;

@ViewColumn()
name: string;
@ViewColumn({ name: "name" })
postName: string;

@ViewColumn()
categoryName: string;
Expand Down
4 changes: 2 additions & 2 deletions test/functional/view-entity/sqlite/view-entity-sqlite.ts
Expand Up @@ -51,12 +51,12 @@ describe("view entity > sqlite", () => {

const postId1 = connection.driver instanceof CockroachDriver ? "1" : 1;
postCategories[0].id.should.be.equal(postId1);
postCategories[0].name.should.be.equal("About BMW");
postCategories[0].postName.should.be.equal("About BMW");
postCategories[0].categoryName.should.be.equal("Cars");

const postId2 = connection.driver instanceof CockroachDriver ? "2" : 2;
postCategories[1].id.should.be.equal(postId2);
postCategories[1].name.should.be.equal("About Boeing");
postCategories[1].postName.should.be.equal("About Boeing");
postCategories[1].categoryName.should.be.equal("Airplanes");

})));
Expand Down

0 comments on commit 3cfcc50

Please sign in to comment.