From 12e1735c76534fcf9066abff68e044531e601831 Mon Sep 17 00:00:00 2001 From: Menci Date: Sun, 21 Apr 2019 15:21:10 +0800 Subject: [PATCH] Fix shallow copy of cached object --- models/common.ts | 22 +++++++++++----------- package.json | 1 + yarn.lock | 12 ++++++++++++ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/models/common.ts b/models/common.ts index 76a0037..ecedc18 100644 --- a/models/common.ts +++ b/models/common.ts @@ -1,6 +1,6 @@ import * as TypeORM from "typeorm"; import * as LRUCache from "lru-cache"; -import * as ObjectAssignDeep from "object-assign-deep"; +import * as DeepCopy from "deepcopy"; interface Paginater { pageCnt: number; @@ -41,27 +41,27 @@ export default class Model extends TypeORM.BaseEntity { if ((this as typeof Model).cache) { let result; if (result = cacheGet(this.name, id)) { - // Got cached result. - // Return a copy of it to avoid issue of adding or assigning properties. - const object = {}; - Object.keys(result).forEach(key => { - object[key] = result[key] && typeof result[key] === 'object' - ? ObjectAssignDeep({}, result[key]) - : result[key]; - }); - return (this as typeof Model).create(object); + return result.clone(); } result = await doQuery(); if (result) { cacheSet(this.name, id, result); } - return result; + return result.clone(); } else { return await doQuery(); } } + clone() { + const object = {}; + TypeORM.getConnection().getMetadata(this.constructor).ownColumns.map(column => column.propertyName).forEach(key => { + object[key] = DeepCopy(this[key]); + }); + return (this.constructor as any).create(object); + } + async destroy() { const id = (this as any).id; await TypeORM.getManager().remove(this); diff --git a/package.json b/package.json index 029ebc4..933f3eb 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "command-line-args": "^5.1.0", "cookie-parser": "^1.4.4", "cssfilter": "0.0.10", + "deepcopy": "^2.0.0", "download": "^7.1.0", "ejs": "^2.5.2", "express": "^4.14.0", diff --git a/yarn.lock b/yarn.lock index eb31abf..bb257ba 100644 --- a/yarn.lock +++ b/yarn.lock @@ -859,6 +859,13 @@ deep-is@~0.1.3: resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz#b369d6fb5dbc13eecf524f91b070feedc357cf34" integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ= +deepcopy@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/deepcopy/-/deepcopy-2.0.0.tgz#2acb9b7645f9f54d815eee991455e790e72e2252" + integrity sha512-d5ZK7pJw7F3k6M5vqDjGiiUS9xliIyWkdzBjnPhnSeRGjkYOGZMCFkdKVwV/WiHOe0NwzB8q+iDo7afvSf0arA== + dependencies: + type-detect "^4.0.8" + delayed-stream@~1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" @@ -3525,6 +3532,11 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" +type-detect@^4.0.8: + version "4.0.8" + resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c" + integrity sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g== + type-is@^1.6.4, type-is@~1.6.16: version "1.6.16" resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz#f89ce341541c672b25ee7ae3c73dee3b2be50194"