|
| 1 | +import { Schema as NormalizrSchema } from 'normalizr' |
| 2 | +import { Schema } from '../../../schema/Schema' |
| 3 | +import { Element, Collection } from '../../../data/Data' |
| 4 | +import { Query } from '../../../query/Query' |
| 5 | +import { Model } from '../../Model' |
| 6 | +import { Relation, Dictionary } from './Relation' |
| 7 | + |
| 8 | +export class MorphOne extends Relation { |
| 9 | + /** |
| 10 | + * The field name that contains id of the parent model. |
| 11 | + */ |
| 12 | + protected morphId: string |
| 13 | + |
| 14 | + /** |
| 15 | + * The field name that contains type of the parent model. |
| 16 | + */ |
| 17 | + protected morphType: string |
| 18 | + |
| 19 | + /** |
| 20 | + * The local key of the model. |
| 21 | + */ |
| 22 | + protected localKey: string |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new morph-one relation instance. |
| 26 | + */ |
| 27 | + constructor( |
| 28 | + parent: Model, |
| 29 | + related: Model, |
| 30 | + morphId: string, |
| 31 | + morphType: string, |
| 32 | + localKey: string |
| 33 | + ) { |
| 34 | + super(parent, related) |
| 35 | + this.morphId = morphId |
| 36 | + this.morphType = morphType |
| 37 | + this.localKey = localKey |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get all related models for the relationship. |
| 42 | + */ |
| 43 | + getRelateds(): Model[] { |
| 44 | + return [this.related] |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Define the normalizr schema for the relation. |
| 49 | + */ |
| 50 | + define(schema: Schema): NormalizrSchema { |
| 51 | + return schema.one(this.related, this.parent) |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Attach the parent type and id to the given relation. |
| 56 | + */ |
| 57 | + attach(record: Element, child: Element): void { |
| 58 | + child[this.morphId] = record[this.localKey] |
| 59 | + child[this.morphType] = this.parent.$entity() |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Set the constraints for an eager load of the relation. |
| 64 | + */ |
| 65 | + addEagerConstraints(query: Query, models: Collection): void { |
| 66 | + query.where(this.morphType, this.parent.$entity()) |
| 67 | + query.whereIn(this.morphId, this.getKeys(models, this.localKey)) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Match the eagerly loaded results to their parents. |
| 72 | + */ |
| 73 | + match(relation: string, models: Collection, results: Collection): void { |
| 74 | + const dictionary = this.buildDictionary(results) |
| 75 | + |
| 76 | + models.forEach((model) => { |
| 77 | + const key = model[this.localKey] |
| 78 | + |
| 79 | + dictionary[key] |
| 80 | + ? model.$setRelation(relation, dictionary[key][0]) |
| 81 | + : model.$setRelation(relation, null) |
| 82 | + }) |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Build model dictionary keyed by the relation's foreign key. |
| 87 | + */ |
| 88 | + protected buildDictionary(results: Collection): Dictionary { |
| 89 | + return this.mapToDictionary(results, (result) => { |
| 90 | + return [result[this.morphId], result] |
| 91 | + }) |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Make a related model. |
| 96 | + */ |
| 97 | + make(element?: Element): Model | null { |
| 98 | + return element ? this.related.$newInstance(element) : null |
| 99 | + } |
| 100 | +} |
0 commit comments