-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for SQL aggregate functions SUM, AVG, MIN, and MAX to t…
…he Repository API (#9737) * feat: Add support for SQL aggregate functions SUM, AVG, MIN, and MAX to the Repository API * rename field name to make tests work in oracle * fix the comments * update the docs * escape column name * address PR comment * format the code
- Loading branch information
Showing
7 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Pick only the keys that match the Type `U` | ||
*/ | ||
export type PickKeysByType<T, U> = string & | ||
keyof { | ||
[P in keyof T as T[P] extends U ? P : never]: T[P] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
test/functional/repository/aggregate-methods/entity/Post.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Entity } from "../../../../../src/decorator/entity/Entity" | ||
import { Column } from "../../../../../src/decorator/columns/Column" | ||
import { PrimaryColumn } from "../../../../../src/decorator/columns/PrimaryColumn" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryColumn() | ||
id: number | ||
|
||
@Column() | ||
counter: number | ||
} |
87 changes: 87 additions & 0 deletions
87
test/functional/repository/aggregate-methods/repository-aggregate-methods.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import "reflect-metadata" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
} from "../../../utils/test-utils" | ||
import { Repository } from "../../../../src/repository/Repository" | ||
import { DataSource } from "../../../../src/data-source/DataSource" | ||
import { Post } from "./entity/Post" | ||
import { LessThan } from "../../../../src" | ||
import { expect } from "chai" | ||
|
||
describe("repository > aggregate methods", () => { | ||
debugger | ||
let connections: DataSource[] | ||
let repository: Repository<Post> | ||
|
||
before(async () => { | ||
connections = await createTestingConnections({ | ||
entities: [Post], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
}) | ||
repository = connections[0].getRepository(Post) | ||
for (let i = 0; i < 100; i++) { | ||
const post = new Post() | ||
post.id = i | ||
post.counter = i + 1 | ||
await repository.save(post) | ||
} | ||
}) | ||
|
||
after(() => closeTestingConnections(connections)) | ||
|
||
describe("sum", () => { | ||
it("should return the aggregate sum", async () => { | ||
const sum = await repository.sum("counter") | ||
expect(sum).to.equal(5050) | ||
}) | ||
|
||
it("should return null when 0 rows match the query", async () => { | ||
const sum = await repository.sum("counter", { id: LessThan(0) }) | ||
expect(sum).to.be.null | ||
}) | ||
}) | ||
|
||
describe("average", () => { | ||
it("should return the aggregate average", async () => { | ||
const average = await repository.average("counter") | ||
expect(average).to.equal(50.5) | ||
}) | ||
|
||
it("should return null when 0 rows match the query", async () => { | ||
const average = await repository.average("counter", { | ||
id: LessThan(0), | ||
}) | ||
expect(average).to.be.null | ||
}) | ||
}) | ||
|
||
describe("minimum", () => { | ||
it("should return the aggregate minimum", async () => { | ||
const minimum = await repository.minimum("counter") | ||
expect(minimum).to.equal(1) | ||
}) | ||
|
||
it("should return null when 0 rows match the query", async () => { | ||
const minimum = await repository.minimum("counter", { | ||
id: LessThan(0), | ||
}) | ||
expect(minimum).to.be.null | ||
}) | ||
}) | ||
|
||
describe("maximum", () => { | ||
it("should return the aggregate maximum", async () => { | ||
const maximum = await repository.maximum("counter") | ||
expect(maximum).to.equal(100) | ||
}) | ||
|
||
it("should return null when 0 rows match the query", async () => { | ||
const maximum = await repository.maximum("counter", { | ||
id: LessThan(0), | ||
}) | ||
expect(maximum).to.be.null | ||
}) | ||
}) | ||
}) |