* Find a row that matches the query, or build (but don't save) the row if none is found. * The successful result of the promise will be (instance, built) * * @param {object} options find options * @param {object} options.where A hash of search attributes. If `where` is a plain object
(options)
| 2244 | * @returns {Promise<Model,boolean>} |
| 2245 | */ |
| 2246 | static async findOrBuild(options) { |
| 2247 | if (!options || !options.where || arguments.length > 1) { |
| 2248 | throw new Error( |
| 2249 | 'Missing where attribute in the options parameter passed to findOrBuild. ' + |
| 2250 | 'Please note that the API has changed, and is now options only (an object with where, defaults keys, transaction etc.)' |
| 2251 | ); |
| 2252 | } |
| 2253 | |
| 2254 | let values; |
| 2255 | |
| 2256 | let instance = await this.findOne(options); |
| 2257 | if (instance === null) { |
| 2258 | values = { ...options.defaults }; |
| 2259 | if (_.isPlainObject(options.where)) { |
| 2260 | values = Utils.defaults(values, options.where); |
| 2261 | } |
| 2262 | |
| 2263 | instance = this.build(values, options); |
| 2264 | |
| 2265 | return [instance, true]; |
| 2266 | } |
| 2267 | |
| 2268 | return [instance, false]; |
| 2269 | } |
| 2270 | |
| 2271 | /** |
| 2272 | * Find a row that matches the query, or build and save the row if none is found |
no test coverage detected