From 3cfcc5087f4be90cb5dd44898ad39b93b23077ac Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 16 May 2020 05:18:52 -0400 Subject: [PATCH] feat: add name option to view column (#5962) 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 ++ test/functional/view-entity/mssql/entity/PostCategory.ts | 4 ++-- test/functional/view-entity/mssql/view-entity-mssql.ts | 4 ++-- test/functional/view-entity/mysql/entity/PostCategory.ts | 4 ++-- test/functional/view-entity/mysql/view-entity-mysql.ts | 4 ++-- .../functional/view-entity/oracle/entity/PostCategory.ts | 4 ++-- test/functional/view-entity/oracle/view-entity-oracle.ts | 4 ++-- .../view-entity/postgres/entity/PostCategory.ts | 4 ++-- .../view-entity/postgres/view-entity-postgres.ts | 4 ++-- .../functional/view-entity/sqlite/entity/PostCategory.ts | 4 ++-- test/functional/view-entity/sqlite/view-entity-sqlite.ts | 4 ++-- 14 files changed, 38 insertions(+), 22 deletions(-) create mode 100644 src/decorator/options/ViewColumnOptions.ts diff --git a/src/decorator/columns/ViewColumn.ts b/src/decorator/columns/ViewColumn.ts index 998af7bfbd..e3df7bcd4d 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 0000000000..a2b132cd61 --- /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 1fea2be7ea..7bd97279d8 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 1e456a2f67..d1275d3ed6 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/mssql/entity/PostCategory.ts b/test/functional/view-entity/mssql/entity/PostCategory.ts index d3540166e2..975d5d83d2 100644 --- a/test/functional/view-entity/mssql/entity/PostCategory.ts +++ b/test/functional/view-entity/mssql/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/mssql/view-entity-mssql.ts b/test/functional/view-entity/mssql/view-entity-mssql.ts index 4d0a41b5a4..274f6d34a8 100644 --- a/test/functional/view-entity/mssql/view-entity-mssql.ts +++ b/test/functional/view-entity/mssql/view-entity-mssql.ts @@ -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"); }))); diff --git a/test/functional/view-entity/mysql/entity/PostCategory.ts b/test/functional/view-entity/mysql/entity/PostCategory.ts index 4b1df7d531..24be89ec28 100644 --- a/test/functional/view-entity/mysql/entity/PostCategory.ts +++ b/test/functional/view-entity/mysql/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/mysql/view-entity-mysql.ts b/test/functional/view-entity/mysql/view-entity-mysql.ts index 17a55aef28..4ff2e3c84d 100644 --- a/test/functional/view-entity/mysql/view-entity-mysql.ts +++ b/test/functional/view-entity/mysql/view-entity-mysql.ts @@ -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"); }))); diff --git a/test/functional/view-entity/oracle/entity/PostCategory.ts b/test/functional/view-entity/oracle/entity/PostCategory.ts index d3540166e2..975d5d83d2 100644 --- a/test/functional/view-entity/oracle/entity/PostCategory.ts +++ b/test/functional/view-entity/oracle/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/oracle/view-entity-oracle.ts b/test/functional/view-entity/oracle/view-entity-oracle.ts index 570d2c3afa..61cbc0f817 100644 --- a/test/functional/view-entity/oracle/view-entity-oracle.ts +++ b/test/functional/view-entity/oracle/view-entity-oracle.ts @@ -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"); }))); diff --git a/test/functional/view-entity/postgres/entity/PostCategory.ts b/test/functional/view-entity/postgres/entity/PostCategory.ts index d3540166e2..975d5d83d2 100644 --- a/test/functional/view-entity/postgres/entity/PostCategory.ts +++ b/test/functional/view-entity/postgres/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/postgres/view-entity-postgres.ts b/test/functional/view-entity/postgres/view-entity-postgres.ts index 4e9763f4cf..c034a4d746 100644 --- a/test/functional/view-entity/postgres/view-entity-postgres.ts +++ b/test/functional/view-entity/postgres/view-entity-postgres.ts @@ -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"); }))); diff --git a/test/functional/view-entity/sqlite/entity/PostCategory.ts b/test/functional/view-entity/sqlite/entity/PostCategory.ts index d3540166e2..975d5d83d2 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 31183cf3b3..cd1b0aef51 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"); })));