Skip to content

Commit b6b46fb

Browse files
authored
feat: add exists and exists by (#10291)
This PR deprecates `exist` in favor of `exists` and `existsBy`.
1 parent cf7147f commit b6b46fb

File tree

7 files changed

+102
-17
lines changed

7 files changed

+102
-17
lines changed

docs/entity-manager-api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ await manager.increment(User, { firstName: "Timber" }, "age", 3)
183183
await manager.decrement(User, { firstName: "Timber" }, "age", 3)
184184
```
185185

186+
- `exists` - Check whether any entity exists that matches `FindOptions`.
187+
188+
```typescript
189+
const exists = await manager.exists(User, {
190+
where: {
191+
firstName: "Timber",
192+
},
193+
})
194+
```
195+
196+
- `existsBy` - Checks whether any entity exists that matches `FindOptionsWhere`.
197+
198+
```typescript
199+
const exists = await manager.existsBy(User, { firstName: "Timber" })
200+
```
201+
186202
- `count` - Counts entities that match `FindOptions`. Useful for pagination.
187203

188204
```typescript

docs/repository-api.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ await repository.increment({ firstName: "Timber" }, "age", 3)
257257
await repository.decrement({ firstName: "Timber" }, "age", 3)
258258
```
259259

260+
- `exists` - Check whether any entity exists that matches `FindOptions`.
261+
262+
```typescript
263+
const exists = await repository.exists({
264+
where: {
265+
firstName: "Timber",
266+
},
267+
})
268+
```
269+
270+
- `existsBy` - Checks whether any entity exists that matches `FindOptionsWhere`.
271+
272+
```typescript
273+
const exists = await repository.existsBy({ firstName: "Timber" })
274+
```
275+
260276
- `count` - Counts entities that match `FindOptions`. Useful for pagination.
261277

262278
```typescript

src/entity-manager/EntityManager.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ export class EntityManager {
955955
}
956956

957957
/**
958-
* Checks whether any entity exists with the given condition
958+
* Checks whether any entity exists with the given options.
959959
*/
960960
exists<Entity extends ObjectLiteral>(
961961
entityClass: EntityTarget<Entity>,
@@ -971,6 +971,19 @@ export class EntityManager {
971971
.getExists()
972972
}
973973

974+
/**
975+
* Checks whether any entity exists with the given conditions.
976+
*/
977+
async existsBy<Entity extends ObjectLiteral>(
978+
entityClass: EntityTarget<Entity>,
979+
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
980+
): Promise<boolean> {
981+
const metadata = this.connection.getMetadata(entityClass)
982+
return this.createQueryBuilder(entityClass, metadata.name)
983+
.setFindOptions({ where })
984+
.getExists()
985+
}
986+
974987
/**
975988
* Counts entities that match given options.
976989
* Useful for pagination.

src/repository/BaseEntity.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,26 @@ export class BaseEntity {
389389
return this.getRepository<T>().delete(criteria)
390390
}
391391

392+
/**
393+
* Checks whether any entity exists that matches the given options.
394+
*/
395+
static exists<T extends BaseEntity>(
396+
this: { new (): T } & typeof BaseEntity,
397+
options?: FindManyOptions<T>,
398+
): Promise<boolean> {
399+
return this.getRepository<T>().exists(options)
400+
}
401+
402+
/**
403+
* Checks whether any entity exists that matches the given conditions.
404+
*/
405+
static existsBy<T extends BaseEntity>(
406+
this: { new (): T } & typeof BaseEntity,
407+
where: FindOptionsWhere<T>,
408+
): Promise<boolean> {
409+
return this.getRepository<T>().existsBy(where)
410+
}
411+
392412
/**
393413
* Counts entities that match given options.
394414
*/

src/repository/Repository.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,32 @@ export class Repository<Entity extends ObjectLiteral> {
453453
}
454454

455455
/**
456-
* Checks whether any entity exists that match given options.
456+
* Checks whether any entity exists that matches the given options.
457+
*
458+
* @deprecated use `exists` method instead, for example:
459+
*
460+
* .exists()
457461
*/
458462
exist(options?: FindManyOptions<Entity>): Promise<boolean> {
459463
return this.manager.exists(this.metadata.target, options)
460464
}
461465

466+
/**
467+
* Checks whether any entity exists that matches the given options.
468+
*/
469+
exists(options?: FindManyOptions<Entity>): Promise<boolean> {
470+
return this.manager.exists(this.metadata.target, options)
471+
}
472+
473+
/**
474+
* Checks whether any entity exists that matches the given conditions.
475+
*/
476+
existsBy(
477+
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
478+
): Promise<boolean> {
479+
return this.manager.existsBy(this.metadata.target, where)
480+
}
481+
462482
/**
463483
* Counts entities that match given options.
464484
* Useful for pagination.

test/functional/query-builder/exists/query-builder-exists.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DataSource } from "../../../../src/data-source/DataSource"
77
import { expect } from "chai"
88
import { Test } from "./entity/Test"
99

10-
describe("query builder > exist", () => {
10+
describe("query builder > exists", () => {
1111
let connections: DataSource[]
1212
before(
1313
async () =>
@@ -25,8 +25,8 @@ describe("query builder > exist", () => {
2525
connections.map(async (connection) => {
2626
const repo = connection.getRepository(Test)
2727

28-
const exist = await repo.exist()
29-
expect(exist).to.be.equal(false)
28+
const exists = await repo.exists()
29+
expect(exists).to.be.equal(false)
3030
}),
3131
))
3232

@@ -38,8 +38,8 @@ describe("query builder > exist", () => {
3838
await repo.save({ id: "ok" })
3939
await repo.save({ id: "nok" })
4040

41-
const exist = await repo.exist()
42-
expect(exist).to.be.equal(true)
41+
const exists = await repo.exists()
42+
expect(exists).to.be.equal(true)
4343
}),
4444
))
4545
})

test/functional/repository/find-methods/repostiory-find-methods.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ describe("repository > find methods", () => {
149149
await postRepository.save(post)
150150
}
151151

152-
// check exist method
153-
const exists = await postRepository.exist({
152+
// check exists method
153+
const exists = await postRepository.exists({
154154
order: { id: "ASC" },
155155
})
156156
exists.should.be.equal(true)
@@ -169,8 +169,8 @@ describe("repository > find methods", () => {
169169
await postRepository.save(post)
170170
}
171171

172-
// check exist method
173-
const exists = await postRepository.exist({
172+
// check exists method
173+
const exists = await postRepository.exists({
174174
where: { categoryName: "odd" },
175175
order: { id: "ASC" },
176176
})
@@ -191,8 +191,8 @@ describe("repository > find methods", () => {
191191
await postRepository.save(post)
192192
}
193193

194-
// check exist method
195-
const exists = await postRepository.exist({
194+
// check exists method
195+
const exists = await postRepository.exists({
196196
where: { categoryName: "odd", isNew: true },
197197
order: { id: "ASC" },
198198
})
@@ -215,8 +215,8 @@ describe("repository > find methods", () => {
215215
await postRepository.save(post)
216216
}
217217

218-
// check exist method
219-
const exists = await postRepository.exist()
218+
// check exists method
219+
const exists = await postRepository.exists()
220220
exists.should.be.equal(true)
221221
}),
222222
))
@@ -236,8 +236,8 @@ describe("repository > find methods", () => {
236236
await postRepository.save(post)
237237
}
238238

239-
// check exist method
240-
const exists = await postRepository.exist({
239+
// check exists method
240+
const exists = await postRepository.exists({
241241
where: { categoryName: "even", isNew: true },
242242
skip: 1,
243243
take: 2,

0 commit comments

Comments
 (0)