* Count the number of records matching the provided where clause. * * If you provide an `include` option, the number of matching associations will be counted instead. * * @param {object} [options] options * @param {object} [options.where] A hash of search attributes.
(options)
| 2019 | * @returns {Promise<number>} |
| 2020 | */ |
| 2021 | static async count(options) { |
| 2022 | options = Utils.cloneDeep(options); |
| 2023 | options = _.defaults(options, { hooks: true }); |
| 2024 | options.raw = true; |
| 2025 | if (options.hooks) { |
| 2026 | await this.runHooks('beforeCount', options); |
| 2027 | } |
| 2028 | let col = options.col || '*'; |
| 2029 | if (options.include) { |
| 2030 | col = `${this.name}.${options.col || this.primaryKeyField}`; |
| 2031 | } |
| 2032 | if (options.distinct && col === '*') { |
| 2033 | col = this.primaryKeyField; |
| 2034 | } |
| 2035 | options.plain = !options.group; |
| 2036 | options.dataType = new DataTypes.INTEGER(); |
| 2037 | options.includeIgnoreAttributes = false; |
| 2038 | |
| 2039 | // No limit, offset or order for the options max be given to count() |
| 2040 | // Set them to null to prevent scopes setting those values |
| 2041 | options.limit = null; |
| 2042 | options.offset = null; |
| 2043 | options.order = null; |
| 2044 | |
| 2045 | const result = await this.aggregate(col, 'count', options); |
| 2046 | |
| 2047 | // When grouping is used, some dialects such as PG are returning the count as string |
| 2048 | // --> Manually convert it to number |
| 2049 | if (Array.isArray(result)) { |
| 2050 | return result.map(item => ({ |
| 2051 | ...item, |
| 2052 | count: Number(item.count) |
| 2053 | })); |
| 2054 | } |
| 2055 | |
| 2056 | return result; |
| 2057 | } |
| 2058 | |
| 2059 | /** |
| 2060 | * Find all the rows matching your query, within a specified offset / limit, and get the total number of rows matching your query. This is very useful for paging |
no test coverage detected