-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement exists query method (#9303)
Adding `Exists` method to query builder and EntityManager, to check whether a row exists given the conditions Closes: #2815 Co-authored-by: mortzprk <mortz.prk@gmail.com>
- Loading branch information
1 parent
53fad8f
commit 598e269
Showing
10 changed files
with
296 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
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
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 |
---|---|---|
|
@@ -451,6 +451,13 @@ export class Repository<Entity extends ObjectLiteral> { | |
) | ||
} | ||
|
||
/** | ||
* Checks whether any entity exists that match given options. | ||
*/ | ||
exist(options?: FindManyOptions<Entity>): Promise<boolean> { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
pleerock
Member
|
||
return this.manager.exists(this.metadata.target, options) | ||
} | ||
|
||
/** | ||
* Counts entities that match given options. | ||
* Useful for pagination. | ||
|
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 @@ | ||
import { Entity, PrimaryColumn } from "../../../../../src" | ||
|
||
@Entity("tests") | ||
export class Test { | ||
@PrimaryColumn() | ||
id: string | ||
} |
45 changes: 45 additions & 0 deletions
45
test/functional/query-builder/exists/query-builder-exists.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,45 @@ | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../../utils/test-utils" | ||
import { DataSource } from "../../../../src/data-source/DataSource" | ||
import { expect } from "chai" | ||
import { Test } from "./entity/Test" | ||
|
||
describe("query builder > exist", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [Test], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("Exists query of empty table should be false", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const repo = connection.getRepository(Test) | ||
|
||
const exist = await repo.exist() | ||
expect(exist).to.be.equal(false) | ||
}), | ||
)) | ||
|
||
it("Exists query of non empty table should be true", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const repo = connection.getRepository(Test) | ||
|
||
await repo.save({ id: "ok" }) | ||
await repo.save({ id: "nok" }) | ||
|
||
const exist = await repo.exist() | ||
expect(exist).to.be.equal(true) | ||
}), | ||
)) | ||
}) |
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
typo?