* Gets the count of collection items from database instead of counting loaded items. * The value is cached (unless you use the `where` option), use `refresh: true` to force reload it. * When the dataloader is enabled (globally or per-query), multiple calls are batched into a single grouped que
(options: LoadCountOptions<T> | boolean = {})
| 139 | * When the dataloader is enabled (globally or per-query), multiple calls are batched into a single grouped query. |
| 140 | */ |
| 141 | async loadCount(options: LoadCountOptions<T> | boolean = {}): Promise<number> { |
| 142 | options = typeof options === 'boolean' ? { refresh: options } : options; |
| 143 | const { refresh, where, dataloader, ...countOptions } = options; |
| 144 | |
| 145 | if (!refresh && !where && this.#count != null) { |
| 146 | return this.#count; |
| 147 | } |
| 148 | |
| 149 | const em = this.getEntityManager()!; |
| 150 | |
| 151 | if ( |
| 152 | !em.getPlatform().usesPivotTable() && |
| 153 | this.property.kind === ReferenceKind.MANY_TO_MANY && |
| 154 | this.property.owner |
| 155 | ) { |
| 156 | return (this.#count = this.length); |
| 157 | } |
| 158 | |
| 159 | let count: number; |
| 160 | |
| 161 | if (dataloader ?? [DataloaderType.ALL, DataloaderType.COLLECTION].includes(em.config.getDataloaderType())) { |
| 162 | const loader = await em.getDataLoader('count'); |
| 163 | count = await loader.load([this, { where, ...countOptions }]); |
| 164 | } else { |
| 165 | const cond = this.createLoadCountCondition(where ?? ({} as FilterQuery<T>)); |
| 166 | count = await em.count(this.property.targetMeta!.class, cond, countOptions as any); |
| 167 | } |
| 168 | |
| 169 | if (!where) { |
| 170 | this.#count = count; |
| 171 | } |
| 172 | |
| 173 | return count; |
| 174 | } |
| 175 | |
| 176 | /** Queries a subset of the collection items from the database with custom filtering, ordering, and pagination. */ |
| 177 | async matching<TT extends T, P extends string = never>(options: MatchingOptions<T, P>): Promise<Loaded<TT, P>[]> { |
no test coverage detected