| 1 |
import { |
| 2 |
RELATION_TYPES, |
| 3 |
DATE_TIME_TYPE, |
| 4 |
X2MANY_TYPES, |
| 5 |
RAW_SYMBOL, |
| 6 |
convertRawToDateTime, |
| 7 |
convertRawToDate, |
| 8 |
STORE_SYMBOL, |
| 9 |
} from "./utils"; |
| 10 |
import { Base } from "./base"; |
| 11 |
|
| 12 |
// It allows us to track which fields have been dynamically added to the model. |
| 13 |
// This is usefull to avoid conflicts during Hoot tests, where the model class prototype |
| 14 |
// is shared across multiple tests, and we want to ensure that dynamic fields are not |
| 15 |
// redefined or conflict with existing properties. |
| 16 |
const INITIALIZED_CLASSES = new Set(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Processes model definitions to dynamically define getter and setter properties |
| 20 |
* on model fields, providing controlled access to the raw data. |
| 21 |
*/ |
| 22 |
export function processModelClasses(modelDefs, modelClasses = {}) { |
| 23 |
const modelNames = new Set(Object.keys(modelDefs)); |
| 24 |
for (const modelName of modelNames) { |
| 25 |
if (INITIALIZED_CLASSES.has(modelName)) { |
| 26 |
continue; // Skip already initialized classes in the registry |
| 27 |
} |
| 28 |
|
| 29 |
const fields = modelDefs[modelName]; |
| 30 |
const ModelRecordClass = modelClasses[modelName] || class ModelRecord extends Base {}; |
| 31 |
const excludedLazyGetters = []; |
| 32 |
|
| 33 |
if (modelClasses[modelName]) { |
| 34 |
INITIALIZED_CLASSES.add(modelName); |
| 35 |
} |
| 36 |
|
| 37 |
modelClasses[modelName] = ModelRecordClass; |
| 38 |
|
| 39 |
for (const fieldName in fields) { |
| 40 |
const field = fields[fieldName]; |
| 41 |
if (field.dummy || fieldName === "id") { |
| 42 |
continue; |
| 43 |
} |
| 44 |
if (fieldName in ModelRecordClass.prototype) { |
| 45 |
throw new Error( |
| 46 |
`The property "${fieldName}" defined in the class "${ModelRecordClass.name}" matches an existing model "${modelName}" property. Please use a different property name.` |
| 47 |
); |
| 48 |
} |
| 49 |
const isRelationNotInModelDef = field.relation && !modelNames.has(field.relation); |
| 50 |
if (!RELATION_TYPES.has(field.type) || isRelationNotInModelDef) { |
| 51 |
if (!DATE_TIME_TYPE.has(field.type)) { |
| 52 |
excludedLazyGetters.push(fieldName); |
| 53 |
} |
| 54 |
Object.defineProperty(ModelRecordClass.prototype, fieldName, { |
| 55 |
get: function () { |
| 56 |
const value = this[RAW_SYMBOL][fieldName]; |
| 57 |
if (DATE_TIME_TYPE.has(field.type)) { |
| 58 |
return field.type === "datetime" |
| 59 |
? convertRawToDateTime(this, value, field) |
| 60 |
: convertRawToDate(this, value, field); |
| 61 |
} else if (isRelationNotInModelDef && value instanceof Set) { |
| 62 |
return unmodifiableArray( |
| 63 |
[...value], |
| 64 |
`The '${fieldName}' array cannot be modified.` |
| 65 |
); |
| 66 |
} |
| 67 |
return value; |
| 68 |
}, |
| 69 |
set: function (newValue) { |
| 70 |
this.update({ [fieldName]: newValue }); |
| 71 |
}, |
| 72 |
enumerable: true, |
| 73 |
}); |
| 74 |
} else { |
| 75 |
const relationModel = field.relation; |
| 76 |
const updateErrorMessage = `The '${fieldName}' array cannot be modified. Use the update method instead.`; |
| 77 |
if (X2MANY_TYPES.has(field.type)) { |
| 78 |
Object.defineProperty(ModelRecordClass.prototype, fieldName, { |
| 79 |
get: function () { |
| 80 |
return unmodifiableArray( |
| 81 |
Array.from(this[RAW_SYMBOL][fieldName] || new Set(), (recordID) => |
| 82 |
this[STORE_SYMBOL].getById(relationModel, recordID) |
| 83 |
).filter((s) => s), //avoid empty records, |
| 84 |
updateErrorMessage |
| 85 |
); |
| 86 |
}, |
| 87 |
set: function (values) { |
| 88 |
this.update({ [fieldName]: values }); |
| 89 |
}, |
| 90 |
enumerable: true, |
| 91 |
}); |
| 92 |
} else if (field.type === "many2one") { |
| 93 |
Object.defineProperty(ModelRecordClass.prototype, fieldName, { |
| 94 |
get: function () { |
| 95 |
const id = this[RAW_SYMBOL][fieldName]; |
| 96 |
if (!id) { |
| 97 |
return undefined; |
| 98 |
} |
| 99 |
return this[STORE_SYMBOL].getById(relationModel, id); |
| 100 |
}, |
| 101 |
set: function (newValue) { |
| 102 |
this.update({ [fieldName]: newValue }); |
| 103 |
}, |
| 104 |
enumerable: true, |
| 105 |
}); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
if (excludedLazyGetters.length > 0) { |
| 110 |
ModelRecordClass.excludedLazyGetters = [ |
| 111 |
...ModelRecordClass.excludedLazyGetters, |
| 112 |
...excludedLazyGetters, |
| 113 |
]; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
export function createExtraField(record, extraFields, serverData, vals) { |
| 118 |
if (!extraFields?.length) { |
| 119 |
return; |
| 120 |
} |
| 121 |
if (!serverData) { |
| 122 |
// Assign the value to the instance (not in raw data) |
| 123 |
for (let i = 0; i < extraFields.length; i++) { |
| 124 |
const field = extraFields[i]; |
| 125 |
record[field] = vals[field]; |
| 126 |
} |
| 127 |
return; |
| 128 |
} |
| 129 |
// Create raw data shortcuts getter for the given fields |
| 130 |
for (let i = 0; i < extraFields.length; i++) { |
| 131 |
const fieldName = extraFields[i]; |
| 132 |
if (fieldName in record) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
Object.defineProperty(record, fieldName, { |
| 136 |
get: function () { |
| 137 |
const value = this[RAW_SYMBOL][fieldName]; |
| 138 |
if (Array.isArray(value)) { |
| 139 |
return unmodifiableArray(value, `The '${fieldName}' array cannot be modified`); |
| 140 |
} |
| 141 |
return value; |
| 142 |
}, |
| 143 |
set: function (newValue) { |
| 144 |
throw new Error(`${fieldName} is read-only`); |
| 145 |
}, |
| 146 |
enumerable: true, |
| 147 |
}); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function unmodifiableArray(arr, message) { |
| 152 |
return new Proxy(arr, { |
| 153 |
set(target, prop, value) { |
| 154 |
throw new Error(message); |
| 155 |
}, |
| 156 |
deleteProperty(target, prop) { |
| 157 |
throw new Error(message); |
| 158 |
}, |
| 159 |
defineProperty(target, prop, descriptor) { |
| 160 |
throw new Error(message); |
| 161 |
}, |
| 162 |
get(target, prop, receiver) { |
| 163 |
return Reflect.get(target, prop, receiver); |
| 164 |
}, |
| 165 |
}); |
| 166 |
} |