Skip to content

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

Merged
merged 11 commits into from
Dec 3, 2021
Merged

feat: add MorphTo relation #106

merged 11 commits into from
Dec 3, 2021

Conversation

kiaking
Copy link
Member

@kiaking kiaking commented Nov 19, 2021

Should target to #105

@codecov
Copy link

codecov bot commented Nov 19, 2021

Codecov Report

Merging #106 (9fb309d) into master (8d5245a) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##            master      #106    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           38        40     +2     
  Lines         1153      1255   +102     
  Branches       185       185            
==========================================
+ Hits          1153      1255   +102     
Impacted Files Coverage Δ
src/model/attributes/relations/Relation.ts 100.00% <ø> (ø)
src/database/Database.ts 100.00% <100.00%> (ø)
src/model/Model.ts 100.00% <100.00%> (ø)
src/model/attributes/relations/BelongsTo.ts 100.00% <100.00%> (ø)
src/model/attributes/relations/HasMany.ts 100.00% <100.00%> (ø)
src/model/attributes/relations/HasManyBy.ts 100.00% <100.00%> (ø)
src/model/attributes/relations/HasOne.ts 100.00% <100.00%> (ø)
src/model/attributes/relations/MorphOne.ts 100.00% <100.00%> (ø)
src/model/attributes/relations/MorphTo.ts 100.00% <100.00%> (ø)
...c/model/decorators/attributes/relations/MorphTo.ts 100.00% <100.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8d5245a...9fb309d. Read the comment docs.

* Refactoring match to remove results and rely on query instead

* Exporting Schemas type

* Moving DictionaryByEntities interface to Relation

* Copy eager loads to newQueryWithConstraints method
@cuebit cuebit mentioned this pull request Nov 23, 2021
10 tasks
* Create a new MorphTo relation instance.
*/
static morphTo(
related: typeof Model[],
Copy link
Member

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.

Copy link
Member Author

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 👀

Comment on lines +102 to +108
protected buildDictionary(models: Collection): Record<string, Model> {
return models.reduce<Record<string, Model>>((dictionary, model) => {
dictionary[model[this.ownerKey]] = model

return dictionary
}, {})
}
Copy link
Member

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).

Copy link
Member Author

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.

This was referenced Nov 24, 2021
@ryandialpad ryandialpad changed the title Refactor morphto MorphTo Relation Nov 29, 2021
@kiaking kiaking changed the title MorphTo Relation feat: add MorphTo relation Dec 3, 2021
@kiaking kiaking added the enhancement New feature or request label Dec 3, 2021
@kiaking kiaking changed the title feat: add MorphTo relation feat: add MorphTo relation (#105) Dec 3, 2021
@kiaking kiaking changed the title feat: add MorphTo relation (#105) feat: add MorphTo relation Dec 3, 2021
@kiaking kiaking added this to the v1.0.0-alpha.1 milestone Dec 3, 2021
@kiaking kiaking merged commit 0caced0 into master Dec 3, 2021
@kiaking kiaking deleted the refactor-morphto branch December 3, 2021 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants