feat: add name option to view column (#5962) · typeorm/typeorm@3cfcc50

File tree

14 files changed

lines changed

  • test/functional/view-entity

14 files changed

lines changed

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+

}

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

}

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

});

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;

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

})));

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;

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

})));

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;

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

})));