-
-
Notifications
You must be signed in to change notification settings - Fork 11
feat: add MorphTo
relation
#106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… not know if the database will be properly initialized before define is called
Codecov Report
@@ Coverage Diff @@
## master #106 +/- ##
==========================================
Coverage 100.00% 100.00%
==========================================
Files 38 40 +2
Lines 1153 1255 +102
Branches 185 185
==========================================
+ Hits 1153 1255 +102
Continue to review full report at Codecov.
|
* Refactoring match to remove results and rely on query instead * Exporting Schemas type * Moving DictionaryByEntities interface to Relation * Copy eager loads to newQueryWithConstraints method
* Create a new MorphTo relation instance. | ||
*/ | ||
static morphTo( | ||
related: typeof Model[], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would be the disadvantage of supporting single OR multiple model references here? For example, supporting this.morphTo(Post, ...)
and this.morphTo([Post, Comment], ...)
. I think this feels more natural and consistent with other behaviours where single/multiple values co-exist.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I think it's OK to let it only access array of Models. Because if you only need one model, that is BelongsTo
relation 👀
protected buildDictionary(models: Collection): Record<string, Model> { | ||
return models.reduce<Record<string, Model>>((dictionary, model) => { | ||
dictionary[model[this.ownerKey]] = model | ||
|
||
return dictionary | ||
}, {}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a recurring theme with this type of dictionary that might warrant a shorthand method equivalent to the mapToDictionary
on the Relation
object. Though, I'm not sure what it'd be called to distinguish a dictionary of Model (singular) and Models (plural).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah maybe. inverse relationship is a bit special so 👀 But yeah we could create a shared function and share it. We could refactor this later.
Should target to #105