From 59e82a4657b6b75e4df7303b6f1d4ceb00ec1457 Mon Sep 17 00:00:00 2001 From: Jelle De Loecker Date: Sat, 19 Oct 2024 18:40:32 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20`=5FmodelInstance`=20cache?= =?UTF-8?q?=20when=20keys=20are=20not=20strings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 4 ++++ lib/app/helper_model/model.js | 5 +++++ lib/core/base.js | 8 ++++---- package.json | 2 +- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d13bf7f1..220b1fbf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.4.0-alpha.8 (WIP) + +* Fix `_modelInstance` cache when keys are not strings + ## 1.4.0-alpha.7 (2024-10-10) * End requests with an error when parsing posted body contents fail diff --git a/lib/app/helper_model/model.js b/lib/app/helper_model/model.js index f31c9ace..183c5b2d 100644 --- a/lib/app/helper_model/model.js +++ b/lib/app/helper_model/model.js @@ -680,6 +680,11 @@ Model.setMethod(function getAliasModel(alias) { config = this.schema.associations[alias]; } + // @TODO: associated fields with an alias name inside a nested schema + // somehow breaks! Should be looked in to? + // Workaround: set `recursive: 0` on the root schema field + // console.log('Alias:', alias, 'config:', config, this.schema); + if (config) { result = this.getModel(config.modelName); } else { diff --git a/lib/core/base.js b/lib/core/base.js index 1ee902c3..e8849067 100644 --- a/lib/core/base.js +++ b/lib/core/base.js @@ -564,7 +564,7 @@ Base.setMethod(function attachConduit(conduit) { * * @author Jelle De Loecker * @since 0.3.0 - * @version 1.1.8 + * @version 1.4.0 * * @param {string} name The name of the model to get * @param {boolean} init Initialize the class [true] @@ -597,9 +597,9 @@ Base.setMethod(function getModel(name, init, options) { if (options.cache !== false) { if (!this._modelInstances) { - this._modelInstances = {}; + this._modelInstances = new Map(); } else { - instance = this._modelInstances[name]; + instance = this._modelInstances.get(name); } } @@ -622,7 +622,7 @@ Base.setMethod(function getModel(name, init, options) { } if (options.cache !== false) { - this._modelInstances[name] = instance; + this._modelInstances.set(name, instance); } return instance; diff --git a/package.json b/package.json index ea950be4..23a245fd 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "alchemymvc", "description": "MVC framework for Node.js", - "version": "1.4.0-alpha.7", + "version": "1.4.0-alpha.8", "author": "Jelle De Loecker ", "keywords": [ "alchemy",