Skip to content

Commit 16184ef

Browse files
committed
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 typeorm#5708
1 parent e92c743 commit 16184ef

File tree

14 files changed

+38
-22
lines changed

14 files changed

+38
-22
lines changed

src/decorator/columns/ViewColumn.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import {getMetadataArgsStorage} from "../../";
22
import {ColumnMetadataArgs} from "../../metadata-args/ColumnMetadataArgs";
3+
import { ViewColumnOptions } from "../options/ViewColumnOptions";
34

45
/**
56
* ViewColumn decorator is used to mark a specific class property as a view column.
67
*/
7-
export function ViewColumn(): Function {
8+
export function ViewColumn(options?: ViewColumnOptions): Function {
89
return function (object: Object, propertyName: string) {
910
getMetadataArgsStorage().columns.push({
1011
target: object.constructor,
1112
propertyName: propertyName,
1213
mode: "regular",
13-
options: {}
14+
options: options || {}
1415
} as ColumnMetadataArgs);
1516
};
1617
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Describes all view column's options.
3+
*/
4+
export interface ViewColumnOptions {
5+
/**
6+
* Column name in the database.
7+
*/
8+
name?: string;
9+
}

test/functional/view-entity/general/entity/PhotoAlbumCategory.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {Photo} from "./Photo";
99
expression: (connection: Connection) => connection.createQueryBuilder()
1010
.select("photo.id", "id")
1111
.addSelect("photo.name", "name")
12+
.addSelect("photo.albumId", "albumId")
1213
.addSelect("category.name", "categoryName")
1314
.addSelect("album.name", "albumName")
1415
.from(Photo, "photo")
@@ -29,4 +30,7 @@ export class PhotoAlbumCategory {
2930

3031
@ViewColumn()
3132
albumName: string;
33+
34+
@ViewColumn({ name: "albumId" })
35+
photoAlbumId: number;
3236
}

test/functional/view-entity/general/view-entity-general.ts

+2
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ describe("view entity > general", () => {
104104
photoAlbumCategories[1].albumName.should.be.equal("BMW photos");
105105
photoAlbumCategories[1].categoryName.should.be.equal("Cars");
106106

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

113115
})));
114116
});

test/functional/view-entity/mssql/entity/PostCategory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class PostCategory {
1111
@ViewColumn()
1212
id: number;
1313

14-
@ViewColumn()
15-
name: string;
14+
@ViewColumn({ name: "name" })
15+
postName: string;
1616

1717
@ViewColumn()
1818
categoryName: string;

test/functional/view-entity/mssql/view-entity-mssql.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ describe("view entity > mssql", () => {
5151

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

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

6262
})));

test/functional/view-entity/mysql/entity/PostCategory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class PostCategory {
1111
@ViewColumn()
1212
id: number;
1313

14-
@ViewColumn()
15-
name: string;
14+
@ViewColumn({ name: "name" })
15+
postName: string;
1616

1717
@ViewColumn()
1818
categoryName: string;

test/functional/view-entity/mysql/view-entity-mysql.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ describe("view entity > mysql", () => {
5151

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

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

6262
})));

test/functional/view-entity/oracle/entity/PostCategory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class PostCategory {
1111
@ViewColumn()
1212
id: number;
1313

14-
@ViewColumn()
15-
name: string;
14+
@ViewColumn({ name: "name" })
15+
postName: string;
1616

1717
@ViewColumn()
1818
categoryName: string;

test/functional/view-entity/oracle/view-entity-oracle.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ describe("view entity > oracle", () => {
5151

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

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

6262
})));

test/functional/view-entity/postgres/entity/PostCategory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class PostCategory {
1111
@ViewColumn()
1212
id: number;
1313

14-
@ViewColumn()
15-
name: string;
14+
@ViewColumn({ name: "name" })
15+
postName: string;
1616

1717
@ViewColumn()
1818
categoryName: string;

test/functional/view-entity/postgres/view-entity-postgres.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ describe("view entity > postgres", () => {
7777

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

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

8888
})));

test/functional/view-entity/sqlite/entity/PostCategory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export class PostCategory {
1111
@ViewColumn()
1212
id: number;
1313

14-
@ViewColumn()
15-
name: string;
14+
@ViewColumn({ name: "name" })
15+
postName: string;
1616

1717
@ViewColumn()
1818
categoryName: string;

test/functional/view-entity/sqlite/view-entity-sqlite.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ describe("view entity > sqlite", () => {
5151

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

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

6262
})));

0 commit comments

Comments
 (0)