Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/baseEntity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@ class BaseEntity {
constructor() {
for (const [name, definition] of Object.entries(this.meta.schema)) {
// ignore functions
if (checker.isFunction(definition)) continue
if (checker.isFunction(definition)) continue;

Object.defineProperty(this, name, {
set: function(value) {
this[`_${name}`] = value;
this.updateFunctionProperties();
},
get: function() {
return this[`_${name}`];
},
});

this[name] = definition.defaultValue;
}
}

this[name] = definition.defaultValue
updateFunctionProperties() {
for (const [field, definition] of Object.entries(this.meta.schema)) {
if (definition.options && checker.isFunction(definition.options.value)) {
const functionValue = definition.options.value;

delete this[field];
Object.defineProperty(this, field, {
configurable: true,
get: () => functionValue(this),
set: () => {
throw new Error('Cannot set a property of function type');
},
});
}
}
}

Expand Down