* Search for a single instance. Returns the first instance found, or null if none can be found. * * @param {object} [options] A hash of options to describe the scope of the search * @param {Transaction} [options.transaction] Transaction to run query under * @param {string}
(options)
| 1968 | * @returns {Promise<Model|null>} |
| 1969 | */ |
| 1970 | static async findOne(options) { |
| 1971 | if (options !== undefined && !_.isPlainObject(options)) { |
| 1972 | throw new Error('The argument passed to findOne must be an options object, use findByPk if you wish to pass a single primary key value'); |
| 1973 | } |
| 1974 | options = Utils.cloneDeep(options); |
| 1975 | |
| 1976 | // Add CLS transaction |
| 1977 | if (options.transaction === undefined && this.sequelize.constructor._cls) { |
| 1978 | const t = this.sequelize.constructor._cls.get('transaction'); |
| 1979 | if (t) { |
| 1980 | options.transaction = t; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | if (options.limit === undefined) { |
| 1985 | const uniqueSingleColumns = _.chain(this.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value(); |
| 1986 | |
| 1987 | // Don't add limit if querying directly on the pk or a unique column |
| 1988 | if (!options.where || !_.some(options.where, (value, key) => |
| 1989 | (key === this.primaryKeyAttribute || uniqueSingleColumns.includes(key)) && |
| 1990 | (Utils.isPrimitive(value) || Buffer.isBuffer(value)) |
| 1991 | )) { |
| 1992 | options.limit = 1; |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | // Bypass a possible overloaded findAll. |
| 1997 | // note: in v6, we don't bypass overload https://github.com/sequelize/sequelize/issues/14003 |
| 1998 | return await this.findAll(_.defaults(options, { |
| 1999 | plain: true |
| 2000 | })); |
| 2001 | } |
| 2002 | |
| 2003 | /** |
| 2004 | * Run an aggregation method on the specified field |