diff --git a/src/entity/in_memory.js b/src/entity/in_memory.js index 3e4b5930..b0844c3a 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 @@ -12,8 +12,11 @@ export class InMemoryEntity { return new this.prototype.constructor(config); } - constructor(config) { - this._json = deepClone(config || {}); + // Override if config deepClone is needed + static _isDeepCloneRequired = false; + + constructor(config = {}) { + this._json = this.constructor._isDeepCloneRequired ? deepClone(config) : clone(config); } /** @@ -46,11 +49,24 @@ export class InMemoryEntity { /** * @summary Array of fields to exclude from resulted JSON - * @param exclude {String[]} + * @param {String[]} exclude */ toJSON(exclude = []) { - const config = deepClone(lodash.omit(this._json, exclude)); - return this.clean(config); + return this.constructor._isDeepCloneRequired + ? this.toJSONSafe(exclude) + : this.toJSONQuick(exclude); + } + + toJSONSafe(exclude = []) { + const config = lodash.omit(this._json, exclude); + + return this.clean(deepClone(config)); + } + + toJSONQuick(exclude = []) { + const config = lodash.omit(this._json, exclude); + + return this.clean(clone(config)); } /** @@ -90,10 +106,12 @@ export class InMemoryEntity { isValid() { const ctx = this.schema.newContext(); - ctx.validate(this.toJSON()); + const json = this.toJSON(); + + ctx.validate(json); if (!ctx.isValid()) { - console.log(JSON.stringify(this.toJSON())); + console.log(JSON.stringify(json)); 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 }; +}