-
-
Notifications
You must be signed in to change notification settings - Fork 11
MorphMany #111
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
Open
ryandialpad
wants to merge
23
commits into
master
Choose a base branch
from
morph-many
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
MorphMany #111
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8bec10a
Init MorphMany
ryandialpad 2f75dcd
Adding MorphMany tests, removing TODOs from tested MorphMany methods.
ryandialpad 1bba446
Removing TODO from tested method in MorphMany
ryandialpad e774cbe
Fixing a typo
ryandialpad 03160be
Moving fill state into before all block
ryandialpad d6dd6c6
Correcting a typo
ryandialpad 41aa1a8
Touching up some test descriptions
ryandialpad cafc529
Addressing PR comments
ryandialpad 98d366f
Merge branch 'master' into morph-many
ryandialpad 7d7c50d
Refactoring MorphMany match method to use query instead of results
ryandialpad 6eb7366
Init MorphMany
ryandialpad f3b449d
Adding MorphMany tests, removing TODOs from tested MorphMany methods.
ryandialpad f13c298
Removing TODO from tested method in MorphMany
ryandialpad 3defb88
Fixing a typo
ryandialpad b83e961
Moving fill state into before all block
ryandialpad de213c4
Correcting a typo
ryandialpad ae8453e
Touching up some test descriptions
ryandialpad ea25ea6
Addressing PR comments
ryandialpad 8afbfd8
Refactoring MorphMany match method to use query instead of results
ryandialpad 739dd38
Merge branch 'morph-many' of github.com:vuex-orm/vuex-orm-next into m…
ryandialpad ad65710
Resolving some duplicate code after resolving conflicts
ryandialpad 7e5f0fe
Removing another duplicated import
ryandialpad f16c621
Fixing up some comment headers
ryandialpad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { Schema as NormalizrSchema } from 'normalizr' | ||
import { Schema } from '../../../schema/Schema' | ||
import { Element, Collection } from '../../../data/Data' | ||
import { Query } from '../../../query/Query' | ||
import { Model } from '../../Model' | ||
import { Relation, Dictionary } from './Relation' | ||
|
||
export class MorphMany extends Relation { | ||
/** | ||
* The field name that contains id of the parent model. | ||
*/ | ||
protected morphId: string | ||
|
||
/** | ||
* The field name that contains type of the parent model. | ||
*/ | ||
protected morphType: string | ||
|
||
/** | ||
* The local key of the model. | ||
*/ | ||
protected localKey: string | ||
|
||
/** | ||
* Create a new morph-many relation instance. | ||
*/ | ||
constructor( | ||
parent: Model, | ||
related: Model, | ||
morphId: string, | ||
morphType: string, | ||
localKey: string | ||
) { | ||
super(parent, related) | ||
this.morphId = morphId | ||
this.morphType = morphType | ||
this.localKey = localKey | ||
} | ||
|
||
/** | ||
* Get all related models for the relationship. | ||
*/ | ||
getRelateds(): Model[] { | ||
return [this.related] | ||
} | ||
|
||
/** | ||
* Define the normalizr schema for the relation. | ||
*/ | ||
define(schema: Schema): NormalizrSchema { | ||
return schema.many(this.related, this.parent) | ||
} | ||
|
||
/** | ||
* Attach the parent type and id to the given relation. | ||
*/ | ||
attach(record: Element, child: Element): void { | ||
child[this.morphId] = record[this.localKey] | ||
child[this.morphType] = this.parent.$entity() | ||
} | ||
|
||
/** | ||
* Set the constraints for an eager load of the relation. | ||
*/ | ||
addEagerConstraints(query: Query, models: Collection): void { | ||
query.where(this.morphType, this.parent.$entity()) | ||
query.whereIn(this.morphId, this.getKeys(models, this.localKey)) | ||
} | ||
|
||
/** | ||
* Match the eagerly loaded results to their parents. | ||
*/ | ||
match(relation: string, models: Collection, query: Query): void { | ||
const dictionary = this.buildDictionary(query.get()) | ||
|
||
models.forEach((model) => { | ||
const key = model[this.localKey] | ||
|
||
dictionary[key] | ||
? model.$setRelation(relation, dictionary[key]) | ||
: model.$setRelation(relation, []) | ||
}) | ||
} | ||
|
||
/** | ||
* Build model dictionary keyed by the relation's foreign key. | ||
*/ | ||
protected buildDictionary(results: Collection): Dictionary { | ||
return this.mapToDictionary(results, (result) => { | ||
return [result[this.morphId], result] | ||
}) | ||
} | ||
|
||
/** | ||
* Make related models. | ||
*/ | ||
make(elements?: Element[]): Model[] { | ||
return elements | ||
? elements.map((element) => this.related.$newInstance(element)) | ||
: [] | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Model } from '../../../Model' | ||
import { PropertyDecorator } from '../../Contracts' | ||
|
||
/** | ||
* Create a morph-many attribute property decorator. | ||
*/ | ||
export function MorphMany( | ||
related: () => typeof Model, | ||
id: string, | ||
type: string, | ||
localKey?: string | ||
): PropertyDecorator { | ||
return (target, propertyKey) => { | ||
const self = target.$self() | ||
|
||
self.setRegistry(propertyKey, () => | ||
self.morphMany(related(), id, type, localKey) | ||
) | ||
} | ||
} |
Oops, something went wrong.
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.
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.
I am noticing a fair amount of code duplication, at some point we might want to DRY these up once the library is feature complete (1.x).