View Entities

What is View Entity?

View entity is a class that maps to a database view. You can create a view entity by defining a new class and mark it with @ViewEntity():
@ViewEntity() accepts following options:
  • name - view name. If not specified, then view name is generated from entity class name.
  • database - database name in selected DB server.
  • schema - schema name.
  • expression - view definition. Required parameter.
  • dependsOn - List of other views on which the current views depends. If your view uses another view in it's definition, you can add it here so that migrations are generated in the correct order.
expression can be string with properly escaped columns and tables, depend on database used (postgres in example):
1
@ViewEntity({
2
expression: `
3
SELECT "post"."id" AS "id", "post"."name" AS "name", "category"."name" AS "categoryName"
4
FROM "post" "post"
5
LEFT JOIN "category" "category" ON "post"."categoryId" = "category"."id"
6
`
7
})
Copied!
or an instance of QueryBuilder
1
@ViewEntity({
2
expression: (connection: Connection) => connection.createQueryBuilder()
3
.select("post.id", "id")
4
.addSelect("post.name", "name")
5
.addSelect("category.name", "categoryName")
6
.from(Post, "post")
7
.leftJoin(Category, "category", "category.id = post.categoryId")
8
})
Copied!
Note: parameter binding is not supported due to drivers limitations. Use the literal parameters instead.
1
@ViewEntity({
2
expression: (connection: Connection) => connection.createQueryBuilder()
3
.select("post.id", "id")
4
.addSelect("post.name", "name")
5
.addSelect("category.name", "categoryName")
6
.from(Post, "post")
7
.leftJoin(Category, "category", "category.id = post.categoryId")
8
.where("category.name = :name", { name: "Cars" }) // <-- this is wrong
9
.where("category.name = 'Cars'") // <-- and this is right
10
})
Copied!
Each view entity must be registered in your connection options:
1
import {createConnection, Connection} from "typeorm";
2
import {UserView} from "./entity/UserView";
3
​
4
const connection: Connection = await createConnection({
5
type: "mysql",
6
host: "localhost",
7
port: 3306,
8
username: "test",
9
password: "test",
10
database: "test",
11
entities: [UserView]
12
});
Copied!
Or you can specify the whole directory with all entities inside - and all of them will be loaded:
1
import {createConnection, Connection} from "typeorm";
2
​
3
const connection: Connection = await createConnection({
4
type: "mysql",
5
host: "localhost",
6
port: 3306,
7
username: "test",
8
password: "test",
9
database: "test",
10
entities: ["entity/*.js"]
11
});
Copied!

View Entity columns

To map data from view into the correct entity columns you must mark entity columns with @ViewColumn() decorator and specify these columns as select statement aliases.
example with string expression definition:
1
import {ViewEntity, ViewColumn} from "typeorm";
2
​
3
@ViewEntity({
4
expression: `
5
SELECT "post"."id" AS "id", "post"."name" AS "name", "category"."name" AS "categoryName"
6
FROM "post" "post"
7
LEFT JOIN "category" "category" ON "post"."categoryId" = "category"."id"
8
`
9
})
10
export class PostCategory {
11
​
12
@ViewColumn()
13
id: number;
14
​
15
@ViewColumn()
16
name: string;
17
​
18
@ViewColumn()
19
categoryName: string;
20
​
21
}
Copied!
example using QueryBuilder:
1
import {ViewEntity, ViewColumn} from "typeorm";
2
​
3
@ViewEntity({
4
expression: (connection: Connection) => connection.createQueryBuilder()
5
.select("post.id", "id")
6
.addSelect("post.name", "name")
7
.addSelect("category.name", "categoryName")
8
.from(Post, "post")
9
.leftJoin(Category, "category", "category.id = post.categoryId")
10
})
11
export class PostCategory {
12
​
13
@ViewColumn()
14
id: number;
15
​
16
@ViewColumn()
17
name: string;
18
​
19
@ViewColumn()
20
categoryName: string;
21
​
22
}
Copied!

Complete example

Let create two entities and a view containing aggregated data from these entities:
1
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm";
2
​
3
@Entity()
4
export class Category {
5
​
6
@PrimaryGeneratedColumn()
7
id: number;
8
​
9
@Column()
10
name: string;
11
​
12
}
Copied!
1
import {Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn} from "typeorm";
2
import {Category} from "./Category";
3
​
4
@Entity()
5
export class Post {
6
​
7
@PrimaryGeneratedColumn()
8
id: number;
9
​
10
@Column()
11
name: string;
12
​
13
@Column()
14
categoryId: number;
15
​
16
@ManyToOne(() => Category)
17
@JoinColumn({ name: "categoryId" })
18
category: Category;
19
​
20
}
Copied!
1
import {ViewEntity, ViewColumn, Connection} from "typeorm";
2
​
3
@ViewEntity({
4
expression: (connection: Connection) => connection.createQueryBuilder()
5
.select("post.id", "id")
6
.addSelect("post.name", "name")
7
.addSelect("category.name", "categoryName")
8
.from(Post, "post")
9
.leftJoin(Category, "category", "category.id = post.categoryId")
10
})
11
export class PostCategory {
12
​
13
@ViewColumn()
14
id: number;
15
​
16
@ViewColumn()
17
name: string;
18
​
19
@ViewColumn()
20
categoryName: string;
21
​
22
}
Copied!
then fill these tables with data and request all data from PostCategory view:
1
import {getManager} from "typeorm";
2
import {Category} from "./entity/Category";
3
import {Post} from "./entity/Post";
4
import {PostCategory} from "./entity/PostCategory";
5
​
6
const entityManager = getManager();
7
​
8
const category1 = new Category();
9
category1.name = "Cars";
10
await entityManager.save(category1);
11
​
12
const category2 = new Category();
13
category2.name = "Airplanes";
14
await entityManager.save(category2);
15
​
16
const post1 = new Post();
17
post1.name = "About BMW";
18
post1.categoryId = category1.id;
19
await entityManager.save(post1);
20
​
21
const post2 = new Post();
22
post2.name = "About Boeing";
23
post2.categoryId = category2.id;
24
await entityManager.save(post2);
25
​
26
const postCategories = await entityManager.find(PostCategory);
27
const postCategory = await entityManager.findOne(PostCategory, { id: 1 });
Copied!
the result in postCategories will be:
1
[ PostCategory { id: 1, name: 'About BMW', categoryName: 'Cars' },
2
PostCategory { id: 2, name: 'About Boeing', categoryName: 'Airplanes' } ]
Copied!
and in postCategory:
1
PostCategory { id: 1, name: 'About BMW', categoryName: 'Cars' }
Copied!