Use "Symbol" for internal entity properties #135
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
GitHub
Changes
__type
and__primaryKey
. Instead, it uses symbols:Symbol('type')
Symbol('primaryKey')
__nodeId
is removed from internal entity properties. It was a left-over from the previous implementation of relationships and is no longer used.InternalEntity
type. The entity is now the same internally and publicly, as the Symbols representing internal properties are non-enumerable.BroadcastChannel
, so we need to preserve the transmitted entity’s internal properties in a manually attached regular property. The event listener then re-attaches the symbols to the received entity using theinheritInternalProperties
function..toEqual
assertion. The end-developer never sees nor operates with the internal symbols as they are non-enumerable.Database
class to restrictmodelName
values to the keys of the given dictionary.Motivation
This change originates from #132, where a recursive relationship resulted in an infinite loop. Upon investigation, the loop originated from the
removeInternalProperties
function that stripped a given entity of its internal properties (those were stored as regular properties under reserved keys). That function eventually stumbled upon a relational property, read it, tried to clean it, then found the nested relation, then nested, etc (the relation is expected to be circular but the library must not hang on it).Diving into the matter further, I’ve tried storing the internal properties in a different way. My first try was to use
Object.defineProperties
, which appends properties that are non-enumerable by default. This worked nicely until I realized that the properties appended that way are lost when you assign the object (entity) into another data structure:This corrupted each entity once it was stored in the
Database
. UsingObject.defineProperties
was not an option for this use case.Afterward, I’ve recalled symbols and thought to give them a try. It turned out symbols are great for this particular case, because they are non-enumerable, but also persist with the object regardless of where it’s being stored. It was decided to use symbols for storing internal entity properties.
Consequences
BroadcastChannel
in thesync
extension (database operations synchronization between open tabs), it loses its symbols, corrupting the entity. I’d have to add a manual plain property calledSERIALIZED_INTERNAL_PROPERTIES
that temporarily stores respective properties, and then re-define them on the entity in another client.