diff --git a/src/baseEntity.js b/src/baseEntity.js index e09189f..0fadcba 100644 --- a/src/baseEntity.js +++ b/src/baseEntity.js @@ -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; + + Object.defineProperty(this, field, { + get: function() { + return functionValue(this); + }, + set: function() { + throw new Error('Cannot set a property of function type'); + }, + }); + } } } diff --git a/test/field/scalarTypes/string.js b/test/field/scalarTypes/string.js index 5bb738a..e11ec76 100644 --- a/test/field/scalarTypes/string.js +++ b/test/field/scalarTypes/string.js @@ -38,7 +38,7 @@ describe('A field', () => { }) //when const instance = new AnEntity() - //then + //then assert.deepStrictEqual(instance['field1'], "Of Life") })