[FIX] mail: introduce findFromIdentifyingData

This is necessary to check the existence of a record based on identifying data
without having to use `insert` (or the inefficient `find`).

PR: #56243
Part of task-2323670

X-original-commit: c4bbfc5e2a8d236e5fc79a63c13575de146afbd7
This commit is contained in:
Sébastien Theys
2020-08-24 13:46:59 +00:00
parent c66d10a971
commit ef873e0c29
2 changed files with 28 additions and 2 deletions
+15 -2
View File
@@ -171,6 +171,20 @@ class ModelManager {
return this.all(Model).find(findFunc);
}
/**
* Gets the unique record of provided model that matches the given
* identifying data, if it exists.
* @see `_createRecordLocalId` for criteria of identification.
*
* @param {mail.model} Model class
* @param {Object} data
* @returns {mail.model|undefined}
*/
findFromIdentifyingData(Model, data) {
const localId = Model._createRecordLocalId(data);
return Model.get(localId);
}
/**
* This method returns the record of provided model that matches provided
* local id. Useful to convert a local id to a record.
@@ -785,8 +799,7 @@ class ModelManager {
const dataList = isMulti ? data : [data];
const records = [];
for (const data of dataList) {
const localId = Model._createRecordLocalId(data);
let record = Model.get(localId);
let record = Model.findFromIdentifyingData(data);
if (!record) {
record = this._create(Model, data);
} else {
@@ -110,6 +110,19 @@ function factory() {
return this.env.modelManager.find(this, findFunc);
}
/**
* Gets the unique record that matches the given identifying data, if it
* exists.
* @see `_createRecordLocalId` for criteria of identification.
*
* @static
* @param {Object} data
* @returns {mail.model|undefined}
*/
static findFromIdentifyingData(data) {
return this.env.modelManager.findFromIdentifyingData(this, data);
}
/**
* This method returns the record of this model that matches provided
* local id. Useful to convert a local id to a record. Note that even