|
|
@ -1,6 +1,6 @@ |
|
|
|
import * as TypeORM from "typeorm"; |
|
|
|
import * as TypeORM from "typeorm"; |
|
|
|
import * as LRUCache from "lru-cache"; |
|
|
|
import * as LRUCache from "lru-cache"; |
|
|
|
import * as ObjectAssignDeep from "object-assign-deep"; |
|
|
|
import * as DeepCopy from "deepcopy"; |
|
|
|
|
|
|
|
|
|
|
|
interface Paginater { |
|
|
|
interface Paginater { |
|
|
|
pageCnt: number; |
|
|
|
pageCnt: number; |
|
|
@ -41,27 +41,27 @@ export default class Model extends TypeORM.BaseEntity { |
|
|
|
if ((this as typeof Model).cache) { |
|
|
|
if ((this as typeof Model).cache) { |
|
|
|
let result; |
|
|
|
let result; |
|
|
|
if (result = cacheGet(this.name, id)) { |
|
|
|
if (result = cacheGet(this.name, id)) { |
|
|
|
// Got cached result.
|
|
|
|
return result.clone(); |
|
|
|
// 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<T>(object); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
result = await doQuery(); |
|
|
|
result = await doQuery(); |
|
|
|
if (result) { |
|
|
|
if (result) { |
|
|
|
cacheSet(this.name, id, result); |
|
|
|
cacheSet(this.name, id, result); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
return result.clone(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return await doQuery(); |
|
|
|
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() { |
|
|
|
async destroy() { |
|
|
|
const id = (this as any).id; |
|
|
|
const id = (this as any).id; |
|
|
|
await TypeORM.getManager().remove(this); |
|
|
|
await TypeORM.getManager().remove(this); |
|
|
|