From d7c82dd4796005a0f9c0a5eb9bcad224a46de831 Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Thu, 8 Jun 2023 14:39:56 +0300 Subject: [PATCH 1/7] chore: in_memory entity constructor spread --- src/entity/in_memory.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 3e4b5930..60a8e8b8 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -12,8 +12,8 @@ export class InMemoryEntity { return new this.prototype.constructor(config); } - constructor(config) { - this._json = deepClone(config || {}); + constructor(config = {}) { + this._json = this.constructor._useDeepClone ? deepClone(config) : { ...config }; } /** From 2ce5a4fbc07a1f9dfdf95722598bd1a55e37ee03 Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Fri, 9 Jun 2023 17:47:36 +0300 Subject: [PATCH 2/7] chore: in_memory entity performance improvements --- src/entity/in_memory.js | 21 ++++++++++++--------- src/utils/clone.js | 4 ++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 60a8e8b8..a0c9d137 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -1,7 +1,7 @@ import lodash from "lodash"; // import { ESSE } from "@exabyte-io/esse.js"; -import { deepClone } from "../utils/clone"; +import { clone, deepClone } from "../utils/clone"; import { getSchemaByClassName } from "../utils/schemas"; // TODO: https://exabyte.atlassian.net/browse/SOF-5946 @@ -13,7 +13,7 @@ export class InMemoryEntity { } constructor(config = {}) { - this._json = this.constructor._useDeepClone ? deepClone(config) : { ...config }; + this._json = this.constructor._useDeepClone ? deepClone(config) : clone(config); } /** @@ -46,11 +46,14 @@ export class InMemoryEntity { /** * @summary Array of fields to exclude from resulted JSON - * @param exclude {String[]} + * @param {String[]} exclude + * @param {Boolean} unsafeClone use deepClone if true */ - toJSON(exclude = []) { - const config = deepClone(lodash.omit(this._json, exclude)); - return this.clean(config); + toJSON(exclude = [], unsafeClone = false) { + const config = lodash.omit(this._json, exclude); + const clonedConfig = + this.constructor._useDeepClone && !unsafeClone ? deepClone(config) : clone(config); + return this.clean(clonedConfig); } /** @@ -77,7 +80,7 @@ export class InMemoryEntity { */ validate() { if (this.schema) { - this.schema.validate(this.toJSON()); + this.schema.validate(this.toJSON([], true)); } } @@ -90,10 +93,10 @@ export class InMemoryEntity { isValid() { const ctx = this.schema.newContext(); - ctx.validate(this.toJSON()); + ctx.validate(this.toJSON([], true)); if (!ctx.isValid()) { - console.log(JSON.stringify(this.toJSON())); + console.log(JSON.stringify(this.toJSON([], true))); if (ctx.getErrorObject) { console.log(ctx.getErrorObject()); } diff --git a/src/utils/clone.js b/src/utils/clone.js index 1a753088..46aa48a3 100644 --- a/src/utils/clone.js +++ b/src/utils/clone.js @@ -2,3 +2,7 @@ export function deepClone(obj) { return JSON.parse(JSON.stringify(obj)); } + +export function clone(obj) { + return { ...obj }; +} From 0649f885e1c09ad587d3d3f09187b88ccfa73eda Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Tue, 13 Jun 2023 06:17:28 +0300 Subject: [PATCH 3/7] chore: in_memory entity constructor spread --- src/entity/in_memory.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index a0c9d137..5b2ca14a 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -13,7 +13,7 @@ export class InMemoryEntity { } constructor(config = {}) { - this._json = this.constructor._useDeepClone ? deepClone(config) : clone(config); + this._json = this.constructor._isDeepCloneRequired ? deepClone(config) : clone(config); } /** @@ -52,7 +52,9 @@ export class InMemoryEntity { toJSON(exclude = [], unsafeClone = false) { const config = lodash.omit(this._json, exclude); const clonedConfig = - this.constructor._useDeepClone && !unsafeClone ? deepClone(config) : clone(config); + this.constructor._isDeepCloneRequired && !unsafeClone + ? deepClone(config) + : clone(config); return this.clean(clonedConfig); } From 96dd124031af6c2d5a966828640efbd820382fd8 Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Wed, 14 Jun 2023 17:27:37 +0300 Subject: [PATCH 4/7] fix: use safe clone --- src/entity/in_memory.js | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 5b2ca14a..4965e375 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -47,15 +47,23 @@ export class InMemoryEntity { /** * @summary Array of fields to exclude from resulted JSON * @param {String[]} exclude - * @param {Boolean} unsafeClone use deepClone if true */ - toJSON(exclude = [], unsafeClone = false) { + toJSON(exclude = []) { + return this.constructor._isDeepCloneRequired + ? this.toJSONSafe(exclude) + : this.toJSONQuick(exclude); + } + + toJSONSafe(exclude = []) { const config = lodash.omit(this._json, exclude); - const clonedConfig = - this.constructor._isDeepCloneRequired && !unsafeClone - ? deepClone(config) - : clone(config); - return this.clean(clonedConfig); + + return this.clean(deepClone(config)); + } + + toJSONQuick(exclude = []) { + const config = lodash.omit(this._json, exclude); + + return this.clean(clone(config)); } /** @@ -64,7 +72,7 @@ export class InMemoryEntity { * @returns {*} */ clone(extraContext = {}) { - return new this.constructor({ ...this.toJSON(), ...extraContext }); + return new this.constructor({ ...this.toJSONSafe(), ...extraContext }); } // override upon inheritance @@ -82,7 +90,7 @@ export class InMemoryEntity { */ validate() { if (this.schema) { - this.schema.validate(this.toJSON([], true)); + this.schema.validate(this.toJSONQuick([], true)); } } @@ -95,10 +103,10 @@ export class InMemoryEntity { isValid() { const ctx = this.schema.newContext(); - ctx.validate(this.toJSON([], true)); + ctx.validate(this.toJSONQuick([], true)); if (!ctx.isValid()) { - console.log(JSON.stringify(this.toJSON([], true))); + console.log(JSON.stringify(this.toJSONQuick([], true))); if (ctx.getErrorObject) { console.log(ctx.getErrorObject()); } From 2a0914037e3de442a29ba2e0d2efe91e9ba17b93 Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Thu, 15 Jun 2023 03:11:21 +0300 Subject: [PATCH 5/7] chore: in_memory clone fix --- src/entity/in_memory.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 4965e375..9fcf308d 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -72,7 +72,7 @@ export class InMemoryEntity { * @returns {*} */ clone(extraContext = {}) { - return new this.constructor({ ...this.toJSONSafe(), ...extraContext }); + return new this.constructor({ ...this.toJSON(), ...extraContext }); } // override upon inheritance @@ -90,7 +90,7 @@ export class InMemoryEntity { */ validate() { if (this.schema) { - this.schema.validate(this.toJSONQuick([], true)); + this.schema.validate(this.toJSONSafe()); } } @@ -103,10 +103,10 @@ export class InMemoryEntity { isValid() { const ctx = this.schema.newContext(); - ctx.validate(this.toJSONQuick([], true)); + ctx.validate(this.toJSONSafe()); if (!ctx.isValid()) { - console.log(JSON.stringify(this.toJSONQuick([], true))); + console.log(JSON.stringify(this.toJSONSafe())); if (ctx.getErrorObject) { console.log(ctx.getErrorObject()); } From 41fd1b7b0f8ad601da9297881ea59223cdd58a21 Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Thu, 15 Jun 2023 15:06:15 +0300 Subject: [PATCH 6/7] chore: in_memory clone fix --- src/entity/in_memory.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 9fcf308d..81da8376 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -90,7 +90,7 @@ export class InMemoryEntity { */ validate() { if (this.schema) { - this.schema.validate(this.toJSONSafe()); + this.schema.validate(this.toJSON()); } } @@ -103,10 +103,12 @@ export class InMemoryEntity { isValid() { const ctx = this.schema.newContext(); - ctx.validate(this.toJSONSafe()); + const json = this.toJSON(); + + ctx.validate(json); if (!ctx.isValid()) { - console.log(JSON.stringify(this.toJSONSafe())); + console.log(JSON.stringify(json)); if (ctx.getErrorObject) { console.log(ctx.getErrorObject()); } From 2b7b3b7456a13169c04daa58beadec1d4169e915 Mon Sep 17 00:00:00 2001 From: Kostiantyn Dvornik Date: Fri, 16 Jun 2023 22:56:02 +0300 Subject: [PATCH 7/7] chore: add default _isDeepCloneRequired --- src/entity/in_memory.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 81da8376..b0844c3a 100644 --- a/src/entity/in_memory.js +++ b/src/entity/in_memory.js @@ -12,6 +12,9 @@ export class InMemoryEntity { return new this.prototype.constructor(config); } + // Override if config deepClone is needed + static _isDeepCloneRequired = false; + constructor(config = {}) { this._json = this.constructor._isDeepCloneRequired ? deepClone(config) : clone(config); }