Describe the bug
I started recently using mikro-orm in my projects, and it works beautifully (❤️), however I encountered an issue with an Entity that also uses private class fields (used to store variables that are unrelated to the data itself, but are needed for the structure). Thanks to @kyranet, we found the issue to be this line in EntityFactory
// creates new entity instance, bypassing constructor call as its already persisted entity const entity = Object.create(meta.class.prototype);
This is fine for normal usage, and even for fields that are soft-privates (annotated with the private
keyword in TS, but NOT using the #
types of privates). I don't know if this is easy to solve, or if we should just not use private class fields with mikro-orm (something I don't directly mind, but would be a nice to have)
Stack trace
Encountered unexpected error: TypeError: Cannot write private member #client from an object whose class did not declare it
at ScheduleEntity.setup (C:\Users\Vlad\Desktop\Development\TypeScript\TicketBot\dist\lib\schedule\ScheduleEntity.js:47:26)
at ScheduleManager.init (C:\Users\Vlad\Desktop\Development\TypeScript\TicketBot\dist\lib\schedule\ScheduleManager.js:16:38)
To Reproduce
Steps to reproduce the behavior:
@Entity() export class Example { @PrimaryKey() public id!: number; #myPrivateProp!: boolean; public setup(isAwesome: boolean) { this.#myPrivateProp = isAwesome; } }
// This code sample will work just fine const entry = new Example(); example.setup(true); await em.persistAndFlush(entry);
const fetchedEntry = await em.findOne({ id: 1 })!; // This WILL throw, even though the `prototype` IS the "same" fetchedEntry.setup(false);
Expected behavior
There should be no issues with creating such structures, however, due to the current way they're created (by bypassing the constructor), they become "shadow extensions" of the base class, and will throw.
Additional context
fetchedEntry instanceof Example
-> truefetchedEntry.__proto__ === Example
-> fetchedEntry
extends Example
but isn't strictly an Example
; this
isn't Example
, while this.__proto__
is
Versions
Dependency Version node v14.15.3 typescript 4.1.3 mikro-orm 4.3.4 @mikro-orm/postgresql 4.3.4RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4