(meta: EntityMetadata<T>, em: EntityManager)
| 39 | } |
| 40 | |
| 41 | static decorate<T extends object>(meta: EntityMetadata<T>, em: EntityManager): void { |
| 42 | const fork = em.fork(); // use fork so we can access `EntityFactory` |
| 43 | const serializedPrimaryKey = meta.props.find(p => p.serializedPrimaryKey); |
| 44 | |
| 45 | if (serializedPrimaryKey) { |
| 46 | Object.defineProperty(meta.prototype, serializedPrimaryKey.name, { |
| 47 | get(): string | null { |
| 48 | return this._id ? em.getPlatform().normalizePrimaryKey<string>(this._id) : null; |
| 49 | }, |
| 50 | set(id: string): void { |
| 51 | this._id = id ? em.getPlatform().denormalizePrimaryKey(id) : null; |
| 52 | }, |
| 53 | configurable: true, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | EntityHelper.defineBaseProperties(meta, meta.prototype, fork); |
| 58 | EntityHelper.defineCustomInspect(meta); |
| 59 | |
| 60 | if (em.config.get('propagationOnPrototype') && !meta.embeddable && !meta.virtual) { |
| 61 | EntityHelper.defineProperties(meta, fork); |
| 62 | } |
| 63 | |
| 64 | const prototype = meta.prototype as Dictionary; |
| 65 | |
| 66 | if (!prototype.toJSON) { |
| 67 | // toJSON can be overridden |
| 68 | Object.defineProperty(prototype, 'toJSON', { |
| 69 | value: function (this: T, ...args: any[]) { |
| 70 | return EntityTransformer.toObject<T>(this, ...args); |
| 71 | }, |
| 72 | writable: true, |
| 73 | configurable: true, |
| 74 | enumerable: false, |
| 75 | }); |
| 76 | } |
| 77 | |
| 78 | // Walkers / serializers reaching the prototype directly invoke its methods and |
| 79 | // accessors with `this === prototype`. Wrap each so that case is a no-op rather |
| 80 | // than throwing (when a user `@Property({ persist: false })` getter dereferences |
| 81 | // unhydrated instance state) or installing state on the prototype itself (#7151). |
| 82 | for (const name of Object.getOwnPropertyNames(prototype)) { |
| 83 | const desc = Object.getOwnPropertyDescriptor(prototype, name)!; |
| 84 | const fn: any = desc.get ?? desc.value; |
| 85 | |
| 86 | if (name === 'constructor' || typeof fn !== 'function' || fn.__guarded) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | const guarded: any = function (this: T, ...args: any[]) { |
| 91 | return this === prototype ? undefined : fn.apply(this, args); |
| 92 | }; |
| 93 | guarded.__guarded = true; |
| 94 | Object.defineProperty(prototype, name, desc.get ? { ...desc, get: guarded } : { ...desc, value: guarded }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
no test coverage detected