From c32108820ed75e3f3cdd1248b74fa11e3d3cd817 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 27 Dec 2022 21:46:54 -0300 Subject: [PATCH 1/3] feat(entity computed property): define function properties, updating them when properties are set feature #68 --- src/baseEntity.js | 31 +++++++++++++++++++++++++++++-- test/field/scalarTypes/string.js | 2 +- 2 files changed, 30 insertions(+), 3 deletions(-) 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") }) From dce2088d3f3f874f206f24484b50c35fd1c26320 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 27 Dec 2022 21:48:29 -0300 Subject: [PATCH 2/3] Remove spaces --- test/field/scalarTypes/string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/field/scalarTypes/string.js b/test/field/scalarTypes/string.js index e11ec76..5bb738a 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") }) From 39323d1247a85e97bcab2b9a6f2842d5acb34923 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 27 Dec 2022 22:19:57 -0300 Subject: [PATCH 3/3] Fix implementation bug --- src/baseEntity.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/baseEntity.js b/src/baseEntity.js index 0fadcba..ace2dc2 100644 --- a/src/baseEntity.js +++ b/src/baseEntity.js @@ -15,7 +15,7 @@ class BaseEntity { get: function() { return this[`_${name}`]; }, - }); + }); this[name] = definition.defaultValue; } @@ -26,11 +26,11 @@ class BaseEntity { if (definition.options && checker.isFunction(definition.options.value)) { const functionValue = definition.options.value; + delete this[field]; Object.defineProperty(this, field, { - get: function() { - return functionValue(this); - }, - set: function() { + configurable: true, + get: () => functionValue(this), + set: () => { throw new Error('Cannot set a property of function type'); }, });