feat: add exists and exists by (#10291)

This PR deprecates `exist` in favor of `exists` and `existsBy`.
This commit is contained in:
Nemanja Stanic 2023-12-29 12:29:02 +01:00 committed by GitHub
parent cf7147fa7c
commit b6b46fb133
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 17 deletions

View File

@ -183,6 +183,22 @@ await manager.increment(User, { firstName: "Timber" }, "age", 3)
await manager.decrement(User, { firstName: "Timber" }, "age", 3)
```
- `exists` - Check whether any entity exists that matches `FindOptions`.
```typescript
const exists = await manager.exists(User, {
where: {
firstName: "Timber",
},
})
```
- `existsBy` - Checks whether any entity exists that matches `FindOptionsWhere`.
```typescript
const exists = await manager.existsBy(User, { firstName: "Timber" })
```
- `count` - Counts entities that match `FindOptions`. Useful for pagination.
```typescript

View File

@ -257,6 +257,22 @@ await repository.increment({ firstName: "Timber" }, "age", 3)
await repository.decrement({ firstName: "Timber" }, "age", 3)
```
- `exists` - Check whether any entity exists that matches `FindOptions`.
```typescript
const exists = await repository.exists({
where: {
firstName: "Timber",
},
})
```
- `existsBy` - Checks whether any entity exists that matches `FindOptionsWhere`.
```typescript
const exists = await repository.existsBy({ firstName: "Timber" })
```
- `count` - Counts entities that match `FindOptions`. Useful for pagination.
```typescript

View File

@ -955,7 +955,7 @@ export class EntityManager {
}
/**
* Checks whether any entity exists with the given condition
* Checks whether any entity exists with the given options.
*/
exists<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
@ -971,6 +971,19 @@ export class EntityManager {
.getExists()
}
/**
* Checks whether any entity exists with the given conditions.
*/
async existsBy<Entity extends ObjectLiteral>(
entityClass: EntityTarget<Entity>,
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<boolean> {
const metadata = this.connection.getMetadata(entityClass)
return this.createQueryBuilder(entityClass, metadata.name)
.setFindOptions({ where })
.getExists()
}
/**
* Counts entities that match given options.
* Useful for pagination.

View File

@ -389,6 +389,26 @@ export class BaseEntity {
return this.getRepository<T>().delete(criteria)
}
/**
* Checks whether any entity exists that matches the given options.
*/
static exists<T extends BaseEntity>(
this: { new (): T } & typeof BaseEntity,
options?: FindManyOptions<T>,
): Promise<boolean> {
return this.getRepository<T>().exists(options)
}
/**
* Checks whether any entity exists that matches the given conditions.
*/
static existsBy<T extends BaseEntity>(
this: { new (): T } & typeof BaseEntity,
where: FindOptionsWhere<T>,
): Promise<boolean> {
return this.getRepository<T>().existsBy(where)
}
/**
* Counts entities that match given options.
*/

View File

@ -453,12 +453,32 @@ export class Repository<Entity extends ObjectLiteral> {
}
/**
* Checks whether any entity exists that match given options.
* Checks whether any entity exists that matches the given options.
*
* @deprecated use `exists` method instead, for example:
*
* .exists()
*/
exist(options?: FindManyOptions<Entity>): Promise<boolean> {
return this.manager.exists(this.metadata.target, options)
}
/**
* Checks whether any entity exists that matches the given options.
*/
exists(options?: FindManyOptions<Entity>): Promise<boolean> {
return this.manager.exists(this.metadata.target, options)
}
/**
* Checks whether any entity exists that matches the given conditions.
*/
existsBy(
where: FindOptionsWhere<Entity> | FindOptionsWhere<Entity>[],
): Promise<boolean> {
return this.manager.existsBy(this.metadata.target, where)
}
/**
* Counts entities that match given options.
* Useful for pagination.

View File

@ -7,7 +7,7 @@ import { DataSource } from "../../../../src/data-source/DataSource"
import { expect } from "chai"
import { Test } from "./entity/Test"
describe("query builder > exist", () => {
describe("query builder > exists", () => {
let connections: DataSource[]
before(
async () =>
@ -25,8 +25,8 @@ describe("query builder > exist", () => {
connections.map(async (connection) => {
const repo = connection.getRepository(Test)
const exist = await repo.exist()
expect(exist).to.be.equal(false)
const exists = await repo.exists()
expect(exists).to.be.equal(false)
}),
))
@ -38,8 +38,8 @@ describe("query builder > exist", () => {
await repo.save({ id: "ok" })
await repo.save({ id: "nok" })
const exist = await repo.exist()
expect(exist).to.be.equal(true)
const exists = await repo.exists()
expect(exists).to.be.equal(true)
}),
))
})

View File

@ -149,8 +149,8 @@ describe("repository > find methods", () => {
await postRepository.save(post)
}
// check exist method
const exists = await postRepository.exist({
// check exists method
const exists = await postRepository.exists({
order: { id: "ASC" },
})
exists.should.be.equal(true)
@ -169,8 +169,8 @@ describe("repository > find methods", () => {
await postRepository.save(post)
}
// check exist method
const exists = await postRepository.exist({
// check exists method
const exists = await postRepository.exists({
where: { categoryName: "odd" },
order: { id: "ASC" },
})
@ -191,8 +191,8 @@ describe("repository > find methods", () => {
await postRepository.save(post)
}
// check exist method
const exists = await postRepository.exist({
// check exists method
const exists = await postRepository.exists({
where: { categoryName: "odd", isNew: true },
order: { id: "ASC" },
})
@ -215,8 +215,8 @@ describe("repository > find methods", () => {
await postRepository.save(post)
}
// check exist method
const exists = await postRepository.exist()
// check exists method
const exists = await postRepository.exists()
exists.should.be.equal(true)
}),
))
@ -236,8 +236,8 @@ describe("repository > find methods", () => {
await postRepository.save(post)
}
// check exist method
const exists = await postRepository.exist({
// check exists method
const exists = await postRepository.exists({
where: { categoryName: "even", isNew: true },
skip: 1,
take: 2,