From e8fac66b1130342533c81999451119ced282913d Mon Sep 17 00:00:00 2001 From: dsbert Date: Mon, 27 Apr 2020 22:05:17 -0400 Subject: [PATCH] feat: add name option to view column Add 'name' option to @ViewColumn to align with @Column and allow a different property name for a column in a view. Closes #5708 --- src/decorator/columns/ViewColumn.ts | 5 +++-- src/decorator/options/ViewColumnOptions.ts | 9 +++++++++ .../view-entity/general/entity/PhotoAlbumCategory.ts | 4 ++++ .../view-entity/general/view-entity-general.ts | 2 ++ .../functional/view-entity/sqlite/entity/PostCategory.ts | 4 ++-- test/functional/view-entity/sqlite/view-entity-sqlite.ts | 4 ++-- 6 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 src/decorator/options/ViewColumnOptions.ts diff --git a/src/decorator/columns/ViewColumn.ts b/src/decorator/columns/ViewColumn.ts index 998af7bfbd2..0c733f1a15a 100644 --- a/src/decorator/columns/ViewColumn.ts +++ b/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); }; } diff --git a/src/decorator/options/ViewColumnOptions.ts b/src/decorator/options/ViewColumnOptions.ts new file mode 100644 index 00000000000..99e1888aa54 --- /dev/null +++ b/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; +} diff --git a/test/functional/view-entity/general/entity/PhotoAlbumCategory.ts b/test/functional/view-entity/general/entity/PhotoAlbumCategory.ts index 1fea2be7ea9..7bd97279d8d 100644 --- a/test/functional/view-entity/general/entity/PhotoAlbumCategory.ts +++ b/test/functional/view-entity/general/entity/PhotoAlbumCategory.ts @@ -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") @@ -29,4 +30,7 @@ export class PhotoAlbumCategory { @ViewColumn() albumName: string; + + @ViewColumn({ name: "albumId" }) + photoAlbumId: number; } diff --git a/test/functional/view-entity/general/view-entity-general.ts b/test/functional/view-entity/general/view-entity-general.ts index 1e456a2f67a..d1275d3ed6a 100644 --- a/test/functional/view-entity/general/view-entity-general.ts +++ b/test/functional/view-entity/general/view-entity-general.ts @@ -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); }))); }); diff --git a/test/functional/view-entity/sqlite/entity/PostCategory.ts b/test/functional/view-entity/sqlite/entity/PostCategory.ts index d3540166e23..975d5d83d21 100644 --- a/test/functional/view-entity/sqlite/entity/PostCategory.ts +++ b/test/functional/view-entity/sqlite/entity/PostCategory.ts @@ -11,8 +11,8 @@ export class PostCategory { @ViewColumn() id: number; - @ViewColumn() - name: string; + @ViewColumn({ name: "name" }) + postName: string; @ViewColumn() categoryName: string; diff --git a/test/functional/view-entity/sqlite/view-entity-sqlite.ts b/test/functional/view-entity/sqlite/view-entity-sqlite.ts index 31183cf3b35..cd1b0aef514 100644 --- a/test/functional/view-entity/sqlite/view-entity-sqlite.ts +++ b/test/functional/view-entity/sqlite/view-entity-sqlite.ts @@ -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"); })));