* 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)
| 1907 | * @returns {Promise<Model|null>} |
| 1908 | */ |
| 1909 | static async findOne(options) { |
| 1910 | if (options !== undefined && !_.isPlainObject(options)) { |
| 1911 | throw new Error(class="st">'The argument passed to findOne must be an options object, use findByPk if you wish to pass a single primary key value'); |
| 1912 | } |
| 1913 | options = Utils.cloneDeep(options); |
| 1914 | |
| 1915 | if (options.limit === undefined) { |
| 1916 | const uniqueSingleColumns = _.chain(this.uniqueKeys).values().filter(c => c.fields.length === 1).map(class="st">'column').value(); |
| 1917 | |
| 1918 | class="cm">// Don't add limit if querying directly on the pk or a unique column |
| 1919 | if (!options.where || !_.some(options.where, (value, key) => |
| 1920 | (key === this.primaryKeyAttribute || uniqueSingleColumns.includes(key)) && |
| 1921 | (Utils.isPrimitive(value) || Buffer.isBuffer(value)) |
| 1922 | )) { |
| 1923 | options.limit = 1; |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | class="cm">// Bypass a possible overloaded findAll. |
| 1928 | return await this.findAll(_.defaults(options, { |
| 1929 | plain: true |
| 1930 | })); |
| 1931 | } |
| 1932 | |
| 1933 | /** |
| 1934 | * Run an aggregation method on the specified field |