Skip to content

Commit 14a64a3

Browse files
committed
test: update functional tests for VirtualColumn
1 parent 9599054 commit 14a64a3

File tree

10 files changed

+212
-188
lines changed

10 files changed

+212
-188
lines changed

docs/decorator-reference.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,20 +381,20 @@ Special column that is never saved to the database and thus acts as a readonly p
381381
Each time you call `find` or `findOne` from the entity manager, the value is recalculated based on the query function that was provided in the VirtualColumn Decorator. The alias argument passed to the query references the exact entity alias of the generated query behind the scenes.
382382

383383
```typescript
384-
@Entity({ name: "companies", alias: "COMP" })
385-
export class Company extends BaseEntity {
384+
@Entity({ name: "companies" })
385+
export class Company {
386386
@PrimaryColumn("varchar", { length: 50 })
387387
name: string;
388388

389-
@VirtualColumn({ query: (alias) => `SELECT COUNT("name") FROM "employees" WHERE "companyName" = ${alias}.name` })
389+
@VirtualColumn({ query: (alias) => `SELECT COUNT("name") FROM "employees" WHERE "companyName" = ${alias}."name"` })
390390
totalEmployeesCount: number;
391391

392392
@OneToMany((type) => Employee, (employee) => employee.company)
393393
employees: Employee[];
394394
}
395395

396396
@Entity({ name: "employees" })
397-
export class Employee extends BaseEntity {
397+
export class Employee {
398398
@PrimaryColumn("varchar", { length: 50 })
399399
name: string;
400400

src/decorator/relations/RelationCount.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { SelectQueryBuilder } from "../../query-builder/SelectQueryBuilder"
55
/**
66
* Holds a number of children in the closure table of the column.
77
*
8-
* @deprecated Do not use this decorator, it may be removed in the future versions
8+
* @deprecated This decorator will removed in the future versions.
9+
* Use {@link VirtualColumn} to calculate the count instead.
910
*/
1011
export function RelationCount<T>(
1112
relation: string | ((object: T) => any),
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import {
2-
ManyToOne,
2+
Column,
33
Entity,
4-
BaseEntity,
4+
ManyToOne,
55
PrimaryGeneratedColumn,
6-
Column,
7-
} from "../../../../src"
8-
import TimeSheet from "./TimeSheet"
6+
} from "../../../../../src"
7+
import { TimeSheet } from "./TimeSheet"
98

109
@Entity({ name: "activities" })
11-
export default class Activity extends BaseEntity {
10+
export class Activity {
1211
@PrimaryGeneratedColumn()
1312
id: number
1413

1514
@Column("int")
1615
hours: number
1716

18-
@ManyToOne((type) => TimeSheet, (timesheet) => timesheet.activities)
17+
@ManyToOne(() => TimeSheet, (timesheet) => timesheet.activities)
1918
timesheet: TimeSheet
2019
}

test/github-issues/9323/entity/Company.ts renamed to test/functional/columns/virtual-columns/entity/Company.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ import {
33
OneToMany,
44
PrimaryColumn,
55
VirtualColumn,
6-
BaseEntity,
7-
} from "../../../../src"
8-
import Employee from "./Employee"
6+
} from "../../../../../src"
7+
import { Employee } from "./Employee"
98

109
@Entity({ name: "companies" })
11-
export default class Company extends BaseEntity {
10+
export class Company {
1211
@PrimaryColumn("varchar", { length: 50 })
1312
name: string
1413

1514
@VirtualColumn({
1615
query: (alias) =>
17-
`SELECT COUNT("name") FROM "employees" WHERE "companyName" = ${alias}.name`,
16+
`SELECT COUNT("name") FROM "employees" WHERE "companyName" = ${alias}."name"`,
1817
})
1918
totalEmployeesCount: number
2019

21-
@OneToMany((type) => Employee, (employee) => employee.company)
20+
@OneToMany(() => Employee, (employee) => employee.company)
2221
employees: Employee[]
2322
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
Entity,
3+
ManyToOne,
4+
OneToMany,
5+
PrimaryColumn
6+
} from "../../../../../src"
7+
import { Company } from "./Company"
8+
import { TimeSheet } from "./TimeSheet"
9+
10+
@Entity({ name: "employees" })
11+
export class Employee {
12+
@PrimaryColumn("varchar", { length: 50 })
13+
name: string
14+
15+
@ManyToOne(() => Company, (company) => company.employees)
16+
company: Company
17+
18+
@OneToMany(() => TimeSheet, (timesheet) => timesheet.employee)
19+
timesheets: TimeSheet[]
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
Entity,
3+
ManyToOne,
4+
PrimaryGeneratedColumn,
5+
VirtualColumn,
6+
} from "../../../../../src"
7+
import { Activity } from "./Activity"
8+
import { Employee } from "./Employee"
9+
10+
@Entity({ name: "timesheets" })
11+
export class TimeSheet {
12+
@PrimaryGeneratedColumn()
13+
id: number
14+
15+
@VirtualColumn({
16+
query: (alias) =>
17+
`SELECT SUM("hours") FROM "activities" WHERE "timesheetId" = ${alias}."id"`,
18+
})
19+
totalActivityHours: number
20+
21+
@ManyToOne(() => Activity, (activity) => activity.timesheet)
22+
activities: Activity[]
23+
24+
@ManyToOne(() => Employee, (employee) => employee.timesheets)
25+
employee: Employee
26+
}

0 commit comments

Comments
 (0)