MCPcopy
hub / github.com/sequelize/sequelize / findOrBuild

Method findOrBuild

src/model.js:2325–2348  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

2323 * @returns {Promise<Model,boolean>}
2324 */
2325 static async findOrBuild(options) {
2326 if (!options || !options.where || arguments.length > 1) {
2327 throw new Error(
2328 'Missing where attribute in the options parameter passed to findOrBuild. ' +
2329 'Please note that the API has changed, and is now options only (an object with where, defaults keys, transaction etc.)'
2330 );
2331 }
2332
2333 let values;
2334
2335 let instance = await this.findOne(options);
2336 if (instance === null) {
2337 values = { ...options.defaults };
2338 if (_.isPlainObject(options.where)) {
2339 values = Utils.defaults(values, options.where);
2340 }
2341
2342 instance = this.build(values, options);
2343
2344 return [instance, true];
2345 }
2346
2347 return [instance, false];
2348 }
2349
2350 /**
2351 * Find a row that matches the query, or build and save the row if none is found

Callers 3

model.test.jsFile · 0.80
model.tsFile · 0.80

Calls 2

findOneMethod · 0.95
buildMethod · 0.95

Tested by

no test coverage detected