[IMP] mail: remove model manager from model field

Part of task-2582313
This commit is contained in:
Sébastien Theys
2021-08-19 13:34:59 +00:00
parent bccfb6e5d0
commit fc0c947062
2 changed files with 14 additions and 21 deletions
+14 -19
View File
@@ -20,7 +20,6 @@ export class ModelField {
fieldType,
inverse,
isCausal = false,
modelManager,
readonly = false,
related,
relationType,
@@ -64,10 +63,6 @@ export class ModelField {
* relation is removed, the related record is automatically deleted.
*/
this.isCausal = isCausal;
/**
* Reference to the manager of this field.
*/
this.modelManager = modelManager;
/**
* Determines whether the field is read only. Read only field
* can't be updated once the record is created.
@@ -482,8 +477,8 @@ export class ModelField {
* @returns {boolean} whether the value changed for the current field
*/
_setRelationCreate(record, data, options) {
const OtherModel = this.modelManager.models[this.to];
const other = this.modelManager._create(OtherModel, data);
const OtherModel = record.models[this.to];
const other = record.modelManager._create(OtherModel, data);
return this._setRelationLink(record, other, options);
}
@@ -500,8 +495,8 @@ export class ModelField {
* @returns {boolean} whether the value changed for the current field
*/
_setRelationInsert(record, data, options) {
const OtherModel = this.modelManager.models[this.to];
const other = this.modelManager._insert(OtherModel, data);
const OtherModel = record.models[this.to];
const other = record.modelManager._insert(OtherModel, data);
return this._setRelationLink(record, other, options);
}
@@ -517,8 +512,8 @@ export class ModelField {
* @returns {boolean} whether the value changed for the current field
*/
_setRelationInsertAndReplace(record, data, options) {
const OtherModel = this.modelManager.models[this.to];
const newValue = this.modelManager._insert(OtherModel, data);
const OtherModel = record.models[this.to];
const newValue = record.modelManager._insert(OtherModel, data);
return this._setRelationReplace(record, newValue, options);
}
@@ -569,7 +564,7 @@ export class ModelField {
otherRecords.add(recordToLink);
// link current record to other records
if (hasToUpdateInverse) {
this.modelManager._update(
record.modelManager._update(
recordToLink,
{ [this.inverse]: link(record) },
{ allowWriteReadonly: true, hasToUpdateInverse: false }
@@ -605,7 +600,7 @@ export class ModelField {
record.__values[this.fieldName] = recordToLink;
// link current record to other record
if (hasToUpdateInverse) {
this.modelManager._update(
record.modelManager._update(
recordToLink,
{ [this.inverse]: link(record) },
{ allowWriteReadonly: true, hasToUpdateInverse: false }
@@ -713,7 +708,7 @@ export class ModelField {
if (!otherRecord) {
throw Error(`Record ${record.localId} cannot update undefined relational field ${this.fieldName}.`);
}
this.modelManager._update(otherRecord, data);
record.modelManager._update(otherRecord, data);
return false;
}
@@ -756,9 +751,9 @@ export class ModelField {
}
// apply causality
if (this.isCausal) {
this.modelManager._delete(recordToUnlink);
record.modelManager._delete(recordToUnlink);
} else {
this.modelManager._update(
record.modelManager._update(
recordToUnlink,
{ [this.inverse]: unlink(record) },
{ allowWriteReadonly: true, hasToUpdateInverse: false }
@@ -800,9 +795,9 @@ export class ModelField {
}
// apply causality
if (this.isCausal) {
this.modelManager._delete(otherRecord);
record.modelManager._delete(otherRecord);
} else {
this.modelManager._update(
record.modelManager._update(
otherRecord,
{ [this.inverse]: unlink(record) },
{ allowWriteReadonly: true, hasToUpdateInverse: false }
@@ -822,7 +817,7 @@ export class ModelField {
* @throws {Error} if record does not satisfy related model
*/
_verifyRelationalValue(record) {
const OtherModel = this.modelManager.models[this.to];
const OtherModel = record.modelManager.models[this.to];
if (!OtherModel.get(record.localId, { isCheckingInheritance: true })) {
throw Error(`Record ${record.localId} is not valid for relational field ${this.fieldName}.`);
}
@@ -980,7 +980,6 @@ export class ModelManager {
relFunc(Model.modelName, { inverse: field.fieldName }),
{
fieldName: `_inverse_${Model.modelName}/${field.fieldName}`,
modelManager: this,
}
));
return inverseField;
@@ -1062,7 +1061,6 @@ export class ModelManager {
for (const [fieldName, fieldData] of Object.entries(Model.fields)) {
Model.fields[fieldName] = new ModelField(Object.assign({}, fieldData, {
fieldName,
modelManager: this,
}));
}
}